GeezerVison is showing github.com — src: use ObjectTemplate for creating stream req objs · nodejs/node@d93c48b
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.h +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -329,7 +329,7 @@ struct PackageConfig { | 329 | 329 | V(script_context_constructor_template, v8::FunctionTemplate) \ | 330 | 330 | V(script_data_constructor_function, v8::Function) \ | 331 | 331 | V(secure_context_constructor_template, v8::FunctionTemplate) \ | 332 | | - V(shutdown_wrap_constructor_function, v8::Function) \ | | 332 | + V(shutdown_wrap_template, v8::ObjectTemplate) \ | 333 | 333 | V(tcp_constructor_template, v8::FunctionTemplate) \ | 334 | 334 | V(tick_callback_function, v8::Function) \ | 335 | 335 | V(timers_callback_function, v8::Function) \ | @@ -338,7 +338,7 @@ struct PackageConfig { | 338 | 338 | V(udp_constructor_function, v8::Function) \ | 339 | 339 | V(vm_parsing_context_symbol, v8::Symbol) \ | 340 | 340 | V(url_constructor_function, v8::Function) \ | 341 | | - V(write_wrap_constructor_function, v8::Function) \ | | 341 | + V(write_wrap_template, v8::ObjectTemplate) \ | 342 | 342 | V(fs_use_promises_symbol, v8::Symbol) | 343 | 343 |
| 344 | 344 | class Environment; | |
Collapse file Copy file name to clipboardExpand all lines: src/stream_base-inl.h +13-2Lines changed: 13 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -164,8 +164,9 @@ inline int StreamBase::Shutdown(v8::Local<v8::Object> req_wrap_obj) { | 164 | 164 |
| 165 | 165 | if (req_wrap_obj.IsEmpty()) { | 166 | 166 | req_wrap_obj = | 167 | | - env->shutdown_wrap_constructor_function() | | 167 | + env->shutdown_wrap_template() | 168 | 168 | ->NewInstance(env->context()).ToLocalChecked(); | | 169 | + StreamReq::ResetObject(req_wrap_obj); | 169 | 170 | } | 170 | 171 |
| 171 | 172 | AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap()); | @@ -203,8 +204,9 @@ inline StreamWriteResult StreamBase::Write( | 203 | 204 |
| 204 | 205 | if (req_wrap_obj.IsEmpty()) { | 205 | 206 | req_wrap_obj = | 206 | | - env->write_wrap_constructor_function() | | 207 | + env->write_wrap_template() | 207 | 208 | ->NewInstance(env->context()).ToLocalChecked(); | | 209 | + StreamReq::ResetObject(req_wrap_obj); | 208 | 210 | } | 209 | 211 |
| 210 | 212 | AsyncHooks::DefaultTriggerAsyncIdScope trigger_scope(GetAsyncWrap()); | @@ -427,6 +429,15 @@ inline void StreamReq::Done(int status, const char* error_str) { | 427 | 429 | OnDone(status); | 428 | 430 | } | 429 | 431 |
| | 432 | +inline void StreamReq::ResetObject(v8::Local<v8::Object> obj) { | | 433 | +#ifdef DEBUG | | 434 | + CHECK_GT(obj->InternalFieldCount(), StreamReq::kStreamReqField); | | 435 | +#endif | | 436 | + ClearWrap(obj); | | 437 | + obj->SetAlignedPointerInInternalField(StreamReq::kStreamReqField, nullptr); | | 438 | +} | | 439 | + | | 440 | + | 430 | 441 | } // namespace node | 431 | 442 |
| 432 | 443 | #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS | |
Collapse file Copy file name to clipboardExpand all lines: src/stream_base.h +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -46,6 +46,13 @@ class StreamReq { | 46 | 46 |
| 47 | 47 | static StreamReq* FromObject(v8::Local<v8::Object> req_wrap_obj); | 48 | 48 |
| | 49 | + // Sets all internal fields of `req_wrap_obj` to `nullptr`. | | 50 | + // This is what the `WriteWrap` and `ShutdownWrap` JS constructors do, | | 51 | + // and what we use in C++ after creating these objects from their | | 52 | + // v8::ObjectTemplates, to avoid the overhead of calling the | | 53 | + // constructor explicitly. | | 54 | + static inline void ResetObject(v8::Local<v8::Object> req_wrap_obj); | | 55 | + | 49 | 56 | protected: | 50 | 57 | virtual void OnDone(int status) = 0; | 51 | 58 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/stream_wrap.cc +3-5Lines changed: 3 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -60,9 +60,7 @@ void LibuvStreamWrap::Initialize(Local<Object> target, | 60 | 60 | auto is_construct_call_callback = | 61 | 61 | [](const FunctionCallbackInfo<Value>& args) { | 62 | 62 | CHECK(args.IsConstructCall()); | 63 | | - ClearWrap(args.This()); | 64 | | - args.This()->SetAlignedPointerInInternalField( | 65 | | - StreamReq::kStreamReqField, nullptr); | | 63 | + StreamReq::ResetObject(args.This()); | 66 | 64 | }; | 67 | 65 | Local<FunctionTemplate> sw = | 68 | 66 | FunctionTemplate::New(env->isolate(), is_construct_call_callback); | @@ -72,7 +70,7 @@ void LibuvStreamWrap::Initialize(Local<Object> target, | 72 | 70 | sw->SetClassName(wrapString); | 73 | 71 | AsyncWrap::AddWrapMethods(env, sw); | 74 | 72 | target->Set(wrapString, sw->GetFunction()); | 75 | | - env->set_shutdown_wrap_constructor_function(sw->GetFunction()); | | 73 | + env->set_shutdown_wrap_template(sw->InstanceTemplate()); | 76 | 74 |
| 77 | 75 | Local<FunctionTemplate> ww = | 78 | 76 | FunctionTemplate::New(env->isolate(), is_construct_call_callback); | @@ -82,7 +80,7 @@ void LibuvStreamWrap::Initialize(Local<Object> target, | 82 | 80 | ww->SetClassName(writeWrapString); | 83 | 81 | AsyncWrap::AddWrapMethods(env, ww); | 84 | 82 | target->Set(writeWrapString, ww->GetFunction()); | 85 | | - env->set_write_wrap_constructor_function(ww->GetFunction()); | | 83 | + env->set_write_wrap_template(ww->InstanceTemplate()); | 86 | 84 | } | 87 | 85 |
| 88 | 86 |
| |
|