zig-pug 0.3.0 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -0
- package/binding.gyp +16 -10
- package/index.mjs +192 -0
- package/package.json +12 -3
- package/prebuilts/darwin-arm64/libzig-pug.a +0 -0
- package/prebuilts/darwin-x64/libzig-pug.a +0 -0
- package/prebuilts/linux-arm64/libzig-pug.a +0 -0
- package/prebuilts/linux-x64/libzig-pug.a +0 -0
- package/prebuilts/win32-x64/zig-pug.lib +0 -0
- package/vendor/mujs/COPYING +0 -16
- package/vendor/mujs/README +0 -50
- package/vendor/mujs/astnames.h +0 -92
- package/vendor/mujs/jsarray.c +0 -832
- package/vendor/mujs/jsboolean.c +0 -38
- package/vendor/mujs/jsbuiltin.c +0 -249
- package/vendor/mujs/jscompile.c +0 -1428
- package/vendor/mujs/jsdate.c +0 -861
- package/vendor/mujs/jsdtoa.c +0 -749
- package/vendor/mujs/jserror.c +0 -139
- package/vendor/mujs/jsfunction.c +0 -231
- package/vendor/mujs/jsgc.c +0 -284
- package/vendor/mujs/jsi.h +0 -870
- package/vendor/mujs/jsintern.c +0 -137
- package/vendor/mujs/jslex.c +0 -878
- package/vendor/mujs/jsmath.c +0 -194
- package/vendor/mujs/jsnumber.c +0 -198
- package/vendor/mujs/jsobject.c +0 -560
- package/vendor/mujs/json.c +0 -422
- package/vendor/mujs/jsparse.c +0 -1065
- package/vendor/mujs/jsproperty.c +0 -341
- package/vendor/mujs/jsregexp.c +0 -232
- package/vendor/mujs/jsrepr.c +0 -285
- package/vendor/mujs/jsrun.c +0 -2096
- package/vendor/mujs/jsstate.c +0 -334
- package/vendor/mujs/jsstring.c +0 -852
- package/vendor/mujs/jsvalue.c +0 -708
- package/vendor/mujs/libmujs.a +0 -0
- package/vendor/mujs/main.c +0 -396
- package/vendor/mujs/mujs.h +0 -253
- package/vendor/mujs/one.c +0 -25
- package/vendor/mujs/opnames.h +0 -85
- package/vendor/mujs/pp.c +0 -980
- package/vendor/mujs/regexp.c +0 -1277
- package/vendor/mujs/regexp.h +0 -46
- package/vendor/mujs/utf.c +0 -305
- package/vendor/mujs/utf.h +0 -52
- package/vendor/mujs/utfdata.h +0 -2209
package/vendor/mujs/jsproperty.c
DELETED
|
@@ -1,341 +0,0 @@
|
|
|
1
|
-
#include "jsi.h"
|
|
2
|
-
|
|
3
|
-
#include <assert.h>
|
|
4
|
-
|
|
5
|
-
/*
|
|
6
|
-
Use an AA-tree to quickly look up properties in objects:
|
|
7
|
-
|
|
8
|
-
The level of every leaf node is one.
|
|
9
|
-
The level of every left child is one less than its parent.
|
|
10
|
-
The level of every right child is equal or one less than its parent.
|
|
11
|
-
The level of every right grandchild is less than its grandparent.
|
|
12
|
-
Every node of level greater than one has two children.
|
|
13
|
-
|
|
14
|
-
A link where the child's level is equal to that of its parent is called a horizontal link.
|
|
15
|
-
Individual right horizontal links are allowed, but consecutive ones are forbidden.
|
|
16
|
-
Left horizontal links are forbidden.
|
|
17
|
-
|
|
18
|
-
skew() fixes left horizontal links.
|
|
19
|
-
split() fixes consecutive right horizontal links.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
static js_Property sentinel = {
|
|
23
|
-
&sentinel, &sentinel,
|
|
24
|
-
0, 0,
|
|
25
|
-
{ { {0}, JS_TUNDEFINED } },
|
|
26
|
-
NULL, NULL, ""
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
static js_Property *newproperty(js_State *J, js_Object *obj, const char *name)
|
|
30
|
-
{
|
|
31
|
-
int n = strlen(name) + 1;
|
|
32
|
-
js_Property *node = js_malloc(J, offsetof(js_Property, name) + n);
|
|
33
|
-
node->left = node->right = &sentinel;
|
|
34
|
-
node->level = 1;
|
|
35
|
-
node->atts = 0;
|
|
36
|
-
node->value.t.type = JS_TUNDEFINED;
|
|
37
|
-
node->value.u.number = 0;
|
|
38
|
-
node->getter = NULL;
|
|
39
|
-
node->setter = NULL;
|
|
40
|
-
memcpy(node->name, name, n);
|
|
41
|
-
++obj->count;
|
|
42
|
-
++J->gccounter;
|
|
43
|
-
return node;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
static js_Property *lookup(js_Property *node, const char *name)
|
|
47
|
-
{
|
|
48
|
-
while (node != &sentinel) {
|
|
49
|
-
int c = strcmp(name, node->name);
|
|
50
|
-
if (c == 0)
|
|
51
|
-
return node;
|
|
52
|
-
else if (c < 0)
|
|
53
|
-
node = node->left;
|
|
54
|
-
else
|
|
55
|
-
node = node->right;
|
|
56
|
-
}
|
|
57
|
-
return NULL;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
static js_Property *skew(js_Property *node)
|
|
61
|
-
{
|
|
62
|
-
if (node->left->level == node->level) {
|
|
63
|
-
js_Property *temp = node;
|
|
64
|
-
node = node->left;
|
|
65
|
-
temp->left = node->right;
|
|
66
|
-
node->right = temp;
|
|
67
|
-
}
|
|
68
|
-
return node;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
static js_Property *split(js_Property *node)
|
|
72
|
-
{
|
|
73
|
-
if (node->right->right->level == node->level) {
|
|
74
|
-
js_Property *temp = node;
|
|
75
|
-
node = node->right;
|
|
76
|
-
temp->right = node->left;
|
|
77
|
-
node->left = temp;
|
|
78
|
-
++node->level;
|
|
79
|
-
}
|
|
80
|
-
return node;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
static js_Property *insert(js_State *J, js_Object *obj, js_Property *node, const char *name, js_Property **result)
|
|
84
|
-
{
|
|
85
|
-
if (node != &sentinel) {
|
|
86
|
-
int c = strcmp(name, node->name);
|
|
87
|
-
if (c < 0)
|
|
88
|
-
node->left = insert(J, obj, node->left, name, result);
|
|
89
|
-
else if (c > 0)
|
|
90
|
-
node->right = insert(J, obj, node->right, name, result);
|
|
91
|
-
else
|
|
92
|
-
return *result = node;
|
|
93
|
-
node = skew(node);
|
|
94
|
-
node = split(node);
|
|
95
|
-
return node;
|
|
96
|
-
}
|
|
97
|
-
return *result = newproperty(J, obj, name);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
static void freeproperty(js_State *J, js_Object *obj, js_Property *node)
|
|
101
|
-
{
|
|
102
|
-
js_free(J, node);
|
|
103
|
-
--obj->count;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
static js_Property *unlinkproperty(js_Property *node, const char *name, js_Property **garbage)
|
|
107
|
-
{
|
|
108
|
-
js_Property *temp, *a, *b;
|
|
109
|
-
if (node != &sentinel) {
|
|
110
|
-
int c = strcmp(name, node->name);
|
|
111
|
-
if (c < 0) {
|
|
112
|
-
node->left = unlinkproperty(node->left, name, garbage);
|
|
113
|
-
} else if (c > 0) {
|
|
114
|
-
node->right = unlinkproperty(node->right, name, garbage);
|
|
115
|
-
} else {
|
|
116
|
-
*garbage = node;
|
|
117
|
-
if (node->left == &sentinel && node->right == &sentinel) {
|
|
118
|
-
return &sentinel;
|
|
119
|
-
}
|
|
120
|
-
else if (node->left == &sentinel) {
|
|
121
|
-
a = node->right;
|
|
122
|
-
while (a->left != &sentinel)
|
|
123
|
-
a = a->left;
|
|
124
|
-
b = unlinkproperty(node->right, a->name, &temp);
|
|
125
|
-
temp->level = node->level;
|
|
126
|
-
temp->left = node->left;
|
|
127
|
-
temp->right = b;
|
|
128
|
-
node = temp;
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
a = node->left;
|
|
132
|
-
while (a->right != &sentinel)
|
|
133
|
-
a = a->right;
|
|
134
|
-
b = unlinkproperty(node->left, a->name, &temp);
|
|
135
|
-
temp->level = node->level;
|
|
136
|
-
temp->left = b;
|
|
137
|
-
temp->right = node->right;
|
|
138
|
-
node = temp;
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (node->left->level < node->level - 1 || node->right->level < node->level - 1)
|
|
143
|
-
{
|
|
144
|
-
if (node->right->level > --node->level)
|
|
145
|
-
node->right->level = node->level;
|
|
146
|
-
node = skew(node);
|
|
147
|
-
node->right = skew(node->right);
|
|
148
|
-
node->right->right = skew(node->right->right);
|
|
149
|
-
node = split(node);
|
|
150
|
-
node->right = split(node->right);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
return node;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
static js_Property *deleteproperty(js_State *J, js_Object *obj, js_Property *tree, const char *name)
|
|
157
|
-
{
|
|
158
|
-
js_Property *garbage = &sentinel;
|
|
159
|
-
tree = unlinkproperty(tree, name, &garbage);
|
|
160
|
-
if (garbage != &sentinel)
|
|
161
|
-
freeproperty(J, obj, garbage);
|
|
162
|
-
return tree;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
js_Object *jsV_newobject(js_State *J, enum js_Class type, js_Object *prototype)
|
|
166
|
-
{
|
|
167
|
-
js_Object *obj = js_malloc(J, sizeof *obj);
|
|
168
|
-
memset(obj, 0, sizeof *obj);
|
|
169
|
-
obj->gcmark = 0;
|
|
170
|
-
obj->gcnext = J->gcobj;
|
|
171
|
-
J->gcobj = obj;
|
|
172
|
-
++J->gccounter;
|
|
173
|
-
|
|
174
|
-
obj->type = type;
|
|
175
|
-
obj->properties = &sentinel;
|
|
176
|
-
obj->prototype = prototype;
|
|
177
|
-
obj->extensible = 1;
|
|
178
|
-
return obj;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
js_Property *jsV_getownproperty(js_State *J, js_Object *obj, const char *name)
|
|
182
|
-
{
|
|
183
|
-
return lookup(obj->properties, name);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
js_Property *jsV_getpropertyx(js_State *J, js_Object *obj, const char *name, int *own)
|
|
187
|
-
{
|
|
188
|
-
*own = 1;
|
|
189
|
-
do {
|
|
190
|
-
js_Property *ref = lookup(obj->properties, name);
|
|
191
|
-
if (ref)
|
|
192
|
-
return ref;
|
|
193
|
-
obj = obj->prototype;
|
|
194
|
-
*own = 0;
|
|
195
|
-
} while (obj);
|
|
196
|
-
return NULL;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
js_Property *jsV_getproperty(js_State *J, js_Object *obj, const char *name)
|
|
200
|
-
{
|
|
201
|
-
do {
|
|
202
|
-
js_Property *ref = lookup(obj->properties, name);
|
|
203
|
-
if (ref)
|
|
204
|
-
return ref;
|
|
205
|
-
obj = obj->prototype;
|
|
206
|
-
} while (obj);
|
|
207
|
-
return NULL;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
static js_Property *jsV_getenumproperty(js_State *J, js_Object *obj, const char *name)
|
|
211
|
-
{
|
|
212
|
-
do {
|
|
213
|
-
js_Property *ref = lookup(obj->properties, name);
|
|
214
|
-
if (ref && !(ref->atts & JS_DONTENUM))
|
|
215
|
-
return ref;
|
|
216
|
-
obj = obj->prototype;
|
|
217
|
-
} while (obj);
|
|
218
|
-
return NULL;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
js_Property *jsV_setproperty(js_State *J, js_Object *obj, const char *name)
|
|
222
|
-
{
|
|
223
|
-
js_Property *result;
|
|
224
|
-
|
|
225
|
-
if (!obj->extensible) {
|
|
226
|
-
result = lookup(obj->properties, name);
|
|
227
|
-
if (J->strict && !result)
|
|
228
|
-
js_typeerror(J, "object is non-extensible");
|
|
229
|
-
return result;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
obj->properties = insert(J, obj, obj->properties, name, &result);
|
|
233
|
-
|
|
234
|
-
return result;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
void jsV_delproperty(js_State *J, js_Object *obj, const char *name)
|
|
238
|
-
{
|
|
239
|
-
obj->properties = deleteproperty(J, obj, obj->properties, name);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/* Flatten hierarchy of enumerable properties into an iterator object */
|
|
243
|
-
|
|
244
|
-
static js_Iterator *itnewnode(js_State *J, const char *name, js_Iterator *next) {
|
|
245
|
-
int n = strlen(name) + 1;
|
|
246
|
-
js_Iterator *node = js_malloc(J, offsetof(js_Iterator, name) + n);
|
|
247
|
-
node->next = next;
|
|
248
|
-
memcpy(node->name, name, n);
|
|
249
|
-
return node;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
static js_Iterator *itwalk(js_State *J, js_Iterator *iter, js_Property *prop, js_Object *seen)
|
|
253
|
-
{
|
|
254
|
-
if (prop->right != &sentinel)
|
|
255
|
-
iter = itwalk(J, iter, prop->right, seen);
|
|
256
|
-
if (!(prop->atts & JS_DONTENUM)) {
|
|
257
|
-
if (!seen || !jsV_getenumproperty(J, seen, prop->name)) {
|
|
258
|
-
iter = itnewnode(J, prop->name, iter);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
if (prop->left != &sentinel)
|
|
262
|
-
iter = itwalk(J, iter, prop->left, seen);
|
|
263
|
-
return iter;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
static js_Iterator *itflatten(js_State *J, js_Object *obj)
|
|
267
|
-
{
|
|
268
|
-
js_Iterator *iter = NULL;
|
|
269
|
-
if (obj->prototype)
|
|
270
|
-
iter = itflatten(J, obj->prototype);
|
|
271
|
-
if (obj->properties != &sentinel)
|
|
272
|
-
iter = itwalk(J, iter, obj->properties, obj->prototype);
|
|
273
|
-
return iter;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
js_Object *jsV_newiterator(js_State *J, js_Object *obj, int own)
|
|
277
|
-
{
|
|
278
|
-
js_Object *io = jsV_newobject(J, JS_CITERATOR, NULL);
|
|
279
|
-
io->u.iter.target = obj;
|
|
280
|
-
io->u.iter.i = 0;
|
|
281
|
-
io->u.iter.n = 0;
|
|
282
|
-
if (own) {
|
|
283
|
-
io->u.iter.head = NULL;
|
|
284
|
-
if (obj->properties != &sentinel)
|
|
285
|
-
io->u.iter.head = itwalk(J, io->u.iter.head, obj->properties, NULL);
|
|
286
|
-
} else {
|
|
287
|
-
io->u.iter.head = itflatten(J, obj);
|
|
288
|
-
}
|
|
289
|
-
io->u.iter.current = io->u.iter.head;
|
|
290
|
-
|
|
291
|
-
if (obj->type == JS_CSTRING)
|
|
292
|
-
io->u.iter.n = obj->u.s.length;
|
|
293
|
-
|
|
294
|
-
if (obj->type == JS_CARRAY && obj->u.a.simple)
|
|
295
|
-
io->u.iter.n = obj->u.a.flat_length;
|
|
296
|
-
|
|
297
|
-
return io;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
const char *jsV_nextiterator(js_State *J, js_Object *io)
|
|
301
|
-
{
|
|
302
|
-
if (io->type != JS_CITERATOR)
|
|
303
|
-
js_typeerror(J, "not an iterator");
|
|
304
|
-
if (io->u.iter.i < io->u.iter.n) {
|
|
305
|
-
js_itoa(J->scratch, io->u.iter.i);
|
|
306
|
-
io->u.iter.i++;
|
|
307
|
-
return J->scratch;
|
|
308
|
-
}
|
|
309
|
-
while (io->u.iter.current) {
|
|
310
|
-
const char *name = io->u.iter.current->name;
|
|
311
|
-
io->u.iter.current = io->u.iter.current->next;
|
|
312
|
-
if (jsV_getproperty(J, io->u.iter.target, name))
|
|
313
|
-
return name;
|
|
314
|
-
}
|
|
315
|
-
return NULL;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/* Walk all the properties and delete them one by one for arrays */
|
|
319
|
-
|
|
320
|
-
void jsV_resizearray(js_State *J, js_Object *obj, int newlen)
|
|
321
|
-
{
|
|
322
|
-
char buf[32];
|
|
323
|
-
const char *s;
|
|
324
|
-
int k;
|
|
325
|
-
assert(!obj->u.a.simple);
|
|
326
|
-
if (newlen < obj->u.a.length) {
|
|
327
|
-
if (obj->u.a.length > obj->count * 2) {
|
|
328
|
-
js_Object *it = jsV_newiterator(J, obj, 1);
|
|
329
|
-
while ((s = jsV_nextiterator(J, it))) {
|
|
330
|
-
k = jsV_numbertointeger(jsV_stringtonumber(J, s));
|
|
331
|
-
if (k >= newlen && !strcmp(s, jsV_numbertostring(J, buf, k)))
|
|
332
|
-
jsV_delproperty(J, obj, s);
|
|
333
|
-
}
|
|
334
|
-
} else {
|
|
335
|
-
for (k = newlen; k < obj->u.a.length; ++k) {
|
|
336
|
-
jsV_delproperty(J, obj, js_itoa(buf, k));
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
obj->u.a.length = newlen;
|
|
341
|
-
}
|
package/vendor/mujs/jsregexp.c
DELETED
|
@@ -1,232 +0,0 @@
|
|
|
1
|
-
#include "jsi.h"
|
|
2
|
-
#include "regexp.h"
|
|
3
|
-
|
|
4
|
-
static char *escaperegexp(js_State *J, const char *pattern) {
|
|
5
|
-
char *copy, *p;
|
|
6
|
-
const char *s;
|
|
7
|
-
int n = 0;
|
|
8
|
-
for (s = pattern; *s; ++s) {
|
|
9
|
-
if (*s == '/')
|
|
10
|
-
++n;
|
|
11
|
-
++n;
|
|
12
|
-
}
|
|
13
|
-
copy = p = js_malloc(J, n+1);
|
|
14
|
-
for (s = pattern; *s; ++s) {
|
|
15
|
-
if (*s == '/')
|
|
16
|
-
*p++ = '\\';
|
|
17
|
-
*p++ = *s;
|
|
18
|
-
}
|
|
19
|
-
*p = 0;
|
|
20
|
-
return copy;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
static void js_newregexpx(js_State *J, const char *pattern, int flags, int is_clone)
|
|
24
|
-
{
|
|
25
|
-
const char *error;
|
|
26
|
-
js_Object *obj;
|
|
27
|
-
Reprog *prog;
|
|
28
|
-
int opts;
|
|
29
|
-
|
|
30
|
-
obj = jsV_newobject(J, JS_CREGEXP, J->RegExp_prototype);
|
|
31
|
-
|
|
32
|
-
opts = 0;
|
|
33
|
-
if (flags & JS_REGEXP_I) opts |= REG_ICASE;
|
|
34
|
-
if (flags & JS_REGEXP_M) opts |= REG_NEWLINE;
|
|
35
|
-
|
|
36
|
-
prog = js_regcompx(J->alloc, J->actx, pattern, opts, &error);
|
|
37
|
-
if (!prog)
|
|
38
|
-
js_syntaxerror(J, "regular expression: %s", error);
|
|
39
|
-
|
|
40
|
-
obj->u.r.prog = prog;
|
|
41
|
-
obj->u.r.source = is_clone ? js_strdup(J, pattern) : escaperegexp(J, pattern);
|
|
42
|
-
obj->u.r.flags = flags;
|
|
43
|
-
obj->u.r.last = 0;
|
|
44
|
-
js_pushobject(J, obj);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
void js_newregexp(js_State *J, const char *pattern, int flags)
|
|
48
|
-
{
|
|
49
|
-
js_newregexpx(J, pattern, flags, 0);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
void js_RegExp_prototype_exec(js_State *J, js_Regexp *re, const char *text)
|
|
53
|
-
{
|
|
54
|
-
const char *haystack;
|
|
55
|
-
int result;
|
|
56
|
-
int i;
|
|
57
|
-
int opts;
|
|
58
|
-
Resub m;
|
|
59
|
-
|
|
60
|
-
haystack = text;
|
|
61
|
-
opts = 0;
|
|
62
|
-
if (re->flags & JS_REGEXP_G) {
|
|
63
|
-
if (re->last > strlen(haystack)) {
|
|
64
|
-
re->last = 0;
|
|
65
|
-
js_pushnull(J);
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
if (re->last > 0) {
|
|
69
|
-
haystack = text + re->last;
|
|
70
|
-
opts |= REG_NOTBOL;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
result = js_regexec(re->prog, haystack, &m, opts);
|
|
75
|
-
if (result < 0)
|
|
76
|
-
js_error(J, "regexec failed");
|
|
77
|
-
if (result == 0) {
|
|
78
|
-
js_newarray(J);
|
|
79
|
-
js_pushstring(J, text);
|
|
80
|
-
js_setproperty(J, -2, "input");
|
|
81
|
-
js_pushnumber(J, js_utfptrtoidx(text, m.sub[0].sp));
|
|
82
|
-
js_setproperty(J, -2, "index");
|
|
83
|
-
for (i = 0; i < m.nsub; ++i) {
|
|
84
|
-
js_pushlstring(J, m.sub[i].sp, m.sub[i].ep - m.sub[i].sp);
|
|
85
|
-
js_setindex(J, -2, i);
|
|
86
|
-
}
|
|
87
|
-
if (re->flags & JS_REGEXP_G)
|
|
88
|
-
re->last = m.sub[0].ep - text;
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (re->flags & JS_REGEXP_G)
|
|
93
|
-
re->last = 0;
|
|
94
|
-
|
|
95
|
-
js_pushnull(J);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
static void Rp_test(js_State *J)
|
|
99
|
-
{
|
|
100
|
-
js_Regexp *re;
|
|
101
|
-
const char *text;
|
|
102
|
-
int result;
|
|
103
|
-
int opts;
|
|
104
|
-
Resub m;
|
|
105
|
-
|
|
106
|
-
re = js_toregexp(J, 0);
|
|
107
|
-
text = js_tostring(J, 1);
|
|
108
|
-
|
|
109
|
-
opts = 0;
|
|
110
|
-
if (re->flags & JS_REGEXP_G) {
|
|
111
|
-
if (re->last > strlen(text)) {
|
|
112
|
-
re->last = 0;
|
|
113
|
-
js_pushboolean(J, 0);
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
if (re->last > 0) {
|
|
117
|
-
text += re->last;
|
|
118
|
-
opts |= REG_NOTBOL;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
result = js_regexec(re->prog, text, &m, opts);
|
|
123
|
-
if (result < 0)
|
|
124
|
-
js_error(J, "regexec failed");
|
|
125
|
-
if (result == 0) {
|
|
126
|
-
if (re->flags & JS_REGEXP_G)
|
|
127
|
-
re->last = re->last + (m.sub[0].ep - text);
|
|
128
|
-
js_pushboolean(J, 1);
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (re->flags & JS_REGEXP_G)
|
|
133
|
-
re->last = 0;
|
|
134
|
-
|
|
135
|
-
js_pushboolean(J, 0);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
static void jsB_new_RegExp(js_State *J)
|
|
139
|
-
{
|
|
140
|
-
js_Regexp *old;
|
|
141
|
-
const char *pattern;
|
|
142
|
-
int flags;
|
|
143
|
-
int is_clone = 0;
|
|
144
|
-
|
|
145
|
-
if (js_isregexp(J, 1)) {
|
|
146
|
-
if (js_isdefined(J, 2))
|
|
147
|
-
js_typeerror(J, "cannot supply flags when creating one RegExp from another");
|
|
148
|
-
old = js_toregexp(J, 1);
|
|
149
|
-
pattern = old->source;
|
|
150
|
-
flags = old->flags;
|
|
151
|
-
is_clone = 1;
|
|
152
|
-
} else if (js_isundefined(J, 1)) {
|
|
153
|
-
pattern = "(?:)";
|
|
154
|
-
flags = 0;
|
|
155
|
-
} else {
|
|
156
|
-
pattern = js_tostring(J, 1);
|
|
157
|
-
flags = 0;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (strlen(pattern) == 0)
|
|
161
|
-
pattern = "(?:)";
|
|
162
|
-
|
|
163
|
-
if (js_isdefined(J, 2)) {
|
|
164
|
-
const char *s = js_tostring(J, 2);
|
|
165
|
-
int g = 0, i = 0, m = 0;
|
|
166
|
-
while (*s) {
|
|
167
|
-
if (*s == 'g') ++g;
|
|
168
|
-
else if (*s == 'i') ++i;
|
|
169
|
-
else if (*s == 'm') ++m;
|
|
170
|
-
else js_syntaxerror(J, "invalid regular expression flag: '%c'", *s);
|
|
171
|
-
++s;
|
|
172
|
-
}
|
|
173
|
-
if (g > 1) js_syntaxerror(J, "invalid regular expression flag: 'g'");
|
|
174
|
-
if (i > 1) js_syntaxerror(J, "invalid regular expression flag: 'i'");
|
|
175
|
-
if (m > 1) js_syntaxerror(J, "invalid regular expression flag: 'm'");
|
|
176
|
-
if (g) flags |= JS_REGEXP_G;
|
|
177
|
-
if (i) flags |= JS_REGEXP_I;
|
|
178
|
-
if (m) flags |= JS_REGEXP_M;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
js_newregexpx(J, pattern, flags, is_clone);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
static void jsB_RegExp(js_State *J)
|
|
185
|
-
{
|
|
186
|
-
if (js_isregexp(J, 1))
|
|
187
|
-
return;
|
|
188
|
-
jsB_new_RegExp(J);
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
static void Rp_toString(js_State *J)
|
|
192
|
-
{
|
|
193
|
-
js_Regexp *re;
|
|
194
|
-
char * volatile out = NULL;
|
|
195
|
-
|
|
196
|
-
re = js_toregexp(J, 0);
|
|
197
|
-
|
|
198
|
-
if (js_try(J)) {
|
|
199
|
-
js_free(J, out);
|
|
200
|
-
js_throw(J);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
out = js_malloc(J, strlen(re->source) + 6); /* extra space for //gim */
|
|
204
|
-
strcpy(out, "/");
|
|
205
|
-
strcat(out, re->source);
|
|
206
|
-
strcat(out, "/");
|
|
207
|
-
if (re->flags & JS_REGEXP_G) strcat(out, "g");
|
|
208
|
-
if (re->flags & JS_REGEXP_I) strcat(out, "i");
|
|
209
|
-
if (re->flags & JS_REGEXP_M) strcat(out, "m");
|
|
210
|
-
|
|
211
|
-
js_pop(J, 0);
|
|
212
|
-
js_pushstring(J, out);
|
|
213
|
-
js_endtry(J);
|
|
214
|
-
js_free(J, out);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
static void Rp_exec(js_State *J)
|
|
218
|
-
{
|
|
219
|
-
js_RegExp_prototype_exec(J, js_toregexp(J, 0), js_tostring(J, 1));
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
void jsB_initregexp(js_State *J)
|
|
223
|
-
{
|
|
224
|
-
js_pushobject(J, J->RegExp_prototype);
|
|
225
|
-
{
|
|
226
|
-
jsB_propf(J, "RegExp.prototype.toString", Rp_toString, 0);
|
|
227
|
-
jsB_propf(J, "RegExp.prototype.test", Rp_test, 0);
|
|
228
|
-
jsB_propf(J, "RegExp.prototype.exec", Rp_exec, 0);
|
|
229
|
-
}
|
|
230
|
-
js_newcconstructor(J, jsB_RegExp, jsB_new_RegExp, "RegExp", 1);
|
|
231
|
-
js_defglobal(J, "RegExp", JS_DONTENUM);
|
|
232
|
-
}
|