GeezerVison is showing github.com — src: use JS inheritance for `AsyncWrap` · nodejs/node@5a83967
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/worker.js +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -73,7 +73,8 @@ Object.setPrototypeOf(MessagePort.prototype, EventEmitter.prototype); | 73 | 73 | delete MessagePort.prototype.stop; | 74 | 74 | delete MessagePort.prototype.drain; | 75 | 75 | delete MessagePort.prototype.hasRef; | 76 | | -delete MessagePort.prototype.getAsyncId; | | 76 | +MessagePort.prototype.ref = MessagePortPrototype.ref; | | 77 | +MessagePort.prototype.unref = MessagePortPrototype.unref; | 77 | 78 |
| 78 | 79 | // A communication channel consisting of a handle (that wraps around an | 79 | 80 | // uv_async_t) which can receive information from other threads and emits | |
Collapse file Copy file name to clipboardExpand all lines: node.gyp -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 426 | 426 | 'src/node_root_certs.h', | 427 | 427 | 'src/node_version.h', | 428 | 428 | 'src/node_watchdog.h', | 429 | | - 'src/node_wrap.h', | 430 | 429 | 'src/node_revert.h', | 431 | 430 | 'src/node_i18n.h', | 432 | 431 | 'src/node_worker.h', | |
Collapse file Copy file name to clipboardExpand all lines: src/async_wrap.cc +16-9Lines changed: 16 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -63,7 +63,7 @@ struct AsyncWrapObject : public AsyncWrap { | 63 | 63 | static inline void New(const FunctionCallbackInfo<Value>& args) { | 64 | 64 | Environment* env = Environment::GetCurrent(args); | 65 | 65 | CHECK(args.IsConstructCall()); | 66 | | - CHECK(env->async_wrap_constructor_template()->HasInstance(args.This())); | | 66 | + CHECK(env->async_wrap_object_ctor_template()->HasInstance(args.This())); | 67 | 67 | CHECK(args[0]->IsUint32()); | 68 | 68 | auto type = static_cast<ProviderType>(args[0].As<Uint32>()->Value()); | 69 | 69 | new AsyncWrapObject(env, args.This(), type); | @@ -423,12 +423,16 @@ void AsyncWrap::QueueDestroyAsyncId(const FunctionCallbackInfo<Value>& args) { | 423 | 423 | args[0].As<Number>()->Value()); | 424 | 424 | } | 425 | 425 |
| 426 | | -void AsyncWrap::AddWrapMethods(Environment* env, | 427 | | - Local<FunctionTemplate> constructor, | 428 | | - int flag) { | 429 | | - env->SetProtoMethod(constructor, "getAsyncId", AsyncWrap::GetAsyncId); | 430 | | - if (flag & kFlagHasReset) | 431 | | - env->SetProtoMethod(constructor, "asyncReset", AsyncWrap::AsyncReset); | | 426 | +Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(Environment* env) { | | 427 | + Local<FunctionTemplate> tmpl = env->async_wrap_ctor_template(); | | 428 | + if (tmpl.IsEmpty()) { | | 429 | + tmpl = env->NewFunctionTemplate(nullptr); | | 430 | + tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncWrap")); | | 431 | + env->SetProtoMethod(tmpl, "getAsyncId", AsyncWrap::GetAsyncId); | | 432 | + env->SetProtoMethod(tmpl, "asyncReset", AsyncWrap::AsyncReset); | | 433 | + env->set_async_wrap_ctor_template(tmpl); | | 434 | + } | | 435 | + return tmpl; | 432 | 436 | } | 433 | 437 |
| 434 | 438 | void AsyncWrap::Initialize(Local<Object> target, | @@ -524,17 +528,20 @@ void AsyncWrap::Initialize(Local<Object> target, | 524 | 528 | env->set_async_hooks_promise_resolve_function(Local<Function>()); | 525 | 529 | env->set_async_hooks_binding(target); | 526 | 530 |
| | 531 | + // TODO(addaleax): This block might better work as a | | 532 | + // AsyncWrapObject::Initialize() or AsyncWrapObject::GetConstructorTemplate() | | 533 | + // function. | 527 | 534 | { | 528 | 535 | auto class_name = FIXED_ONE_BYTE_STRING(env->isolate(), "AsyncWrap"); | 529 | 536 | auto function_template = env->NewFunctionTemplate(AsyncWrapObject::New); | 530 | 537 | function_template->SetClassName(class_name); | 531 | | - AsyncWrap::AddWrapMethods(env, function_template); | | 538 | + function_template->Inherit(AsyncWrap::GetConstructorTemplate(env)); | 532 | 539 | auto instance_template = function_template->InstanceTemplate(); | 533 | 540 | instance_template->SetInternalFieldCount(1); | 534 | 541 | auto function = | 535 | 542 | function_template->GetFunction(env->context()).ToLocalChecked(); | 536 | 543 | target->Set(env->context(), class_name, function).FromJust(); | 537 | | - env->set_async_wrap_constructor_template(function_template); | | 544 | + env->set_async_wrap_object_ctor_template(function_template); | 538 | 545 | } | 539 | 546 | } | 540 | 547 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/async_wrap.h +2-8Lines changed: 2 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -105,21 +105,15 @@ class AsyncWrap : public BaseObject { | 105 | 105 | PROVIDERS_LENGTH, | 106 | 106 | }; | 107 | 107 |
| 108 | | - enum Flags { | 109 | | - kFlagNone = 0x0, | 110 | | - kFlagHasReset = 0x1 | 111 | | - }; | 112 | | - | 113 | 108 | AsyncWrap(Environment* env, | 114 | 109 | v8::Local<v8::Object> object, | 115 | 110 | ProviderType provider, | 116 | 111 | double execution_async_id = -1); | 117 | 112 |
| 118 | 113 | virtual ~AsyncWrap(); | 119 | 114 |
| 120 | | - static void AddWrapMethods(Environment* env, | 121 | | - v8::Local<v8::FunctionTemplate> constructor, | 122 | | - int flags = kFlagNone); | | 115 | + static v8::Local<v8::FunctionTemplate> GetConstructorTemplate( | | 116 | + Environment* env); | 123 | 117 |
| 124 | 118 | static void Initialize(v8::Local<v8::Object> target, | 125 | 119 | v8::Local<v8::Value> unused, | |
Collapse file Copy file name to clipboardExpand all lines: src/cares_wrap.cc +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2220,23 +2220,23 @@ void Initialize(Local<Object> target, | 2220 | 2220 |
| 2221 | 2221 | Local<FunctionTemplate> aiw = | 2222 | 2222 | BaseObject::MakeLazilyInitializedJSTemplate(env); | 2223 | | - AsyncWrap::AddWrapMethods(env, aiw); | | 2223 | + aiw->Inherit(AsyncWrap::GetConstructorTemplate(env)); | 2224 | 2224 | Local<String> addrInfoWrapString = | 2225 | 2225 | FIXED_ONE_BYTE_STRING(env->isolate(), "GetAddrInfoReqWrap"); | 2226 | 2226 | aiw->SetClassName(addrInfoWrapString); | 2227 | 2227 | target->Set(addrInfoWrapString, aiw->GetFunction(context).ToLocalChecked()); | 2228 | 2228 |
| 2229 | 2229 | Local<FunctionTemplate> niw = | 2230 | 2230 | BaseObject::MakeLazilyInitializedJSTemplate(env); | 2231 | | - AsyncWrap::AddWrapMethods(env, niw); | | 2231 | + niw->Inherit(AsyncWrap::GetConstructorTemplate(env)); | 2232 | 2232 | Local<String> nameInfoWrapString = | 2233 | 2233 | FIXED_ONE_BYTE_STRING(env->isolate(), "GetNameInfoReqWrap"); | 2234 | 2234 | niw->SetClassName(nameInfoWrapString); | 2235 | 2235 | target->Set(nameInfoWrapString, niw->GetFunction(context).ToLocalChecked()); | 2236 | 2236 |
| 2237 | 2237 | Local<FunctionTemplate> qrw = | 2238 | 2238 | BaseObject::MakeLazilyInitializedJSTemplate(env); | 2239 | | - AsyncWrap::AddWrapMethods(env, qrw); | | 2239 | + qrw->Inherit(AsyncWrap::GetConstructorTemplate(env)); | 2240 | 2240 | Local<String> queryWrapString = | 2241 | 2241 | FIXED_ONE_BYTE_STRING(env->isolate(), "QueryReqWrap"); | 2242 | 2242 | qrw->SetClassName(queryWrapString); | @@ -2245,7 +2245,7 @@ void Initialize(Local<Object> target, | 2245 | 2245 | Local<FunctionTemplate> channel_wrap = | 2246 | 2246 | env->NewFunctionTemplate(ChannelWrap::New); | 2247 | 2247 | channel_wrap->InstanceTemplate()->SetInternalFieldCount(1); | 2248 | | - AsyncWrap::AddWrapMethods(env, channel_wrap); | | 2248 | + channel_wrap->Inherit(AsyncWrap::GetConstructorTemplate(env)); | 2249 | 2249 |
| 2250 | 2250 | env->SetProtoMethod(channel_wrap, "queryAny", Query<QueryAnyWrap>); | 2251 | 2251 | env->SetProtoMethod(channel_wrap, "queryA", Query<QueryAWrap>); | |
Collapse file Copy file name to clipboardExpand all lines: src/env.h +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -319,7 +319,8 @@ struct PackageConfig { | 319 | 319 | V(async_hooks_destroy_function, v8::Function) \ | 320 | 320 | V(async_hooks_init_function, v8::Function) \ | 321 | 321 | V(async_hooks_promise_resolve_function, v8::Function) \ | 322 | | - V(async_wrap_constructor_template, v8::FunctionTemplate) \ | | 322 | + V(async_wrap_object_ctor_template, v8::FunctionTemplate) \ | | 323 | + V(async_wrap_ctor_template, v8::FunctionTemplate) \ | 323 | 324 | V(buffer_prototype_object, v8::Object) \ | 324 | 325 | V(context, v8::Context) \ | 325 | 326 | V(domain_callback, v8::Function) \ | @@ -329,13 +330,15 @@ struct PackageConfig { | 329 | 330 | V(filehandlereadwrap_template, v8::ObjectTemplate) \ | 330 | 331 | V(fsreqpromise_constructor_template, v8::ObjectTemplate) \ | 331 | 332 | V(fs_use_promises_symbol, v8::Symbol) \ | | 333 | + V(handle_wrap_ctor_template, v8::FunctionTemplate) \ | 332 | 334 | V(host_import_module_dynamically_callback, v8::Function) \ | 333 | 335 | V(host_initialize_import_meta_object_callback, v8::Function) \ | 334 | 336 | V(http2ping_constructor_template, v8::ObjectTemplate) \ | 335 | 337 | V(http2settings_constructor_template, v8::ObjectTemplate) \ | 336 | 338 | V(http2stream_constructor_template, v8::ObjectTemplate) \ | 337 | 339 | V(immediate_callback_function, v8::Function) \ | 338 | 340 | V(inspector_console_api_object, v8::Object) \ | | 341 | + V(libuv_stream_wrap_ctor_template, v8::FunctionTemplate) \ | 339 | 342 | V(message_port, v8::Object) \ | 340 | 343 | V(message_port_constructor_template, v8::FunctionTemplate) \ | 341 | 344 | V(pipe_constructor_template, v8::FunctionTemplate) \ | |
Collapse file Copy file name to clipboardExpand all lines: src/fs_event_wrap.cc +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -105,7 +105,7 @@ void FSEventWrap::Initialize(Local<Object> target, | 105 | 105 | t->InstanceTemplate()->SetInternalFieldCount(1); | 106 | 106 | t->SetClassName(fsevent_string); | 107 | 107 |
| 108 | | - AsyncWrap::AddWrapMethods(env, t); | | 108 | + t->Inherit(AsyncWrap::GetConstructorTemplate(env)); | 109 | 109 | env->SetProtoMethod(t, "start", Start); | 110 | 110 | env->SetProtoMethod(t, "close", Close); | 111 | 111 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/handle_wrap.cc +13-7Lines changed: 13 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -130,13 +130,19 @@ void HandleWrap::OnClose(uv_handle_t* handle) { | 130 | 130 | } | 131 | 131 | } | 132 | 132 |
| 133 | | - | 134 | | -void HandleWrap::AddWrapMethods(Environment* env, | 135 | | - Local<FunctionTemplate> t) { | 136 | | - env->SetProtoMethod(t, "close", HandleWrap::Close); | 137 | | - env->SetProtoMethodNoSideEffect(t, "hasRef", HandleWrap::HasRef); | 138 | | - env->SetProtoMethod(t, "ref", HandleWrap::Ref); | 139 | | - env->SetProtoMethod(t, "unref", HandleWrap::Unref); | | 133 | +Local<FunctionTemplate> HandleWrap::GetConstructorTemplate(Environment* env) { | | 134 | + Local<FunctionTemplate> tmpl = env->handle_wrap_ctor_template(); | | 135 | + if (tmpl.IsEmpty()) { | | 136 | + tmpl = env->NewFunctionTemplate(nullptr); | | 137 | + tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HandleWrap")); | | 138 | + tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env)); | | 139 | + env->SetProtoMethod(tmpl, "close", HandleWrap::Close); | | 140 | + env->SetProtoMethodNoSideEffect(tmpl, "hasRef", HandleWrap::HasRef); | | 141 | + env->SetProtoMethod(tmpl, "ref", HandleWrap::Ref); | | 142 | + env->SetProtoMethod(tmpl, "unref", HandleWrap::Unref); | | 143 | + env->set_handle_wrap_ctor_template(tmpl); | | 144 | + } | | 145 | + return tmpl; | 140 | 146 | } | 141 | 147 |
| 142 | 148 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/handle_wrap.h +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -73,8 +73,8 @@ class HandleWrap : public AsyncWrap { | 73 | 73 | virtual void Close( | 74 | 74 | v8::Local<v8::Value> close_callback = v8::Local<v8::Value>()); | 75 | 75 |
| 76 | | - static void AddWrapMethods(Environment* env, | 77 | | - v8::Local<v8::FunctionTemplate> constructor); | | 76 | + static v8::Local<v8::FunctionTemplate> GetConstructorTemplate( | | 77 | + Environment* env); | 78 | 78 |
| 79 | 79 | protected: | 80 | 80 | HandleWrap(Environment* env, | |
Collapse file Copy file name to clipboardExpand all lines: src/inspector_js_api.cc +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -307,7 +307,7 @@ void Initialize(Local<Object> target, Local<Value> unused, | 307 | 307 | env->NewFunctionTemplate(JSBindingsConnection::New); | 308 | 308 | tmpl->InstanceTemplate()->SetInternalFieldCount(1); | 309 | 309 | tmpl->SetClassName(conn_str); | 310 | | - AsyncWrap::AddWrapMethods(env, tmpl); | | 310 | + tmpl->Inherit(AsyncWrap::GetConstructorTemplate(env)); | 311 | 311 | env->SetProtoMethod(tmpl, "dispatch", JSBindingsConnection::Dispatch); | 312 | 312 | env->SetProtoMethod(tmpl, "disconnect", JSBindingsConnection::Disconnect); | 313 | 313 | target | |
|