Skip to content

Commit

Permalink
Revert "lib,src: drop dependency on v8::Private::ForApi()"
Browse files Browse the repository at this point in the history
This reverts commit 334ef4f.

nodejs#7082 was landed too fast and did
not have sufficient review time.

That PR also broke some things (testcases will follow separately).
  • Loading branch information
ChALkeR committed Jun 1, 2016
1 parent 5dc1f6f commit ecdae5b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 62 deletions.
9 changes: 3 additions & 6 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
const binding = process.binding('util');
const prefix = `(${process.release.name}:${process.pid}) `;

const kArrowMessagePrivateSymbolIndex = binding['arrow_message_private_symbol'];
const kDecoratedPrivateSymbolIndex = binding['decorated_private_symbol'];

exports.getHiddenValue = binding.getHiddenValue;
exports.setHiddenValue = binding.setHiddenValue;

Expand Down Expand Up @@ -68,14 +65,14 @@ exports._deprecate = function(fn, msg) {

exports.decorateErrorStack = function decorateErrorStack(err) {
if (!(exports.isError(err) && err.stack) ||
exports.getHiddenValue(err, kDecoratedPrivateSymbolIndex) === true)
exports.getHiddenValue(err, 'node:decorated') === true)
return;

const arrow = exports.getHiddenValue(err, kArrowMessagePrivateSymbolIndex);
const arrow = exports.getHiddenValue(err, 'node:arrowMessage');

if (arrow) {
err.stack = arrow + err.stack;
exports.setHiddenValue(err, kDecoratedPrivateSymbolIndex, true);
exports.setHiddenValue(err, 'node:decorated', true);
}
};

Expand Down
8 changes: 7 additions & 1 deletion src/debug-agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,16 @@ void Agent::WorkerRun() {
Isolate::Scope isolate_scope(isolate);

HandleScope handle_scope(isolate);
IsolateData isolate_data(isolate, &child_loop_);
Local<Context> context = Context::New(isolate);

Context::Scope context_scope(context);

// FIXME(bnoordhuis) Work around V8 bug: v8::Private::ForApi() dereferences
// a nullptr when a context hasn't been entered first. The symbol registry
// is a lazily created JS object but object instantiation does not work
// without a context.
IsolateData isolate_data(isolate, &child_loop_);

Environment* env = CreateEnvironment(
&isolate_data,
context,
Expand Down
6 changes: 5 additions & 1 deletion src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ inline IsolateData::IsolateData(v8::Isolate* isolate, uv_loop_t* event_loop)
#define V(PropertyName, StringValue) \
PropertyName ## _( \
isolate, \
v8::Private::New( \
v8::Private::ForApi( \
isolate, \
v8::String::NewFromOneByte( \
isolate, \
Expand Down Expand Up @@ -545,6 +545,10 @@ inline v8::Local<v8::Object> Environment::NewInternalFieldObject() {
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
#undef V

#undef ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES
#undef PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES
#undef PER_ISOLATE_STRING_PROPERTIES

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
Expand Down
8 changes: 7 additions & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4385,9 +4385,15 @@ static void StartNodeInstance(void* arg) {
Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
IsolateData isolate_data(isolate, instance_data->event_loop());
Local<Context> context = Context::New(isolate);
Context::Scope context_scope(context);

// FIXME(bnoordhuis) Work around V8 bug: v8::Private::ForApi() dereferences
// a nullptr when a context hasn't been entered first. The symbol registry
// is a lazily created JS object but object instantiation does not work
// without a context.
IsolateData isolate_data(isolate, instance_data->event_loop());

Environment* env = CreateEnvironment(&isolate_data,
context,
instance_data->argc(),
Expand Down
38 changes: 9 additions & 29 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ namespace util {
using v8::Array;
using v8::Context;
using v8::FunctionCallbackInfo;
using v8::Integer;
using v8::Local;
using v8::Object;
using v8::Private;
using v8::Proxy;
using v8::String;
using v8::Value;


Expand Down Expand Up @@ -53,28 +53,18 @@ static void GetProxyDetails(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(ret);
}

inline Local<Private> IndexToPrivateSymbol(Environment* env, uint32_t index) {
#define V(name, _) &Environment::name,
static Local<Private> (Environment::*const methods[])() const = {
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
};
#undef V
CHECK_LT(index, arraysize(methods));
return (env->*methods[index])();
}

static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (!args[0]->IsObject())
return env->ThrowTypeError("obj must be an object");

if (!args[1]->IsUint32())
return env->ThrowTypeError("index must be an uint32");
if (!args[1]->IsString())
return env->ThrowTypeError("name must be a string");

Local<Object> obj = args[0].As<Object>();
auto index = args[1]->Uint32Value(env->context()).FromJust();
auto private_symbol = IndexToPrivateSymbol(env, index);
Local<String> name = args[1].As<String>();
auto private_symbol = Private::ForApi(env->isolate(), name);
auto maybe_value = obj->GetPrivate(env->context(), private_symbol);

args.GetReturnValue().Set(maybe_value.ToLocalChecked());
Expand All @@ -86,12 +76,12 @@ static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) {
if (!args[0]->IsObject())
return env->ThrowTypeError("obj must be an object");

if (!args[1]->IsUint32())
return env->ThrowTypeError("index must be an uint32");
if (!args[1]->IsString())
return env->ThrowTypeError("name must be a string");

Local<Object> obj = args[0].As<Object>();
auto index = args[1]->Uint32Value(env->context()).FromJust();
auto private_symbol = IndexToPrivateSymbol(env, index);
Local<String> name = args[1].As<String>();
auto private_symbol = Private::ForApi(env->isolate(), name);
auto maybe_value = obj->SetPrivate(env->context(), private_symbol, args[2]);

args.GetReturnValue().Set(maybe_value.FromJust());
Expand All @@ -107,16 +97,6 @@ void Initialize(Local<Object> target,
VALUE_METHOD_MAP(V)
#undef V

#define V(name, _) \
target->Set(context, \
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
Integer::NewFromUnsigned(env->isolate(), index++));
{
uint32_t index = 0;
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V)
}
#undef V

env->SetMethod(target, "getHiddenValue", GetHiddenValue);
env->SetMethod(target, "setHiddenValue", SetHiddenValue);
env->SetMethod(target, "getProxyDetails", GetProxyDetails);
Expand Down
38 changes: 14 additions & 24 deletions test/parallel/test-util-internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ const assert = require('assert');
const internalUtil = require('internal/util');
const spawnSync = require('child_process').spawnSync;

const binding = process.binding('util');
const kArrowMessagePrivateSymbolIndex = binding['arrow_message_private_symbol'];

function getHiddenValue(obj, index) {
function getHiddenValue(obj, name) {
return function() {
internalUtil.getHiddenValue(obj, index);
internalUtil.getHiddenValue(obj, name);
};
}

function setHiddenValue(obj, index, val) {
function setHiddenValue(obj, name, val) {
return function() {
internalUtil.setHiddenValue(obj, index, val);
internalUtil.setHiddenValue(obj, name, val);
};
}

Expand All @@ -26,36 +23,29 @@ assert.throws(getHiddenValue(null, 'foo'), /obj must be an object/);
assert.throws(getHiddenValue(undefined, 'foo'), /obj must be an object/);
assert.throws(getHiddenValue('bar', 'foo'), /obj must be an object/);
assert.throws(getHiddenValue(85, 'foo'), /obj must be an object/);
assert.throws(getHiddenValue({}), /index must be an uint32/);
assert.throws(getHiddenValue({}, null), /index must be an uint32/);
assert.throws(getHiddenValue({}, []), /index must be an uint32/);
assert.deepStrictEqual(
internalUtil.getHiddenValue({}, kArrowMessagePrivateSymbolIndex),
undefined);
assert.throws(getHiddenValue({}), /name must be a string/);
assert.throws(getHiddenValue({}, null), /name must be a string/);
assert.throws(getHiddenValue({}, []), /name must be a string/);
assert.deepStrictEqual(internalUtil.getHiddenValue({}, 'foo'), undefined);

assert.throws(setHiddenValue(), /obj must be an object/);
assert.throws(setHiddenValue(null, 'foo'), /obj must be an object/);
assert.throws(setHiddenValue(undefined, 'foo'), /obj must be an object/);
assert.throws(setHiddenValue('bar', 'foo'), /obj must be an object/);
assert.throws(setHiddenValue(85, 'foo'), /obj must be an object/);
assert.throws(setHiddenValue({}), /index must be an uint32/);
assert.throws(setHiddenValue({}, null), /index must be an uint32/);
assert.throws(setHiddenValue({}, []), /index must be an uint32/);
assert.throws(setHiddenValue({}), /name must be a string/);
assert.throws(setHiddenValue({}, null), /name must be a string/);
assert.throws(setHiddenValue({}, []), /name must be a string/);
const obj = {};
assert.strictEqual(
internalUtil.setHiddenValue(obj, kArrowMessagePrivateSymbolIndex, 'bar'),
true);
assert.strictEqual(
internalUtil.getHiddenValue(obj, kArrowMessagePrivateSymbolIndex),
'bar');
assert.strictEqual(internalUtil.setHiddenValue(obj, 'foo', 'bar'), true);
assert.strictEqual(internalUtil.getHiddenValue(obj, 'foo'), 'bar');

let arrowMessage;

try {
require('../fixtures/syntax/bad_syntax');
} catch (err) {
arrowMessage =
internalUtil.getHiddenValue(err, kArrowMessagePrivateSymbolIndex);
arrowMessage = internalUtil.getHiddenValue(err, 'node:arrowMessage');
}

assert(/bad_syntax\.js:1/.test(arrowMessage));
Expand Down

0 comments on commit ecdae5b

Please sign in to comment.