- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs.h
More file actions
Latest commit
406 lines (324 loc) · 9.11 KB
/
Copy pathjs.h
File metadata and controls
406 lines (324 loc) · 9.11 KB
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
#pragma once
#include<stdio.h>
#include<stdint.h>
#include<stdbool.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<inttypes.h>
#include"database/db.h"
#include"database/db_api.h"
#include"database/db_handle.h"
#ifdef_WIN32
#definestrcasecmp _strnicmp
#endif
externboolparseDebug;
externboolhoistDebug;
externboolevalDebug;
externboolmathNums;
externbooldebug;
// Strings
typedefstruct {
uint32_tlen;
uint8_tval[];
} string_t;
// memory allocation
void*js_realloc(void*old, uint32_t*size, boolzeroit);
void*js_alloc(uint32_tlen, boolzeroit);
uint32_tjs_size (void*obj);
voidjs_free(void*obj);
// typedefs from structures
typedefstructValuevalue_t;
typedefstructClosureclosure_t;
typedefstructSymTablesymtab_t;
typedefstructFcnDeclNodefcnDeclNode;
typedefstructDocumentdocument_t;
typedefstructDbObjectdbobject_t;
typedefstructObjectobject_t;
typedefstructDbArraydbarray_t;
typedefstructArrayarray_t;
// reference counting
boolabandonValueIfDiff(value_tval, value_ttest);
voidincrRefCnt (value_tval);
voidabandonValue(value_tval);
voiddeleteValue(value_tval);
voidabandonSlot(value_t*slot);
voiddeleteSlot(value_t*slot);
booldecrRefCnt (value_tval);
#include"js_error.h"
typedefstruct {
uint32_tframeIdx:32; // var frame idx
uint32_tdepth:16; // frame depth
uint32_tscoped:1; // symbol is scoped
uint32_tfixed:1; // scope fix-up done
} symbol_t;
typedefenum {
ctl_none,
ctl_return,
ctl_continue,
ctl_break,
ctl_error,
ctl_throw,
ctl_delete,
} ctlType;
typedefenum {
aux_none,
aux_newobj,
aux_endstmt,
} auxType;
enumflagType {
flag_decl=1, // node is a symbol declaration
flag_lval=2, // node produces lval
flag_scope=4, // variable declared in block scope
flag_operand=8// node produces operand
};
#ifdefapple
#defineStatus int
#endif
value_tnewString(void*value, intlen);
// Values
typedefenum {
vt_undef=0,
vt_bool,
vt_int,
vt_dbl,
vt_date,
vt_infinite,
vt_number,
vt_string,
vt_nan,
vt_null,
vt_file,
vt_status,
vt_control,
vt_closure,
vt_endlist,
vt_document,
vt_docId,
vt_txnId, // 64 bit immediate
vt_lval,
vt_centi,
vt_array,
vt_object,
vt_binary,
vt_function,
vt_symbol,
vt_uuid, // 16 byte string
vt_md5,
vt_propfcn,
vt_propval,
vt_weakref,
vt_hndl,
vt_key,
vt_builtin,
vt_MAX
} valuetype_t;
structValue {
union {
struct {
uint8_ttype:8;
uint8_tsubType:8;
uint8_trefcount:1; // value is reference counted.
uint8_tweakcount:1; // value is weak reference.
uint8_tmarshaled:1; // value is marshaled in a document
uint8_tobjvalue:1; // object value occurs at ptr
uint8_treadonly:1; // value is read-only
uint8_tlvalue:1; // value is an lvalue
uint8_tfiller:2; // available bits
uint8_tfiller8; // available bits
union {
uint32_toffset; // offset from document base
uint32_thndlIdx; // docId docStore Idx
};
};
valuetype_tdisp:8;
uint64_tbits; // set bits to valueType to initialize
};
union {
void*addr;
symbol_tsym[1];
string_t*str;
int64_tnval;
doubledbl;
FILE*file;
ctlTypectl;
uint64_tbuiltIn;
value_t*lval;
Statusstatus;
uint8_tkey[8];
uint64_tboolean;
uint64_tnegative;
uint64_tidBits;
int64_tdate;
object_t*oval;
array_t*aval;
closure_t*closure;
structRawObj*raw;
structFcnDeclNode*fcn;
document_t*rawDoc;
DbHandlehndl[1];
ObjIdobjId[1];
uint64_tbits2;
};
};
// convert dbaddr_t to void *
externvoid*js_dbaddr(value_tval, document_t*rawDoc);
#pragma pack(push, 4)
typedefstruct {
value_tname;
value_tvalue;
} pair_t;
// database marshaled Objects
structDbObject {
uint32_tcnt; // number of pair entries
pair_tpairs[]; // pairs & hash table follow
};
// Objects
structObject {
value_tprotoChain; // the prototype chain
value_tbaseVal[1]; // primitive value
pair_t*pairsPtr; // key/value pairs followed by hash table
uint8_tprotoBase; // base prototype type
uint8_tprotoSub; // sub type
};
value_t*setAttribute(object_t*oval, value_tfield, uint32_th);
value_tcloneObject(value_tobj);
value_tnewObject(valuetype_tprotoBase);
value_t*baseObject(value_tobj);
// Symbol tables
structSymTable {
uint32_tdepth;
uint32_tbaseIdx;
uint32_tframeIdx;
uint32_tscopeCnt;
uint32_tchildFcns;
symtab_t*parent;
object_tentries;
};
// Arrays
externintArraySize[];
structDbArray {
uint32_tcnt;
value_tvalueArray[0];
};
structArray {
value_tobj; // Array object -- must be first as objvalue
union {
value_t*valuePtr;
uint8_t*array;
};
};
enumArrayType {
array_value=0,
array_int8,
array_uint8,
array_int16,
array_uint16,
array_int32,
array_uint32,
array_float32,
array_float64
};
value_tnewArray(enumArrayTypesubType, uint32_tinitSize);
value_tcloneArray(value_tvalue);
#pragma pack(pop)
#include"js_parse.h"
#include"js_vector.h"
// function call/local frames of variables
typedefstruct {
uint32_tcount;
value_tthisVal;
value_tnextThis;
value_targuments;
symtab_t*symbols; // frame symbols
value_tvalues[1]; // first slot is return value
} frame_t;
// block scope variables sub-frame
typedefstruct {
uint32_tcount; // current block scope variables
frame_t*frame; // frame (regular) variables
symtab_t*symbols; // current block scope symbols
value_tvalues[1]; // current block scope slots
} scope_t;
// Closures
structClosure {
value_tobj; // Function object -- must be first as objvalue
Node*table;
fcnDeclNode*fd;
value_tprotoObj; // the prototype property
symtab_t*symbols; // block scope symbols
uint32_tdepth; // depth of the scopes
scope_t*scope[]; // lexical variable scopes
};
// Interpreter environment
typedefstruct {
scope_t*scope; // current block scope variables
firstNode*first; // first node of current script
frame_t*topFrame; // top level varable frame
value_t*literals; // vector of evaluation literals
closure_t*closure; // current function closure
Node*table; // current function node table
boollval;
} environment_t;
// new literal handling
voidabandonLiterals(environment_t*env);
// lookup fields in objects
value_tlookupAttribute(value_tobj, string_t*fldstr, value_toriginal, boollVal, booleval);
intlookupValue(value_tobj, value_tname, uint64_thash, boolfind);
voidhashStore(void*table, uint32_thashEnt, uint32_tidx, uint32_tval);
uint32_thashEntry(void*table, uint32_thashEnt, uint32_tidx);
value_t*deleteField(object_t*obj, value_tname);
uint64_thashStr(uint8_t*str, uint32_tlen);
uint32_thashBytes(uint32_tcap);
value_tlookup(value_tobj, value_tname, booladdBit, uint64_thash);
voidhashStore(void*table, uint32_thashEnt, uint32_tidx, uint32_tval);
uint32_thashEntry(void*table, uint32_thashEnt, uint32_tidx);
value_t*deleteField(object_t*obj, value_tname);
value_tfcnCall (value_tfcnClosure, value_targs, value_tthisVal, boolrtnVal, environment_t*env);
value_tnewClosure( fcnDeclNode*fcn, environment_t*env);
// Built-in property and fcns
value_tcallFcnFcn(value_tfcn, value_t*args, environment_t*env);
value_tcallFcnProp(value_tprop, value_toriginal, boollval);
value_tcallObjFcn(value_tobj, string_t*name, boolabandon, environment_t*env);
value_tgetPropFcnName(value_tslot);
voidincrScopeCnt (scope_t*scope);
voidabandonScope(scope_t*scope);
// Interpreter dispatch
typedefvalue_t (*dispatchFcn)(Node*hdr, environment_t*env);
externdispatchFcndispatchTable[node_MAX];
externvalue_tbuiltinProtoHndl[Hndl_max];
externvalue_tbuiltinProto[vt_MAX];
value_teval_arg(uint32_t*args, environment_t*env);
value_treplaceValue(value_tlval, value_tvalue);
value_tcloneValue(value_tvalue);
voidstoreArrayValue(value_tleft, value_tright);
voidreplaceSlot(value_t*slot, value_tvalue);
voidprintValue(value_t, uint32_tdepth);
char*strtype(value_t);
#definedispatch(slot, env) ((dispatchTable[env->table[slot].type])(&env->table[slot], env))
// Status
char*strstatus(Status);
voidinstallStatus(uint8_t*, Status, symtab_t*);
// Post-parse pass
voidcompileScripts(uint32_tmax, Node*table, symtab_t*symbols, symtab_t*block);
uint32_tinsertSymbol(string_t*name, symtab_t*symbols, boolscoped);
// install function closures
voidinstallFcns(uint32_tdecl, environment_t*env);
// execute script modules
voidexecScripts(Node*table, uint32_tsize, value_targs, symtab_t*symbols, environment_t*env);
// value conversions
voidvalueCat(value_t*left, value_tright, boolabandon);
voidvalueCatStr (value_t*left, uint8_t*rightval, uint32_trightlen);
value_tvalue2Str(value_tv, booljson, boolraw);
value_tconvArray2Value(void*lval, enumArrayTypetype);
value_tconv2Str(value_t, bool, bool);
value_tconv2ObjId(value_t, bool);
value_tconv2Bool(value_t, bool);
value_tconv2Int(value_t, bool);
value_tconv2Dbl(value_t, bool);
value_tincludeDocument(value_tval, void*dest, environment_t*env);
value_tconvDocObject(value_tval);
value_tgetDocObject(value_tval);
// Errors
value_tmakeError(Node*node, environment_t*env, char*msg);
voiderrorText(Statuss);