From 05992367a0d5be3f08d077753b9bb724c18d3008 Mon Sep 17 00:00:00 2001 From: Nicolas DUBIEN Date: Wed, 1 Feb 2023 08:28:04 +0000 Subject: [PATCH] vm: properly support symbols on globals A regression has been introduced in node 18.2.0, it makes the following snippet fails while it used to work in the past: ``` const assert = require('assert'); const vm = require('vm'); const global = vm.runInContext('this', vm.createContext()); const totoSymbol = Symbol.for('toto'); Object.defineProperty(global, totoSymbol, { enumerable: true, writable: true, value: 4, configurable: true, }); assert(Object.getOwnPropertySymbols(global).includes(totoSymbol)); ``` Regression introduced by: https://github.com/nodejs/node/pull/42963. So I basically attempted to start understanding what it changed to make it fix the initial issue while not breaking the symbol related one. Fixes: https://github.com/nodejs/node/issues/45983 --- src/node_contextify.cc | 4 +++- test/parallel/test-vm-global-symbol.js | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-vm-global-symbol.js diff --git a/src/node_contextify.cc b/src/node_contextify.cc index ed552ddd559f51..fdab91714d7da0 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -530,7 +530,9 @@ void ContextifyContext::PropertySetterCallback( return; USE(ctx->sandbox()->Set(context, property, value)); - args.GetReturnValue().Set(value); + if (is_function) { + args.GetReturnValue().Set(value); + } } // static diff --git a/test/parallel/test-vm-global-symbol.js b/test/parallel/test-vm-global-symbol.js new file mode 100644 index 00000000000000..72ca935cd82438 --- /dev/null +++ b/test/parallel/test-vm-global-symbol.js @@ -0,0 +1,15 @@ +'use strict'; +require('../common'); +const assert = require('assert'); +const vm = require('vm'); + +const global = vm.runInContext('this', vm.createContext()); +const totoSymbol = Symbol.for('toto'); +Object.defineProperty(global, totoSymbol, { + enumerable: true, + writable: true, + value: 4, + configurable: true, +}); +assert.ok(global[totoSymbol] === 4); +assert.ok(Object.getOwnPropertySymbols(global).includes(totoSymbol));