summaryrefslogtreecommitdiff
path: root/chapter-7/hoot/reflect.js
blob: b00874c0e746789fd2d14f75ac88c377407e794b (plain)
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
// -*- js2-basic-offset: 4 -*-
class Char {
    constructor(codepoint) {
        this.codepoint = codepoint;
    }
    toString() {
        let ch = String.fromCodePoint(this.codepoint);
        if (ch.match(/[a-zA-Z0-9$[\]().]/)) return `#\\${ch}`;
        return `#\\x${this.codepoint.toString(16)}`;
    }
}
class Eof { toString() { return "#<eof>"; } }
class Nil { toString() { return "#nil"; } }
class Null { toString() { return "()"; } }
class Unspecified { toString() { return "#<unspecified>"; } }

class Complex {
    constructor(real, imag) {
        this.real = real;
        this.imag = imag;
    }
    toString() {
        const sign = this.imag >= 0 && Number.isFinite(this.imag) ? "+": "";
        return `${flonum_to_string(this.real)}${sign}${flonum_to_string(this.imag)}i`;
    }
}
class Fraction {
    constructor(num, denom) {
        this.num = num;
        this.denom = denom;
    }
    toString() {
        return `${this.num}/${this.denom}`;
    }
}

class HeapObject {
    constructor(reflector, obj) {
        this.reflector = reflector;
        this.obj = obj;
    }
    repr() { return this.toString(); } // Default implementation.
}

class Pair extends HeapObject {
    toString() { return "#<pair>"; }
    repr() {
        let car_repr = repr(this.reflector.car(this));
        let cdr_repr = repr(this.reflector.cdr(this));
        if (cdr_repr == '()')
            return `(${car_repr})`;
        if (cdr_repr.charAt(0) == '(')
            return `(${car_repr} ${cdr_repr.substring(1)}`;
        return `(${car_repr} . ${cdr_repr})`;
    }
}
class MutablePair extends Pair { toString() { return "#<mutable-pair>"; } }

class Vector extends HeapObject {
    toString() { return "#<vector>"; }
    repr() {
        let len = this.reflector.vector_length(this);
        let out = '#(';
        for (let i = 0; i < len; i++) {
            if (i) out += ' ';
            out += repr(this.reflector.vector_ref(this, i));
        }
        out += ')';
        return out;
    }
}
class MutableVector extends Vector {
    toString() { return "#<mutable-vector>"; }
}

class Bytevector extends HeapObject {
    toString() { return "#<bytevector>"; }
    repr() {
        let len = this.reflector.bytevector_length(this);
        let out = '#vu8(';
        for (let i = 0; i < len; i++) {
            if (i) out += ' ';
            out += this.reflector.bytevector_ref(this, i);
        }
        out += ')';
        return out;
    }
}
class MutableBytevector extends Bytevector {
    toString() { return "#<mutable-bytevector>"; }
}

class Bitvector extends HeapObject {
    toString() { return "#<bitvector>"; }
    repr() {
        let len = this.reflector.bitvector_length(this);
        let out = '#*';
        for (let i = 0; i < len; i++) {
            out += this.reflector.bitvector_ref(this, i) ? '1' : '0';
        }
        return out;
    }
}
class MutableBitvector extends Bitvector {
    toString() { return "#<mutable-bitvector>"; }
}

class MutableString extends HeapObject {
    toString() { return "#<mutable-string>"; }
    repr() { return string_repr(this.reflector.string_value(this)); }
}

class Procedure extends HeapObject {
    toString() { return "#<procedure>"; }
    call(...arg) {
        return this.reflector.call(this, ...arg);
    }
    async call_async(...arg) {
        return await this.reflector.call_async(this, ...arg);
    }
}

class Sym extends HeapObject {
    toString() { return "#<symbol>"; }
    repr() { return this.reflector.symbol_name(this); }
}

class Keyword extends HeapObject {
    toString() { return "#<keyword>"; }
    repr() { return `#:${this.reflector.keyword_name(this)}`; }
}

class Variable extends HeapObject { toString() { return "#<variable>"; } }
class AtomicBox extends HeapObject { toString() { return "#<atomic-box>"; } }
class HashTable extends HeapObject { toString() { return "#<hash-table>"; } }
class WeakTable extends HeapObject { toString() { return "#<weak-table>"; } }
class Fluid extends HeapObject { toString() { return "#<fluid>"; } }
class DynamicState extends HeapObject { toString() { return "#<dynamic-state>"; } }
class Syntax extends HeapObject { toString() { return "#<syntax>"; } }
class Port extends HeapObject { toString() { return "#<port>"; } }
class Struct extends HeapObject { toString() { return "#<struct>"; } }

function instantiate_streaming(path, imports) {
    if (typeof fetch !== 'undefined')
        return WebAssembly.instantiateStreaming(fetch(path), imports);
    let bytes;
    if (typeof read !== 'undefined') {
        bytes = read(path, 'binary');
    } else if (typeof readFile !== 'undefined') {
        bytes = readFile(path);
    } else {
        let fs = require('fs');
        bytes = fs.readFileSync(path);
    }
    return WebAssembly.instantiate(bytes, imports);
}

class Scheme {
    #instance;
    #abi;
    constructor(instance, abi) {
        this.#instance = instance;
        this.#abi = abi;
    }

    static async reflect(abi, {reflect_wasm_dir = '.'}) {
        let debug = {
            debug_str(x) { console.log(`reflect debug: ${x}`); },
            debug_str_i32(x, y) { console.log(`reflect debug: ${x}: ${y}`); },
            debug_str_scm: (x, y) => {
                console.log(`reflect debug: ${x}: #<scm>`);
            },
        };
        let reflect_wasm = reflect_wasm_dir + '/reflect.wasm';
        let rt = {
            die(tag, data) { throw new SchemeTrapError(tag, data); },
            wtf8_to_string(wtf8) { return wtf8_to_string(wtf8); },
            string_to_wtf8(str) { return string_to_wtf8(str); },
        };
        let { module, instance } =
            await instantiate_streaming(reflect_wasm, { abi, debug, rt });
        return new Scheme(instance, abi);
    }

    #init_module(mod) {
        mod.set_debug_handler({
            debug_str(x) { console.log(`debug: ${x}`); },
            debug_str_i32(x, y) { console.log(`debug: ${x}: ${y}`); },
            debug_str_scm: (x, y) => {
                console.log(`debug: ${x}: ${repr(this.#to_js(y))}`);
            },
        });
        mod.set_ffi_handler({
            procedure_to_extern: (obj) => {
                const proc = this.#to_js(obj);
                return (...args) => {
                    return proc.call(...args);
                };
            }
        });
        let proc = new Procedure(this, mod.get_export('$load').value);
        return proc.call();
    }
    static async load_main(path, opts = {}) {
        let mod = await SchemeModule.fetch_and_instantiate(path, opts);
        let reflect = await mod.reflect(opts);
        return reflect.#init_module(mod);
    }
    async load_extension(path, opts = {}) {
        opts = Object.assign({ abi: this.#abi }, opts);
        let mod = await SchemeModule.fetch_and_instantiate(path, opts);
        return this.#init_module(mod);
    }

    #to_scm(js) {
        let api = this.#instance.exports;
        if (typeof(js) == 'number') {
            return api.scm_from_f64(js);
        } else if (typeof(js) == 'bigint') {
            if (BigInt(api.scm_most_negative_fixnum()) <= js
                && js <= BigInt(api.scm_most_positive_fixnum()))
                return api.scm_from_fixnum(Number(js));
            return api.scm_from_bignum(js);
        } else if (typeof(js) == 'boolean') {
            return js ? api.scm_true() : api.scm_false();
        } else if (typeof(js) == 'string') {
            return api.scm_from_string(js);
        } else if (typeof(js) == 'object') {
            if (js instanceof Eof) return api.scm_eof();
            if (js instanceof Nil) return api.scm_nil();
            if (js instanceof Null) return api.scm_null();
            if (js instanceof Unspecified) return api.scm_unspecified();
            if (js instanceof Char) return api.scm_from_char(js.codepoint);
            if (js instanceof HeapObject) return js.obj;
            if (js instanceof Fraction)
                return api.scm_from_fraction(this.#to_scm(js.num),
                                             this.#to_scm(js.denom));
            if (js instanceof Complex)
                return api.scm_from_complex(js.real, js.imag);
            return api.scm_from_extern(js);
        } else if (typeof(js) == 'function') {
            return api.scm_from_extern(js);
        } else {
            throw new Error(`unexpected; ${typeof(js)}`);
        }
    }

    #to_js(scm) {
        let api = this.#instance.exports;
        let descr = api.describe(scm);
        let handlers = {
            fixnum: () => BigInt(api.fixnum_value(scm)),
            char: () => new Char(api.char_value(scm)),
            true: () => true,
            false: () => false,
            eof: () => new Eof,
            nil: () => new Nil,
            null: () => new Null,
            unspecified: () => new Unspecified,
            flonum: () => api.flonum_value(scm),
            bignum: () => api.bignum_value(scm),
            complex: () => new Complex(api.complex_real(scm),
                                       api.complex_imag(scm)),
            fraction: () => new Fraction(this.#to_js(api.fraction_num(scm)),
                                         this.#to_js(api.fraction_denom(scm))),
            pair: () => new Pair(this, scm),
            'mutable-pair': () => new MutablePair(this, scm),
            vector: () => new Vector(this, scm),
            'mutable-vector': () => new MutableVector(this, scm),
            bytevector: () => new Bytevector(this, scm),
            'mutable-bytevector': () => new MutableBytevector(this, scm),
            bitvector: () => new Bitvector(this, scm),
            'mutable-bitvector': () => new MutableBitvector(this, scm),
            string: () => api.string_value(scm),
            'mutable-string': () => new MutableString(this, scm),
            procedure: () => new Procedure(this, scm),
            symbol: () => new Sym(this, scm),
            keyword: () => new Keyword(this, scm),
            variable: () => new Variable(this, scm),
            'atomic-box': () => new AtomicBox(this, scm),
            'hash-table': () => new HashTable(this, scm),
            'weak-table': () => new WeakTable(this, scm),
            fluid: () => new Fluid(this, scm),
            'dynamic-state': () => new DynamicState(this, scm),
            syntax: () => new Syntax(this, scm),
            port: () => new Port(this, scm),
            struct: () => new Struct(this, scm),
            'extern-ref': () => api.extern_value(scm)
        };
        let handler = handlers[descr];
        return handler ? handler() : scm;
    }

    call(func, ...args) {
        let api = this.#instance.exports;
        let argv = api.make_vector(args.length + 1, api.scm_false());
        func = this.#to_scm(func);
        api.vector_set(argv, 0, func);
        for (let [idx, arg] of args.entries())
            api.vector_set(argv, idx + 1, this.#to_scm(arg));
        argv = api.call(func, argv);
        let results = [];
        for (let idx = 0; idx < api.vector_length(argv); idx++)
            results.push(this.#to_js(api.vector_ref(argv, idx)))
        return results;
    }

    call_async(func, ...args) {
        return new Promise((resolve, reject) => {
            this.call(func,
                      val => resolve(this.#to_js(val)),
                      err => reject(this.#to_js(err)),
                      ...args);
        })
    }

    car(x) { return this.#to_js(this.#instance.exports.car(x.obj)); }
    cdr(x) { return this.#to_js(this.#instance.exports.cdr(x.obj)); }

    vector_length(x) { return this.#instance.exports.vector_length(x.obj); }
    vector_ref(x, i) {
        return this.#to_js(this.#instance.exports.vector_ref(x.obj, i));
    }

    bytevector_length(x) {
        return this.#instance.exports.bytevector_length(x.obj);
    }
    bytevector_ref(x, i) {
        return this.#instance.exports.bytevector_ref(x.obj, i);
    }

    bitvector_length(x) {
        return this.#instance.exports.bitvector_length(x.obj);
    }
    bitvector_ref(x, i) {
        return this.#instance.exports.bitvector_ref(x.obj, i) == 1;
    }

    string_value(x) { return this.#instance.exports.string_value(x.obj); }
    symbol_name(x) { return this.#instance.exports.symbol_name(x.obj); }
    keyword_name(x) { return this.#instance.exports.keyword_name(x.obj); }
}

class SchemeTrapError extends Error {
    constructor(tag, data) { super(); this.tag = tag; this.data = data; }
    // FIXME: data is raw Scheme object; would need to be reflected to
    // have a toString.
    toString() { return `SchemeTrap(${this.tag}, <data>)`; }
}

function string_repr(str) {
    // FIXME: Improve to match Scheme.
    return '"' + str.replace(/(["\\])/g, '\\$1').replace(/\n/g, '\\n') + '"';
}

function flonum_to_string(f64) {
    if (Object.is(f64, -0)) {
        return '-0.0';
    } else if (Number.isFinite(f64)) {
        let repr = f64 + '';
        return /^-?[0-9]+$/.test(repr) ? repr + '.0' : repr;
    } else if (Number.isNaN(f64)) {
        return '+nan.0';
    } else {
        return f64 < 0 ? '-inf.0' : '+inf.0';
    }
}

let async_invoke = typeof queueMicrotask !== 'undefined'
    ? queueMicrotask
    : thunk => setTimeout(thunk, 0);
function async_invoke_later(thunk, jiffies) {
    setTimeout(thunk, jiffies / 1000);
}

let wtf8_helper;

function wtf8_to_string(wtf8) {
    let { as_iter, iter_next } = wtf8_helper.exports;
    let codepoints = [];
    let iter = as_iter(wtf8);
    for (let cp = iter_next(iter); cp != -1; cp = iter_next(iter))
        codepoints.push(cp);

    // Passing too many codepoints can overflow the stack.
    let maxcp = 100000;
    if (codepoints.length <= maxcp) {
        return String.fromCodePoint(...codepoints);
    }

    // For converting large strings, concatenate several smaller
    // strings.
    let substrings = [];
    let end = 0;
    for (let start = 0; start != codepoints.length; start = end) {
        end = Math.min(start + maxcp, codepoints.length);
        substrings.push(String.fromCodePoint(...codepoints.slice(start, end)));
    }
    return substrings.join('');
}

function string_to_wtf8(str) {
    let { string_builder, builder_push_codepoint, finish_builder } =
        wtf8_helper.exports;
    let builder = string_builder()
    for (let cp of str)
        builder_push_codepoint(builder, cp.codePointAt(0));
    return finish_builder(builder);
}

async function load_wtf8_helper_module(reflect_wasm_dir = '') {
    if (wtf8_helper) return;
    let wtf8_wasm = reflect_wasm_dir + "/wtf8.wasm";
    let { module, instance } = await instantiate_streaming(wtf8_wasm);
    wtf8_helper = instance;
}

class SchemeModule {
    #instance;
    #io_handler;
    #debug_handler;
    #ffi_handler;
    static #rt = {
        bignum_from_string(str) { return BigInt(str); },
        bignum_from_i32(n) { return BigInt(n); },
        bignum_from_i64(n) { return n; },
        bignum_from_u64(n) { return n < 0n ? 0xffff_ffff_ffff_ffffn + (n + 1n) : n; },
        bignum_is_i64(n) {
            return -0x8000_0000_0000_0000n <= n && n <= 0x7FFF_FFFF_FFFF_FFFFn;
        },
        bignum_is_u64(n) {
            return 0n <= n && n <= 0xFFFF_FFFF_FFFF_FFFFn;
        },
        // This truncates; see https://tc39.es/ecma262/#sec-tobigint64.
        bignum_get_i64(n) { return n; },

        bignum_add(a, b) { return BigInt(a) + BigInt(b) },
        bignum_sub(a, b) { return BigInt(a) - BigInt(b) },
        bignum_mul(a, b) { return BigInt(a) * BigInt(b) },
        bignum_lsh(a, b) { return BigInt(a) << BigInt(b) },
        bignum_rsh(a, b) { return BigInt(a) >> BigInt(b) },
        bignum_quo(a, b) { return BigInt(a) / BigInt(b) },
        bignum_rem(a, b) { return BigInt(a) % BigInt(b) },
        bignum_mod(a, b) {
            let r = BigInt(a) % BigInt(b);
            if ((b > 0n && r < 0n) || (b < 0n && r > 0n)) {
                return b + r;
            } else {
                return r;
            }
        },
        bignum_gcd(a, b) {
            a = BigInt(a);
            b = BigInt(b);
            if (a < 0n) { a = -a; }
            if (b < 0n) { b = -b; }
            if (a == 0n) { return b; }
            if (b == 0n) { return a; }

            let r;
            while (b != 0n) {
                r = a % b;
                a = b;
                b = r;
            }
            return a;
        },

        bignum_logand(a, b) { return BigInt(a) & BigInt(b); },
        bignum_logior(a, b) { return BigInt(a) | BigInt(b); },
        bignum_logxor(a, b) { return BigInt(a) ^ BigInt(b); },
        bignum_logsub(a, b) { return BigInt(a) & (~ BigInt(b)); },

        bignum_lt(a, b) { return a < b; },
        bignum_le(a, b) { return a <= b; },
        bignum_eq(a, b) { return a == b; },

        bignum_to_f64(n) { return Number(n); },

        f64_is_nan(n) { return Number.isNaN(n); },
        f64_is_infinite(n) { return !Number.isFinite(n); },

        flonum_to_string,

        string_upcase: Function.call.bind(String.prototype.toUpperCase),
        string_downcase: Function.call.bind(String.prototype.toLowerCase),

        make_weak_map() { return new WeakMap; },
        weak_map_get(map, k, fail) {
            const val = map.get(k);
            return val === undefined ? fail: val;
        },
        weak_map_set(map, k, v) { return map.set(k, v); },
        weak_map_delete(map, k) { return map.delete(k); },

        fsqrt: Math.sqrt,
        fsin: Math.sin,
        fcos: Math.cos,
        ftan: Math.tan,
        fasin: Math.asin,
        facos: Math.acos,
        fatan: Math.atan,
        fatan2: Math.atan2,
        flog: Math.log,
        fexp: Math.exp,

        jiffies_per_second() { return 1000000; },
        current_jiffy() { return performance.now() * 1000; },
        current_second() { return Date.now() / 1000; },

        async_invoke,
        async_invoke_later,
        promise_on_completed(p, kt, kf) { p.then(kt, kf); },
        promise_complete(callback, val) { callback(val); },

        // Wrap in functions to allow for lazy loading of the wtf8
        // module.
        wtf8_to_string(wtf8) { return wtf8_to_string(wtf8); },
        string_to_wtf8(str) { return string_to_wtf8(str); },

        die(tag, data) { throw new SchemeTrapError(tag, data); }
    };

    constructor(instance) {
        this.#instance = instance;
        let open_file_error = (filename) => {
            throw new Error('No file system access');
        };
        if (typeof printErr === 'function') { // v8/sm dev console
            // On the console, try to use 'write' (v8) or 'putstr' (sm),
            // as these don't add an extraneous newline.  Unfortunately
            // JSC doesn't have a printer that doesn't add a newline.
            let write_no_newline =
                typeof write === 'function' ? write
                : typeof putstr === 'function' ? putstr : print;
            // Use readline when available.  v8 strips newlines so
            // we need to add them back.
            let read_stdin =
                typeof readline == 'function' ? () => {
                    let line = readline();
                    if (line) {
                        return `${line}\n`;
                    } else {
                        return '\n';
                    }
                }: () => '';
            this.#io_handler = {
                write_stdout: write_no_newline,
                write_stderr: printErr,
                read_stdin,
                file_exists: (filename) => false,
                open_input_file: open_file_error,
                open_output_file: open_file_error,
                close_file: () => undefined,
                read_file: (handle, length) => 0,
                write_file: (handle, length) => 0,
                seek_file: (handle, offset, whence) => -1,
                file_random_access: (handle) => false,
                file_buffer_size: (handle) => 0,
                file_buffer_ref: (handle, i) => 0,
                file_buffer_set: (handle, i, x) => undefined,
                delete_file: (filename) => undefined
            };
        } else if (typeof window !== 'undefined') { // web browser
            this.#io_handler = {
                write_stdout: console.log,
                write_stderr: console.error,
                read_stdin: () => '',
                file_exists: (filename) => false,
                open_input_file: open_file_error,
                open_output_file: open_file_error,
                close_file: () => undefined,
                read_file: (handle, length) => 0,
                write_file: (handle, length) => 0,
                seek_file: (handle, offset, whence) => -1,
                file_random_access: (handle) => false,
                file_buffer_size: (handle) => 0,
                file_buffer_ref: (handle, i) => 0,
                file_buffer_set: (handle, i, x) => undefined,
                delete_file: (filename) => undefined
            };
        } else { // nodejs
            const fs = require('fs');
            const process = require('process');
            const bufLength = 1024;
            const stdinBuf = Buffer.alloc(bufLength);
            const SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2;
            this.#io_handler = {
                write_stdout: console.log,
                write_stderr: console.error,
                read_stdin: () => {
                    let n = fs.readSync(process.stdin.fd, stdinBuf, 0, stdinBuf.length);
                    return stdinBuf.toString('utf8', 0, n);
                },
                file_exists: fs.existsSync.bind(fs),
                open_input_file: (filename) => {
                    let fd = fs.openSync(filename, 'r');
                    return {
                        fd,
                        buf: Buffer.alloc(bufLength),
                        pos: 0
                    };
                },
                open_output_file: (filename) => {
                    let fd = fs.openSync(filename, 'w');
                    return {
                        fd,
                        buf: Buffer.alloc(bufLength),
                        pos: 0
                    };
                },
                close_file: (handle) => {
                    fs.closeSync(handle.fd);
                },
                read_file: (handle, count) => {
                    const n = fs.readSync(handle.fd, handle.buf, 0, count, handle.pos);
                    handle.pos += n;
                    return n;
                },
                write_file: (handle, count) => {
                    const n = fs.writeSync(handle.fd, handle.buf, 0, count, handle.pos);
                    handle.pos += n;
                    return n;
                },
                seek_file: (handle, offset, whence) => {
                    // There doesn't seem to be a way to ask NodeJS if
                    // a position is valid or not.
                    if (whence == SEEK_SET) {
                        handle.pos = offset;
                        return handle.pos;
                    } else if (whence == SEEK_CUR) {
                        handle.pos += offset;
                        return handle.pos;
                    }

                    // SEEK_END not supported.
                    return -1;
                },
                file_random_access: (handle) => {
                    return true;
                },
                file_buffer_size: (handle) => {
                    return handle.buf.length;
                },
                file_buffer_ref: (handle, i) => {
                    return handle.buf[i];
                },
                file_buffer_set: (handle, i, x) => {
                    handle.buf[i] = x;
                },
                delete_file: fs.rmSync.bind(fs)
            };
        }
        this.#debug_handler = {
            debug_str(x) { console.log(`debug: ${x}`); },
            debug_str_i32(x, y) { console.log(`debug: ${x}: ${y}`); },
            debug_str_scm(x, y) { console.log(`debug: ${x}: #<scm>`); },
        };
    }
    static async fetch_and_instantiate(path, { abi, reflect_wasm_dir = '.',
                                               user_imports = {} }) {
        await load_wtf8_helper_module(reflect_wasm_dir);
        let io = {
            write_stdout(str) { mod.#io_handler.write_stdout(str); },
            write_stderr(str) { mod.#io_handler.write_stderr(str); },
            read_stdin() { return mod.#io_handler.read_stdin(); },
            file_exists(filename) { return mod.#io_handler.file_exists(filename); },
            open_input_file(filename) { return mod.#io_handler.open_input_file(filename); },
            open_output_file(filename) { return mod.#io_handler.open_output_file(filename); },
            close_file(handle) { mod.#io_handler.close_file(handle); },
            read_file(handle, length) { return mod.#io_handler.read_file(handle, length); },
            write_file(handle, length) { return mod.#io_handler.write_file(handle, length); },
            seek_file(handle, offset, whence) { return mod.#io_handler.seek_file(handle, offset, whence); },
            file_random_access(handle) { return mod.#io_handler.file_random_access(handle); },
            file_buffer_size(handle) { return mod.#io_handler.file_buffer_size(handle); },
            file_buffer_ref(handle, i) { return mod.#io_handler.file_buffer_ref(handle, i); },
            file_buffer_set(handle, i, x) { return mod.#io_handler.file_buffer_set(handle, i, x); },
            delete_file(filename) { mod.#io_handler.delete_file(filename); }
        };
        let debug = {
            debug_str(x) { mod.#debug_handler.debug_str(x); },
            debug_str_i32(x, y) { mod.#debug_handler.debug_str_i32(x, y); },
            debug_str_scm(x, y) { mod.#debug_handler.debug_str_scm(x, y); },
        }
        let ffi = {
            procedure_to_extern(proc) {
                return mod.#ffi_handler.procedure_to_extern(proc);
            }
        };
        let imports = {
          rt: SchemeModule.#rt,
          abi, debug, io, ffi, ...user_imports
        };
        let { module, instance } = await instantiate_streaming(path, imports);
        let mod = new SchemeModule(instance);
        return mod;
    }
    set_io_handler(h) { this.#io_handler = h; }
    set_debug_handler(h) { this.#debug_handler = h; }
    set_ffi_handler(h) { this.#ffi_handler = h; }
    all_exports() { return this.#instance.exports; }
    exported_abi() {
        let abi = {}
        for (let [k, v] of Object.entries(this.all_exports())) {
            if (k.startsWith("$"))
                abi[k] = v;
        }
        return abi;
    }
    exports() {
        let ret = {}
        for (let [k, v] of Object.entries(this.all_exports())) {
            if (!k.startsWith("$"))
                ret[k] = v;
        }
        return ret;
    }
    get_export(name) {
        if (name in this.all_exports())
            return this.all_exports()[name];
        throw new Error(`unknown export: ${name}`)
    }
    async reflect(opts = {}) {
        return await Scheme.reflect(this.exported_abi(), opts);
    }
}

function repr(obj) {
    if (obj instanceof HeapObject)
        return obj.repr();
    if (typeof obj === 'boolean')
        return obj ? '#t' : '#f';
    if (typeof obj === 'number')
        return flonum_to_string(obj);
    if (typeof obj === 'string')
        return string_repr(obj);
    return obj + '';
}