-
Notifications
You must be signed in to change notification settings - Fork 8
/
spidermonkey_streams.cc
340 lines (267 loc) · 7.85 KB
/
spidermonkey_streams.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 2009 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Christophe Robin <[email protected]> |
+----------------------------------------------------------------------+
$Id$
$Revision$
*/
#include "php_spidermonkey.h"
/* this native is used for read from streams */
JSBool js_stream_read(JSContext *ctx, unsigned argc, JS::Value *vp)
{
TSRMLS_FETCH();
PHPJS_START(ctx);
php_jscontext_object *intern;
php_jsobject_ref *jsref;
php_stream *stream = NULL;
JSClass *cls;
JSObject *obj = JS_THIS_OBJECT(ctx, vp);
jsval *argv = JS_ARGV(ctx, vp);
jsval *rval = &JS_RVAL(ctx, vp);
intern = (php_jscontext_object*)JS_GetContextPrivate(ctx);
cls = &intern->script_class;
if (obj == intern->obj) {
cls =&intern->global_class;
}
jsref = (php_jsobject_ref*)JS_GetInstancePrivate(ctx, obj, cls, NULL);
if (jsref != NULL && jsref->obj != NULL && Z_TYPE_P(jsref->obj) == IS_RESOURCE) {
JSString *jstr;
char *buf;
size_t buf_len, nbytes;
if (argc >= 1)
{
buf_len = JSVAL_TO_INT(argv[0]);
}
else
{
/* default buffer length is 4K */
buf_len = 4096;
}
/* fetch php_stream */
php_stream_from_zval_no_verify(stream, &jsref->obj);
if (stream == NULL) {
reportError(ctx, "Failed to read stream", NULL);
PHPJS_END(ctx);
return JS_FALSE;
}
buf = (char*)emalloc(buf_len * sizeof(char));
memset(buf, 0, buf_len);
// read from string
nbytes = php_stream_read(stream, buf, buf_len);
if (nbytes > 0) {
jstr = JS_NewStringCopyN(ctx, buf, nbytes);
*rval = STRING_TO_JSVAL(jstr);
}
else {
*rval = JSVAL_NULL;
}
efree(buf);
}
PHPJS_END(ctx);
return JS_TRUE;
}
/* this native is used to retrieve a line from a stream */
JSBool js_stream_getline(JSContext *ctx, unsigned argc, JS::Value *vp)
{
TSRMLS_FETCH();
PHPJS_START(ctx);
php_jscontext_object *intern;
php_jsobject_ref *jsref;
php_stream *stream = NULL;
JSClass *cls;
JSObject *obj = JS_THIS_OBJECT(ctx, vp);
jsval *argv = JS_ARGV(ctx, vp);
jsval *rval = &JS_RVAL(ctx, vp);
intern = (php_jscontext_object*)JS_GetContextPrivate(ctx);
cls = &intern->script_class;
if (obj == intern->obj) {
cls = &intern->global_class;
}
jsref = (php_jsobject_ref*)JS_GetInstancePrivate(ctx, obj, cls, NULL);
if (jsref != NULL && jsref->obj != NULL && Z_TYPE_P(jsref->obj) == IS_RESOURCE) {
JSString *jstr;
char *buf;
size_t buf_len, nbytes;
if (argc >= 1)
{
buf_len = JSVAL_TO_INT(argv[0]);
}
else
{
// default buffer length is 4K
buf_len = 4096;
}
// fetch php_stream
php_stream_from_zval_no_verify(stream, &jsref->obj);
if (stream == NULL) {
reportError(ctx, "Failed to read stream", NULL);
PHPJS_END(ctx);
return JS_FALSE;
}
buf = (char*)emalloc(buf_len * sizeof(char));
// read from string
if (php_stream_get_line(stream, buf, buf_len, &nbytes) == NULL) {
nbytes = 0;
}
if (nbytes > 0) {
jstr = JS_NewStringCopyN(ctx, buf, nbytes);
*rval = STRING_TO_JSVAL(jstr);
}
else {
*rval = JSVAL_NULL;
}
efree(buf);
}
PHPJS_END(ctx);
return JS_TRUE;
}
/* this native is used to seek in a stream */
JSBool js_stream_seek(JSContext *ctx, unsigned argc, JS::Value *vp)
{
TSRMLS_FETCH();
PHPJS_START(ctx);
php_jscontext_object *intern;
php_jsobject_ref *jsref;
php_stream *stream = NULL;
JSClass *cls;
JSObject *obj = JS_THIS_OBJECT(ctx, vp);
jsval *argv = JS_ARGV(ctx,vp);
jsval *rval = &JS_RVAL(ctx,vp);
intern = (php_jscontext_object*)JS_GetContextPrivate(ctx);
cls = &intern->script_class;
if (obj == intern->obj) {
cls =&intern->global_class;
}
jsref = (php_jsobject_ref*)JS_GetInstancePrivate(ctx, obj, cls, NULL);
if (jsref != NULL && jsref->obj != NULL && Z_TYPE_P(jsref->obj) == IS_RESOURCE && argc >= 1) {
off_t pos;
int whence;
// fetch php_stream
php_stream_from_zval_no_verify(stream, &jsref->obj);
if (stream == NULL) {
reportError(ctx, "Failed to access stream", NULL);
PHPJS_END(ctx);
return JS_FALSE;
}
pos = JSVAL_TO_INT(argv[0]);
if (argc >= 2) {
whence = JSVAL_TO_INT(argv[1]);
}
else {
// default buffer length is 4K
whence = SEEK_SET;
}
php_stream_seek(stream, pos, whence);
// this function doesn't return anything
*rval = JSVAL_VOID;
}
PHPJS_END(ctx);
return JS_TRUE;
}
/* this native is used for writing to a stream */
JSBool js_stream_write(JSContext *ctx, unsigned argc, JS::Value *vp)
{
TSRMLS_FETCH();
PHPJS_START(ctx);
php_jscontext_object *intern;
php_jsobject_ref *jsref;
php_stream *stream = NULL;
JSClass *cls;
JSObject *obj = JS_THIS_OBJECT(ctx, vp);
jsval *argv = JS_ARGV(ctx,vp);
jsval *rval = &JS_RVAL(ctx,vp);
intern = (php_jscontext_object*)JS_GetContextPrivate(ctx);
cls = &intern->script_class;
if (obj == intern->obj) {
cls =&intern->global_class;
}
jsref = (php_jsobject_ref*)JS_GetInstancePrivate(ctx, obj, cls, NULL);
if (jsref != NULL && jsref->obj != NULL && Z_TYPE_P(jsref->obj) == IS_RESOURCE && argc >= 1) {
JSString *jstr;
size_t buf_len, nbytes;
// fetch php_stream
php_stream_from_zval_no_verify(stream, &jsref->obj);
if (stream == NULL) {
reportError(ctx, "Failed to write to stream", NULL);
PHPJS_END(ctx);
return JS_FALSE;
}
jstr = JS_ValueToString(ctx, argv[0]);
if (jstr != NULL) {
// then we retrieve the pointer to the string
char *txt = JS_EncodeString(ctx, jstr);
if (argc >= 2)
{
buf_len = JSVAL_TO_INT(argv[1]);
nbytes = php_stream_write(stream, txt, buf_len);
}
else
{
nbytes = php_stream_write_string(stream, txt);
}
JS_free(ctx, txt);
}
else {
reportError(ctx, "Failed to convert type to string", NULL);
PHPJS_END(ctx);
return JS_FALSE;
}
JS_SET_RVAL(ctx, rval, JS_NumberValue(nbytes));
}
PHPJS_END(ctx);
return JS_TRUE;
}
/* this native is used for telling the position in the file */
JSBool js_stream_tell(JSContext *ctx, unsigned argc, JS::Value *vp)
{
TSRMLS_FETCH();
PHPJS_START(ctx);
php_jscontext_object *intern;
php_jsobject_ref *jsref;
php_stream *stream = NULL;
JSClass *cls;
JSObject *obj = JS_THIS_OBJECT(ctx, vp);
jsval *argv = JS_ARGV(ctx,vp);
jsval *rval = &JS_RVAL(ctx,vp);
intern = (php_jscontext_object*)JS_GetContextPrivate(ctx);
cls = &intern->script_class;
if (obj == intern->obj) {
cls =&intern->global_class;
}
jsref = (php_jsobject_ref*)JS_GetInstancePrivate(ctx, obj, cls, NULL);
if (jsref != NULL && jsref->obj != NULL && Z_TYPE_P(jsref->obj) == IS_RESOURCE) {
off_t file_pos;
// fetch php_stream
php_stream_from_zval_no_verify(stream, &jsref->obj);
if (stream == NULL) {
reportError(ctx, "Failed to fetch stream", NULL);
PHPJS_END(ctx);
return JS_FALSE;
}
file_pos = php_stream_tell(stream);
// read from string
JS_SET_RVAL(ctx, rval, JS_NumberValue(file_pos));
}
PHPJS_END(ctx);
return JS_TRUE;
}
/*
* Local Variables:
* c-basic-offset: 4
* tab-width: 4
* End:
* vim600: fdm=marker
* vim: noet sw=4 ts=4
*/