Skip to content

Commit e076b5d

Browse files
committed
src: use Environment* as the first arg of CallbackScope
Since we are using the first argument, an Isolate*, to figure out the current Environment* again, we should pass the Environment* directly. Signed-off-by: Darshan Sen <[email protected]>
1 parent ca19775 commit e076b5d

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

src/api/async_resource.cc

+1-3
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ async_id AsyncResource::get_trigger_async_id() const {
6262
return async_context_.trigger_async_id;
6363
}
6464

65-
// TODO(addaleax): We shouldn’t need to use env_->isolate() if we’re just going
66-
// to end up using the Isolate* to figure out the Environment* again.
6765
AsyncResource::CallbackScope::CallbackScope(AsyncResource* res)
68-
: node::CallbackScope(res->env_->isolate(),
66+
: node::CallbackScope(res->env_,
6967
res->resource_.Get(res->env_->isolate()),
7068
res->async_context_) {}
7169

src/api/callback.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ using v8::Object;
1616
using v8::String;
1717
using v8::Value;
1818

19-
CallbackScope::CallbackScope(Isolate* isolate,
19+
CallbackScope::CallbackScope(Environment* env,
2020
Local<Object> object,
2121
async_context asyncContext)
22-
: private_(new InternalCallbackScope(Environment::GetCurrent(isolate),
22+
: private_(new InternalCallbackScope(env,
2323
object,
2424
asyncContext)),
25-
try_catch_(isolate) {
25+
try_catch_(env->isolate()) {
2626
try_catch_.SetVerbose(true);
2727
}
2828

src/node.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ class InternalCallbackScope;
10021002
*/
10031003
class NODE_EXTERN CallbackScope {
10041004
public:
1005-
CallbackScope(v8::Isolate* isolate,
1005+
CallbackScope(Environment* env,
10061006
v8::Local<v8::Object> resource,
10071007
async_context asyncContext);
10081008
~CallbackScope();

src/node_api.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ class AsyncContext {
539539
class CallbackScope : public node::CallbackScope {
540540
public:
541541
explicit CallbackScope(AsyncContext* async_context)
542-
: node::CallbackScope(async_context->node_env()->isolate(),
542+
: node::CallbackScope(async_context->node_env(),
543543
async_context->resource_.Get(
544544
async_context->node_env()->isolate()),
545545
async_context->async_context()) {}

test/addons/callback-scope/binding.cc

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#include "node.h"
2-
#include "v8.h"
32
#include "uv.h"
3+
#include "v8.h"
44

55
#include <assert.h>
6-
#include <vector>
76
#include <memory>
7+
#include <vector>
88

99
namespace {
1010

1111
void RunInCallbackScope(const v8::FunctionCallbackInfo<v8::Value>& args) {
1212
v8::Isolate* isolate = args.GetIsolate();
13+
v8::Local<v8::Context> context = isolate->GetCurrentContext();
14+
node::Environment* env = node::GetCurrentEnvironment(context);
1315

1416
assert(args.Length() == 4);
1517
assert(args[0]->IsObject());
@@ -22,11 +24,11 @@ void RunInCallbackScope(const v8::FunctionCallbackInfo<v8::Value>& args) {
2224
args[2].As<v8::Number>()->Value()
2325
};
2426

25-
node::CallbackScope scope(isolate, args[0].As<v8::Object>(), asyncContext);
27+
node::CallbackScope scope(env, args[0].As<v8::Object>(), asyncContext);
2628
v8::Local<v8::Function> fn = args[3].As<v8::Function>();
2729

2830
v8::MaybeLocal<v8::Value> ret =
29-
fn->Call(isolate->GetCurrentContext(), args[0], 0, nullptr);
31+
fn->Call(context, args[0], 0, nullptr);
3032

3133
if (!ret.IsEmpty())
3234
args.GetReturnValue().Set(ret.ToLocalChecked());
@@ -35,12 +37,14 @@ void RunInCallbackScope(const v8::FunctionCallbackInfo<v8::Value>& args) {
3537
static void Callback(uv_work_t* req, int ignored) {
3638
v8::Isolate* isolate = v8::Isolate::GetCurrent();
3739
v8::HandleScope scope(isolate);
38-
node::CallbackScope callback_scope(isolate, v8::Object::New(isolate),
40+
v8::Local<v8::Context> context = isolate->GetCurrentContext();
41+
node::Environment* env = node::GetCurrentEnvironment(context);
42+
node::CallbackScope callback_scope(env, v8::Object::New(isolate),
3943
node::async_context{0, 0});
4044
std::unique_ptr<v8::Global<v8::Promise::Resolver>> persistent {
4145
static_cast<v8::Global<v8::Promise::Resolver>*>(req->data) };
4246
v8::Local<v8::Promise::Resolver> local = persistent->Get(isolate);
43-
local->Resolve(isolate->GetCurrentContext(),
47+
local->Resolve(context,
4448
v8::Undefined(isolate)).ToChecked();
4549
delete req;
4650
}

0 commit comments

Comments
 (0)