From 0c236d1d362b579fbdc4b4fa467f5f16b35e72a8 Mon Sep 17 00:00:00 2001 From: AnnaMag Date: Thu, 6 Oct 2016 22:50:41 +0200 Subject: [PATCH] src: replace SetNamedPropertyHandler() The changes introdcued here replace the deprecated v8 method SetNamedPropertyHandler() to SetHandler() in node.cc. Prior to refactoring, the method defined callbacks when accessing object properties defined by Strings and not Symbols. test/parallel/test-v8-interceptStrings-not-Symbols.js demonstrates that this behaviour remained unchanged after refactoring. PR-URL: https://github.com/nodejs/node/pull/9062 Reviewed-By: Ben Noordhuis Reviewed-By: James M Snell Reviewed-By: Colin Ihrig Reviewed-By: Franziska Hinkelmann --- src/node.cc | 25 ++++++++------ .../test-v8-interceptStrings-not-Symbols.js | 34 +++++++++++++++++++ 2 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 test/parallel/test-v8-interceptStrings-not-Symbols.js diff --git a/src/node.cc b/src/node.cc index 58c739facf..fb85af7227 100644 --- a/src/node.cc +++ b/src/node.cc @@ -114,6 +114,7 @@ using v8::Locker; using v8::MaybeLocal; using v8::Message; using v8::Name; +using v8::NamedPropertyHandlerConfiguration; using v8::Null; using v8::Number; using v8::Object; @@ -121,6 +122,7 @@ using v8::ObjectTemplate; using v8::Promise; using v8::PromiseRejectMessage; using v8::PropertyCallbackInfo; +using v8::PropertyHandlerFlags; using v8::ScriptOrigin; using v8::SealHandleScope; using v8::String; @@ -2673,7 +2675,7 @@ static void ProcessTitleSetter(Local property, } -static void EnvGetter(Local property, +static void EnvGetter(Local property, const PropertyCallbackInfo& info) { Isolate* isolate = info.GetIsolate(); #ifdef __POSIX__ @@ -2701,7 +2703,7 @@ static void EnvGetter(Local property, } -static void EnvSetter(Local property, +static void EnvSetter(Local property, Local value, const PropertyCallbackInfo& info) { #ifdef __POSIX__ @@ -2722,7 +2724,7 @@ static void EnvSetter(Local property, } -static void EnvQuery(Local property, +static void EnvQuery(Local property, const PropertyCallbackInfo& info) { int32_t rc = -1; // Not found unless proven otherwise. #ifdef __POSIX__ @@ -2748,7 +2750,7 @@ static void EnvQuery(Local property, } -static void EnvDeleter(Local property, +static void EnvDeleter(Local property, const PropertyCallbackInfo& info) { #ifdef __POSIX__ node::Utf8Value key(info.GetIsolate(), property); @@ -3147,12 +3149,15 @@ void SetupProcessObject(Environment* env, // create process.env Local process_env_template = ObjectTemplate::New(env->isolate()); - process_env_template->SetNamedPropertyHandler(EnvGetter, - EnvSetter, - EnvQuery, - EnvDeleter, - EnvEnumerator, - env->as_external()); + process_env_template->SetHandler(NamedPropertyHandlerConfiguration( + EnvGetter, + EnvSetter, + EnvQuery, + EnvDeleter, + EnvEnumerator, + env->as_external(), + PropertyHandlerFlags::kOnlyInterceptStrings)); + Local process_env = process_env_template->NewInstance(env->context()).ToLocalChecked(); process->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "env"), process_env); diff --git a/test/parallel/test-v8-interceptStrings-not-Symbols.js b/test/parallel/test-v8-interceptStrings-not-Symbols.js new file mode 100644 index 0000000000..e999aae0b7 --- /dev/null +++ b/test/parallel/test-v8-interceptStrings-not-Symbols.js @@ -0,0 +1,34 @@ +'use strict'; +require('../common'); + +const assert = require('assert'); + +// Test that the v8 named property handler intercepts callbacks +// when properties are defined as Strings and NOT for Symbols. +// +// With the kOnlyInterceptStrings flag, manipulating properties via +// Strings is intercepted by the callbacks, while Symbols adopt +// the default global behaviour. +// Removing the kOnlyInterceptStrings flag, adds intercepting to Symbols, +// which causes Type Error at process.env[symbol]=42 due to process.env being +// strongly typed for Strings +// (node::Utf8Value key(info.GetIsolate(), property);). + + +const symbol = Symbol('sym'); + +// check if its undefined +assert.strictEqual(process.env[symbol], undefined); + +// set a value using a Symbol +process.env[symbol] = 42; + +// set a value using a String (call to EnvSetter, node.cc) +process.env['s'] = 42; + +//check the values after substitutions +assert.strictEqual(42, process.env[symbol]); +assert.strictEqual('42', process.env['s']); + +delete process.env[symbol]; +assert.strictEqual(undefined, process.env[symbol]);