GeezerVison is showing github.com — util: use private symbols in JS land directly · nodejs/node@3f18a83
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/node.js +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -77,8 +77,9 @@ const { | 77 | 77 | exposeInterface, | 78 | 78 | } = require('internal/util'); | 79 | 79 | const { | 80 | | - exiting_aliased_Uint32Array, | 81 | | - getHiddenValue, | | 80 | + privateSymbols: { | | 81 | + exiting_aliased_Uint32Array, | | 82 | + }, | 82 | 83 | } = internalBinding('util'); | 83 | 84 |
| 84 | 85 | setupProcessObject(); | @@ -88,8 +89,7 @@ setupBuffer(); | 88 | 89 |
| 89 | 90 | process.domain = null; | 90 | 91 | { | 91 | | - const exitingAliasedUint32Array = | 92 | | - getHiddenValue(process, exiting_aliased_Uint32Array); | | 92 | + const exitingAliasedUint32Array = process[exiting_aliased_Uint32Array]; | 93 | 93 | ObjectDefineProperty(process, '_exiting', { | 94 | 94 | __proto__: null, | 95 | 95 | get() { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/buffer.js +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,9 +32,11 @@ const { | 32 | 32 | utf8Write, | 33 | 33 | getZeroFillToggle | 34 | 34 | } = internalBinding('buffer'); | | 35 | + | 35 | 36 | const { | 36 | | - untransferable_object_private_symbol, | 37 | | - setHiddenValue, | | 37 | + privateSymbols: { | | 38 | + untransferable_object_private_symbol, | | 39 | + }, | 38 | 40 | } = internalBinding('util'); | 39 | 41 |
| 40 | 42 | // Temporary buffers to convert numbers. | @@ -1048,7 +1050,7 @@ function addBufferPrototypeMethods(proto) { | 1048 | 1050 | function markAsUntransferable(obj) { | 1049 | 1051 | if ((typeof obj !== 'object' && typeof obj !== 'function') || obj === null) | 1050 | 1052 | return; // This object is a primitive and therefore already untransferable. | 1051 | | - setHiddenValue(obj, untransferable_object_private_symbol, true); | | 1053 | + obj[untransferable_object_private_symbol] = true; | 1052 | 1054 | } | 1053 | 1055 |
| 1054 | 1056 | // A toggle used to access the zero fill setting of the array buffer allocator | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/errors.js +6-8Lines changed: 6 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -816,16 +816,14 @@ const fatalExceptionStackEnhancers = { | 816 | 816 | } | 817 | 817 | }; | 818 | 818 |
| | 819 | +const { | | 820 | + privateSymbols: { | | 821 | + arrow_message_private_symbol, | | 822 | + } | | 823 | +} = internalBinding('util'); | 819 | 824 | // Ensures the printed error line is from user code. | 820 | | -let _kArrowMessagePrivateSymbol, _setHiddenValue; | 821 | 825 | function setArrowMessage(err, arrowMessage) { | 822 | | - if (!_kArrowMessagePrivateSymbol) { | 823 | | - ({ | 824 | | - arrow_message_private_symbol: _kArrowMessagePrivateSymbol, | 825 | | - setHiddenValue: _setHiddenValue, | 826 | | - } = internalBinding('util')); | 827 | | - } | 828 | | - _setHiddenValue(err, _kArrowMessagePrivateSymbol, arrowMessage); | | 826 | + err[arrow_message_private_symbol] = arrowMessage; | 829 | 827 | } | 830 | 828 |
| 831 | 829 | // Hide stack lines before the first user code line. | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util.js +7-8Lines changed: 7 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -42,10 +42,10 @@ const { | 42 | 42 | } = require('internal/errors'); | 43 | 43 | const { signals } = internalBinding('constants').os; | 44 | 44 | const { | 45 | | - getHiddenValue, | 46 | | - setHiddenValue, | 47 | | - arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex, | 48 | | - decorated_private_symbol: kDecoratedPrivateSymbolIndex, | | 45 | + privateSymbols: { | | 46 | + arrow_message_private_symbol, | | 47 | + decorated_private_symbol, | | 48 | + }, | 49 | 49 | sleep: _sleep, | 50 | 50 | toUSVString: _toUSVString, | 51 | 51 | } = internalBinding('util'); | @@ -140,15 +140,14 @@ function deprecate(fn, msg, code) { | 140 | 140 | } | 141 | 141 |
| 142 | 142 | function decorateErrorStack(err) { | 143 | | - if (!(isError(err) && err.stack) || | 144 | | - getHiddenValue(err, kDecoratedPrivateSymbolIndex) === true) | | 143 | + if (!(isError(err) && err.stack) || err[decorated_private_symbol]) | 145 | 144 | return; | 146 | 145 |
| 147 | | - const arrow = getHiddenValue(err, kArrowMessagePrivateSymbolIndex); | | 146 | + const arrow = err[arrow_message_private_symbol]; | 148 | 147 |
| 149 | 148 | if (arrow) { | 150 | 149 | err.stack = arrow + err.stack; | 151 | | - setHiddenValue(err, kDecoratedPrivateSymbolIndex, true); | | 150 | + err[decorated_private_symbol] = true; | 152 | 151 | } | 153 | 152 | } | 154 | 153 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_util.cc +12-49Lines changed: 12 additions & 49 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,7 +26,6 @@ using v8::Object; | 26 | 26 | using v8::ONLY_CONFIGURABLE; | 27 | 27 | using v8::ONLY_ENUMERABLE; | 28 | 28 | using v8::ONLY_WRITABLE; | 29 | | -using v8::Private; | 30 | 29 | using v8::Promise; | 31 | 30 | using v8::PropertyFilter; | 32 | 31 | using v8::Proxy; | @@ -159,44 +158,6 @@ static void PreviewEntries(const FunctionCallbackInfo<Value>& args) { | 159 | 158 | Array::New(env->isolate(), ret, arraysize(ret))); | 160 | 159 | } | 161 | 160 |
| 162 | | -inline Local<Private> IndexToPrivateSymbol(Environment* env, uint32_t index) { | 163 | | -#define V(name, _) &Environment::name, | 164 | | - static Local<Private> (Environment::*const methods[])() const = { | 165 | | - PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V) | 166 | | - }; | 167 | | -#undef V | 168 | | - CHECK_LT(index, arraysize(methods)); | 169 | | - return (env->*methods[index])(); | 170 | | -} | 171 | | - | 172 | | -static void GetHiddenValue(const FunctionCallbackInfo<Value>& args) { | 173 | | - Environment* env = Environment::GetCurrent(args); | 174 | | - | 175 | | - CHECK(args[0]->IsObject()); | 176 | | - CHECK(args[1]->IsUint32()); | 177 | | - | 178 | | - Local<Object> obj = args[0].As<Object>(); | 179 | | - uint32_t index = args[1].As<Uint32>()->Value(); | 180 | | - Local<Private> private_symbol = IndexToPrivateSymbol(env, index); | 181 | | - Local<Value> ret; | 182 | | - if (obj->GetPrivate(env->context(), private_symbol).ToLocal(&ret)) | 183 | | - args.GetReturnValue().Set(ret); | 184 | | -} | 185 | | - | 186 | | -static void SetHiddenValue(const FunctionCallbackInfo<Value>& args) { | 187 | | - Environment* env = Environment::GetCurrent(args); | 188 | | - | 189 | | - CHECK(args[0]->IsObject()); | 190 | | - CHECK(args[1]->IsUint32()); | 191 | | - | 192 | | - Local<Object> obj = args[0].As<Object>(); | 193 | | - uint32_t index = args[1].As<Uint32>()->Value(); | 194 | | - Local<Private> private_symbol = IndexToPrivateSymbol(env, index); | 195 | | - bool ret; | 196 | | - if (obj->SetPrivate(env->context(), private_symbol, args[2]).To(&ret)) | 197 | | - args.GetReturnValue().Set(ret); | 198 | | -} | 199 | | - | 200 | 161 | static void Sleep(const FunctionCallbackInfo<Value>& args) { | 201 | 162 | CHECK(args[0]->IsUint32()); | 202 | 163 | uint32_t msec = args[0].As<Uint32>()->Value(); | @@ -386,8 +347,6 @@ static void ToUSVString(const FunctionCallbackInfo<Value>& args) { | 386 | 347 | } | 387 | 348 |
| 388 | 349 | void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | 389 | | - registry->Register(GetHiddenValue); | 390 | | - registry->Register(SetHiddenValue); | 391 | 350 | registry->Register(GetPromiseDetails); | 392 | 351 | registry->Register(GetProxyDetails); | 393 | 352 | registry->Register(PreviewEntries); | @@ -412,16 +371,22 @@ void Initialize(Local<Object> target, | 412 | 371 | Environment* env = Environment::GetCurrent(context); | 413 | 372 | Isolate* isolate = env->isolate(); | 414 | 373 |
| 415 | | -#define V(name, _) \ | 416 | | - target->Set(context, \ | 417 | | - FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ | 418 | | - Integer::NewFromUnsigned(env->isolate(), index++)).Check(); | 419 | 374 | { | 420 | | - uint32_t index = 0; | | 375 | + Local<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(isolate); | | 376 | +#define V(PropertyName, _) \ | | 377 | + tmpl->Set(FIXED_ONE_BYTE_STRING(env->isolate(), #PropertyName), \ | | 378 | + env->PropertyName()); | | 379 | + | 421 | 380 | PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V) | 422 | | - } | 423 | 381 | #undef V | 424 | 382 |
| | 383 | + target | | 384 | + ->Set(context, | | 385 | + FIXED_ONE_BYTE_STRING(isolate, "privateSymbols"), | | 386 | + tmpl->NewInstance(context).ToLocalChecked()) | | 387 | + .Check(); | | 388 | + } | | 389 | + | 425 | 390 | #define V(name) \ | 426 | 391 | target->Set(context, \ | 427 | 392 | FIXED_ONE_BYTE_STRING(env->isolate(), #name), \ | @@ -432,8 +397,6 @@ void Initialize(Local<Object> target, | 432 | 397 | V(kRejected); | 433 | 398 | #undef V | 434 | 399 |
| 435 | | - SetMethodNoSideEffect(context, target, "getHiddenValue", GetHiddenValue); | 436 | | - SetMethod(context, target, "setHiddenValue", SetHiddenValue); | 437 | 400 | SetMethodNoSideEffect( | 438 | 401 | context, target, "getPromiseDetails", GetPromiseDetails); | 439 | 402 | SetMethodNoSideEffect(context, target, "getProxyDetails", GetProxyDetails); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-internal-util-decorate-error-stack.js +8-7Lines changed: 8 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,12 +5,14 @@ const fixtures = require('../common/fixtures'); | 5 | 5 | const assert = require('assert'); | 6 | 6 | const internalUtil = require('internal/util'); | 7 | 7 | const { internalBinding } = require('internal/test/binding'); | 8 | | -const binding = internalBinding('util'); | | 8 | +const { | | 9 | + privateSymbols: { | | 10 | + arrow_message_private_symbol, | | 11 | + decorated_private_symbol, | | 12 | + } | | 13 | +} = internalBinding('util'); | 9 | 14 | const spawnSync = require('child_process').spawnSync; | 10 | 15 |
| 11 | | -const kArrowMessagePrivateSymbolIndex = binding.arrow_message_private_symbol; | 12 | | -const kDecoratedPrivateSymbolIndex = binding.decorated_private_symbol; | 13 | | - | 14 | 16 | const decorateErrorStack = internalUtil.decorateErrorStack; | 15 | 17 |
| 16 | 18 | // Verify that decorateErrorStack does not throw with non-objects. | @@ -73,9 +75,8 @@ const arrowMessage = 'arrow_message'; | 73 | 75 | err = new Error('foo'); | 74 | 76 | originalStack = err.stack; | 75 | 77 |
| 76 | | -binding.setHiddenValue(err, kArrowMessagePrivateSymbolIndex, arrowMessage); | | 78 | +err[arrow_message_private_symbol] = arrowMessage; | 77 | 79 | decorateErrorStack(err); | 78 | 80 |
| 79 | 81 | assert.strictEqual(err.stack, `${arrowMessage}${originalStack}`); | 80 | | -assert.strictEqual( | 81 | | - binding.getHiddenValue(err, kDecoratedPrivateSymbolIndex), true); | | 82 | +assert.strictEqual(err[decorated_private_symbol], true); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-util-internal.js +9-15Lines changed: 9 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,30 +7,24 @@ const fixtures = require('../common/fixtures'); | 7 | 7 | const { internalBinding } = require('internal/test/binding'); | 8 | 8 |
| 9 | 9 | const { | 10 | | - getHiddenValue, | 11 | | - setHiddenValue, | 12 | | - arrow_message_private_symbol: kArrowMessagePrivateSymbolIndex | | 10 | + privateSymbols: { | | 11 | + arrow_message_private_symbol, | | 12 | + }, | 13 | 13 | } = internalBinding('util'); | 14 | 14 |
| 15 | | -assert.strictEqual( | 16 | | - getHiddenValue({}, kArrowMessagePrivateSymbolIndex), | 17 | | - undefined); | 18 | | - | 19 | 15 | const obj = {}; | 20 | | -assert.strictEqual( | 21 | | - setHiddenValue(obj, kArrowMessagePrivateSymbolIndex, 'bar'), | 22 | | - true); | 23 | | -assert.strictEqual( | 24 | | - getHiddenValue(obj, kArrowMessagePrivateSymbolIndex), | 25 | | - 'bar'); | | 16 | +assert.strictEqual(obj[arrow_message_private_symbol], undefined); | | 17 | + | | 18 | +obj[arrow_message_private_symbol] = 'bar'; | | 19 | +assert.strictEqual(obj[arrow_message_private_symbol], 'bar'); | | 20 | +assert.deepStrictEqual(Reflect.ownKeys(obj), []); | 26 | 21 |
| 27 | 22 | let arrowMessage; | 28 | 23 |
| 29 | 24 | try { | 30 | 25 | require(fixtures.path('syntax', 'bad_syntax')); | 31 | 26 | } catch (err) { | 32 | | - arrowMessage = | 33 | | - getHiddenValue(err, kArrowMessagePrivateSymbolIndex); | | 27 | + arrowMessage = err[arrow_message_private_symbol]; | 34 | 28 | } | 35 | 29 |
| 36 | 30 | assert.match(arrowMessage, /bad_syntax\.js:1/); |
|