-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_props.c
580 lines (454 loc) · 12.9 KB
/
js_props.c
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#include <ctype.h>
#include "js.h"
#include "js_db.h"
#include "js_props.h"
#include "js_string.h"
value_t builtinProtoHndl[Hndl_max];
uint32_t builtinMapHndl[Hndl_max];
value_t builtinProto[vt_MAX];
uint32_t builtinMap[vt_MAX];
value_t propBoolLength(value_t val, bool lVal) {
value_t len;
len.bits = vt_int;
len.nval = 1;
return len;
}
value_t propFcnLength(value_t val, bool lVal) {
value_t len;
len.bits = vt_int;
len.nval = val.closure->fd->nparams;
return len;
}
value_t propFcnName(value_t val, bool lVal) {
stringNode *sn;
symNode *sym;
value_t obj;
if (!val.closure->fd->name)
return obj.bits = vt_undef, obj;
sym = (symNode *)(val.closure->table + val.closure->fd->name);
sn = (stringNode *)(val.closure->table + sym->name);
return newString(sn->str.val, sn->str.len);
}
value_t propFcnDisplayName(value_t val, bool lVal) {
stringNode *sn;
symNode *sym;
value_t obj;
if (!val.closure->fd->name)
return obj.bits = vt_undef, obj;
sym = (symNode *)(val.closure->table + val.closure->fd->name);
sn = (stringNode *)(val.closure->table + sym->name);
return newString(sn->str.val, sn->str.len);
}
value_t fcnFcnApply(value_t *args, value_t thisVal, environment_t *env) {
value_t arguments = newArray(array_value, vec_cnt(args) - 1);
array_t *aval = arguments.addr;
uint32_t idx;
for (idx = 1; idx < vec_cnt(args); idx++) {
aval->valuePtr[idx - 1] = args[idx];
incrRefCnt(args[idx]);
}
return fcnCall(thisVal, arguments, args[0], false, env);
}
value_t fcnFcnCall(value_t *args, value_t thisVal, environment_t *env) {
value_t arguments = newArray(array_value, vec_cnt(args) - 1);
array_t *aval = arguments.addr;
uint32_t idx;
for (idx = 1; idx < vec_cnt(args); idx++) {
aval->valuePtr[idx - 1] = args[idx];
incrRefCnt(args[idx]);
}
return fcnCall(thisVal, arguments, args[0], false, env);
}
value_t propFcnProto(value_t val, bool lVal) {
value_t ref;
if (!lVal)
return val.closure->protoObj;
ref.bits = vt_lval;
ref.lval = &val.closure->protoObj;
return ref;
}
value_t fcnFcnValueOf(value_t *args, value_t thisVal, environment_t *env) {
if (vec_cnt(args))
return *args;
else
return thisVal;
}
value_t fcnBoolValueOf(value_t *args, value_t thisVal, environment_t *env) {
if (vec_cnt(args))
return *args;
else
return thisVal;
}
value_t fcnBoolToString(value_t *args, value_t thisVal, environment_t *env) {
value_t obj, val;
if (vec_cnt(args))
obj = *args;
else
obj = thisVal;
val.bits = vt_string;
if (obj.boolean)
val.addr = &TrueStr;
else
val.addr = &FalseStr;
return val;
}
value_t fcnNumToString(value_t *args, value_t thisVal, environment_t *env) {
value_t val, s, obj;
char buff[64];
int len = 0;
if (vec_cnt(args))
obj = *args;
else
obj = thisVal;
switch (obj.type) {
case vt_int:
#ifndef _WIN32
len = snprintf(buff, sizeof(buff), "%" PRIi64, obj.nval);
#else
len = _snprintf_s(buff, sizeof(buff), _TRUNCATE, "%" PRIi64, obj.nval);
#endif
break;
case vt_dbl:
#ifndef _WIN32
len = snprintf(buff, sizeof(buff), "%.16G", obj.dbl);
#else
len = _snprintf_s(buff, sizeof(buff), _TRUNCATE, "%.16G", obj.dbl);
#endif
if (!(obj.dbl - (uint64_t)obj.dbl))
buff[len++] = '.', buff[len++] = '0', buff[len] = 0;
break;
case vt_infinite:
val.bits = vt_string;
if (obj.negative)
val.addr = &InfinityStr;
else
val.addr = &MInfinityStr;
return val;
case vt_null:
val.bits = vt_string;
val.addr = &NullStr;
return val;
case vt_nan:
val.bits = vt_string;
val.addr = &NaNStr;
return val;
default:
fprintf(stderr, "Error: NumberToString => invalid type: %s\n", strtype(obj));
return s.status = ERROR_script_internal, s;
}
return newString(buff, len);
}
value_t fcnNumValueOf(value_t *args, value_t thisVal, environment_t *env) {
if (vec_cnt(args))
return *args;
else
return thisVal;
}
value_t fcnNumToPrecision(value_t *args, value_t thisVal, environment_t *env) {
value_t digits;
char buff[512];
double dbl;
int len;
if (thisVal.type == vt_int)
dbl = (double)thisVal.nval;
if (thisVal.type == vt_dbl)
dbl = thisVal.dbl;
if (vec_cnt(args) > 0)
digits = conv2Int(args[0], false);
else {
digits.bits = vt_int;
digits.nval = 15;
}
#ifndef _WIN32
len = snprintf(buff, sizeof(buff), "%.*e", (int)digits.nval, dbl);
#else
len = _snprintf_s(buff, sizeof(buff), _TRUNCATE, "%.*e", (int)digits.nval, dbl);
#endif
return newString(buff, len);
}
value_t fcnNumToFixed(value_t *args, value_t thisVal, environment_t *env) {
value_t digits;
char buff[512];
double dbl;
int len;
if (thisVal.type == vt_int)
dbl = (double)thisVal.nval;
if (thisVal.type == vt_dbl)
dbl = thisVal.dbl;
if (vec_cnt(args) > 0)
digits = conv2Int(args[0], false);
else {
digits.bits = vt_int;
digits.nval = 15;
}
#ifndef _WIN32
len = snprintf(buff, sizeof(buff), "%.*e", (int)digits.nval, dbl);
#else
len = _snprintf_s(buff, sizeof(buff), _TRUNCATE, "%.*e", (int)digits.nval, dbl);
#endif
return newString(buff, len);
}
value_t fcnNumToExponential(value_t *args, value_t thisVal, environment_t *env) {
value_t digits;
char buff[512];
double dbl;
int len;
if (thisVal.type == vt_int)
dbl = (double)thisVal.nval;
if (thisVal.type == vt_dbl)
dbl = thisVal.dbl;
if (vec_cnt(args) > 0)
digits = conv2Int(args[0], false);
else {
digits.bits = vt_int;
digits.nval = 15;
}
#ifndef _WIN32
len = snprintf(buff, sizeof(buff), "%.*e", (int)digits.nval, dbl);
#else
len = _snprintf_s(buff, sizeof(buff), _TRUNCATE, "%.*e", (int)digits.nval, dbl);
#endif
return newString(buff, len);
}
extern PropFcn builtinKeyFcns[];
extern PropFcn builtinDateFcns[];
extern PropVal builtinStrProp[];
extern PropVal builtinObjProp[];
extern PropVal builtinDocIdProp[];
extern PropVal builtinArrayProp[];
PropVal builtinBoolProp[] = {
{ propBoolLength, "length" },
{ NULL, NULL}
};
PropVal builtinNumProp[] = {
{ NULL, NULL}
};
extern PropVal builtinDateProp[];
PropVal builtinFcnProp[] = {
{ propFcnName, "name" },
{ propFcnLength, "length" },
{ propFcnProto, "prototype", true },
{ propFcnDisplayName, "displayName" },
{ NULL, NULL}
};
extern PropFcn builtinStrFcns[];
extern PropFcn builtinObjFcns[];
extern PropFcn builtinDocIdFcns[];
extern PropFcn builtinArrayFcns[];
PropFcn builtinNumFcns[] = {
{ fcnNumValueOf, "valueOf" },
{ fcnNumToString, "toString" },
{ fcnNumToExponential, "toExponential" },
{ fcnNumToFixed, "toFixed" },
{ fcnNumToPrecision, "toPrecision" },
{ NULL, NULL}
};
PropFcn builtinBoolFcns[] = {
{ fcnBoolValueOf, "valueOf" },
{ fcnBoolToString, "toString" },
{ NULL, NULL}
};
PropFcn builtinFcnFcns[] = {
{ fcnFcnValueOf, "valueOf" },
{ fcnFcnApply, "apply" },
{ fcnFcnCall, "call" },
{ NULL, NULL}
};
extern PropVal builtinDbProp[];
extern PropVal builtinStoreProp[];
extern PropVal builtinIdxProp[];
extern PropVal builtinCursorProp[];
extern PropVal builtinIterProp[];
extern PropVal builtinTxnProp[];
extern PropVal builtinDocProp[];
extern PropVal builtinDocIdProp[];
extern PropVal builtinKeyProp[];
extern PropVal builtinCatalogProp[];
extern PropFcn builtinDbFcns[];
extern PropFcn builtinStoreFcns[];
extern PropFcn builtinIdxFcns[];
extern PropFcn builtinCursorFcns[];
extern PropFcn builtinIterFcns[];
extern PropFcn builtinTxnFcns[];
extern PropFcn builtinDocFcns[];
extern PropFcn builtinDocIdFcns[];
extern PropFcn builtinCatalogFcns[];
PropVal *builtinProp[] = {
builtinStrProp,
builtinObjProp,
builtinArrayProp,
builtinNumProp,
builtinBoolProp,
builtinDateProp,
builtinFcnProp,
builtinDocProp,
builtinDocIdProp,
builtinKeyProp,
builtinCatalogProp,
builtinDbProp,
builtinStoreProp,
builtinIdxProp,
builtinCursorProp,
builtinIterProp,
builtinTxnProp,
NULL
};
PropFcn *builtinFcn[] = {
builtinStrFcns,
builtinObjFcns,
builtinArrayFcns,
builtinNumFcns,
builtinBoolFcns,
builtinDateFcns,
builtinFcnFcns,
builtinDocFcns,
builtinDocIdFcns,
builtinKeyFcns,
builtinCatalogFcns,
builtinDbFcns,
builtinStoreFcns,
builtinIdxFcns,
builtinCursorFcns,
builtinIterFcns,
builtinTxnFcns};
char *builtinNames[] = {
"String.prototype.",
"Object.prototype.",
"Array.prototype.",
"Number.prototype.",
"Boolean.prototype.",
"Date.prototype.",
"Functions.prototype.",
"Doc.prototype.",
"DocId.prototype.",
"Key.prototype.",
"Catalog.prototype.",
"Db.prototype.",
"DocStore.prototype.",
"Index.prototype.",
"Cursor.prototype.",
"Iterator.prototype.",
"Txn.prototype."
};
value_t builtinVal[sizeof(builtinNames) / sizeof(char *)];
// install built-in properties into system level object
value_t js_installProps(uint32_t args, environment_t *env) {
PropVal *proptbl;
PropFcn *fcntbl;
value_t table, s;
value_t fcn, obj;
s.bits = vt_status;
obj = eval_arg(&args, env);
if (vt_closure != obj.type) {
fprintf(stderr, "Error: installProps => expecting closure => %s\n", strtype(obj));
return s.status = ERROR_script_internal, s;
}
table = eval_arg(&args, env);
if (vt_int != table.type) {
fprintf(stderr, "Error: installProps => expecting int => %s\n", strtype(table));
return s.status = ERROR_script_internal, s;
}
if (table.nval < sizeof(builtinProp) / sizeof(*builtinProp))
if ((proptbl = builtinProp[table.nval])) {
value_t base;
while (proptbl->fcn) {
if (proptbl->isBase)
base = obj.closure->obj;
else
base = obj.closure->protoObj;
proptbl->str = newString(proptbl->name, -1);
fcn.bits = vt_propval;
fcn.nval = (PropVal *)proptbl - builtinProp[table.nval];
fcn.subType = (uint32_t)table.nval;
replaceValue(lookup(base, proptbl->str, true, 0), fcn);
proptbl++;
}
} else {
fprintf(stderr, "Error: installProps => expecting property table idx => %d\n", (int)table.nval);
return s.status = ERROR_script_internal, s;
}
if (table.nval < sizeof(builtinFcn) / sizeof(*builtinFcn))
if ((fcntbl = builtinFcn[table.nval])) {
value_t base;
while (fcntbl->fcn) {
if (fcntbl->isBase)
base = obj.closure->obj;
else
base = obj.closure->protoObj;
fcntbl->str = newString(fcntbl->name, -1);
fcn.bits = vt_propfcn;
fcn.nval = (PropFcn *)fcntbl - builtinFcn[table.nval];
fcn.subType = (uint32_t)table.nval;
replaceValue(lookup(base, fcntbl->str, true, 0), fcn);
fcntbl++;
}
} else {
fprintf(stderr, "Error: installProps => expecting fcn table idx => %d\n", (int)table.nval);
return s.status = ERROR_script_internal, s;
}
if (table.nval < sizeof(builtinNames) / sizeof(*builtinNames))
builtinVal[table.nval] = newString(builtinNames[table.nval], -1);
else {
fprintf(stderr, "Error: installProps => expecting name table idx => %d\n", (int)table.nval);
return s.status = ERROR_script_internal, s;
}
if (args) for(;;) {
value_t v = eval_arg(&args, env);
if (v.type == vt_endlist)
break;
if (v.type == vt_int && v.nval == vt_hndl) {
v = eval_arg(&args, env);
if (v.type == vt_endlist) break;
if (v.type == vt_int)
if (v.nval < sizeof(builtinProtoHndl)/sizeof(builtinProtoHndl[0])) {
builtinProtoHndl[v.nval] = obj.closure->protoObj;
incrRefCnt(obj.closure->protoObj);
builtinMapHndl[v.nval] = (uint32_t)table.nval;
break;
}
} else if (v.type == vt_int)
if (v.nval < vt_MAX) {
builtinProto[v.nval] = obj.closure->protoObj;
incrRefCnt(obj.closure->protoObj);
builtinMap[v.nval] = (uint32_t)table.nval;
}
}
s.status = OK;
return s;
}
value_t getPropFcnName(value_t fcn) {
value_t name, ans[1];
if (fcn.type == vt_propfcn)
name = builtinFcn[fcn.subType][fcn.nval].str;
else
name = builtinProp[fcn.subType][fcn.nval].str;
*ans = builtinVal[fcn.subType];
valueCat(ans, name, false);
return *ans;
}
value_t callObjFcn(value_t obj, string_t *name, bool abandon, environment_t *env) {
value_t result, original = obj;
if (obj.type == vt_lval)
obj = *obj.lval;
if (obj.type == vt_document)
obj = getDocObject(obj);
// find the function in the object, or its prototype chain
result = lookupAttribute(obj, name, original, false, true);
if (abandon)
abandonValueIfDiff(obj, result);
return result;
}
value_t callFcnProp(value_t prop, value_t original, bool lVal) {
value_t v;
if (prop.subType == builtinMap[original.type])
v = (builtinProp[prop.subType][prop.nval].fcn)(original, lVal);
else
v.bits = vt_undef;
return v;
}
value_t callFcnFcn(value_t fcn, value_t *args, environment_t *env) {
value_t thisVal = env->topFrame->nextThis;
if (thisVal.type == vt_lval)
thisVal = *thisVal.lval;
return (builtinFcn[fcn.subType][fcn.nval].fcn)(args, thisVal, env);
}