-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs_lib.c
500 lines (375 loc) · 11.3 KB
/
js_lib.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
#ifdef _WIN32
#include <windows.h>
#else
#include <dirent.h>
#endif
#ifdef __APPLE__
#include <sys/syslimits.h>
#endif
#ifdef __linux__
#include <linux/limits.h>
#endif
#include <errno.h>
#include "js.h"
#include "js.tab.h"
#include "js.lex.h"
#ifndef _WIN32
#define fopen_s(file, path, mode) ((*file = fopen(path, mode)) ? 0 : errno)
#define freopen_s(dummy, path, mode, file) (((*dummy) = freopen(path, mode,file)) ? 0 : errno)
#define strerror_s(buf,siz,err) (strerror_r(err,buf,siz))
#endif
void deletePath (char *path) {
#ifdef _WIN32
_unlink(path);
#else
unlink(path);
#endif
}
value_t js_setOption(uint32_t args, environment_t *env) {
string_t *str;
value_t v, s;
if (debug) fprintf(stderr, "funcall : setOption\n");
s.bits = vt_status;
v = eval_arg(&args, env);
str = js_dbaddr(v, NULL);
if(v.type != vt_string) {
fprintf(stderr, "Error: setOption => expecting string => %s\n", strtype(v));
return s.status = ERROR_script_internal, s;
}
if (!memcmp(str->val, "Debug", 6))
debug = true;
if (!memcmp(str->val, "Math", 5))
mathNums = true;
abandonValue(v);
return s.status = OK, s;
}
value_t js_isNaN(uint32_t args, environment_t *env) {
value_t v, val;
if (debug) fprintf(stderr, "funcall : isNaN\n");
v = eval_arg(&args, env);
val.bits = vt_bool;
val.boolean = v.type == vt_nan;
abandonValue(v);
return val;
}
value_t js_parseInt(uint32_t args, environment_t *env) {
value_t v, val;
if (debug) fprintf(stderr, "funcall : parseInt\n");
v = eval_arg(&args, env);
val = conv2Int(v, true);
return val;
}
value_t js_parseFlt(uint32_t args, environment_t *env) {
value_t v, val;
if (debug) fprintf(stderr, "funcall : parseFloat\n");
v = eval_arg(&args, env);
val = conv2Dbl(v, true);
return val;
}
// json operation 1=stringify, 2=parse
extern value_t jsonParse(value_t v);
value_t js_json(uint32_t args, environment_t *env) {
if (debug) fprintf(stderr, "funcall : json\n");
value_t v, t, r;
int type;
t = eval_arg(&args, env);
v = eval_arg(&args, env);
type = (int)conv2Int(t, true).nval;
switch (type) {
case 1:
r = conv2Str(v, false, false);
break;
case 2:
r = jsonParse(v);
break;
default:
r.bits = vt_status;
r.status = ERROR_script_unrecognized_function;
break;
}
abandonValue(v);
return r;
}
value_t js_print(uint32_t args, environment_t *env) {
value_t s, v;
s.bits = vt_status;
if (args) for(;;) {
string_t *str;
v = eval_arg(&args, env);
if (v.type == vt_endlist)
break;
v = conv2Str(v, true, false);
str = js_dbaddr(v, NULL);
fwrite(str->val, str->len, 1, stdout);
abandonValue(v);
}
printf("\n");
return s.status = OK, s;
}
value_t js_quit(uint32_t args, environment_t *env) {
if (debug) fprintf(stderr, "funcall : Exit\n");
exit(0);
}
value_t js_makeWeakRef(uint32_t args, environment_t *env) {
value_t o, v, slot, s;
s.bits = vt_status;
if (debug) fprintf(stderr, "funcall : makeWeakRef\n");
o = eval_arg(&args, env);
if (vt_object != o.type && vt_array != o.type) {
fprintf(stderr, "Error: makeWeakRef => expecting object => %s\n", strtype(o));
return s.status = ERROR_script_internal, s;
}
slot = eval_arg(&args, env);
if (vt_lval != slot.type) {
fprintf(stderr, "Error: makeWeakRef => expecting reference => %s\n", strtype(slot));
return s.status = ERROR_script_internal, s;
}
v.bits = vt_weakref;
v.weakcount = 1;
v.raw = o.raw;
incrRefCnt(v);
replaceValue(slot, v);
abandonValue(o);
return s.status = OK, s;
}
// parse and eval a javascript program string
// js_parseEval(fragmentName, programString, argsVector);
value_t js_parseEval(uint32_t args, environment_t *env) {
value_t program, s, name, arguments;
YY_BUFFER_STATE buffer;
s.bits = vt_status;
string_t *progstr;
string_t *namestr;
symtab_t symbols;
parseData pd[1];
uint32_t first;
firstNode *fn;
char *string;
if (debug) fprintf(stderr, "funcall : parseEval\n");
memset(&symbols, 0, sizeof(symbols));
symbols.parent = env->closure->symbols;
symbols.depth = env->closure->symbols->depth + 1;
memset(pd, 0, sizeof(pd));
yylex_init(&pd->scanInfo);
name = eval_arg(&args, env);
namestr = js_dbaddr(name, NULL);
if (vt_string != name.type) {
fprintf(stderr, "Error: parseEval => expecting Script Name:string => %s\n", strtype(name));
return s.status = ERROR_script_internal, s;
}
pd->script = (char *)namestr->val;
pd->lineNo = 1;
first = newNode(pd, node_first, sizeof(firstNode) + namestr->len + 1, false);
fn = (firstNode *)(pd->table + first);
fn->hdr->aux = (uint32_t)strlen(pd->script);
memcpy (fn->script, namestr->val, namestr->len);
fn->script[namestr->len] = 0;
program = eval_arg(&args, env);
progstr = js_dbaddr(program, NULL);
if (vt_string != program.type) {
fprintf(stderr, "Error: parseEval => expecting program name string => %s\n", strtype(program));
return s.status = ERROR_script_internal, s;
}
string = js_alloc(progstr->len + 2, false);
memcpy (string, progstr->val, progstr->len);
string[progstr->len] = 0;
string[progstr->len + 1] = 0;
abandonValue(name);
abandonValue(program);
buffer = yy_scan_buffer(string, progstr->len + 2, pd->scanInfo);
switch (yyparse(pd->scanInfo, pd)) {
case 1:
return s.status = ERROR_script_parse, s;
case 2:
return s.status = ERROR_outofmemory, s;
}
yy_delete_buffer(buffer, pd->scanInfo);
yylex_destroy(pd->scanInfo);
arguments = eval_arg(&args, env);
fn = (firstNode *)(pd->table + first);
fn->moduleSize = pd->tableNext - first;
fn->begin = pd->beginning;
execScripts(pd->table, pd->tableNext, arguments, &symbols, env);
abandonValue(arguments);
return s.status = OK, s;
}
// load and run a saved script package
// js_loadScript(scrName, argumentVector);
value_t js_loadScript(uint32_t args, environment_t *env) {
value_t name, argVector, s;
string_t *namestr;
char errmsg[1024];
#ifdef _WIN32
char fname[MAX_PATH];
#else
char fname[PATH_MAX];
#endif
uint32_t fsize, count;
symtab_t symbols;
FILE *script;
Node *table;
int err;
memset (&symbols, 0, sizeof(symbols));
symbols.parent = env->closure->symbols;
symbols.depth = env->closure->symbols->depth + 1;
s.bits = vt_status;
if (debug) fprintf(stderr, "funcall : loadScript\n");
name = eval_arg(&args, env);
namestr = js_dbaddr(name, NULL);
if (vt_string != name.type) {
fprintf(stderr, "Error: loadScript => expecting ScriptName:string => %s\n", strtype(name));
return s.status = ERROR_script_internal, s;
}
if (namestr->len > 1023) {
fprintf(stderr, "Error: loadScript => filename too long (%d > 1023)\n", namestr->len);
return s.status = ERROR_script_internal, s;
}
memcpy(fname, namestr->val, namestr->len);
fname[namestr->len] = 0;
if((err = fopen_s(&script, fname, "rb"))) {
strerror_s(errmsg, sizeof(errmsg), err);
fprintf(stderr, "Error: loadScript => fopen failed on '%s' with %s\n", fname, errmsg);
return s.status = ERROR_script_internal, s;
}
fseek(script, 0, SEEK_END);
fsize = ftell(script);
fseek(script, 0, SEEK_SET);
table = js_alloc(fsize, false);
count = (uint32_t)fread(table, sizeof(Node), fsize / sizeof(Node), script);
argVector = eval_arg(&args, env);
execScripts(table, count, argVector, &symbols, env);
abandonValue(name);
abandonValue(argVector);
return s.status = OK, s;
}
value_t js_eval(uint32_t args, environment_t *env) {
value_t script, s;
if (debug) fprintf(stderr, "funcall : loadScript\n");
script = eval_arg(&args, env);
if (vt_string != script.type) {
fprintf(stderr, "Error: eval => expecting Script:string => %s\n", strtype(script));
return s.status = ERROR_script_internal, s;
}
return s.status = OK, s;
}
extern value_t newDate(value_t *args);
value_t js_newDate (uint32_t args, environment_t *env) {
value_t arglist, s, result;
array_t *aval;
arglist = eval_arg(&args, env);
aval = js_dbaddr(arglist, NULL);
if (arglist.type != vt_array) {
fprintf(stderr, "Error: js_date => expecting argument array => %s\n", strtype(arglist));
abandonValue(arglist);
return s.status = ERROR_script_internal, s;
}
result.bits = vt_status;
result.status = ERROR_script_internal;
result = newDate(aval->valuePtr);
abandonValue(arglist);
return result;
}
value_t js_fromCharCode (uint32_t args, environment_t *env) {
value_t arglist, s, result;
array_t *aval;
uint32_t idx;
arglist = eval_arg(&args, env);
aval = js_dbaddr(arglist, NULL);
if (arglist.type != vt_array) {
fprintf(stderr, "Error: js_fromCharCode => expecting argument array => %s\n", strtype(arglist));
abandonValue(arglist);
return s.status = ERROR_script_internal, s;
}
result.bits = vt_status;
result.status = ERROR_script_internal;
result = newString(NULL, vec_cnt(aval->valuePtr));
string_t *str = result.addr;
for (idx = 0; idx < str->len; idx++)
str->val[idx] = (uint8_t)conv2Int(aval->valuePtr[idx], false).nval;
abandonValue(arglist);
return result;
}
value_t js_listFiles(uint32_t args, environment_t *env) {
value_t s, path, result = newArray(array_value, 0);
array_t *aval = result.addr;
string_t *pathstr;
s.bits = vt_status;
if (debug) fprintf(stderr, "funcall : findFtw\n");
path = eval_arg(&args, env);
pathstr = js_dbaddr(path, NULL);
if (vt_string != path.type) {
fprintf(stderr, "Error: listFiles => expecting path:string => %s\n", strtype(path));
return s.status = ERROR_script_internal, s;
}
#ifdef _WIN32
char pattern[MAX_PATH];
WIN32_FIND_DATA fd[1];
HANDLE hndl;
memcpy (pattern, pathstr->val, pathstr->len > MAX_PATH - 2 ? MAX_PATH - 2 : pathstr->len);
pattern[pathstr->len] = '/';
pattern[pathstr->len + 1] = '*';
pattern[pathstr->len + 2] = 0;
abandonValue(path);
hndl = FindFirstFile(pattern, fd);
if (hndl == INVALID_HANDLE_VALUE)
return s.status = ERROR_endoffile, s;
do {
if (strcmp (fd->cFileName, ".") && strcmp (fd->cFileName, "..") )
if (~fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
value_t name = newString(fd->cFileName, -1);
vec_push (aval->valuePtr, name);
incrRefCnt(name);
}
} while (FindNextFile(hndl, fd));
FindClose(hndl);
#else
DIR *dir = opendir ((char *)pathstr->val);
struct dirent *ent;
abandonValue(path);
if (!dir)
return s.status = ERROR_doesnot_exist, s;
while ((ent = readdir(dir)))
if (ent->d_ino) {
value_t name = newString(ent->d_name, -1);
vec_push (aval->valuePtr, name);
incrRefCnt(name);
}
closedir(dir);
#endif
return result;
}
value_t js_deleteFile(uint32_t args, environment_t *env) {
string_t *namestr;
char fname[1024];
value_t name, s;
uint32_t idx;
s.bits = vt_status;
if (debug) fprintf(stderr, "funcall : delete\n");
name = eval_arg(&args, env);
if (name.type == vt_array) {
array_t *aval = name.addr;
value_t *values = aval->valuePtr;
for (idx = 0; idx < vec_cnt(values); idx++) {
if (values[idx].type == vt_string) {
namestr = js_dbaddr(values[idx], NULL);
memcpy(fname, namestr->val, namestr->len);
fname[namestr->len] = 0;
deletePath(fname);
return s.status = OK, s;
}
}
}
namestr = js_dbaddr(name, NULL);
if (vt_string != name.type) {
fprintf(stderr, "Error: openFile => expecting fname:String => %s\n", strtype(name));
return s.status = ERROR_script_internal, s;
}
if (namestr->len > 1023) {
fprintf(stderr, "Error: openFile => filename too long (%d > 1023)\n", namestr->len);
return s.status = ERROR_script_internal, s;
}
memcpy(fname, namestr->val, namestr->len);
fname[namestr->len] = 0;
deletePath(fname);
return s.status = OK, s;
}