/
fl7_core.js
705 lines (670 loc) · 23 KB
/
fl7_core.js
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
const fs = require("fs");
const path = require("path");
const child_process = require("child_process");
const parser = require(process.env.app_dir + "/fluorite-7.js");
const heapdump = require("heapdump");
const syncRequest = require("sync-request");
let globalResult;
const cacheUse = new Map();
function parse(source, startRule, scriptFile) {
const result = parser.parse(source, {
startRule: startRule,
});
const constantIdsScript = [];
const env = new result.fl7c.Environment();
var objects = {};
result.loadAliases(env, objects);
const c = (key, value) => {
var constantId = env.allocateConstantId();
env.setAlias(key, new result.fl7c.FluoriteAliasConstant(constantId));
env.setConstant(constantId, value);
return constantId;
};
function loadStringUtf8(fd) {
var buffers = [];
var length = 0;
while (true) {
var buffer = new Buffer.alloc(4096);
var bytesRead;
while (true) {
try {
bytesRead = fs.readSync(fd, buffer, 0, 4096, null);
} catch (e) {
if (e.code === "EAGAIN") {
continue;
} else {
throw e;
}
}
break;
}
if (bytesRead === 0) break;
buffers.push(buffer.subarray(0, bytesRead));
length += bytesRead;
}
return Buffer.concat(buffers, length).toString('utf8', 0, length);
}
function createUtf8LineReader(fd, size, doClose) {
let closed = false;
let buffers = [];
let buffer;
let start = 0;
let length = 0;
let afterCR = false;
return {
next: () => {
while (true) {
// 既に閉じている場合は何も返さない
if (closed) return undefined;
// 前回の残りがないなら新しく読み込む
if (start >= length) {
while (true) {
try {
buffer = Buffer.alloc(size);
start = 0;
length = fs.readSync(fd, buffer, 0, buffer.length, null);
} catch (e) {
if (e.code === "EAGAIN") {
continue;
} else {
throw e;
}
}
break;
}
}
// ファイルの終端に来た場合はこれまでにたまっていたバッファをくっつけて返す
// ただし何も貯まっていない場合は何も返さない
if (length === 0) {
closed = true;
if (doClose) fs.closeSync(fd);
if (buffers.length === 0) {
return undefined;
} else {
return Buffer.concat(buffers).toString("utf8");
}
}
// CRの直後のLFは無視する
if (afterCR) {
if (buffer[start] === 10) {
start++;
afterCR = false;
continue;
}
}
// 改行文字が出る位置を探す
let index = -1;
for (let i = start; i < length; i++) {
if (buffer[i] === 10) {
index = i;
afterCR = false;
break;
} else if (buffer[i] === 13) {
index = i;
afterCR = true;
break;
}
}
// 改行文字があった場合、これまでにたまっていたバッファとこのバッファをくっつけたバッファを返す
if (index !== -1) {
if (buffers.length === 0) {
const result = buffer.subarray(start, index);
start = index + 1;
return result.toString("utf8");
} else {
const result = Buffer.concat([...buffers, buffer.subarray(start, index)]);
buffers = [];
start = index + 1;
return result.toString("utf8");
}
}
// 改行文字が見つからなかった場合、このバッファをためる
buffers.push(buffer.subarray(start, length));
start = length;
}
},
};
}
function createBufferReader(fd, size, doClose) {
let closed = false;
return {
next: () => {
while (true) {
// 既に閉じている場合は何も返さない
if (closed) return undefined;
// 新しく読み込む
let buffer;
let length;
while (true) {
try {
buffer = Buffer.alloc(size);
length = fs.readSync(fd, buffer, 0, buffer.length, null);
} catch (e) {
if (e.code === "EAGAIN") {
continue;
} else {
throw e;
}
}
break;
}
// ファイルの終端に来た場合は終了する
if (length === 0) {
closed = true;
if (doClose) fs.closeSync(fd);
return undefined;
}
return Array.from(buffer.subarray(0, length));
}
},
};
}
c("RESOLVE", new result.fl7.FluoriteFunction(args => {
if (args.length !== 1) throw new Error("Illegal Argument");
var pathes = result.fl7.util.toStream(args[0]).toArray();
return path.resolve.apply(null, pathes);
}));
c("MODULE", new result.fl7.FluoriteFunction(args => {
let baseFiles;
let moduleName;
let moduleScript;
if (args.length == 2) {
baseFiles = result.fl7.util.toStream(args[0]).toArray();
moduleName = result.fl7.util.toString(args[1]);
moduleScript = "main.fl7";
} else if (args.length == 3) {
baseFiles = result.fl7.util.toStream(args[0]).toArray();
moduleName = result.fl7.util.toString(args[1]);
moduleScript = result.fl7.util.toString(args[2]);
} else {
throw new Error("Illegal Argument");
}
for (let baseFile of baseFiles) {
let modulePath = path.resolve(baseFile, "../fl7_modules", "_" + encodeURIComponent(moduleName)
.replace(/\-/g, "%2D")
.replace(/\./g, "%2E")
.replace(/\!/g, "%21")
.replace(/\~/g, "%7E")
.replace(/\*/g, "%2A")
.replace(/\'/g, "%27")
.replace(/\(/g, "%28")
.replace(/\)/g, "%29"));
let stat = fs.statSync(modulePath, {throwIfNoEntry: false});
if (stat !== undefined && stat.isDirectory()) {
return path.resolve(modulePath, moduleScript);
}
}
throw new Error("No such module: " + moduleName);
}));
c("INC", result.fl7.util.toStreamFromArray([
"./fl7_modules",
path.resolve(process.env.HOME, ".fl7/fl7_modules"),
path.resolve(process.env.app_dir, "fl7_modules"),
]));
c("UTF8", new result.fl7.FluoriteFunction(args => {
if (args.length == 1) {
return Array.from(Buffer.from(result.fl7.util.toString(args[0])));
}
throw new Error("Illegal argument");
}));
c("FROM_UTF8", new result.fl7.FluoriteFunction(args => {
if (args.length == 1) {
if (args[0] instanceof Array) {
return Buffer.from(args[0]).toString();
}
}
throw new Error("Illegal argument");
}));
c("IN", (function(){
class FluoriteStreamerStdin extends result.fl7.FluoriteStreamer {
constructor () {
super();
}
start() {
return createUtf8LineReader(process.stdin.fd, 4096, false);
}
}
return new FluoriteStreamerStdin();
})());
c("INB", (function(){
class FluoriteStreamerStdin extends result.fl7.FluoriteStreamer {
constructor () {
super();
}
start() {
return createBufferReader(process.stdin.fd, 4096, false);
}
}
return new FluoriteStreamerStdin();
})());
c("OUTB", new result.fl7.FluoriteFunction(args => {
if (args.length == 1) {
var stream = result.fl7.util.toStream(args[0]).start();
while (true) {
var next = stream.next();
if (next === undefined) break;
if (next instanceof Array) {
process.stdout.write(Buffer.from(next))
} else if (typeof next === "string") {
process.stdout.write(next);
} else {
throw new Error("Illegal argument");
}
}
return args[0];
}
throw new Error("Illegal argument");
}));
c("ERR", new result.fl7.FluoriteFunction(args => {
if (args.length == 1) {
var stream = result.fl7.util.toStream(args[0]).start();
while (true) {
var next = stream.next();
if (next === undefined) break;
console.error("%s", result.fl7.util.toString(next));
}
return args[0];
}
throw new Error("Illegal argument");
}));
c("ERRB", new result.fl7.FluoriteFunction(args => {
if (args.length == 1) {
var stream = result.fl7.util.toStream(args[0]).start();
while (true) {
var next = stream.next();
if (next === undefined) break;
if (next instanceof Array) {
process.stderr.write(Buffer.from(next))
} else if (typeof next === "string") {
process.stderr.write(next);
} else {
throw new Error("Illegal argument");
}
}
return args[0];
}
throw new Error("Illegal argument");
}));
c("READ", (function(){
class FluoriteStreamerRead extends result.fl7.FluoriteStreamer {
constructor (filename) {
super();
this._filename = filename;
}
start() {
return createUtf8LineReader(fs.openSync(this._filename, "r"), 4096, true);
}
}
return new result.fl7.FluoriteFunction(args => {
var filename = args[0];
if (filename === undefined) throw new Error("Illegal argument");
return new FluoriteStreamerRead(result.fl7.util.toString(filename));
});
})());
c("READB", (function(){
class FluoriteStreamerRead extends result.fl7.FluoriteStreamer {
constructor (filename) {
super();
this._filename = filename;
}
start() {
return createBufferReader(fs.openSync(this._filename, "r"), 4096, true);
}
}
return new result.fl7.FluoriteFunction(args => {
var filename = args[0];
if (filename === undefined) throw new Error("Illegal argument");
return new FluoriteStreamerRead(result.fl7.util.toString(filename));
});
})());
c("WRITE", (function(){
return new result.fl7.FluoriteFunction(args => {
var filename = args[0];
if (filename === undefined) throw new Error("Illegal argument");
filename = result.fl7.util.toString(filename);
var streamer = args[1];
if (streamer === undefined) throw new Error("Illegal argument");
streamer = result.fl7.util.toStream(streamer);
const fd = fs.openSync(filename, "w");
const stream = streamer.start();
while (true) {
const next = stream.next();
if (next === undefined) break;
fs.writeSync(fd, result.fl7.util.toString(next));
fs.writeSync(fd, "\n");
}
fs.closeSync(fd);
return null;
});
})());
c("WRITEB", (function(){
return new result.fl7.FluoriteFunction(args => {
var filename = args[0];
if (filename === undefined) throw new Error("Illegal argument");
filename = result.fl7.util.toString(filename);
var streamer = args[1];
if (streamer === undefined) throw new Error("Illegal argument");
streamer = result.fl7.util.toStream(streamer);
const fd = fs.openSync(filename, "w");
const stream = streamer.start();
while (true) {
const next = stream.next();
if (next === undefined) break;
if (next instanceof Array) {
fs.writeSync(fd, Buffer.from(next));
} else if (typeof next === "string") {
fs.writeSync(fd, next);
} else {
throw new Error("Illegal argument");
}
}
fs.closeSync(fd);
return null;
});
})());
c("LS", new result.fl7.FluoriteFunction(args => {
if (args.length === 0) {
return result.fl7.util.toStreamFromArray(fs.readdirSync("."));
} else if (args.length === 1) {
return result.fl7.util.toStreamFromArray(fs.readdirSync(result.fl7.util.toString(args[0])));
} else {
throw new Error("Illegal argument");
}
}));
c("STAT", new result.fl7.FluoriteFunction(args => {
if (args.length != 1) throw new Error("Illegal argument");
let stats;
try { // 旧バージョンnodeでエラーになる対策
stats = fs.statSync(result.fl7.util.toString(args[0]), {throwIfNoEntry: false});
} catch (e) {
if (e.code === "ENOENT") {
return null;
} else {
throw e;
}
}
if (stats === undefined) return null;
return new result.fl7.FluoriteObject(null, {
is_blockDevice: new result.fl7.FluoriteFunction(args => stats.isBlockDevice()),
is_characterDevice: new result.fl7.FluoriteFunction(args => stats.isCharacterDevice()),
is_directory: new result.fl7.FluoriteFunction(args => stats.isDirectory()),
is_fifo: new result.fl7.FluoriteFunction(args => stats.isFIFO()),
is_file: new result.fl7.FluoriteFunction(args => stats.isFile()),
is_socket: new result.fl7.FluoriteFunction(args => stats.isSocket()),
is_symbolicLink: new result.fl7.FluoriteFunction(args => stats.isSymbolicLink()),
dev: stats.dev,
ino: stats.ino,
mode: stats.mode,
nlink: stats.nlink,
uid: stats.uid,
gid: stats.gid,
rdev: stats.rdev,
size: stats.size,
blksize: stats.blksize,
blocks: stats.blocks,
atime: stats.atimeMs,
mtime: stats.mtimeMs,
ctime: stats.ctimeMs,
birthtime: stats.birthtimeMs,
TO_NUMBER: new result.fl7.FluoriteFunction(args => stats.size),
LENGTH: new result.fl7.FluoriteFunction(args => stats.size),
});
}));
c("EXEC", new result.fl7.FluoriteFunction(args => {
let filename = args[0];
if (filename === undefined) throw new Error("Illegal argument");
filename = result.fl7.util.toString(filename);
let argsExec = [];
{
let streamer = args[1];
if (streamer === undefined) streamer = null;
if (streamer === null) streamer = result.fl7.util.empty();
streamer = result.fl7.util.toStream(streamer);
const stream = streamer.start();
while (true) {
const next = stream.next();
if (next === undefined) break;
argsExec[argsExec.length] = result.fl7.util.toString(next);
}
}
let stdin = args[2];
if (stdin === undefined) stdin = null;
if (stdin === null) stdin = "";
if (stdin instanceof Array) {
stdin = Buffer.from(stdin);
} else {
stdin = result.fl7.util.toString(stdin);
}
let env = process.env;
{
let extraEnv = args[3];
if (extraEnv === undefined) extraEnv = null;
if (extraEnv === null) {
} else if (extraEnv instanceof result.fl7.FluoriteObject) {
env = {
...env,
...extraEnv.map,
};
} else {
throw new Error("Illegal argument");
}
}
const stringOut = child_process.execFileSync(filename, argsExec, {
input: stdin,
encoding: "utf8",
maxBuffer: 64 * 1024 * 1024,
env: env,
});
const arrayOut = stringOut.split("\n");
if (arrayOut[arrayOut.length - 1] === "") arrayOut.pop();
return result.fl7.util.toStreamFromArray(arrayOut);
}));
c("EXECB", new result.fl7.FluoriteFunction(args => {
let filename = args[0];
if (filename === undefined) throw new Error("Illegal argument");
filename = result.fl7.util.toString(filename);
let argsExec = [];
{
let streamer = args[1];
if (streamer === undefined) streamer = null;
if (streamer === null) streamer = result.fl7.util.empty();
streamer = result.fl7.util.toStream(streamer);
const stream = streamer.start();
while (true) {
const next = stream.next();
if (next === undefined) break;
argsExec[argsExec.length] = result.fl7.util.toString(next);
}
}
let stdin = args[2];
if (stdin === undefined) stdin = null;
if (stdin === null) stdin = "";
if (stdin instanceof Array) {
stdin = Buffer.from(stdin);
} else {
stdin = Buffer.from(result.fl7.util.toString(stdin));
}
let env = process.env;
{
let extraEnv = args[3];
if (extraEnv === undefined) extraEnv = null;
if (extraEnv === null) {
} else if (extraEnv instanceof result.fl7.FluoriteObject) {
env = {
...env,
...extraEnv.map,
};
} else {
throw new Error("Illegal argument");
}
}
return Array.from(child_process.execFileSync(filename, argsExec, {
input: stdin,
encoding: "buffer",
maxBuffer: 64 * 1024 * 1024,
env: env,
}));
}));
c("SHELL", new result.fl7.FluoriteFunction(args => {
let script = args[0];
if (script === undefined) throw new Error("Illegal argument");
script = result.fl7.util.toString(script);
const stringOut = child_process.execFileSync(process.env.SHELL, [], {
input: script,
encoding: "utf8",
maxBuffer: 64 * 1024 * 1024,
env: process.env,
});
const arrayOut = stringOut.split("\n");
if (arrayOut[arrayOut.length - 1] === "") arrayOut.pop();
return result.fl7.util.toStreamFromArray(arrayOut);
}));
c("SHELLB", new result.fl7.FluoriteFunction(args => {
let script = args[0];
if (script === undefined) throw new Error("Illegal argument");
script = Buffer.from(result.fl7.util.toString(script));
return Array.from(child_process.execFileSync(process.env.SHELL, [], {
input: script,
encoding: "buffer",
maxBuffer: 64 * 1024 * 1024,
env: process.env,
}));
}));
c("EVAL", (function(){
return new result.fl7.FluoriteFunction(args => {
var source = args[0];
if (source === undefined) throw new Error("Illegal argument");
source = result.fl7.util.toString(source);
const result2 = parse(source, "Root", path.resolve(scriptFile, "../<eval>"));
const fl7 = globalResult.fl7;
const util = globalResult.fl7.util;
const constants = Array.from(globalResult.env.getConstants());
for (let i = 0; i < globalResult.constantIdsScript.length; i++) {
constants[globalResult.constantIdsScript[i]] = result2.env.getConstants()[globalResult.constantIdsScript[i]];
}
return eval(result2.code);
});
})());
c("REQUIRE", (function(){
return new result.fl7.FluoriteFunction(args => {
var filename = args[0];
if (filename === undefined) throw new Error("Illegal argument");
filename = result.fl7.util.toString(filename);
const fd = fs.openSync(filename, "r");
const input = loadStringUtf8(fd);
fs.closeSync(fd);
const result2 = parse(input, "Root", path.resolve(scriptFile, "..", filename));
const fl7 = globalResult.fl7;
const util = globalResult.fl7.util;
const constants = Array.from(globalResult.env.getConstants());
for (let i = 0; i < globalResult.constantIdsScript.length; i++) {
constants[globalResult.constantIdsScript[i]] = result2.env.getConstants()[globalResult.constantIdsScript[i]];
}
return eval(result2.code);
});
})());
c("USE", (function(){
return new result.fl7.FluoriteFunction(args => {
var filename = args[0];
if (filename === undefined) throw new Error("Illegal argument");
filename = result.fl7.util.toString(filename);
let res = cacheUse.get(filename);
if (res === undefined) {
const fd = fs.openSync(filename, "r");
const input = loadStringUtf8(fd);
fs.closeSync(fd);
const result2 = parse(input, "Root", path.resolve(scriptFile, "..", filename));
const fl7 = globalResult.fl7;
const util = globalResult.fl7.util;
const constants = Array.from(globalResult.env.getConstants());
for (let i = 0; i < globalResult.constantIdsScript.length; i++) {
constants[globalResult.constantIdsScript[i]] = result2.env.getConstants()[globalResult.constantIdsScript[i]];
}
res = eval(result2.code);
cacheUse.set(filename, res);
}
return res;
});
})());
constantIdsScript.push(c("FILE", path.resolve(scriptFile)));
c("ARGV", process.argv.slice(1));
c("ARGS", result.fl7.util.toStreamFromArray(process.argv.slice(2)));
c("ENV", (function() {
const map = {};
const names = Object.getOwnPropertyNames(process.env);
for (let i = 0; i < names.length; i++) {
map[names[i]] = String(Object.getOwnPropertyDescriptor(process.env, names[i]).value);
}
return new result.fl7.FluoriteObject(null, map);
})());
c("PID", process.pid);
c("PPID", process.ppid);
c("MEMORY_USAGE", new result.fl7.FluoriteFunction(args => {
return new result.fl7.FluoriteObject(null, process.memoryUsage());
}));
c("GC", new result.fl7.FluoriteFunction(args => global.gc()));
c("HEAPDUMP", new result.fl7.FluoriteFunction(args => {
if (args[0] !== undefined) {
heapdump.writeSnapshot(result.fl7.util.toString(args[0]));
} else {
heapdump.writeSnapshot();
}
}));
c("EXIT", new result.fl7.FluoriteFunction(args => {
const code = args[0] === undefined ? 0 : result.fl7.util.toNumber(args[0]);
process.exit(code);
}));
c("HTTP", new result.fl7.FluoriteFunction(args => {
if (args.length !== 1) throw new Error("Illegal argument");
const url = result.fl7.util.toString(args[0]);
const response = syncRequest("GET", url);
if (response.statusCode < 200 || response.statusCode >= 300) throw new Error("HTTP Error: " + response.statusCode);
const stringOut = response.body.toString("utf8");
const arrayOut = stringOut.split("\n");
if (arrayOut[arrayOut.length - 1] === "") arrayOut.pop();
return result.fl7.util.toStreamFromArray(arrayOut);
}));
c("HTTPB", new result.fl7.FluoriteFunction(args => {
if (args.length !== 1) throw new Error("Illegal argument");
const url = result.fl7.util.toString(args[0]);
const response = syncRequest("GET", url);
if (response.statusCode < 200 || response.statusCode >= 300) throw new Error("HTTP Error: " + response.statusCode);
return Array.from(response.body);
}));
const codes = result.node.getCodeGetter(env);
const code = "(function() {\n" + result.fl7c.util.indent(codes[0] + "return " + codes[1] + ";\n") + "}())";
return {
fl7: result.fl7,
objects: objects,
env: env,
code: code,
constantIdsScript: constantIdsScript,
};
}
let result = parse(process.env.exec, process.env.embedded_fluorite !== "0" ? "RootEmbeddedFluorite" : "Root", process.env.script_file);
globalResult = result;
if (process.env.compile !== "0") {
console.log(result.code);
} else {
const fl7 = result.fl7;
function Util() {
}
Util.prototype = result.fl7.util;
const util = new Util();
util.objects = result.objects;
const constants = result.env.getConstants();
const stream = util.toStreamer(eval(result.code)).start();
if (process.env.output_json !== "0") {
while (true) {
const next = stream.next();
if (next === undefined) break;
console.log(JSON.stringify(next));
}
} else {
while (true) {
const next = stream.next();
if (next === undefined) break;
console.log(util.toString(next));
}
}
}