Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change for a deprecated method: SetNamedPropertyHandler() to SetHandler() #9062

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ using v8::Locker;
using v8::MaybeLocal;
using v8::Message;
using v8::Name;
using v8::NamedPropertyHandlerConfiguration;
using v8::Null;
using v8::Number;
using v8::Object;
using v8::ObjectTemplate;
using v8::Promise;
using v8::PromiseRejectMessage;
using v8::PropertyCallbackInfo;
using v8::PropertyHandlerFlags;
using v8::ScriptOrigin;
using v8::SealHandleScope;
using v8::String;
Expand Down Expand Up @@ -2662,7 +2664,7 @@ static void ProcessTitleSetter(Local<Name> property,
}


static void EnvGetter(Local<String> property,
static void EnvGetter(Local<Name> property,
const PropertyCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
#ifdef __POSIX__
Expand Down Expand Up @@ -2690,7 +2692,7 @@ static void EnvGetter(Local<String> property,
}


static void EnvSetter(Local<String> property,
static void EnvSetter(Local<Name> property,
Local<Value> value,
const PropertyCallbackInfo<Value>& info) {
#ifdef __POSIX__
Expand All @@ -2711,7 +2713,7 @@ static void EnvSetter(Local<String> property,
}


static void EnvQuery(Local<String> property,
static void EnvQuery(Local<Name> property,
const PropertyCallbackInfo<Integer>& info) {
int32_t rc = -1; // Not found unless proven otherwise.
#ifdef __POSIX__
Expand All @@ -2737,7 +2739,7 @@ static void EnvQuery(Local<String> property,
}


static void EnvDeleter(Local<String> property,
static void EnvDeleter(Local<Name> property,
const PropertyCallbackInfo<Boolean>& info) {
#ifdef __POSIX__
node::Utf8Value key(info.GetIsolate(), property);
Expand Down Expand Up @@ -3138,12 +3140,16 @@ void SetupProcessObject(Environment* env,
// create process.env
Local<ObjectTemplate> 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<Object> process_env =
process_env_template->NewInstance(env->context()).ToLocalChecked();
process->Set(env->env_string(), process_env);
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-v8-interceptStrings-not-Symbols.js
Original file line number Diff line number Diff line change
@@ -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]);