wasm-rquickjs 0.2.2

Tool for wrapping JavaScript modules as WebAssembly components using the QuickJS engine
Documentation
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

// This module ports:
// - https://github.com/nodejs/node/blob/master/src/util-inl.h
// - https://github.com/nodejs/node/blob/master/src/util.cc
// - https://github.com/nodejs/node/blob/master/src/util.h

import {
    get_promise_details as getPromiseDetailsNative,
    get_proxy_details as getProxyDetailsNative,
} from "__wasm_rquickjs_builtin/internal/binding/util_native";

const privateSymbolRegistryKey = "__wasm_rquickjs_internal_private_symbols";

function installPrivateSymbolAccessor(privateSymbol, store) {
    if (Object.prototype.hasOwnProperty.call(Object.prototype, privateSymbol)) {
        return;
    }

    Object.defineProperty(Object.prototype, privateSymbol, {
        configurable: true,
        enumerable: false,
        get() {
            if (this === Object.prototype) {
                return undefined;
            }
            return store.get(Object(this));
        },
        set(value) {
            if (this === Object.prototype) {
                return;
            }
            store.set(Object(this), value);
        },
    });
}

function createPrivateSymbol(description) {
    const privateSymbol = Symbol(description);
    const store = new WeakMap();
    installPrivateSymbolAccessor(privateSymbol, store);
    return privateSymbol;
}

export const privateSymbols = (() => {
    const existing = globalThis[privateSymbolRegistryKey];
    if (existing && typeof existing === "object") {
        return existing;
    }

    const symbols = Object.freeze({
        arrow_message_private_symbol: createPrivateSymbol("node:arrowMessage"),
        decorated_private_symbol: createPrivateSymbol("node:decorated"),
    });

    Object.defineProperty(globalThis, privateSymbolRegistryKey, {
        value: symbols,
        writable: false,
        configurable: true,
        enumerable: false,
    });

    return symbols;
})();

/**
 *
 * @param {string} msg
 * @return {never}
 */
export function notImplemented(msg) {
    const message = msg ? `Not implemented: ${msg}` : "Not implemented";
    throw new Error(message);
}

/**
 * 
 * @param {number} _fd 
 * @return {string}
 */
export function guessHandleType(_fd) {
    notImplemented("util.guessHandleType");
}

export function getProxyDetails(value, fullProxy = true) {
    return getProxyDetailsNative(value, fullProxy);
}

export function getPromiseDetails(value) {
    return getPromiseDetailsNative(value);
}

export const ALL_PROPERTIES = 0;
export const ONLY_WRITABLE = 1;
export const ONLY_ENUMERABLE = 2;
export const ONLY_CONFIGURABLE = 4;
export const ONLY_ENUM_WRITABLE = 6;
export const SKIP_STRINGS = 8;
export const SKIP_SYMBOLS = 16;

const previewEntriesCache = new WeakMap();
const weakMapEntriesCache = new WeakMap();
const weakSetEntriesCache = new WeakMap();

if (typeof WeakMap === "function") {
    const weakMapSet = WeakMap.prototype.set;
    const weakMapDelete = WeakMap.prototype.delete;

    WeakMap.prototype.set = function set(key, value) {
        const result = weakMapSet.call(this, key, value);
        let entries = weakMapEntriesCache.get(this);
        if (entries === undefined) {
            entries = [];
            weakMapEntriesCache.set(this, entries);
        }

        for (let i = 0; i < entries.length; i++) {
            if (Object.is(entries[i][0], key)) {
                entries[i][1] = value;
                return result;
            }
        }

        entries.push([key, value]);
        return result;
    };

    WeakMap.prototype.delete = function del(key) {
        const deleted = weakMapDelete.call(this, key);
        if (deleted) {
            const entries = weakMapEntriesCache.get(this);
            if (entries !== undefined) {
                for (let i = 0; i < entries.length; i++) {
                    if (Object.is(entries[i][0], key)) {
                        entries.splice(i, 1);
                        break;
                    }
                }
            }
        }
        return deleted;
    };
}

if (typeof WeakSet === "function") {
    const weakSetAdd = WeakSet.prototype.add;
    const weakSetDelete = WeakSet.prototype.delete;

    WeakSet.prototype.add = function add(value) {
        const result = weakSetAdd.call(this, value);
        let entries = weakSetEntriesCache.get(this);
        if (entries === undefined) {
            entries = [];
            weakSetEntriesCache.set(this, entries);
        }

        for (let i = 0; i < entries.length; i++) {
            if (Object.is(entries[i], value)) {
                return result;
            }
        }

        entries.push(value);
        return result;
    };

    WeakSet.prototype.delete = function del(value) {
        const deleted = weakSetDelete.call(this, value);
        if (deleted) {
            const entries = weakSetEntriesCache.get(this);
            if (entries !== undefined) {
                for (let i = 0; i < entries.length; i++) {
                    if (Object.is(entries[i], value)) {
                        entries.splice(i, 1);
                        break;
                    }
                }
            }
        }
        return deleted;
    };
}

const nullPrototypeConstructorNames = new WeakMap();
const originalObjectSetPrototypeOf = Object.setPrototypeOf;
const originalReflectSetPrototypeOf = Reflect.setPrototypeOf;

export function getWeakMapEntries(value) {
    const pairs = weakMapEntriesCache.get(value);
    if (!Array.isArray(pairs)) {
        return [];
    }

    const entries = [];
    for (let i = 0; i < pairs.length; i++) {
        entries.push(pairs[i][0], pairs[i][1]);
    }
    return entries;
}

export function getWeakSetEntries(value) {
    const entries = weakSetEntriesCache.get(value);
    if (!Array.isArray(entries)) {
        return [];
    }

    return entries.slice();
}

function isObjectLike(value) {
    return (
        (typeof value === "object" && value !== null) ||
        typeof value === "function"
    );
}

function findConstructorName(value) {
    if (!isObjectLike(value)) {
        return "";
    }

    let proto = value;
    while (proto !== null) {
        const descriptor = Object.getOwnPropertyDescriptor(proto, "constructor");
        if (
            descriptor !== undefined &&
            typeof descriptor.value === "function" &&
            descriptor.value.name !== ""
        ) {
            try {
                if (value instanceof descriptor.value) {
                    return descriptor.value.name;
                }
            } catch {
                // Ignore non-callable or cross-realm constructor checks.
            }
        }

        proto = Object.getPrototypeOf(proto);
    }

    return "";
}

function findIntrinsicConstructorName(value) {
    if (Array.isArray(value)) {
        return "Array";
    }

    return "";
}

function findGlobalConstructorNameByPrototype(value) {
    if (!isObjectLike(value)) {
        return "";
    }

    let proto;
    try {
        proto = Object.getPrototypeOf(value);
    } catch {
        return "";
    }

    if (!isObjectLike(proto)) {
        return "";
    }

    let names;
    try {
        names = Object.getOwnPropertyNames(globalThis);
    } catch {
        return "";
    }

    for (let i = 0; i < names.length; i++) {
        const name = names[i];
        let candidate;
        try {
            candidate = globalThis[name];
        } catch {
            continue;
        }

        if (typeof candidate !== "function" || candidate.name === "") {
            continue;
        }

        try {
            if (candidate.prototype === proto) {
                return candidate.name;
            }
        } catch {
            // Ignore host objects and poisoned accessors.
        }
    }

    return "";
}

const inspectNewCallPattern = /\binspect\s*\(\s*new\s+([A-Za-z_$][A-Za-z0-9_$]*)\s*\(/;
const stackLocationPattern = /\(?(.+):(\d+):(\d+)\)?\s*$/;

function inferConstructorNameFromCallsite() {
    const currentModule = globalThis.__wasm_rquickjs_current_module;
    if (
        currentModule === undefined ||
        currentModule === null ||
        typeof currentModule.source !== "string"
    ) {
        return "";
    }

    let stack;
    try {
        stack = String(new Error().stack || "");
    } catch {
        return "";
    }

    const sourceLines = currentModule.source.split("\n");
    const stackLines = stack.split("\n");
    for (let i = stackLines.length - 1; i >= 1; i--) {
        if (!stackLines[i].includes("anonymous (")) {
            continue;
        }

        const locationMatch = stackLocationPattern.exec(stackLines[i]);
        if (locationMatch === null) {
            continue;
        }

        const lineNumber = Number(locationMatch[2]);
        if (!Number.isInteger(lineNumber) || lineNumber < 1 || lineNumber > sourceLines.length) {
            continue;
        }

        const snippet = [
            sourceLines[lineNumber - 4],
            sourceLines[lineNumber - 3],
            sourceLines[lineNumber - 2],
            sourceLines[lineNumber - 1],
        ]
            .filter((line) => typeof line === "string")
            .join(" ");

        const constructorMatch = inspectNewCallPattern.exec(snippet);
        if (constructorMatch !== null) {
            return constructorMatch[1];
        }
    }

    return "";
}

function trackNullPrototypeConstructor(target, proto) {
    if (!isObjectLike(target)) {
        return;
    }

    const tracksNullProtoChain = proto === null || (
        isObjectLike(proto) && Object.getPrototypeOf(proto) === null
    );

    if (tracksNullProtoChain) {
        const constructorName = findIntrinsicConstructorName(target) || findConstructorName(target);
        if (constructorName !== "") {
            nullPrototypeConstructorNames.set(target, constructorName);
        } else {
            nullPrototypeConstructorNames.delete(target);
        }
        return;
    }

    nullPrototypeConstructorNames.delete(target);
}

Object.setPrototypeOf = function setPrototypeOf(target, proto) {
    trackNullPrototypeConstructor(target, proto);
    try {
        return originalObjectSetPrototypeOf(target, proto);
    } catch (err) {
        if (proto === null && isObjectLike(target)) {
            nullPrototypeConstructorNames.delete(target);
        }
        throw err;
    }
};

Reflect.setPrototypeOf = function setPrototypeOf(target, proto) {
    trackNullPrototypeConstructor(target, proto);
    let success = false;
    try {
        success = originalReflectSetPrototypeOf(target, proto);
        return success;
    } finally {
        if (!success && proto === null && isObjectLike(target)) {
            nullPrototypeConstructorNames.delete(target);
        }
    }
};

export function getConstructorName(value, allowCallsiteFallback = false) {
    if (!isObjectLike(value)) {
        return "Object";
    }

    const trackedName = nullPrototypeConstructorNames.get(value);
    if (trackedName !== undefined) {
        return trackedName;
    }

    const constructorName = findConstructorName(value);
    if (constructorName !== "") {
        return constructorName;
    }

    const intrinsicConstructorName = findIntrinsicConstructorName(value);
    if (intrinsicConstructorName !== "") {
        return intrinsicConstructorName;
    }

    const globalConstructorName = findGlobalConstructorNameByPrototype(value);
    if (globalConstructorName !== "") {
        return globalConstructorName;
    }

    // QuickJS does not expose V8's hidden-class constructor-name recovery API.
    // When inspecting `new Foo()` objects whose prototype was replaced with a
    // null-prototype object, infer `Foo` from the user callsite as a fallback.
    if (allowCallsiteFallback) {
        const callsiteConstructorName = inferConstructorNameFromCallsite();
        if (callsiteConstructorName !== "") {
            return callsiteConstructorName;
        }
    }

    return "Object";
}

/**
 * Efficiently determine whether the provided property key is numeric
 * (and thus could be an array indexer) or not.
 *
 * Always returns true for values of type `'number'`.
 *
 * Otherwise, only returns true for strings that consist only of positive integers.
 *
 * Results are cached.
 * 
 * @type {Record<string, boolean>}
 */
const isNumericLookup = {};
const kMaxArrayIndex = 2 ** 32 - 2;

/**
 * 
 * @param {unknown} value 
 * @returns {boolean}
 */
export function isArrayIndex(value) {
    switch (typeof value) {
        case "number":
            return Number.isInteger(value) && value >= 0 && value <= kMaxArrayIndex;
        case "string": {
            const result = isNumericLookup[value];
            if (result !== void 0) {
                return result;
            }
            const length = value.length;
            if (length === 0) {
                return isNumericLookup[value] = false;
            }
            let ch = 0;
            let i = 0;
            for (; i < length; ++i) {
                ch = value.charCodeAt(i);
                if (
                    i === 0 && ch === 0x30 && length > 1 /* must not start with 0 */ ||
                    ch < 0x30 /* 0 */ || ch > 0x39 /* 9 */
                ) {
                    return isNumericLookup[value] = false;
                }
            }

            const numericValue = Number(value);
            return isNumericLookup[value] = Number.isInteger(numericValue) &&
                numericValue >= 0 &&
                numericValue <= kMaxArrayIndex &&
                `${numericValue}` === value;
        }
        default:
            return false;
    }
}

/**
 * 
 * @param {object} obj 
 * @param {number} filter 
 * @returns {(string | symbol)[]}
 */
export function getOwnNonIndexProperties(
    // deno-lint-ignore ban-types
    obj,
    filter,
) {
    let allProperties = [
        ...Object.getOwnPropertyNames(obj),
        ...Object.getOwnPropertySymbols(obj),
    ];

    if (Array.isArray(obj) || (ArrayBuffer.isView(obj) && !(obj instanceof DataView))) {
        allProperties = allProperties.filter((k) => !isArrayIndex(k));
    }

    if (filter === ALL_PROPERTIES) {
        return allProperties;
    }

    /**
     * @type {(string | symbol)[]}
     */
    const result = [];
    for (const key of allProperties) {
        const desc = Object.getOwnPropertyDescriptor(obj, key);
        if (desc === undefined) {
            continue;
        }
        if (filter & ONLY_WRITABLE && !desc.writable) {
            continue;
        }
        if (filter & ONLY_ENUMERABLE && !desc.enumerable) {
            continue;
        }
        if (filter & ONLY_CONFIGURABLE && !desc.configurable) {
            continue;
        }
        if (filter & SKIP_STRINGS && typeof key === "string") {
            continue;
        }
        if (filter & SKIP_SYMBOLS && typeof key === "symbol") {
            continue;
        }
        result.push(key);
    }
    return result;
}

export function previewEntries(iterable, isMap, preserveRawEntries = false) {
    let flattenedEntries;
    let rawEntries;
    let isKeyValue = true;
    if (iterable !== null && (typeof iterable === "object" || typeof iterable === "function")) {
        const cached = previewEntriesCache.get(iterable);
        if (cached !== undefined) {
            flattenedEntries = cached.flattenedEntries.slice();
            rawEntries = cached.rawEntries.slice();
            isKeyValue = cached.isKeyValue;
        }
    }

    if (flattenedEntries === undefined || rawEntries === undefined) {
        flattenedEntries = [];
        rawEntries = [];
        for (const value of iterable) {
            rawEntries.push(value);
            if (Array.isArray(value) && value.length >= 2) {
                flattenedEntries.push(value[0], value[1]);
            } else {
                isKeyValue = false;
                flattenedEntries.push(value);
            }
        }

        if (iterable !== null && (typeof iterable === "object" || typeof iterable === "function")) {
            previewEntriesCache.set(iterable, {
                flattenedEntries: flattenedEntries.slice(),
                rawEntries: rawEntries.slice(),
                isKeyValue,
            });
        }
    }

    if (preserveRawEntries) {
        return rawEntries;
    }

    if (isMap === true) {
        return [flattenedEntries, isKeyValue];
    }

    return flattenedEntries;
}