GeezerVison is showing github.com — src: upgrade to new v8::Private api · outsideris/node@924cc6c
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: src/env-inl.h +36-12Lines changed: 36 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -50,12 +50,25 @@ inline Environment::IsolateData::IsolateData(v8::Isolate* isolate, | 50 | 50 | : event_loop_(loop), | 51 | 51 | isolate_(isolate), | 52 | 52 | #define V(PropertyName, StringValue) \ | 53 | | - PropertyName ## _(isolate, \ | 54 | | - v8::String::NewFromOneByte( \ | 55 | | - isolate, \ | 56 | | - reinterpret_cast<const uint8_t*>(StringValue), \ | 57 | | - v8::NewStringType::kInternalized, \ | 58 | | - sizeof(StringValue) - 1).ToLocalChecked()), | | 53 | + PropertyName ## _( \ | | 54 | + isolate, \ | | 55 | + v8::Private::ForApi( \ | | 56 | + isolate, \ | | 57 | + v8::String::NewFromOneByte( \ | | 58 | + isolate, \ | | 59 | + reinterpret_cast<const uint8_t*>(StringValue), \ | | 60 | + v8::NewStringType::kInternalized, \ | | 61 | + sizeof(StringValue) - 1).ToLocalChecked())), | | 62 | + PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V) | | 63 | +#undef V | | 64 | +#define V(PropertyName, StringValue) \ | | 65 | + PropertyName ## _( \ | | 66 | + isolate, \ | | 67 | + v8::String::NewFromOneByte( \ | | 68 | + isolate, \ | | 69 | + reinterpret_cast<const uint8_t*>(StringValue), \ | | 70 | + v8::NewStringType::kInternalized, \ | | 71 | + sizeof(StringValue) - 1).ToLocalChecked()), | 59 | 72 | PER_ISOLATE_STRING_PROPERTIES(V) | 60 | 73 | #undef V | 61 | 74 | ref_count_(0) {} | @@ -525,21 +538,31 @@ inline v8::Local<v8::Object> Environment::NewInternalFieldObject() { | 525 | 538 | return m_obj.ToLocalChecked(); | 526 | 539 | } | 527 | 540 |
| 528 | | -#define V(PropertyName, StringValue) \ | | 541 | +#define VP(PropertyName, StringValue) V(v8::Private, PropertyName, StringValue) | | 542 | +#define VS(PropertyName, StringValue) V(v8::String, PropertyName, StringValue) | | 543 | +#define V(TypeName, PropertyName, StringValue) \ | 529 | 544 | inline \ | 530 | | - v8::Local<v8::String> Environment::IsolateData::PropertyName() const { \ | | 545 | + v8::Local<TypeName> Environment::IsolateData::PropertyName() const { \ | 531 | 546 | /* Strings are immutable so casting away const-ness here is okay. */ \ | 532 | 547 | return const_cast<IsolateData*>(this)->PropertyName ## _.Get(isolate()); \ | 533 | 548 | } | 534 | | - PER_ISOLATE_STRING_PROPERTIES(V) | | 549 | + PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP) | | 550 | + PER_ISOLATE_STRING_PROPERTIES(VS) | 535 | 551 | #undef V | | 552 | +#undef VS | | 553 | +#undef VP | 536 | 554 |
| 537 | | -#define V(PropertyName, StringValue) \ | 538 | | - inline v8::Local<v8::String> Environment::PropertyName() const { \ | | 555 | +#define VP(PropertyName, StringValue) V(v8::Private, PropertyName, StringValue) | | 556 | +#define VS(PropertyName, StringValue) V(v8::String, PropertyName, StringValue) | | 557 | +#define V(TypeName, PropertyName, StringValue) \ | | 558 | + inline v8::Local<TypeName> Environment::PropertyName() const { \ | 539 | 559 | return isolate_data()->PropertyName(); \ | 540 | 560 | } | 541 | | - PER_ISOLATE_STRING_PROPERTIES(V) | | 561 | + PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP) | | 562 | + PER_ISOLATE_STRING_PROPERTIES(VS) | 542 | 563 | #undef V | | 564 | +#undef VS | | 565 | +#undef VP | 543 | 566 |
| 544 | 567 | #define V(PropertyName, TypeName) \ | 545 | 568 | inline v8::Local<TypeName> Environment::PropertyName() const { \ | @@ -552,6 +575,7 @@ inline v8::Local<v8::Object> Environment::NewInternalFieldObject() { | 552 | 575 | #undef V | 553 | 576 |
| 554 | 577 | #undef ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES | | 578 | +#undef PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES | 555 | 579 | #undef PER_ISOLATE_STRING_PROPERTIES | 556 | 580 |
| 557 | 581 | } // namespace node | |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +38-17Lines changed: 38 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,14 +44,24 @@ namespace node { | 44 | 44 | #define NODE_PUSH_VAL_TO_ARRAY_MAX 8 | 45 | 45 | #endif | 46 | 46 |
| | 47 | +// Private symbols are per-isolate primitives but Environment proxies them | | 48 | +// for the sake of convenience. Strings should be ASCII-only and have a | | 49 | +// "node:" prefix to avoid name clashes with third-party code. | | 50 | +#define PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V) \ | | 51 | + V(alpn_buffer_private_symbol, "node:alpnBuffer") \ | | 52 | + V(arrow_message_private_symbol, "node:arrowMessage") \ | | 53 | + V(contextify_private_symbol, "node:contextify") \ | | 54 | + V(decorated_private_symbol, "node:decorated") \ | | 55 | + V(npn_buffer_private_symbol, "node:npnBuffer") \ | | 56 | + V(processed_private_symbol, "node:processed") \ | | 57 | + V(selected_npn_buffer_private_symbol, "node:selectedNpnBuffer") \ | | 58 | + | 47 | 59 | // Strings are per-isolate primitives but Environment proxies them | 48 | 60 | // for the sake of convenience. Strings should be ASCII-only. | 49 | 61 | #define PER_ISOLATE_STRING_PROPERTIES(V) \ | 50 | 62 | V(address_string, "address") \ | 51 | | - V(alpn_buffer_string, "alpnBuffer") \ | 52 | 63 | V(args_string, "args") \ | 53 | 64 | V(argv_string, "argv") \ | 54 | | - V(arrow_message_string, "node:arrowMessage") \ | 55 | 65 | V(async, "async") \ | 56 | 66 | V(async_queue_string, "_asyncQueue") \ | 57 | 67 | V(atime_string, "atime") \ | @@ -73,7 +83,6 @@ namespace node { | 73 | 83 | V(cwd_string, "cwd") \ | 74 | 84 | V(debug_port_string, "debugPort") \ | 75 | 85 | V(debug_string, "debug") \ | 76 | | - V(decorated_string, "node:decorated") \ | 77 | 86 | V(dest_string, "dest") \ | 78 | 87 | V(detached_string, "detached") \ | 79 | 88 | V(dev_string, "dev") \ | @@ -140,7 +149,6 @@ namespace node { | 140 | 149 | V(netmask_string, "netmask") \ | 141 | 150 | V(nice_string, "nice") \ | 142 | 151 | V(nlink_string, "nlink") \ | 143 | | - V(npn_buffer_string, "npnBuffer") \ | 144 | 152 | V(nsname_string, "nsname") \ | 145 | 153 | V(ocsp_request_string, "OCSPRequest") \ | 146 | 154 | V(offset_string, "offset") \ | @@ -176,7 +184,6 @@ namespace node { | 176 | 184 | V(port_string, "port") \ | 177 | 185 | V(preference_string, "preference") \ | 178 | 186 | V(priority_string, "priority") \ | 179 | | - V(processed_string, "processed") \ | 180 | 187 | V(produce_cached_data_string, "produceCachedData") \ | 181 | 188 | V(prototype_string, "prototype") \ | 182 | 189 | V(raw_string, "raw") \ | @@ -192,7 +199,6 @@ namespace node { | 192 | 199 | V(serial_string, "serial") \ | 193 | 200 | V(scavenge_string, "scavenge") \ | 194 | 201 | V(scopeid_string, "scopeid") \ | 195 | | - V(selected_npn_buffer_string, "selectedNpnBuffer") \ | 196 | 202 | V(sent_shutdown_string, "sentShutdown") \ | 197 | 203 | V(serial_number_string, "serialNumber") \ | 198 | 204 | V(service_string, "service") \ | @@ -507,12 +513,17 @@ class Environment { | 507 | 513 |
| 508 | 514 | inline v8::Local<v8::Object> NewInternalFieldObject(); | 509 | 515 |
| 510 | | - // Strings are shared across shared contexts. The getters simply proxy to | 511 | | - // the per-isolate primitive. | 512 | | -#define V(PropertyName, StringValue) \ | 513 | | - inline v8::Local<v8::String> PropertyName() const; | 514 | | - PER_ISOLATE_STRING_PROPERTIES(V) | | 516 | + // Strings and private symbols are shared across shared contexts | | 517 | + // The getters simply proxy to the per-isolate primitive. | | 518 | +#define VP(PropertyName, StringValue) V(v8::Private, PropertyName, StringValue) | | 519 | +#define VS(PropertyName, StringValue) V(v8::String, PropertyName, StringValue) | | 520 | +#define V(TypeName, PropertyName, StringValue) \ | | 521 | + inline v8::Local<TypeName> PropertyName() const; | | 522 | + PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP) | | 523 | + PER_ISOLATE_STRING_PROPERTIES(VS) | 515 | 524 | #undef V | | 525 | +#undef VS | | 526 | +#undef VP | 516 | 527 |
| 517 | 528 | #define V(PropertyName, TypeName) \ | 518 | 529 | inline v8::Local<TypeName> PropertyName() const; \ | @@ -585,10 +596,15 @@ class Environment { | 585 | 596 | inline void Put(); | 586 | 597 | inline uv_loop_t* event_loop() const; | 587 | 598 |
| 588 | | -#define V(PropertyName, StringValue) \ | 589 | | - inline v8::Local<v8::String> PropertyName() const; | 590 | | - PER_ISOLATE_STRING_PROPERTIES(V) | | 599 | +#define VP(PropertyName, StringValue) V(v8::Private, PropertyName, StringValue) | | 600 | +#define VS(PropertyName, StringValue) V(v8::String, PropertyName, StringValue) | | 601 | +#define V(TypeName, PropertyName, StringValue) \ | | 602 | + inline v8::Local<TypeName> PropertyName() const; | | 603 | + PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP) | | 604 | + PER_ISOLATE_STRING_PROPERTIES(VS) | 591 | 605 | #undef V | | 606 | +#undef VS | | 607 | +#undef VP | 592 | 608 |
| 593 | 609 | private: | 594 | 610 | inline static IsolateData* Get(v8::Isolate* isolate); | @@ -598,10 +614,15 @@ class Environment { | 598 | 614 | uv_loop_t* const event_loop_; | 599 | 615 | v8::Isolate* const isolate_; | 600 | 616 |
| 601 | | -#define V(PropertyName, StringValue) \ | 602 | | - v8::Eternal<v8::String> PropertyName ## _; | 603 | | - PER_ISOLATE_STRING_PROPERTIES(V) | | 617 | +#define VP(PropertyName, StringValue) V(v8::Private, PropertyName, StringValue) | | 618 | +#define VS(PropertyName, StringValue) V(v8::String, PropertyName, StringValue) | | 619 | +#define V(TypeName, PropertyName, StringValue) \ | | 620 | + v8::Eternal<TypeName> PropertyName ## _; | | 621 | + PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP) | | 622 | + PER_ISOLATE_STRING_PROPERTIES(VS) | 604 | 623 | #undef V | | 624 | +#undef VS | | 625 | +#undef VP | 605 | 626 |
| 606 | 627 | unsigned int ref_count_; | 607 | 628 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node.cc +23-12Lines changed: 23 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1402,8 +1402,10 @@ ssize_t DecodeWrite(Isolate* isolate, | 1402 | 1402 | bool IsExceptionDecorated(Environment* env, Local<Value> er) { | 1403 | 1403 | if (!er.IsEmpty() && er->IsObject()) { | 1404 | 1404 | Local<Object> err_obj = er.As<Object>(); | 1405 | | - Local<Value> decorated = err_obj->GetHiddenValue(env->decorated_string()); | 1406 | | - return !decorated.IsEmpty() && decorated->IsTrue(); | | 1405 | + auto maybe_value = | | 1406 | + err_obj->GetPrivate(env->context(), env->decorated_private_symbol()); | | 1407 | + Local<Value> decorated; | | 1408 | + return maybe_value.ToLocal(&decorated) && decorated->IsTrue(); | 1407 | 1409 | } | 1408 | 1410 | return false; | 1409 | 1411 | } | @@ -1419,10 +1421,15 @@ void AppendExceptionLine(Environment* env, | 1419 | 1421 | if (!er.IsEmpty() && er->IsObject()) { | 1420 | 1422 | err_obj = er.As<Object>(); | 1421 | 1423 |
| | 1424 | + auto context = env->context(); | | 1425 | + auto processed_private_symbol = env->processed_private_symbol(); | 1422 | 1426 | // Do it only once per message | 1423 | | - if (!err_obj->GetHiddenValue(env->processed_string()).IsEmpty()) | | 1427 | + if (err_obj->HasPrivate(context, processed_private_symbol).FromJust()) | 1424 | 1428 | return; | 1425 | | - err_obj->SetHiddenValue(env->processed_string(), True(env->isolate())); | | 1429 | + err_obj->SetPrivate( | | 1430 | + context, | | 1431 | + processed_private_symbol, | | 1432 | + True(env->isolate())); | 1426 | 1433 | } | 1427 | 1434 |
| 1428 | 1435 | // Print (filename):(line number): (message). | @@ -1492,14 +1499,15 @@ void AppendExceptionLine(Environment* env, | 1492 | 1499 |
| 1493 | 1500 | Local<String> arrow_str = String::NewFromUtf8(env->isolate(), arrow); | 1494 | 1501 |
| 1495 | | - // Allocation failed, just print it out | 1496 | | - if (arrow_str.IsEmpty() || err_obj.IsEmpty() || !err_obj->IsNativeError()) | 1497 | | - goto print; | 1498 | | - | 1499 | | - err_obj->SetHiddenValue(env->arrow_message_string(), arrow_str); | 1500 | | - return; | | 1502 | + if (!arrow_str.IsEmpty() && !err_obj.IsEmpty() && err_obj->IsNativeError()) { | | 1503 | + err_obj->SetPrivate( | | 1504 | + env->context(), | | 1505 | + env->arrow_message_private_symbol(), | | 1506 | + arrow_str); | | 1507 | + return; | | 1508 | + } | 1501 | 1509 |
| 1502 | | - print: | | 1510 | + // Allocation failed, just print it out. | 1503 | 1511 | if (env->printed_error()) | 1504 | 1512 | return; | 1505 | 1513 | env->set_printed_error(true); | @@ -1525,7 +1533,10 @@ static void ReportException(Environment* env, | 1525 | 1533 | Local<Object> err_obj = er->ToObject(env->isolate()); | 1526 | 1534 |
| 1527 | 1535 | trace_value = err_obj->Get(env->stack_string()); | 1528 | | - arrow = err_obj->GetHiddenValue(env->arrow_message_string()); | | 1536 | + arrow = | | 1537 | + err_obj->GetPrivate( | | 1538 | + env->context(), | | 1539 | + env->arrow_message_private_symbol()).ToLocalChecked(); | 1529 | 1540 | } | 1530 | 1541 |
| 1531 | 1542 | node::Utf8Value trace(env->isolate(), trace_value); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_contextify.cc +35-25Lines changed: 35 additions & 25 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -288,11 +288,11 @@ class ContextifyContext { | 288 | 288 | } | 289 | 289 | Local<Object> sandbox = args[0].As<Object>(); | 290 | 290 |
| 291 | | - Local<String> hidden_name = | 292 | | - FIXED_ONE_BYTE_STRING(env->isolate(), "_contextifyHidden"); | 293 | | - | 294 | 291 | // Don't allow contextifying a sandbox multiple times. | 295 | | - CHECK(sandbox->GetHiddenValue(hidden_name).IsEmpty()); | | 292 | + CHECK( | | 293 | + !sandbox->HasPrivate( | | 294 | + env->context(), | | 295 | + env->contextify_private_symbol()).FromJust()); | 296 | 296 |
| 297 | 297 | TryCatch try_catch; | 298 | 298 | ContextifyContext* context = new ContextifyContext(env, sandbox); | @@ -305,8 +305,10 @@ class ContextifyContext { | 305 | 305 | if (context->context().IsEmpty()) | 306 | 306 | return; | 307 | 307 |
| 308 | | - Local<External> hidden_context = External::New(env->isolate(), context); | 309 | | - sandbox->SetHiddenValue(hidden_name, hidden_context); | | 308 | + sandbox->SetPrivate( | | 309 | + env->context(), | | 310 | + env->contextify_private_symbol(), | | 311 | + External::New(env->isolate(), context)); | 310 | 312 | } | 311 | 313 |
| 312 | 314 |
| @@ -319,10 +321,9 @@ class ContextifyContext { | 319 | 321 | } | 320 | 322 | Local<Object> sandbox = args[0].As<Object>(); | 321 | 323 |
| 322 | | - Local<String> hidden_name = | 323 | | - FIXED_ONE_BYTE_STRING(env->isolate(), "_contextifyHidden"); | 324 | | - | 325 | | - args.GetReturnValue().Set(!sandbox->GetHiddenValue(hidden_name).IsEmpty()); | | 324 | + auto result = | | 325 | + sandbox->HasPrivate(env->context(), env->contextify_private_symbol()); | | 326 | + args.GetReturnValue().Set(result.FromJust()); | 326 | 327 | } | 327 | 328 |
| 328 | 329 |
| @@ -342,17 +343,17 @@ class ContextifyContext { | 342 | 343 |
| 343 | 344 |
| 344 | 345 | static ContextifyContext* ContextFromContextifiedSandbox( | 345 | | - Isolate* isolate, | | 346 | + Environment* env, | 346 | 347 | const Local<Object>& sandbox) { | 347 | | - Local<String> hidden_name = | 348 | | - FIXED_ONE_BYTE_STRING(isolate, "_contextifyHidden"); | 349 | | - Local<Value> context_external_v = sandbox->GetHiddenValue(hidden_name); | 350 | | - if (context_external_v.IsEmpty() || !context_external_v->IsExternal()) { | 351 | | - return nullptr; | | 348 | + auto maybe_value = | | 349 | + sandbox->GetPrivate(env->context(), env->contextify_private_symbol()); | | 350 | + Local<Value> context_external_v; | | 351 | + if (maybe_value.ToLocal(&context_external_v) && | | 352 | + context_external_v->IsExternal()) { | | 353 | + Local<External> context_external = context_external_v.As<External>(); | | 354 | + return static_cast<ContextifyContext*>(context_external->Value()); | 352 | 355 | } | 353 | | - Local<External> context_external = context_external_v.As<External>(); | 354 | | - | 355 | | - return static_cast<ContextifyContext*>(context_external->Value()); | | 356 | + return nullptr; | 356 | 357 | } | 357 | 358 |
| 358 | 359 |
| @@ -612,8 +613,7 @@ class ContextifyScript : public BaseObject { | 612 | 613 |
| 613 | 614 | // Get the context from the sandbox | 614 | 615 | ContextifyContext* contextify_context = | 615 | | - ContextifyContext::ContextFromContextifiedSandbox(env->isolate(), | 616 | | - sandbox); | | 616 | + ContextifyContext::ContextFromContextifiedSandbox(env, sandbox); | 617 | 617 | if (contextify_context == nullptr) { | 618 | 618 | return env->ThrowTypeError( | 619 | 619 | "sandbox argument must have been converted to a context."); | @@ -654,15 +654,25 @@ class ContextifyScript : public BaseObject { | 654 | 654 |
| 655 | 655 | AppendExceptionLine(env, exception, try_catch.Message()); | 656 | 656 | Local<Value> stack = err_obj->Get(env->stack_string()); | 657 | | - Local<Value> arrow = err_obj->GetHiddenValue(env->arrow_message_string()); | 658 | | - | 659 | | - if (!(stack->IsString() && arrow->IsString())) | | 657 | + auto maybe_value = | | 658 | + err_obj->GetPrivate( | | 659 | + env->context(), | | 660 | + env->arrow_message_private_symbol()); | | 661 | + | | 662 | + Local<Value> arrow; | | 663 | + if (!(maybe_value.ToLocal(&arrow) && | | 664 | + arrow->IsString() && | | 665 | + stack->IsString())) { | 660 | 666 | return; | | 667 | + } | 661 | 668 |
| 662 | 669 | Local<String> decorated_stack = String::Concat(arrow.As<String>(), | 663 | 670 | stack.As<String>()); | 664 | 671 | err_obj->Set(env->stack_string(), decorated_stack); | 665 | | - err_obj->SetHiddenValue(env->decorated_string(), True(env->isolate())); | | 672 | + err_obj->SetPrivate( | | 673 | + env->context(), | | 674 | + env->decorated_private_symbol(), | | 675 | + True(env->isolate())); | 666 | 676 | } | 667 | 677 |
| 668 | 678 | static int64_t GetTimeoutArg(const FunctionCallbackInfo<Value>& args, | |
|