node/test/parallel/test-vm-util-lazy-properties.js
Chengzhong Wu 4e1f0ccb4d
src: fix crash when lazy getter is invoked in a vm context
V8 should invoke native functions in their creation context,
preventing dynamic context by the caller. However, the lazy getter has
no JavaScript function representation and has no creation context. It
is not invoked in the original creation context. Fix the null realm by
retrieving the creation context via `this` argument.

PR-URL: https://github.com/nodejs/node/pull/57168
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
2025-02-23 14:40:33 +00:00

27 lines
736 B
JavaScript

'use strict';
require('../common');
const vm = require('node:vm');
const util = require('node:util');
const assert = require('node:assert');
// This verifies that invoking property getters defined with
// `require('internal/util').defineLazyProperties` does not crash
// the process.
const ctx = vm.createContext();
const getter = vm.runInContext(`
function getter(object, property) {
return object[property];
}
getter;
`, ctx);
// `util.parseArgs` is a lazy property.
const parseArgs = getter(util, 'parseArgs');
assert.strictEqual(parseArgs, util.parseArgs);
// `globalThis.TextEncoder` is a lazy property.
const TextEncoder = getter(globalThis, 'TextEncoder');
assert.strictEqual(TextEncoder, globalThis.TextEncoder);