GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — src: cut small costs in node::MakeCallback · gabylb/node@3cbedbc

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • api
      • callback.cc
    • async_context_frame.cc
    • env.cc
    • env.h
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/api/callback.cc‎

Copy file name to clipboardExpand all lines: src/api/callback.cc
+10-1Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "node.h"
55
#include "v8.h"
66

7+
#include <optional>
8+
79
namespace node {
810

911
using v8::Context;
@@ -348,7 +350,14 @@ MaybeLocal<Value> InternalMakeCallback(Isolate* isolate,
348350
}
349351
Environment* env = Environment::GetCurrent(context);
350352
CHECK_NOT_NULL(env);
351-
Context::Scope context_scope(env->context());
353+
// Entering the context changes nothing when it is already the entered and
354+
// the current one, which is the common case for addons.
355+
Local<Context> env_context = env->context();
356+
std::optional<Context::Scope> context_scope;
357+
if (isolate->GetEnteredOrMicrotaskContext() != env_context ||
358+
isolate->GetCurrentContext() != env_context) {
359+
context_scope.emplace(env_context);
360+
}
352361
MaybeLocal<Value> ret = InternalMakeCallback(
353362
env, recv, recv, callback, argc, argv, asyncContext, context_frame);
354363
if (ret.IsEmpty() && env->async_callback_scope_depth() == 0) {
Collapse file

‎src/async_context_frame.cc‎

Copy file name to clipboardExpand all lines: src/async_context_frame.cc
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ void set(Isolate* isolate, Local<Value> value) {
4949
// but sometimes (such as enterWith) a direct exchange is needed.
5050
Local<Value> exchange(Environment* env, Local<Value> value) {
5151
auto prior = current(env->isolate());
52-
set(env, value);
52+
// Usually the frame does not change, and there is nothing to set.
53+
if (prior != value) set(env, value);
5354
return prior;
5455
}
5556

Collapse file

‎src/env.cc‎

Copy file name to clipboardExpand all lines: src/env.cc
+12-4Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void Environment::ResetPromiseHooks(Local<Function> init,
133133
void AsyncHooks::push_async_context(
134134
double async_id,
135135
double trigger_async_id,
136-
std::variant<Local<Object>*, Global<Object>*> resource) {
136+
const std::variant<Local<Object>*, Global<Object>*>& resource) {
137137
std::visit([](auto* ptr) { CHECK_IMPLIES(ptr != nullptr, !ptr->IsEmpty()); },
138138
resource);
139139

@@ -161,9 +161,13 @@ void AsyncHooks::push_async_context(
161161
// False positive: https://github.com/cpplint/cpplint/issues/410
162162
// NOLINTNEXTLINE(whitespace/newline)
163163
if (std::visit([](auto* ptr) { return ptr != nullptr; }, resource)) {
164-
native_execution_async_resources_.resize(offset + 1);
165164
// Caveat: This is a v8::Local<>* assignment, we do not keep a v8::Global<>!
166-
native_execution_async_resources_[offset] = resource;
165+
if (native_execution_async_resources_.size() == offset) {
166+
native_execution_async_resources_.push_back(resource);
167+
} else {
168+
native_execution_async_resources_.resize(offset + 1);
169+
native_execution_async_resources_[offset] = resource;
170+
}
167171
}
168172
}
169173

@@ -196,7 +200,11 @@ bool AsyncHooks::pop_async_context(double async_id) {
196200
native_execution_async_resources_[i]);
197201
}
198202
#endif
199-
native_execution_async_resources_.resize(offset);
203+
if (native_execution_async_resources_.size() == offset + 1) {
204+
native_execution_async_resources_.pop_back();
205+
} else {
206+
native_execution_async_resources_.resize(offset);
207+
}
200208
native_execution_async_resources_.shrink_to_fit();
201209
}
202210

Collapse file

‎src/env.h‎

Copy file name to clipboardExpand all lines: src/env.h
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ class AsyncHooks : public MemoryRetainer {
433433
void push_async_context(
434434
double async_id,
435435
double trigger_async_id,
436-
std::variant<v8::Local<v8::Object>*, v8::Global<v8::Object>*>
436+
const std::variant<v8::Local<v8::Object>*, v8::Global<v8::Object>*>&
437437
execution_async_resource);
438438
bool pop_async_context(double async_id);
439439
void clear_async_id_stack(); // Used in fatal exceptions.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.