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 · nodejs/node@14cb1f5

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
@@ -53,7 +53,8 @@ void set(Isolate* isolate, Local<Value> value) {
5353
// but sometimes (such as enterWith) a direct exchange is needed.
5454
Local<Value> exchange(Environment* env, Local<Value> value) {
5555
auto prior = current(env->isolate());
56-
set(env, value);
56+
// Usually the frame does not change, and there is nothing to set.
57+
if (prior != value) set(env, value);
5758
return prior;
5859
}
5960

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
@@ -132,7 +132,7 @@ void Environment::ResetPromiseHooks(Local<Function> init,
132132
void AsyncHooks::push_async_context(
133133
double async_id,
134134
double trigger_async_id,
135-
std::variant<Local<Object>*, Global<Object>*> resource) {
135+
const std::variant<Local<Object>*, Global<Object>*>& resource) {
136136
std::visit([](auto* ptr) { CHECK_IMPLIES(ptr != nullptr, !ptr->IsEmpty()); },
137137
resource);
138138

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

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

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.