| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 717db1d commit 86e22b4
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -185,11 +185,11 @@ static void SetupHooks(const FunctionCallbackInfo<Value>& args) { | |||
| 185 | 185 | static void SetPromiseHooks(const FunctionCallbackInfo<Value>& args) { | |
| 186 | 186 | Environment* env = Environment::GetCurrent(args); | |
| 187 | 187 | ||
| 188 | - env->async_hooks()->SetJSPromiseHooks( | ||
| 189 | - args[0]->IsFunction() ? args[0].As<Function>() : Local<Function>(), | ||
| 190 | - args[1]->IsFunction() ? args[1].As<Function>() : Local<Function>(), | ||
| 191 | - args[2]->IsFunction() ? args[2].As<Function>() : Local<Function>(), | ||
| 192 | - args[3]->IsFunction() ? args[3].As<Function>() : Local<Function>()); | ||
| 188 | + env->ResetPromiseHooks( | ||
| 189 | + args[0]->IsFunction() ? args[0].As<Function>() : Local<Function>(), | ||
| 190 | + args[1]->IsFunction() ? args[1].As<Function>() : Local<Function>(), | ||
| 191 | + args[2]->IsFunction() ? args[2].As<Function>() : Local<Function>(), | ||
| 192 | + args[3]->IsFunction() ? args[3].As<Function>() : Local<Function>()); | ||
| 193 | 193 | } | |
| 194 | 194 | ||
| 195 | 195 | class DestroyParam { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -63,20 +63,28 @@ int const ContextEmbedderTag::kNodeContextTag = 0x6e6f64; | |||
| 63 | 63 | void* const ContextEmbedderTag::kNodeContextTagPtr = const_cast<void*>( | |
| 64 | 64 | static_cast<const void*>(&ContextEmbedderTag::kNodeContextTag)); | |
| 65 | 65 | ||
| 66 | - void AsyncHooks::SetJSPromiseHooks(Local<Function> init, | ||
| 66 | + void AsyncHooks::ResetPromiseHooks(Local<Function> init, | ||
| 67 | 67 | Local<Function> before, | |
| 68 | 68 | Local<Function> after, | |
| 69 | 69 | Local<Function> resolve) { | |
| 70 | 70 | js_promise_hooks_[0].Reset(env()->isolate(), init); | |
| 71 | 71 | js_promise_hooks_[1].Reset(env()->isolate(), before); | |
| 72 | 72 | js_promise_hooks_[2].Reset(env()->isolate(), after); | |
| 73 | 73 | js_promise_hooks_[3].Reset(env()->isolate(), resolve); | |
| 74 | + } | ||
| 75 | + | ||
| 76 | + void Environment::ResetPromiseHooks(Local<Function> init, | ||
| 77 | + Local<Function> before, | ||
| 78 | + Local<Function> after, | ||
| 79 | + Local<Function> resolve) { | ||
| 80 | + async_hooks()->ResetPromiseHooks(init, before, after, resolve); | ||
| 81 | + | ||
| 74 | 82 | for (auto it = contexts_.begin(); it != contexts_.end(); it++) { | |
| 75 | 83 | if (it->IsEmpty()) { | |
| 76 | 84 | contexts_.erase(it--); | |
| 77 | 85 | continue; | |
| 78 | 86 | } | |
| 79 | - PersistentToLocal::Weak(env()->isolate(), *it) | ||
| 87 | + PersistentToLocal::Weak(isolate_, *it) | ||
| 80 | 88 | ->SetPromiseHooks(init, before, after, resolve); | |
| 81 | 89 | } | |
| 82 | 90 | } | |
@@ -179,7 +187,7 @@ void AsyncHooks::clear_async_id_stack() { | |||
| 179 | 187 | fields_[kStackLength] = 0; | |
| 180 | 188 | } | |
| 181 | 189 | ||
| 182 | - void AsyncHooks::AddContext(Local<Context> ctx) { | ||
| 190 | + void AsyncHooks::InstallPromiseHooks(Local<Context> ctx) { | ||
| 183 | 191 | ctx->SetPromiseHooks(js_promise_hooks_[0].IsEmpty() | |
| 184 | 192 | ? Local<Function>() | |
| 185 | 193 | : PersistentToLocal::Strong(js_promise_hooks_[0]), | |
@@ -192,23 +200,24 @@ void AsyncHooks::AddContext(Local<Context> ctx) { | |||
| 192 | 200 | js_promise_hooks_[3].IsEmpty() | |
| 193 | 201 | ? Local<Function>() | |
| 194 | 202 | : PersistentToLocal::Strong(js_promise_hooks_[3])); | |
| 203 | + } | ||
| 195 | 204 | ||
| 205 | + void Environment::TrackContext(Local<Context> context) { | ||
| 196 | 206 | size_t id = contexts_.size(); | |
| 197 | 207 | contexts_.resize(id + 1); | |
| 198 | - contexts_[id].Reset(env()->isolate(), ctx); | ||
| 208 | + contexts_[id].Reset(isolate_, context); | ||
| 199 | 209 | contexts_[id].SetWeak(); | |
| 200 | 210 | } | |
| 201 | 211 | ||
| 202 | - void AsyncHooks::RemoveContext(Local<Context> ctx) { | ||
| 203 | - Isolate* isolate = env()->isolate(); | ||
| 204 | - HandleScope handle_scope(isolate); | ||
| 212 | + void Environment::UntrackContext(Local<Context> context) { | ||
| 213 | + HandleScope handle_scope(isolate_); | ||
| 205 | 214 | contexts_.erase(std::remove_if(contexts_.begin(), | |
| 206 | 215 | contexts_.end(), | |
| 207 | 216 | [&](auto&& el) { return el.IsEmpty(); }), | |
| 208 | 217 | contexts_.end()); | |
| 209 | 218 | for (auto it = contexts_.begin(); it != contexts_.end(); it++) { | |
| 210 | - Local<Context> saved_context = PersistentToLocal::Weak(isolate, *it); | ||
| 211 | - if (saved_context == ctx) { | ||
| 219 | + Local<Context> saved_context = PersistentToLocal::Weak(isolate_, *it); | ||
| 220 | + if (saved_context == context) { | ||
| 212 | 221 | it->Reset(); | |
| 213 | 222 | contexts_.erase(it); | |
| 214 | 223 | break; | |
@@ -543,7 +552,8 @@ void Environment::AssignToContext(Local<v8::Context> context, | |||
| 543 | 552 | inspector_agent()->ContextCreated(context, info); | |
| 544 | 553 | #endif // HAVE_INSPECTOR | |
| 545 | 554 | ||
| 546 | - this->async_hooks()->AddContext(context); | ||
| 555 | + this->async_hooks()->InstallPromiseHooks(context); | ||
| 556 | + TrackContext(context); | ||
| 547 | 557 | } | |
| 548 | 558 | ||
| 549 | 559 | void Environment::TryLoadAddon( | |
@@ -1466,8 +1476,9 @@ AsyncHooks::SerializeInfo AsyncHooks::Serialize(Local<Context> context, | |||
| 1466 | 1476 | context, | |
| 1467 | 1477 | native_execution_async_resources_[i]); | |
| 1468 | 1478 | } | |
| 1469 | - CHECK_EQ(contexts_.size(), 1); | ||
| 1470 | - CHECK_EQ(contexts_[0], env()->context()); | ||
| 1479 | + | ||
| 1480 | + // At the moment, promise hooks are not supported in the startup snapshot. | ||
| 1481 | + // TODO(joyeecheung): support promise hooks in the startup snapshot. | ||
| 1471 | 1482 | CHECK(js_promise_hooks_[0].IsEmpty()); | |
| 1472 | 1483 | CHECK(js_promise_hooks_[1].IsEmpty()); | |
| 1473 | 1484 | CHECK(js_promise_hooks_[2].IsEmpty()); | |
@@ -1602,6 +1613,10 @@ EnvSerializeInfo Environment::Serialize(SnapshotCreator* creator) { | |||
| 1602 | 1613 | should_abort_on_uncaught_toggle_.Serialize(ctx, creator); | |
| 1603 | 1614 | ||
| 1604 | 1615 | info.principal_realm = principal_realm_->Serialize(creator); | |
| 1616 | + // For now we only support serialization of the main context. | ||
| 1617 | + // TODO(joyeecheung): support de/serialization of vm contexts. | ||
| 1618 | + CHECK_EQ(contexts_.size(), 1); | ||
| 1619 | + CHECK_EQ(contexts_[0], context()); | ||
| 1605 | 1620 | return info; | |
| 1606 | 1621 | } | |
| 1607 | 1622 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -303,7 +303,8 @@ class AsyncHooks : public MemoryRetainer { | |||
| 303 | 303 | // The `js_execution_async_resources` array contains the value in that case. | |
| 304 | 304 | inline v8::Local<v8::Object> native_execution_async_resource(size_t index); | |
| 305 | 305 | ||
| 306 | - void SetJSPromiseHooks(v8::Local<v8::Function> init, | ||
| 306 | + void InstallPromiseHooks(v8::Local<v8::Context> ctx); | ||
| 307 | + void ResetPromiseHooks(v8::Local<v8::Function> init, | ||
| 307 | 308 | v8::Local<v8::Function> before, | |
| 308 | 309 | v8::Local<v8::Function> after, | |
| 309 | 310 | v8::Local<v8::Function> resolve); | |
@@ -322,9 +323,6 @@ class AsyncHooks : public MemoryRetainer { | |||
| 322 | 323 | bool pop_async_context(double async_id); | |
| 323 | 324 | void clear_async_id_stack(); // Used in fatal exceptions. | |
| 324 | 325 | ||
| 325 | - void AddContext(v8::Local<v8::Context> ctx); | ||
| 326 | - void RemoveContext(v8::Local<v8::Context> ctx); | ||
| 327 | - | ||
| 328 | 326 | AsyncHooks(const AsyncHooks&) = delete; | |
| 329 | 327 | AsyncHooks& operator=(const AsyncHooks&) = delete; | |
| 330 | 328 | AsyncHooks(AsyncHooks&&) = delete; | |
@@ -387,8 +385,6 @@ class AsyncHooks : public MemoryRetainer { | |||
| 387 | 385 | // Non-empty during deserialization | |
| 388 | 386 | const SerializeInfo* info_ = nullptr; | |
| 389 | 387 | ||
| 390 | - std::vector<v8::Global<v8::Context>> contexts_; | ||
| 391 | - | ||
| 392 | 388 | std::array<v8::Global<v8::Function>, 4> js_promise_hooks_; | |
| 393 | 389 | }; | |
| 394 | 390 | ||
@@ -701,9 +697,15 @@ class Environment : public MemoryRetainer { | |||
| 701 | 697 | template <typename T, typename OnCloseCallback> | |
| 702 | 698 | inline void CloseHandle(T* handle, OnCloseCallback callback); | |
| 703 | 699 | ||
| 700 | + void ResetPromiseHooks(v8::Local<v8::Function> init, | ||
| 701 | + v8::Local<v8::Function> before, | ||
| 702 | + v8::Local<v8::Function> after, | ||
| 703 | + v8::Local<v8::Function> resolve); | ||
| 704 | 704 | void AssignToContext(v8::Local<v8::Context> context, | |
| 705 | 705 | Realm* realm, | |
| 706 | 706 | const ContextInfo& info); | |
| 707 | + void TrackContext(v8::Local<v8::Context> context); | ||
| 708 | + void UntrackContext(v8::Local<v8::Context> context); | ||
| 707 | 709 | ||
| 708 | 710 | void StartProfilerIdleNotifier(); | |
| 709 | 711 | ||
@@ -1145,6 +1147,7 @@ class Environment : public MemoryRetainer { | |||
| 1145 | 1147 | ||
| 1146 | 1148 | EnabledDebugList enabled_debug_list_; | |
| 1147 | 1149 | ||
| 1150 | + std::vector<v8::Global<v8::Context>> contexts_; | ||
| 1148 | 1151 | std::list<node_module> extra_linked_bindings_; | |
| 1149 | 1152 | Mutex extra_linked_bindings_mutex_; | |
| 1150 | 1153 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -164,8 +164,7 @@ ContextifyContext::~ContextifyContext() { | |||
| 164 | 164 | Isolate* isolate = env()->isolate(); | |
| 165 | 165 | HandleScope scope(isolate); | |
| 166 | 166 | ||
| 167 | - env()->async_hooks() | ||
| 168 | - ->RemoveContext(PersistentToLocal::Weak(isolate, context_)); | ||
| 167 | + env()->UntrackContext(PersistentToLocal::Weak(isolate, context_)); | ||
| 169 | 168 | context_.Reset(); | |
| 170 | 169 | } | |
| 171 | 170 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments