-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.ts
959 lines (921 loc) · 24.9 KB
/
runtime.ts
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
import {
Interpreter,
ObiCallable,
ObiFunction,
ObiTable,
RuntimeError,
Token,
TT,
} from "./obi.ts";
import * as path from "https://deno.land/[email protected]/path/mod.ts";
import {
ensureDirSync,
existsSync,
} from "https://deno.land/[email protected]/fs/mod.ts";
export class RtPrint implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
console.log(Interpreter.stringify(args[0]));
}
toString(): string {
return "<native fn>";
}
}
export class RtDelay implements ObiCallable {
arity(): number {
return 2;
}
call(interpreter: Interpreter, args: any[]): any {
const by = args[0] as number;
const func = args[1] as ObiFunction;
const id = Math.random();
interpreter.waitGroup.add(1);
const timer = setTimeout(() => {
func.call(interpreter, []);
interpreter.waitGroup.done();
}, by);
interpreter.timers.set(id, timer);
return id;
}
}
export class RtStrslice implements ObiCallable {
arity(): number {
return 3;
}
call(interpreter: Interpreter, args: any[]): any {
const str = args[0];
const start = args[1];
const end = args[2];
if (typeof str !== "string") {
throw new RuntimeError({} as Token, "Invalid arg to substr");
}
return str.substring(start, end);
}
}
export class RtClearDelay implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
const id = args[0] as number;
const timer = interpreter.timers.get(id);
clearTimeout(timer);
}
}
export class RtType implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
const e = args[0];
if (e instanceof ObiFunction) {
return "function";
} else if (e instanceof Uint8Array) {
return "array";
} else if (e instanceof ObiTable) {
return "table";
} else if (e === null) {
return "nil";
} else if (typeof e === "string") {
return "string";
} else if (typeof e === "number") {
return "number";
} else if (typeof e === "boolean") {
return "boolean";
} else {
return (typeof e);
}
}
}
export class RtKeys implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
if (args[0] instanceof ObiTable) {
const o = args[0] as ObiTable;
const array = new ObiTable();
let i = 0;
for (const key of o.getFields().keys()) {
array.setDyn(i, key);
i += 1;
}
// array.setDyn("len", i);
return array;
} else {
throw new RuntimeError({} as Token, "Invalid type passed to 'keys'");
}
}
}
export class RtListenTcp implements ObiCallable {
arity(): number {
return 3;
}
call(interpreter: Interpreter, args: any[]): any {
const hostname = args[0] as string;
const port = args[1] as number;
const handler = args[2] as ObiFunction;
async function readAll(reader: Deno.Reader) {
const buf = new Uint8Array(4096);
let data = "";
let len = 4096;
while (len >= 4096) {
const read = await reader.read(buf);
if (!read) break;
len = read;
data += (new TextDecoder()).decode(buf.slice(0, len));
}
return data;
}
async function readAllBytes(reader: Deno.Reader) {
const buf = new Uint8Array(4096);
let data = "";
const CHUNK_SIZE = 4096;
let len = CHUNK_SIZE;
const chunks = [];
while (len >= CHUNK_SIZE) {
const read = await reader.read(buf);
if (!read) break;
len = read;
chunks.push(buf.slice(0, len));
}
if (chunks.length < 1) {
return new Uint8Array(0);
}
const totalSize = (chunks.length - 1) * CHUNK_SIZE +
(chunks[chunks.length - 1].length);
const result = new Uint8Array(totalSize);
for (let i = 0; i < chunks.length; i++) {
for (let j = 0; j < chunks[i].length; j++) {
result[i * CHUNK_SIZE + j] = chunks[i][j];
}
}
return result;
}
class R {
bytes: Uint8Array;
where: number = 0;
done: boolean = false;
constructor(bytes: Uint8Array) {
this.bytes = bytes;
}
async read(to: Uint8Array): Promise<number | null> {
if (this.done) return Promise.resolve(null);
const len = to.length;
for (let i = 0; i < this.bytes.length && i < len; i++) {
to[i] = this.bytes[i];
}
this.done = true;
return Promise.resolve(len);
}
}
interpreter.waitGroup.add(1);
(async function () {
const listener = Deno.listen({ hostname, port });
for await (const conn of listener) {
let closed = false;
class WriteString implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
if (closed) return;
const data = args[0];
const buf = (new TextEncoder()).encode(data);
try {
Deno.copy(new R(buf), conn);
} catch (err) {
console.log(err);
}
}
}
class Write implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
if (closed) return;
const data = args[0] as Uint8Array;
try {
conn.write(data);
} catch (err) {
console.log(err);
}
}
}
class OnData implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
const receiver = args[0];
(async function getData() {
if (closed) return;
try {
const bytes = await readAllBytes(conn);
receiver.call(interpreter, [bytes]);
} catch (err) {
if (err.name === "Interrupted") {
if (!closed) closed = true;
} else {
console.error("Unhandled read error", err);
}
}
})();
}
}
class Close implements ObiCallable {
arity(): number {
return 0;
}
call(interpreter: Interpreter, args: any[]): any {
if (closed) return;
closed = true;
setTimeout(() => conn.close(), 0);
}
}
const connectionInstance = new ObiTable();
connectionInstance.setDyn(
"write_string",
new WriteString(),
);
connectionInstance.setDyn(
"on_data",
new OnData(),
);
connectionInstance.setDyn(
"close",
new Close(),
);
connectionInstance.setDyn(
"write",
new Write(),
);
handler.call(interpreter, [connectionInstance]);
}
interpreter.waitGroup.done();
})();
}
}
export class RtStr implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
return Interpreter.stringify(args[0]);
}
}
export class RtStrlen implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
const str = args[0];
if (typeof str !== "string") {
console.log(str);
throw new RuntimeError({} as Token, "Invalid arg to strlen");
}
return str.length;
}
}
export class RtLen implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
const thing = args[0];
if (thing && "length" in thing) {
return thing.length;
}
if (thing instanceof ObiTable) {
return Array.from((thing as ObiTable).getFields().keys()).length;
}
console.log(thing);
throw new RuntimeError({} as Token, "Invalid arg to len");
}
}
export class RtReadDir implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
const path = args[0];
try {
const files = Deno.readDirSync(path);
const result = new ObiTable();
let resultLen = 0;
for (const file of files) {
const o = new ObiTable();
o.setDyn("name", file.name);
o.setDyn("isFile", file.isFile);
o.setDyn("isDirectory", file.isDirectory);
o.setDyn("isSymlink", file.isSymlink);
result.setDyn(resultLen, o);
resultLen += 1;
}
return result;
} catch (err) {
throw new RuntimeError(
{} as Token,
`Failed to read directory '${path}': ` + err.message,
);
}
}
}
export class RtWritefile implements ObiCallable {
arity(): number {
return 2;
}
call(interpreter: Interpreter, args: any[]): any {
try {
const path = args[0];
const contents = args[1];
return Deno.writeFileSync(path, contents);
} catch (err) {
throw new RuntimeError(
{} as Token,
`Failed to write '${args[0]}': ` + err.message,
);
}
}
}
export class RtReadfile implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
try {
const contents = Deno.readTextFileSync(args[0]);
return contents;
} catch (err) {
if (err.name === "NotFound") {
return null;
}
throw new RuntimeError(
{} as Token,
`Failed to read '${args[0]}': ` + err.message,
);
}
}
}
export class RtReadfileBytes implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
try {
const contents = Deno.readFileSync(args[0]);
return contents;
} catch (err) {
if (err.name === "NotFound") {
return null;
}
throw new RuntimeError(
{} as Token,
`Failed to read '${args[0]}': ` + err.message,
);
}
}
}
export class RtProcessArgs implements ObiCallable {
arity(): number {
return 0;
}
call(interpreter: Interpreter, args: any[]): any {
const runtimeArgs = Deno.args.slice(1);
const container = new ObiTable();
for (let i = 0; i < runtimeArgs.length; i++) {
container.setDyn(
i,
runtimeArgs[i],
);
}
container.setDyn(
"count",
runtimeArgs.length,
);
return container;
}
}
export class RtMod implements ObiCallable {
/// Relevant filenames:
/// - .obi_tool/package_resolution.json obipub generated file containing mapping of package name to package location.
/// - obiput.toml : package configuration file whose containing directory is considered the package root.
packages: Map<string, string> | null = null;
context: string | null = null;
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
if (args[0] === "builtins") {
return this.loadBuiltins();
}
const location = this.findLocation(args[0], interpreter);
const previousContext = this.context;
this.context = path.dirname(location);
const source = Deno.readTextFileSync(location);
const module = interpreter.loadModule(source);
this.context = previousContext;
return module;
}
findLocation(name: string, interpreter: Interpreter): string {
if (name.startsWith(".")) {
const localPath = path.join(path.dirname(interpreter.entryFile), name);
if (existsSync(localPath)) {
return localPath;
}
if (this.context) {
const libPath = path.join(this.context, name);
if (existsSync(libPath)) {
return libPath;
}
}
}
this.ensurePackages(interpreter);
const pieces = name.split("/");
const packageName = pieces[0];
const packagePath = pieces.slice(1).join("/");
return path.join(this.getPackage(packageName), packagePath);
}
getPackage(name: string): string {
if (!this.packages) {
throw new RuntimeError({} as Token, "Expect .packages to be loaded");
}
const location = this.packages.get(name);
if (!location) {
throw new RuntimeError({} as Token, "Invalid package name: " + name);
} else {
return location;
}
}
// from the entry file
// search: search directory.
// if .obi-packages exists, use
// else move up one directory
// if up one directory
// if obipub.toml exists, mark root
// goto: search
findObiPackageFile(from: string): string | null {
let root: string | null = null;
const search = (from: string): string | null => {
for (const entry of Deno.readDirSync(from)) {
if (entry.isDirectory && entry.name === ".obi_tool") {
root = from;
const resolutionFile = path.join(
from,
".obi_tool",
"package_resolution.json",
);
if (existsSync(resolutionFile)) {
return resolutionFile;
}
}
if (entry.isFile && entry.name === "obipub.toml") {
root = from;
}
}
if (root) {
return null;
} else {
return search(path.dirname(from));
}
};
return search(from);
}
ensurePackages(interpreter: Interpreter) {
if (!this.packages) {
const packagePath = this.findObiPackageFile(
path.dirname(interpreter.entryFile),
);
if (!packagePath) {
throw new RuntimeError(
{} as Token,
"Expected '.obi_tools/package_resolution.json' file at the package root, but did not find one. Have you run \`obi pub get\`?",
);
}
if (existsSync(packagePath)) {
const packageFile = Deno.readTextFileSync(packagePath);
this.packages = this.parsePackageFile(packageFile);
} else {
this.packages = new Map<string, string>();
}
}
}
parsePackageFile(source: string): Map<string, string> {
const packages = new Map<string, string>();
const info = JSON.parse(source);
for (const p of info.packages) {
packages.set(p.name, p.archiveUri);
}
return packages;
}
loadBuiltins(): ObiTable {
const module = new ObiTable();
module.setDyn("print", new RtPrint());
module.setDyn("clock", new RtClock());
module.setDyn("delay", new RtDelay());
module.setDyn("clear_delay", new RtClearDelay());
module.setDyn("type", new RtType());
module.setDyn("keys", new RtKeys());
module.setDyn("str", new RtStr());
module.setDyn("strlen", new RtStrlen());
module.setDyn("strslice", new RtStrslice());
module.setDyn("parse_float", new RtParseFloat());
module.setDyn("listen_tcp", new RtListenTcp());
module.setDyn("readfile", new RtReadfile());
module.setDyn("readfile_bytes", new RtReadfileBytes());
module.setDyn("bytes_concat", new RtBytesConcat());
module.setDyn("process_args", new RtProcessArgs());
module.setDyn("text_encode", new RtTextEncode());
module.setDyn("text_decode", new RtTextDecode());
module.setDyn("len", new RtLen());
module.setDyn("mod", new RtMod());
module.setDyn("load_wasm", new RtLoadWasm());
return module;
}
}
export class RtClock implements ObiCallable {
arity(): number {
return 0;
}
call(interpreter: Interpreter, args: any[]): any {
return Date.now();
}
}
export class RtRandom implements ObiCallable {
arity(): number {
return 0;
}
call(interpreter: Interpreter, args: any[]): any {
return Math.random();
}
}
export class RtTextEncode implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
const bytes = (new TextEncoder()).encode(args[0]);
return bytes;
}
}
export class RtTextDecode implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
if (!(args[0] instanceof Uint8Array)) {
throw new RuntimeError(
{} as Token,
`text_decode called on "${typeof args[0]}"`,
);
}
return (new TextDecoder()).decode(args[0]);
}
}
export class RtBytesConcat implements ObiCallable {
arity(): number {
return 2;
}
call(interpreter: Interpreter, args: any[]): any {
const a = args[0];
const b = args[1];
if (!(a instanceof Uint8Array && b instanceof Uint8Array)) {
throw new RuntimeError(
new Token(TT.IDENTIFIER, "bytes_concat", null, 0, 0),
"Expect both arguments to be byte arrays.",
);
}
const result = new Uint8Array(a.length + b.length);
result.set(a);
result.set(b, a.length);
return result;
}
}
export class RtParseFloat implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
const v = Number.parseFloat(args[0]);
if (isNaN(v)) return null;
return v;
}
}
export class WasmFunction implements ObiCallable {
func: CallableFunction;
constructor(func: CallableFunction) {
this.func = func;
}
arity(): number {
return this.func.length;
}
call(interpreter: Interpreter, args: any[]): any {
return (this.func as any).apply(null, args);
}
}
export class RtArrayMake implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
return new Uint8Array(args[0]);
}
}
export class RtAtob implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
return atob(args[0]);
}
}
export class RtBtoa implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
return btoa(args[0]);
}
}
export class RtStrcharcode implements ObiCallable {
arity(): number {
return 2;
}
call(interpreter: Interpreter, args: any[]): any {
return args[0].charCodeAt(args[1]);
}
}
export class RtLoadWasm implements ObiCallable {
arity(): number {
return 2;
}
parseImport(table: ObiTable, interpreter: Interpreter): WebAssembly.Imports {
const importObj = {
env: <{ [key: string]: any }> {},
};
const env = table.getDyn("env");
if (null !== env && env instanceof ObiTable) {
const fields: Map<string, any> = (env as ObiTable).getFields();
const keys = fields.keys();
for (const key of keys) {
const value = fields.get(key);
if (value instanceof ObiFunction) {
const fun = value as ObiFunction;
importObj.env[key] = function () {
const args: any[] = arguments as any;
return fun.call(interpreter, args);
};
}
}
}
return importObj;
}
call(interpreter: Interpreter, args: any[]): any {
const wasmCode = args[0] as Uint8Array;
const importObj = args[1] as ObiTable;
const wasmModule = new WebAssembly.Module(wasmCode);
const wasmImportObj = this.parseImport(importObj, interpreter);
const wasmInstance = new WebAssembly.Instance(wasmModule, wasmImportObj);
const obiWasmInstance = new ObiTable();
const keys = Object.keys(wasmInstance.exports);
for (const key of keys) {
if (key === "memory") {
const memory = wasmInstance.exports[key] as WebAssembly.Memory;
const view = new Uint8Array(memory.buffer);
obiWasmInstance.setDyn(
key,
view,
);
} else {
const func = wasmInstance.exports[key] as CallableFunction;
obiWasmInstance.setDyn(
key,
new WasmFunction(func),
);
}
}
return obiWasmInstance;
}
}
export class RtFdOpen implements ObiCallable {
arity(): number {
return 2;
}
call(interpreter: Interpreter, args: any[]): any {
const path = args[0];
const opts = args[1];
const read = !!(opts & 0x0001);
const write = !!(opts & 0x0002);
const append = !!(opts & 0x0004);
const create = !!(opts & 0x0008);
const truncate = !!(opts & 0x0010);
const file = Deno.openSync(path, { read, write, append, create, truncate });
return file.rid;
}
}
export class RtFdClose implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
const rid = args[0];
return Deno.close(rid);
}
}
export class RtFdSeek implements ObiCallable {
arity(): number {
return 2;
}
call(interpreter: Interpreter, args: any[]): any {
const rid = args[0];
const offset = args[1];
return Deno.seekSync(rid, offset, Deno.SeekMode.Start);
}
}
export class RtFdRead implements ObiCallable {
arity(): number {
return 2;
}
call(interpreter: Interpreter, args: any[]): any {
const rid = args[0];
const buffer = args[1];
return Deno.readSync(rid, buffer);
}
}
export class RtFdWrite implements ObiCallable {
arity(): number {
return 2;
}
call(interpreter: Interpreter, args: any[]): any {
const rid = args[0];
const data = args[1];
return Deno.writeSync(rid, data);
}
}
export class RtFdFdatasync implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
const rid = args[0];
return Deno.fdatasyncSync(rid);
}
}
export class RtFdSize implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
const rid = args[0];
return Deno.fstatSync(rid).size;
}
}
export class RtFileExists implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
const path = args[0];
try {
Deno.statSync(path);
} catch (e) {
if (e instanceof Deno.errors.NotFound) {
return 0;
}
}
return 1;
}
}
export class RtProcessExit implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
const code = args[0];
Deno.exit(code);
}
}
function toJson(table: ObiTable): object {
const result: { [key: string]: any } = {};
for (const k of table.getFields().keys()) {
const v = table.getFields().get(k);
if (v instanceof ObiTable) {
result[k] = toJson(v);
} else {
result[k] = v;
}
}
return result;
}
function parseHeaders(headers: Headers): ObiTable {
const result = new ObiTable();
for (const pair of headers.entries()) {
result.setDyn(pair[0], pair[1]);
}
return result;
}
export class RtFetch implements ObiCallable {
arity(): number {
return 4;
}
call(interpreter: Interpreter, args: any[]): any {
const url = args[0];
const method = args[1];
let body: object | null;
if (method === "GET") {
body = null;
} else {
body = toJson(args[2]);
}
const callback = args[3];
interpreter.waitGroup.add(1);
(async () => {
const res: Response = await fetch(url, {
method,
body: body && JSON.stringify(body),
});
const info = new ObiTable();
info.setDyn("headers", parseHeaders(res.headers));
info.setDyn("redirected", !!res.redirected);
info.setDyn("url", res.url.toString());
info.setDyn("status", res.status);
const data = new Uint8Array(await res.arrayBuffer());
interpreter.waitGroup.done();
callback.call(interpreter, [data, info]);
})();
}
}
function toArray(table: ObiTable): string[] {
const result = [];
const tableLen = table.getFields().size;
for (let i = 0; i < tableLen; i++) {
const key = i as unknown;
result.push(table.getFields().get(key as string) as string);
}
return result;
}
export class RtProcessRun implements ObiCallable {
arity(): number {
return 2;
}
call(interpreter: Interpreter, args: any[]): any {
interpreter.waitGroup.add(1);
const p = Deno.run({
cmd: toArray(args[0]),
stdout: "piped",
stderr: "piped",
});
const callback = args[1];
(async () => {
const output = await p.output();
const stderrOutput = await p.stderrOutput();
const result = new ObiTable();
result.setDyn("output", output);
result.setDyn("stderrOutput", stderrOutput);
callback.call(interpreter, [result]);
interpreter.waitGroup.done();
})();
}
}
export class RtProcessEnv implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
return Deno.env.get(args[0]);
}
}
export class RtEnsureDir implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
return ensureDirSync(args[0]);
}
}
export class RtListSort implements ObiCallable {
arity(): number {
return 1;
}
call(interpreter: Interpreter, args: any[]): any {
if (args[0] instanceof ObiTable) {
const table = args[0] as ObiTable;
const values = new Array(table.getFields().size);
let i = 0;
for (const value of table.getFields().values()) {
values[i] = value;
i += 1;
}
const sorted = values.sort();
const result = new ObiTable();
i = 0;
for (let i = 0; i < sorted.length; i++) {
result.setDyn(i, sorted[i]);
}
return result;
}
}
}