wasm-rquickjs-cli 0.4.9

Tool for wrapping JavaScript modules as WebAssembly components using the QuickJS engine
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
import assert from 'node:assert';
import { EventEmitter } from 'node:events';
import fs from 'node:fs';
import module, { createRequire } from 'node:module';
import { inspect } from 'node:util';

const require = createRequire(import.meta.url);

// Test 1: Error.captureStackTrace exists as a function
export const testCaptureStackTraceExists = () => {
    try {
        assert.strictEqual(typeof Error.captureStackTrace, 'function');
        assert.strictEqual(typeof Error.stackTraceLimit, 'number');
        return true;
    } catch (e) {
        console.error('testCaptureStackTraceExists FAIL:', e.message);
        return false;
    }
};

// Test 2: Basic Error.captureStackTrace usage — sets .stack on target
export const testCaptureStackTraceBasic = () => {
    try {
        const obj = {};
        Error.captureStackTrace(obj);
        assert.strictEqual(typeof obj.stack, 'string');
        assert(obj.stack.length > 0, 'stack should be non-empty');
        return true;
    } catch (e) {
        console.error('testCaptureStackTraceBasic FAIL:', e.message);
        return false;
    }
};

// Test 3: Error.prepareStackTrace receives CallSite objects
export const testPrepareStackTrace = () => {
    try {
        let receivedCallSites = null;
        const origPrepare = Error.prepareStackTrace;

        Error.prepareStackTrace = function (error, callSites) {
            receivedCallSites = callSites;
            return callSites;
        };

        const obj = {};
        Error.captureStackTrace(obj);
        // Access .stack to trigger prepareStackTrace
        const stack = obj.stack;

        Error.prepareStackTrace = origPrepare;

        assert(Array.isArray(receivedCallSites), 'callSites should be an array');
        assert(receivedCallSites.length > 0, 'callSites should be non-empty');

        // Each call site should have V8-compatible methods
        const site = receivedCallSites[0];
        assert.strictEqual(typeof site.getFileName, 'function');
        assert.strictEqual(typeof site.getLineNumber, 'function');
        assert.strictEqual(typeof site.getColumnNumber, 'function');
        assert.strictEqual(typeof site.getFunctionName, 'function');

        return true;
    } catch (e) {
        console.error('testPrepareStackTrace FAIL:', e.message);
        return false;
    }
};

// Test 4: CallSite methods return correct types
export const testCallSiteMethods = () => {
    try {
        let callSites = null;
        const origPrepare = Error.prepareStackTrace;

        Error.prepareStackTrace = function (error, sites) {
            // Only capture the CallSites from our captureStackTrace call,
            // not from internal Error construction in the ErrorShim.
            if (callSites === null) {
                callSites = sites;
            }
            return 'custom-stack';
        };

        const obj = {};
        Error.captureStackTrace(obj);
        void obj.stack;

        Error.prepareStackTrace = origPrepare;

        assert(Array.isArray(callSites), 'callSites should be an array');
        assert(callSites.length > 0, 'callSites should be non-empty');

        const site = callSites[0];

        // Check all critical methods
        const methods = [
            'getThis', 'getTypeName', 'getFunction', 'getFunctionName',
            'getMethodName', 'getFileName', 'getLineNumber', 'getColumnNumber',
            'getEvalOrigin', 'isToplevel', 'isEval', 'isNative', 'isConstructor',
            'isAsync', 'isPromiseAll', 'getPromiseIndex', 'getScriptNameOrSourceURL',
            'toString'
        ];

        for (const m of methods) {
            assert.strictEqual(typeof site[m], 'function', `CallSite.${m} should be a function`);
        }

        // Verify return types
        assert(site.getFileName() === null || typeof site.getFileName() === 'string');
        assert.strictEqual(typeof site.getLineNumber(), 'number');
        assert.strictEqual(typeof site.getColumnNumber(), 'number');
        assert.strictEqual(typeof site.isEval(), 'boolean');
        assert.strictEqual(typeof site.isNative(), 'boolean');
        assert.strictEqual(typeof site.isConstructor(), 'boolean');
        assert.strictEqual(typeof site.isToplevel(), 'boolean');
        assert.strictEqual(typeof site.toString(), 'string');

        return true;
    } catch (e) {
        console.error('testCallSiteMethods FAIL:', e.message);
        return false;
    }
};

export const testLatePrepareStackTrace = () => {
    const originalPrepare = Error.prepareStackTrace;
    try {
        Error.prepareStackTrace = undefined;
        const lateTarget = {};
        Error.captureStackTrace(lateTarget);
        Error.prepareStackTrace = () => 'late-prepare';
        assert.strictEqual(lateTarget.stack, 'late-prepare');

        Error.prepareStackTrace = () => 'capture-time-prepare';
        const removedTarget = {};
        Error.captureStackTrace(removedTarget);
        Error.prepareStackTrace = undefined;
        assert.strictEqual(typeof removedTarget.stack, 'string');
        assert.notStrictEqual(removedTarget.stack, 'capture-time-prepare');
        return true;
    } catch (e) {
        console.error('testLatePrepareStackTrace FAIL:', e.message);
        return false;
    } finally {
        Error.prepareStackTrace = originalPrepare;
    }
};

export const testDefaultErrorStackHeaders = () => {
    const originalPrepare = Error.prepareStackTrace;
    try {
        Error.prepareStackTrace = undefined;
        assert.match(new Error('base').stack, /^Error: base(?:\n|$)/);
        assert.match(new TypeError('native').stack, /^TypeError: native(?:\n|$)/);
        assert.match(new Error().stack, /^Error(?:\n|$)/);
        class CustomError extends Error {}
        CustomError.prototype.name = 'CustomError';
        assert.match(new CustomError('custom').stack, /^CustomError: custom(?:\n|$)/);
        class EmptyNameError extends Error {}
        EmptyNameError.prototype.name = '';
        assert.match(new EmptyNameError('message-only').stack, /^message-only(?:\n|$)/);
        const originalToString = Error.prototype.toString;
        Error.prototype.toString = () => 'POISON';
        const protectedHeader = new Error('protected').stack;
        Error.prototype.toString = originalToString;
        assert.match(protectedHeader, /^Error: protected(?:\n|$)/);
        function captureNamedFrame() {
            return new Error('named-frame').stack;
        }
        assert.match(captureNamedFrame().split('\n')[1], /^\s*at captureNamedFrame \(.+:\d+:\d+\)$/);
        const originalObjectToString = Object.prototype.toString;
        let protectedCallSiteStack;
        try {
            Object.prototype.toString = () => 'FAKE';
            protectedCallSiteStack = new Error('protected-call-site').stack;
        } finally {
            Object.prototype.toString = originalObjectToString;
        }
        assert.doesNotMatch(protectedCallSiteStack, /FAKE/);
        assert.match(protectedCallSiteStack.split('\n')[1], /^\s*at .+:\d+:\d+/);
        return true;
    } catch (e) {
        console.error('testDefaultErrorStackHeaders FAIL:', e.message);
        return false;
    } finally {
        Error.prepareStackTrace = originalPrepare;
    }
};

export const testErrorInspectUsesCurrentName = () => {
    try {
        const error = new RangeError('foo');
        error.name = 404;
        assert.match(inspect(error), /^404 \[RangeError\]: foo(?:\n|$)/);

        const manualStackError = new RangeError('foo');
        manualStackError.stack = 'RangeError: foo\n    at manual (x.js:1:2)';
        manualStackError.name = 404;
        assert.strictEqual(
            inspect(manualStackError),
            'RangeError: foo\n    at manual (x.js:1:2)',
        );
        return true;
    } catch (e) {
        console.error('testErrorInspectUsesCurrentName FAIL:', e.message);
        return false;
    }
};

export const testAssertionListenerStackFrame = () => {
    const emitter = new EventEmitter();
    emitter.on('failure', assert);
    try {
        emitter.emit('failure', false);
    } catch (error) {
        return String(error.stack).split('\n')[1] || '';
    }
    return '';
};

export const testCjsCallSiteLineOffset = () => {
    const originalPrepare = Error.prepareStackTrace;
    const filename = '/v8-call-site-line-offset.cjs';
    const mapFilename = '/v8-call-site-line-offset.cjs.map';
    try {
        module.setSourceMapsSupport(true);
        Error.prepareStackTrace = (_error, sites) => sites[0];
        fs.writeFileSync(filename, [
            'function branch() {',
            "    throw Error('site');",
            '}',
            'branch();',
            '//# sourceMappingURL=v8-call-site-line-offset.cjs.map',
        ].join('\n'));
        fs.writeFileSync(mapFilename, JSON.stringify({
            version: 3,
            sources: ['v8-call-site-line-offset.ts'],
            names: [],
            mappings: 'AAAA;AACA;AACA;AACA',
        }));
        try {
            require(filename);
        } catch (error) {
            return String(error.stack.getLineNumber());
        }
        return '';
    } finally {
        delete require.cache[filename];
        Error.prepareStackTrace = originalPrepare;
    }
};

export const testPrepareStackTraceDescriptors = () => {
    try {
        const customPrepare = () => 'custom-prepare';
        Error.prepareStackTrace = customPrepare;
        Object.defineProperty(Error, 'prepareStackTrace', { enumerable: false });
        assert.strictEqual(Error.prepareStackTrace, customPrepare);
        assert.strictEqual(new Error('partial-descriptor').stack, 'custom-prepare');

        let getterCalls = 0;
        Object.defineProperty(Error, 'prepareStackTrace', {
            get() {
                getterCalls++;
                return customPrepare;
            },
            configurable: true,
        });
        assert.strictEqual(getterCalls, 0);
        assert.strictEqual(new TypeError('accessor').stack, 'custom-prepare');

        delete Error.prepareStackTrace;
        assert.match(new Error('deleted').stack, /^Error: deleted(?:\n|$)/);

        Object.defineProperty(Error, 'prepareStackTrace', {
            value: customPrepare,
            writable: true,
            configurable: true,
        });
        Object.freeze(Error);
        const descriptor = Object.getOwnPropertyDescriptor(Error, 'prepareStackTrace');
        assert.strictEqual(descriptor.writable, false);
        assert.strictEqual(descriptor.configurable, false);
        assert.strictEqual(new Error('frozen').stack, 'custom-prepare');
        return true;
    } catch (e) {
        console.error('testPrepareStackTraceDescriptors FAIL:', e.message);
        return false;
    }
};

// Test 5: constructorOpt parameter strips frames
export const testConstructorOpt = () => {
    try {
        let callSites = null;
        const origPrepare = Error.prepareStackTrace;

        Error.prepareStackTrace = function (error, sites) {
            callSites = sites;
            return sites;
        };

        function MyError(message) {
            Error.captureStackTrace(this, MyError);
            this.message = message;
        }

        function callerFunction() {
            return new MyError('test');
        }

        const err = callerFunction();
        // Access .stack to trigger prepareStackTrace
        const stack = err.stack;

        Error.prepareStackTrace = origPrepare;

        assert(Array.isArray(callSites) && callSites.length > 0);

        // The first frame should NOT be MyError (it should be stripped)
        const firstFn = callSites[0].getFunctionName();
        assert.notStrictEqual(firstFn, 'MyError',
            'constructorOpt should strip frames including the constructor');

        return true;
    } catch (e) {
        console.error('testConstructorOpt FAIL:', e.message);
        return false;
    }
};

// Test 6: Error.stackTraceLimit is readable/writable
export const testStackTraceLimit = () => {
    try {
        const origLimit = Error.stackTraceLimit;

        assert.strictEqual(typeof Error.stackTraceLimit, 'number');

        Error.stackTraceLimit = 42;
        assert.strictEqual(Error.stackTraceLimit, 42);

        Error.stackTraceLimit = origLimit;
        return true;
    } catch (e) {
        console.error('testStackTraceLimit FAIL:', e.message);
        return false;
    }
};

// Test 7: The exact depd pattern — the real-world use case this fix enables
export const testDepdPattern = () => {
    try {
        // This is exactly what depd does:
        function prepareObjectStackTrace(obj, stack) {
            return stack;
        }

        function getStack() {
            var limit = Error.stackTraceLimit;
            var obj = {};
            var prep = Error.prepareStackTrace;

            Error.prepareStackTrace = prepareObjectStackTrace;
            Error.stackTraceLimit = Math.max(10, limit);

            Error.captureStackTrace(obj);

            var stack = obj.stack.slice(1);

            Error.prepareStackTrace = prep;
            Error.stackTraceLimit = limit;

            return stack;
        }

        function callSiteLocation(callSite) {
            var file = callSite.getFileName() || '<anonymous>';
            var line = callSite.getLineNumber();
            var colm = callSite.getColumnNumber();

            var site = [file, line, colm];
            site.callSite = callSite;
            site.name = callSite.getFunctionName();

            return site;
        }

        function depd(namespace) {
            var stack = getStack();
            var site = callSiteLocation(stack[1]);
            var file = site[0];
            return { file, namespace };
        }

        // This call should NOT throw — it's the exact pattern that was failing
        const result = depd('test-namespace');
        assert.strictEqual(result.namespace, 'test-namespace');
        assert.strictEqual(typeof result.file, 'string');

        return true;
    } catch (e) {
        console.error('testDepdPattern FAIL:', e.message);
        return false;
    }
};