src: cache and resue isolate and contex pointers

Many places these values are obtained through `env` pointer that
incurs function calls. Source in once and re-use as much as possible.
There are more of this pattern in this file, but those are either
one-time use or used in conditional blocks which means sourcing-in
those data early may not be beneficial.

PR-URL: https://github.com/nodejs/node/pull/23024
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
This commit is contained in:
Gireesh Punathil 2018-09-22 10:09:23 -04:00 committed by Daniel Bevenius
parent f004936a13
commit ea3bb9add2

View File

@ -191,24 +191,27 @@ inline void FileHandle::Close() {
} }
void FileHandle::CloseReq::Resolve() { void FileHandle::CloseReq::Resolve() {
HandleScope scope(env()->isolate()); Isolate* isolate = env()->isolate();
HandleScope scope(isolate);
InternalCallbackScope callback_scope(this); InternalCallbackScope callback_scope(this);
Local<Promise> promise = promise_.Get(env()->isolate()); Local<Promise> promise = promise_.Get(isolate);
Local<Promise::Resolver> resolver = promise.As<Promise::Resolver>(); Local<Promise::Resolver> resolver = promise.As<Promise::Resolver>();
resolver->Resolve(env()->context(), Undefined(env()->isolate())).FromJust(); resolver->Resolve(env()->context(), Undefined(isolate)).FromJust();
} }
void FileHandle::CloseReq::Reject(Local<Value> reason) { void FileHandle::CloseReq::Reject(Local<Value> reason) {
HandleScope scope(env()->isolate()); Isolate* isolate = env()->isolate();
HandleScope scope(isolate);
InternalCallbackScope callback_scope(this); InternalCallbackScope callback_scope(this);
Local<Promise> promise = promise_.Get(env()->isolate()); Local<Promise> promise = promise_.Get(isolate);
Local<Promise::Resolver> resolver = promise.As<Promise::Resolver>(); Local<Promise::Resolver> resolver = promise.As<Promise::Resolver>();
resolver->Reject(env()->context(), reason).FromJust(); resolver->Reject(env()->context(), reason).FromJust();
} }
FileHandle* FileHandle::CloseReq::file_handle() { FileHandle* FileHandle::CloseReq::file_handle() {
HandleScope scope(env()->isolate()); Isolate* isolate = env()->isolate();
Local<Value> val = ref_.Get(env()->isolate()); HandleScope scope(isolate);
Local<Value> val = ref_.Get(isolate);
Local<Object> obj = val.As<Object>(); Local<Object> obj = val.As<Object>();
return Unwrap<FileHandle>(obj); return Unwrap<FileHandle>(obj);
} }
@ -621,13 +624,14 @@ void AfterScanDirWithTypes(uv_fs_t* req) {
} }
Environment* env = req_wrap->env(); Environment* env = req_wrap->env();
Isolate* isolate = env->isolate();
Local<Value> error; Local<Value> error;
int r; int r;
Local<Array> names = Array::New(env->isolate(), 0); Local<Array> names = Array::New(isolate, 0);
Local<Function> fn = env->push_values_to_array_function(); Local<Function> fn = env->push_values_to_array_function();
Local<Value> name_argv[NODE_PUSH_VAL_TO_ARRAY_MAX]; Local<Value> name_argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
size_t name_idx = 0; size_t name_idx = 0;
Local<Value> types = Array::New(env->isolate(), 0); Local<Value> types = Array::New(isolate, 0);
Local<Value> type_argv[NODE_PUSH_VAL_TO_ARRAY_MAX]; Local<Value> type_argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
size_t type_idx = 0; size_t type_idx = 0;
@ -644,7 +648,7 @@ void AfterScanDirWithTypes(uv_fs_t* req) {
} }
MaybeLocal<Value> filename = MaybeLocal<Value> filename =
StringBytes::Encode(env->isolate(), StringBytes::Encode(isolate,
ent.name, ent.name,
req_wrap->encoding(), req_wrap->encoding(),
&error); &error);
@ -662,7 +666,7 @@ void AfterScanDirWithTypes(uv_fs_t* req) {
name_idx = 0; name_idx = 0;
} }
type_argv[type_idx++] = Integer::New(env->isolate(), ent.type); type_argv[type_idx++] = Integer::New(isolate, ent.type);
if (type_idx >= arraysize(type_argv)) { if (type_idx >= arraysize(type_argv)) {
MaybeLocal<Value> ret = fn->Call(env->context(), types, type_idx, MaybeLocal<Value> ret = fn->Call(env->context(), types, type_idx,
@ -690,7 +694,7 @@ void AfterScanDirWithTypes(uv_fs_t* req) {
} }
} }
Local<Array> result = Array::New(env->isolate(), 2); Local<Array> result = Array::New(isolate, 2);
result->Set(0, names); result->Set(0, names);
result->Set(1, types); result->Set(1, types);
req_wrap->Resolve(result); req_wrap->Resolve(result);
@ -783,7 +787,8 @@ inline FSReqBase* GetReqWrap(Environment* env, Local<Value> value,
void Access(const FunctionCallbackInfo<Value>& args) { void Access(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
HandleScope scope(env->isolate()); Isolate* isolate = env->isolate();
HandleScope scope(isolate);
const int argc = args.Length(); const int argc = args.Length();
CHECK_GE(argc, 2); CHECK_GE(argc, 2);
@ -791,7 +796,7 @@ void Access(const FunctionCallbackInfo<Value>& args) {
CHECK(args[1]->IsInt32()); CHECK(args[1]->IsInt32());
int mode = args[1].As<Int32>()->Value(); int mode = args[1].As<Int32>()->Value();
BufferValue path(env->isolate(), args[0]); BufferValue path(isolate, args[0]);
CHECK_NOT_NULL(*path); CHECK_NOT_NULL(*path);
FSReqBase* req_wrap_async = GetReqWrap(env, args[2]); FSReqBase* req_wrap_async = GetReqWrap(env, args[2]);
@ -836,10 +841,11 @@ void Close(const FunctionCallbackInfo<Value>& args) {
// in the file. // in the file.
static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) { static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
uv_loop_t* loop = env->event_loop(); uv_loop_t* loop = env->event_loop();
CHECK(args[0]->IsString()); CHECK(args[0]->IsString());
node::Utf8Value path(env->isolate(), args[0]); node::Utf8Value path(isolate, args[0]);
if (strlen(*path) != path.length()) if (strlen(*path) != path.length())
return; // Contains a nul byte. return; // Contains a nul byte.
@ -890,7 +896,7 @@ static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) {
return; return;
} else { } else {
Local<String> chars_string = Local<String> chars_string =
String::NewFromUtf8(env->isolate(), String::NewFromUtf8(isolate,
&chars[start], &chars[start],
v8::NewStringType::kNormal, v8::NewStringType::kNormal,
size).ToLocalChecked(); size).ToLocalChecked();
@ -1011,13 +1017,14 @@ static void FStat(const FunctionCallbackInfo<Value>& args) {
static void Symlink(const FunctionCallbackInfo<Value>& args) { static void Symlink(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
int argc = args.Length(); int argc = args.Length();
CHECK_GE(argc, 4); CHECK_GE(argc, 4);
BufferValue target(env->isolate(), args[0]); BufferValue target(isolate, args[0]);
CHECK_NOT_NULL(*target); CHECK_NOT_NULL(*target);
BufferValue path(env->isolate(), args[1]); BufferValue path(isolate, args[1]);
CHECK_NOT_NULL(*path); CHECK_NOT_NULL(*path);
CHECK(args[2]->IsInt32()); CHECK(args[2]->IsInt32());
@ -1039,14 +1046,15 @@ static void Symlink(const FunctionCallbackInfo<Value>& args) {
static void Link(const FunctionCallbackInfo<Value>& args) { static void Link(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
int argc = args.Length(); int argc = args.Length();
CHECK_GE(argc, 3); CHECK_GE(argc, 3);
BufferValue src(env->isolate(), args[0]); BufferValue src(isolate, args[0]);
CHECK_NOT_NULL(*src); CHECK_NOT_NULL(*src);
BufferValue dest(env->isolate(), args[1]); BufferValue dest(isolate, args[1]);
CHECK_NOT_NULL(*dest); CHECK_NOT_NULL(*dest);
FSReqBase* req_wrap_async = GetReqWrap(env, args[2]); FSReqBase* req_wrap_async = GetReqWrap(env, args[2]);
@ -1065,14 +1073,15 @@ static void Link(const FunctionCallbackInfo<Value>& args) {
static void ReadLink(const FunctionCallbackInfo<Value>& args) { static void ReadLink(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
int argc = args.Length(); int argc = args.Length();
CHECK_GE(argc, 3); CHECK_GE(argc, 3);
BufferValue path(env->isolate(), args[0]); BufferValue path(isolate, args[0]);
CHECK_NOT_NULL(*path); CHECK_NOT_NULL(*path);
const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8); const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
FSReqBase* req_wrap_async = GetReqWrap(env, args[2]); FSReqBase* req_wrap_async = GetReqWrap(env, args[2]);
if (req_wrap_async != nullptr) { // readlink(path, encoding, req) if (req_wrap_async != nullptr) { // readlink(path, encoding, req)
@ -1091,7 +1100,7 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
const char* link_path = static_cast<const char*>(req_wrap_sync.req.ptr); const char* link_path = static_cast<const char*>(req_wrap_sync.req.ptr);
Local<Value> error; Local<Value> error;
MaybeLocal<Value> rc = StringBytes::Encode(env->isolate(), MaybeLocal<Value> rc = StringBytes::Encode(isolate,
link_path, link_path,
encoding, encoding,
&error); &error);
@ -1107,13 +1116,14 @@ static void ReadLink(const FunctionCallbackInfo<Value>& args) {
static void Rename(const FunctionCallbackInfo<Value>& args) { static void Rename(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
int argc = args.Length(); int argc = args.Length();
CHECK_GE(argc, 3); CHECK_GE(argc, 3);
BufferValue old_path(env->isolate(), args[0]); BufferValue old_path(isolate, args[0]);
CHECK_NOT_NULL(*old_path); CHECK_NOT_NULL(*old_path);
BufferValue new_path(env->isolate(), args[1]); BufferValue new_path(isolate, args[1]);
CHECK_NOT_NULL(*new_path); CHECK_NOT_NULL(*new_path);
FSReqBase* req_wrap_async = GetReqWrap(env, args[2]); FSReqBase* req_wrap_async = GetReqWrap(env, args[2]);
@ -1408,14 +1418,15 @@ static void MKDir(const FunctionCallbackInfo<Value>& args) {
static void RealPath(const FunctionCallbackInfo<Value>& args) { static void RealPath(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
const int argc = args.Length(); const int argc = args.Length();
CHECK_GE(argc, 3); CHECK_GE(argc, 3);
BufferValue path(env->isolate(), args[0]); BufferValue path(isolate, args[0]);
CHECK_NOT_NULL(*path); CHECK_NOT_NULL(*path);
const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8); const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
FSReqBase* req_wrap_async = GetReqWrap(env, args[2]); FSReqBase* req_wrap_async = GetReqWrap(env, args[2]);
if (req_wrap_async != nullptr) { // realpath(path, encoding, req) if (req_wrap_async != nullptr) { // realpath(path, encoding, req)
@ -1435,7 +1446,7 @@ static void RealPath(const FunctionCallbackInfo<Value>& args) {
const char* link_path = static_cast<const char*>(req_wrap_sync.req.ptr); const char* link_path = static_cast<const char*>(req_wrap_sync.req.ptr);
Local<Value> error; Local<Value> error;
MaybeLocal<Value> rc = StringBytes::Encode(env->isolate(), MaybeLocal<Value> rc = StringBytes::Encode(isolate,
link_path, link_path,
encoding, encoding,
&error); &error);
@ -1451,14 +1462,15 @@ static void RealPath(const FunctionCallbackInfo<Value>& args) {
static void ReadDir(const FunctionCallbackInfo<Value>& args) { static void ReadDir(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
const int argc = args.Length(); const int argc = args.Length();
CHECK_GE(argc, 3); CHECK_GE(argc, 3);
BufferValue path(env->isolate(), args[0]); BufferValue path(isolate, args[0]);
CHECK_NOT_NULL(*path); CHECK_NOT_NULL(*path);
const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8); const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
bool with_types = args[2]->IsTrue(); bool with_types = args[2]->IsTrue();
@ -1484,7 +1496,7 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(req_wrap_sync.req.result, 0); CHECK_GE(req_wrap_sync.req.result, 0);
int r; int r;
Local<Array> names = Array::New(env->isolate(), 0); Local<Array> names = Array::New(isolate, 0);
Local<Function> fn = env->push_values_to_array_function(); Local<Function> fn = env->push_values_to_array_function();
Local<Value> name_v[NODE_PUSH_VAL_TO_ARRAY_MAX]; Local<Value> name_v[NODE_PUSH_VAL_TO_ARRAY_MAX];
size_t name_idx = 0; size_t name_idx = 0;
@ -1493,7 +1505,7 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
Local<Value> type_v[NODE_PUSH_VAL_TO_ARRAY_MAX]; Local<Value> type_v[NODE_PUSH_VAL_TO_ARRAY_MAX];
size_t type_idx; size_t type_idx;
if (with_types) { if (with_types) {
types = Array::New(env->isolate(), 0); types = Array::New(isolate, 0);
type_idx = 0; type_idx = 0;
} }
@ -1506,14 +1518,14 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
if (r != 0) { if (r != 0) {
Local<Object> ctx = args[4].As<Object>(); Local<Object> ctx = args[4].As<Object>();
ctx->Set(env->context(), env->errno_string(), ctx->Set(env->context(), env->errno_string(),
Integer::New(env->isolate(), r)).FromJust(); Integer::New(isolate, r)).FromJust();
ctx->Set(env->context(), env->syscall_string(), ctx->Set(env->context(), env->syscall_string(),
OneByteString(env->isolate(), "readdir")).FromJust(); OneByteString(isolate, "readdir")).FromJust();
return; return;
} }
Local<Value> error; Local<Value> error;
MaybeLocal<Value> filename = StringBytes::Encode(env->isolate(), MaybeLocal<Value> filename = StringBytes::Encode(isolate,
ent.name, ent.name,
encoding, encoding,
&error); &error);
@ -1536,7 +1548,7 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
} }
if (with_types) { if (with_types) {
type_v[type_idx++] = Integer::New(env->isolate(), ent.type); type_v[type_idx++] = Integer::New(isolate, ent.type);
if (type_idx >= arraysize(type_v)) { if (type_idx >= arraysize(type_v)) {
MaybeLocal<Value> ret = fn->Call(env->context(), types, type_idx, MaybeLocal<Value> ret = fn->Call(env->context(), types, type_idx,
@ -1564,7 +1576,7 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
} }
if (with_types) { if (with_types) {
Local<Array> result = Array::New(env->isolate(), 2); Local<Array> result = Array::New(isolate, 2);
result->Set(0, names); result->Set(0, names);
result->Set(1, types); result->Set(1, types);
args.GetReturnValue().Set(result); args.GetReturnValue().Set(result);
@ -1606,11 +1618,12 @@ static void Open(const FunctionCallbackInfo<Value>& args) {
static void OpenFileHandle(const FunctionCallbackInfo<Value>& args) { static void OpenFileHandle(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
const int argc = args.Length(); const int argc = args.Length();
CHECK_GE(argc, 3); CHECK_GE(argc, 3);
BufferValue path(env->isolate(), args[0]); BufferValue path(isolate, args[0]);
CHECK_NOT_NULL(*path); CHECK_NOT_NULL(*path);
CHECK(args[1]->IsInt32()); CHECK(args[1]->IsInt32());
@ -1633,7 +1646,7 @@ static void OpenFileHandle(const FunctionCallbackInfo<Value>& args) {
if (result < 0) { if (result < 0) {
return; // syscall failed, no need to continue, error info is in ctx return; // syscall failed, no need to continue, error info is in ctx
} }
HandleScope scope(env->isolate()); HandleScope scope(isolate);
FileHandle* fd = new FileHandle(env, result); FileHandle* fd = new FileHandle(env, result);
args.GetReturnValue().Set(fd->object()); args.GetReturnValue().Set(fd->object());
} }
@ -1641,14 +1654,15 @@ static void OpenFileHandle(const FunctionCallbackInfo<Value>& args) {
static void CopyFile(const FunctionCallbackInfo<Value>& args) { static void CopyFile(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
const int argc = args.Length(); const int argc = args.Length();
CHECK_GE(argc, 3); CHECK_GE(argc, 3);
BufferValue src(env->isolate(), args[0]); BufferValue src(isolate, args[0]);
CHECK_NOT_NULL(*src); CHECK_NOT_NULL(*src);
BufferValue dest(env->isolate(), args[1]); BufferValue dest(isolate, args[1]);
CHECK_NOT_NULL(*dest); CHECK_NOT_NULL(*dest);
CHECK(args[2]->IsInt32()); CHECK(args[2]->IsInt32());
@ -1779,6 +1793,7 @@ static void WriteBuffers(const FunctionCallbackInfo<Value>& args) {
// 3 enc encoding of string // 3 enc encoding of string
static void WriteString(const FunctionCallbackInfo<Value>& args) { static void WriteString(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
const int argc = args.Length(); const int argc = args.Length();
CHECK_GE(argc, 4); CHECK_GE(argc, 4);
@ -1788,7 +1803,7 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
const int64_t pos = GET_OFFSET(args[2]); const int64_t pos = GET_OFFSET(args[2]);
const auto enc = ParseEncoding(env->isolate(), args[3], UTF8); const auto enc = ParseEncoding(isolate, args[3], UTF8);
Local<Value> value = args[1]; Local<Value> value = args[1];
char* buf = nullptr; char* buf = nullptr;
@ -1819,12 +1834,12 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
if (is_async) { // write(fd, string, pos, enc, req) if (is_async) { // write(fd, string, pos, enc, req)
CHECK_NOT_NULL(req_wrap_async); CHECK_NOT_NULL(req_wrap_async);
if (!StringBytes::StorageSize(env->isolate(), value, enc).To(&len)) return; if (!StringBytes::StorageSize(isolate, value, enc).To(&len)) return;
FSReqBase::FSReqBuffer& stack_buffer = FSReqBase::FSReqBuffer& stack_buffer =
req_wrap_async->Init("write", len, enc); req_wrap_async->Init("write", len, enc);
// StorageSize may return too large a char, so correct the actual length // StorageSize may return too large a char, so correct the actual length
// by the write size // by the write size
len = StringBytes::Write(env->isolate(), *stack_buffer, len, args[1], enc); len = StringBytes::Write(isolate, *stack_buffer, len, args[1], enc);
stack_buffer.SetLengthAndZeroTerminate(len); stack_buffer.SetLengthAndZeroTerminate(len);
uv_buf_t uvbuf = uv_buf_init(*stack_buffer, len); uv_buf_t uvbuf = uv_buf_init(*stack_buffer, len);
int err = req_wrap_async->Dispatch(uv_fs_write, int err = req_wrap_async->Dispatch(uv_fs_write,
@ -1847,12 +1862,12 @@ static void WriteString(const FunctionCallbackInfo<Value>& args) {
FSReqWrapSync req_wrap_sync; FSReqWrapSync req_wrap_sync;
FSReqBase::FSReqBuffer stack_buffer; FSReqBase::FSReqBuffer stack_buffer;
if (buf == nullptr) { if (buf == nullptr) {
if (!StringBytes::StorageSize(env->isolate(), value, enc).To(&len)) if (!StringBytes::StorageSize(isolate, value, enc).To(&len))
return; return;
stack_buffer.AllocateSufficientStorage(len + 1); stack_buffer.AllocateSufficientStorage(len + 1);
// StorageSize may return too large a char, so correct the actual length // StorageSize may return too large a char, so correct the actual length
// by the write size // by the write size
len = StringBytes::Write(env->isolate(), *stack_buffer, len = StringBytes::Write(isolate, *stack_buffer,
len, args[1], enc); len, args[1], enc);
stack_buffer.SetLengthAndZeroTerminate(len); stack_buffer.SetLengthAndZeroTerminate(len);
buf = *stack_buffer; buf = *stack_buffer;
@ -2138,14 +2153,15 @@ static void FUTimes(const FunctionCallbackInfo<Value>& args) {
static void Mkdtemp(const FunctionCallbackInfo<Value>& args) { static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
const int argc = args.Length(); const int argc = args.Length();
CHECK_GE(argc, 2); CHECK_GE(argc, 2);
BufferValue tmpl(env->isolate(), args[0]); BufferValue tmpl(isolate, args[0]);
CHECK_NOT_NULL(*tmpl); CHECK_NOT_NULL(*tmpl);
const enum encoding encoding = ParseEncoding(env->isolate(), args[1], UTF8); const enum encoding encoding = ParseEncoding(isolate, args[1], UTF8);
FSReqBase* req_wrap_async = GetReqWrap(env, args[2]); FSReqBase* req_wrap_async = GetReqWrap(env, args[2]);
if (req_wrap_async != nullptr) { // mkdtemp(tmpl, encoding, req) if (req_wrap_async != nullptr) { // mkdtemp(tmpl, encoding, req)
@ -2162,7 +2178,7 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
Local<Value> error; Local<Value> error;
MaybeLocal<Value> rc = MaybeLocal<Value> rc =
StringBytes::Encode(env->isolate(), path, encoding, &error); StringBytes::Encode(isolate, path, encoding, &error);
if (rc.IsEmpty()) { if (rc.IsEmpty()) {
Local<Object> ctx = args[3].As<Object>(); Local<Object> ctx = args[3].As<Object>();
ctx->Set(env->context(), env->error_string(), error).FromJust(); ctx->Set(env->context(), env->error_string(), error).FromJust();
@ -2177,6 +2193,7 @@ void Initialize(Local<Object> target,
Local<Context> context, Local<Context> context,
void* priv) { void* priv) {
Environment* env = Environment::GetCurrent(context); Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();
env->SetMethod(target, "access", Access); env->SetMethod(target, "access", Access);
env->SetMethod(target, "close", Close); env->SetMethod(target, "close", Close);
@ -2218,17 +2235,17 @@ void Initialize(Local<Object> target,
env->SetMethod(target, "mkdtemp", Mkdtemp); env->SetMethod(target, "mkdtemp", Mkdtemp);
target->Set(env->context(), target->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "kFsStatsFieldsLength"), FIXED_ONE_BYTE_STRING(isolate, "kFsStatsFieldsLength"),
Integer::New(env->isolate(), env->kFsStatsFieldsLength)) Integer::New(isolate, env->kFsStatsFieldsLength))
.FromJust(); .FromJust();
target->Set(context, target->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "statValues"), FIXED_ONE_BYTE_STRING(isolate, "statValues"),
env->fs_stats_field_array()->GetJSArray()).FromJust(); env->fs_stats_field_array()->GetJSArray()).FromJust();
target->Set(context, target->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "bigintStatValues"), FIXED_ONE_BYTE_STRING(isolate, "bigintStatValues"),
env->fs_stats_field_bigint_array()->GetJSArray()).FromJust(); env->fs_stats_field_bigint_array()->GetJSArray()).FromJust();
StatWatcher::Initialize(env, target); StatWatcher::Initialize(env, target);
@ -2238,7 +2255,7 @@ void Initialize(Local<Object> target,
fst->InstanceTemplate()->SetInternalFieldCount(1); fst->InstanceTemplate()->SetInternalFieldCount(1);
AsyncWrap::AddWrapMethods(env, fst); AsyncWrap::AddWrapMethods(env, fst);
Local<String> wrapString = Local<String> wrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqCallback"); FIXED_ONE_BYTE_STRING(isolate, "FSReqCallback");
fst->SetClassName(wrapString); fst->SetClassName(wrapString);
target target
->Set(context, wrapString, ->Set(context, wrapString,
@ -2247,20 +2264,20 @@ void Initialize(Local<Object> target,
// Create FunctionTemplate for FileHandleReadWrap. Theres no need // Create FunctionTemplate for FileHandleReadWrap. Theres no need
// to do anything in the constructor, so we only store the instance template. // to do anything in the constructor, so we only store the instance template.
Local<FunctionTemplate> fh_rw = FunctionTemplate::New(env->isolate()); Local<FunctionTemplate> fh_rw = FunctionTemplate::New(isolate);
fh_rw->InstanceTemplate()->SetInternalFieldCount(1); fh_rw->InstanceTemplate()->SetInternalFieldCount(1);
AsyncWrap::AddWrapMethods(env, fh_rw); AsyncWrap::AddWrapMethods(env, fh_rw);
Local<String> fhWrapString = Local<String> fhWrapString =
FIXED_ONE_BYTE_STRING(env->isolate(), "FileHandleReqWrap"); FIXED_ONE_BYTE_STRING(isolate, "FileHandleReqWrap");
fh_rw->SetClassName(fhWrapString); fh_rw->SetClassName(fhWrapString);
env->set_filehandlereadwrap_template( env->set_filehandlereadwrap_template(
fst->InstanceTemplate()); fst->InstanceTemplate());
// Create Function Template for FSReqPromise // Create Function Template for FSReqPromise
Local<FunctionTemplate> fpt = FunctionTemplate::New(env->isolate()); Local<FunctionTemplate> fpt = FunctionTemplate::New(isolate);
AsyncWrap::AddWrapMethods(env, fpt); AsyncWrap::AddWrapMethods(env, fpt);
Local<String> promiseString = Local<String> promiseString =
FIXED_ONE_BYTE_STRING(env->isolate(), "FSReqPromise"); FIXED_ONE_BYTE_STRING(isolate, "FSReqPromise");
fpt->SetClassName(promiseString); fpt->SetClassName(promiseString);
Local<ObjectTemplate> fpo = fpt->InstanceTemplate(); Local<ObjectTemplate> fpo = fpt->InstanceTemplate();
fpo->SetInternalFieldCount(1); fpo->SetInternalFieldCount(1);
@ -2274,7 +2291,7 @@ void Initialize(Local<Object> target,
Local<ObjectTemplate> fdt = fd->InstanceTemplate(); Local<ObjectTemplate> fdt = fd->InstanceTemplate();
fdt->SetInternalFieldCount(1); fdt->SetInternalFieldCount(1);
Local<String> handleString = Local<String> handleString =
FIXED_ONE_BYTE_STRING(env->isolate(), "FileHandle"); FIXED_ONE_BYTE_STRING(isolate, "FileHandle");
fd->SetClassName(handleString); fd->SetClassName(handleString);
StreamBase::AddMethods<FileHandle>(env, fd); StreamBase::AddMethods<FileHandle>(env, fd);
target target
@ -2284,8 +2301,8 @@ void Initialize(Local<Object> target,
env->set_fd_constructor_template(fdt); env->set_fd_constructor_template(fdt);
// Create FunctionTemplate for FileHandle::CloseReq // Create FunctionTemplate for FileHandle::CloseReq
Local<FunctionTemplate> fdclose = FunctionTemplate::New(env->isolate()); Local<FunctionTemplate> fdclose = FunctionTemplate::New(isolate);
fdclose->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), fdclose->SetClassName(FIXED_ONE_BYTE_STRING(isolate,
"FileHandleCloseReq")); "FileHandleCloseReq"));
AsyncWrap::AddWrapMethods(env, fdclose); AsyncWrap::AddWrapMethods(env, fdclose);
Local<ObjectTemplate> fdcloset = fdclose->InstanceTemplate(); Local<ObjectTemplate> fdcloset = fdclose->InstanceTemplate();
@ -2293,11 +2310,11 @@ void Initialize(Local<Object> target,
env->set_fdclose_constructor_template(fdcloset); env->set_fdclose_constructor_template(fdcloset);
Local<Symbol> use_promises_symbol = Local<Symbol> use_promises_symbol =
Symbol::New(env->isolate(), Symbol::New(isolate,
FIXED_ONE_BYTE_STRING(env->isolate(), "use promises")); FIXED_ONE_BYTE_STRING(isolate, "use promises"));
env->set_fs_use_promises_symbol(use_promises_symbol); env->set_fs_use_promises_symbol(use_promises_symbol);
target->Set(env->context(), target->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "kUsePromises"), FIXED_ONE_BYTE_STRING(isolate, "kUsePromises"),
use_promises_symbol).FromJust(); use_promises_symbol).FromJust();
} }