From 75adde07f9a2de7f38a67bec72bd377d450bdb52 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Fri, 21 Feb 2014 17:02:42 +0400 Subject: [PATCH] src: remove `node_isolate` from source fix #6899 --- src/cares_wrap.cc | 148 +++++----- src/env-inl.h | 52 ++++ src/env.h | 77 +++++ src/fs_event_wrap.cc | 22 +- src/handle_wrap.cc | 15 +- src/node.cc | 517 +++++++++++++++++--------------- src/node.h | 100 ++++++- src/node_buffer.cc | 121 ++++---- src/node_buffer.h | 40 ++- src/node_contextify.cc | 118 ++++---- src/node_counters.cc | 8 +- src/node_counters.h | 3 +- src/node_crypto.cc | 624 ++++++++++++++++++++++----------------- src/node_crypto.h | 6 +- src/node_dtrace.cc | 66 +++-- src/node_dtrace.h | 3 +- src/node_file.cc | 130 ++++---- src/node_http_parser.cc | 102 +++---- src/node_internals.h | 71 +++-- src/node_javascript.cc | 14 +- src/node_javascript.h | 5 +- src/node_os.cc | 104 ++++--- src/node_stat_watcher.cc | 13 +- src/node_stat_watcher.h | 2 +- src/node_wrap.h | 2 +- src/node_zlib.cc | 60 ++-- src/pipe_wrap.cc | 22 +- src/process_wrap.cc | 69 ++--- src/signal_wrap.cc | 11 +- src/smalloc.cc | 95 +++--- src/smalloc.h | 17 +- src/spawn_sync.cc | 11 +- src/spawn_sync.h | 4 +- src/stream_wrap.cc | 56 ++-- src/string_bytes.cc | 98 +++--- src/string_bytes.h | 67 ++++- src/tcp_wrap.cc | 47 +-- src/timer_wrap.cc | 26 +- src/tls_wrap.cc | 60 ++-- src/tty_wrap.cc | 24 +- src/udp_wrap.cc | 27 +- src/uv.cc | 16 +- 42 files changed, 1841 insertions(+), 1232 deletions(-) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 558d6d5872..567b159807 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -193,14 +193,14 @@ static void ares_sockstate_cb(void* data, } -static Local HostentToAddresses(struct hostent* host) { - HandleScope scope(node_isolate); +static Local HostentToAddresses(Environment* env, struct hostent* host) { + HandleScope scope(env->isolate()); Local addresses = Array::New(); char ip[INET6_ADDRSTRLEN]; for (uint32_t i = 0; host->h_addr_list[i] != NULL; ++i) { uv_inet_ntop(host->h_addrtype, host->h_addr_list[i], ip, sizeof(ip)); - Local address = OneByteString(node_isolate, ip); + Local address = OneByteString(env->isolate(), ip); addresses->Set(i, address); } @@ -208,12 +208,12 @@ static Local HostentToAddresses(struct hostent* host) { } -static Local HostentToNames(struct hostent* host) { - HandleScope scope(node_isolate); +static Local HostentToNames(Environment* env, struct hostent* host) { + HandleScope scope(env->isolate()); Local names = Array::New(); for (uint32_t i = 0; host->h_aliases[i] != NULL; ++i) { - Local address = OneByteString(node_isolate, host->h_aliases[i]); + Local address = OneByteString(env->isolate(), host->h_aliases[i]); names->Set(i, address); } @@ -377,7 +377,7 @@ class QueryAWrap: public QueryWrap { return; } - Local addresses = HostentToAddresses(host); + Local addresses = HostentToAddresses(env(), host); ares_free_hostent(host); this->CallOnComplete(addresses); @@ -414,7 +414,7 @@ class QueryAaaaWrap: public QueryWrap { return; } - Local addresses = HostentToAddresses(host); + Local addresses = HostentToAddresses(env(), host); ares_free_hostent(host); this->CallOnComplete(addresses); @@ -453,7 +453,7 @@ class QueryCnameWrap: public QueryWrap { // A cname lookup always returns a single record but we follow the // common API here. Local result = Array::New(1); - result->Set(0, OneByteString(node_isolate, host->h_name)); + result->Set(0, OneByteString(env()->isolate(), host->h_name)); ares_free_hostent(host); this->CallOnComplete(result); @@ -490,18 +490,16 @@ class QueryMxWrap: public QueryWrap { } Local mx_records = Array::New(); - Local exchange_symbol = - FIXED_ONE_BYTE_STRING(node_isolate, "exchange"); - Local priority_symbol = - FIXED_ONE_BYTE_STRING(node_isolate, "priority"); + Local exchange_symbol = env()->exchange_string(); + Local priority_symbol = env()->priority_string(); ares_mx_reply* current = mx_start; for (uint32_t i = 0; current != NULL; ++i, current = current->next) { Local mx_record = Object::New(); mx_record->Set(exchange_symbol, - OneByteString(node_isolate, current->host)); + OneByteString(env()->isolate(), current->host)); mx_record->Set(priority_symbol, - Integer::New(current->priority, node_isolate)); + Integer::New(current->priority, env()->isolate())); mx_records->Set(i, mx_record); } @@ -540,7 +538,7 @@ class QueryNsWrap: public QueryWrap { return; } - Local names = HostentToNames(host); + Local names = HostentToNames(env(), host); ares_free_hostent(host); this->CallOnComplete(names); @@ -580,7 +578,7 @@ class QueryTxtWrap: public QueryWrap { ares_txt_reply* current = txt_out; for (uint32_t i = 0; current != NULL; ++i, current = current->next) { - Local txt = OneByteString(node_isolate, current->txt); + Local txt = OneByteString(env()->isolate(), current->txt); txt_records->Set(i, txt); } @@ -620,26 +618,22 @@ class QuerySrvWrap: public QueryWrap { } Local srv_records = Array::New(); - Local name_symbol = - FIXED_ONE_BYTE_STRING(node_isolate, "name"); - Local port_symbol = - FIXED_ONE_BYTE_STRING(node_isolate, "port"); - Local priority_symbol = - FIXED_ONE_BYTE_STRING(node_isolate, "priority"); - Local weight_symbol = - FIXED_ONE_BYTE_STRING(node_isolate, "weight"); + Local name_symbol = env()->name_string(); + Local port_symbol = env()->port_string(); + Local priority_symbol = env()->priority_string(); + Local weight_symbol = env()->weight_string(); ares_srv_reply* current = srv_start; for (uint32_t i = 0; current != NULL; ++i, current = current->next) { Local srv_record = Object::New(); srv_record->Set(name_symbol, - OneByteString(node_isolate, current->host)); + OneByteString(env()->isolate(), current->host)); srv_record->Set(port_symbol, - Integer::New(current->port, node_isolate)); + Integer::New(current->port, env()->isolate())); srv_record->Set(priority_symbol, - Integer::New(current->priority, node_isolate)); + Integer::New(current->priority, env()->isolate())); srv_record->Set(weight_symbol, - Integer::New(current->weight, node_isolate)); + Integer::New(current->weight, env()->isolate())); srv_records->Set(i, srv_record); } @@ -679,34 +673,28 @@ class QueryNaptrWrap: public QueryWrap { } Local naptr_records = Array::New(); - Local flags_symbol = - FIXED_ONE_BYTE_STRING(node_isolate, "flags"); - Local service_symbol = - FIXED_ONE_BYTE_STRING(node_isolate, "service"); - Local regexp_symbol = - FIXED_ONE_BYTE_STRING(node_isolate, "regexp"); - Local replacement_symbol = - FIXED_ONE_BYTE_STRING(node_isolate, "replacement"); - Local order_symbol = - FIXED_ONE_BYTE_STRING(node_isolate, "order"); - Local preference_symbol = - FIXED_ONE_BYTE_STRING(node_isolate, "preference"); + Local flags_symbol = env()->flags_string(); + Local service_symbol = env()->service_string(); + Local regexp_symbol = env()->regexp_string(); + Local replacement_symbol = env()->replacement_string(); + Local order_symbol = env()->order_string(); + Local preference_symbol = env()->preference_string(); ares_naptr_reply* current = naptr_start; for (uint32_t i = 0; current != NULL; ++i, current = current->next) { Local naptr_record = Object::New(); naptr_record->Set(flags_symbol, - OneByteString(node_isolate, current->flags)); + OneByteString(env()->isolate(), current->flags)); naptr_record->Set(service_symbol, - OneByteString(node_isolate, current->service)); + OneByteString(env()->isolate(), current->service)); naptr_record->Set(regexp_symbol, - OneByteString(node_isolate, current->regexp)); + OneByteString(env()->isolate(), current->regexp)); naptr_record->Set(replacement_symbol, - OneByteString(node_isolate, current->replacement)); + OneByteString(env()->isolate(), current->replacement)); naptr_record->Set(order_symbol, - Integer::New(current->order, node_isolate)); + Integer::New(current->order, env()->isolate())); naptr_record->Set(preference_symbol, - Integer::New(current->preference, node_isolate)); + Integer::New(current->preference, env()->isolate())); naptr_records->Set(i, naptr_record); } @@ -748,20 +736,20 @@ class QuerySoaWrap: public QueryWrap { Local soa_record = Object::New(); - soa_record->Set(FIXED_ONE_BYTE_STRING(node_isolate, "nsname"), - OneByteString(node_isolate, soa_out->nsname)); - soa_record->Set(FIXED_ONE_BYTE_STRING(node_isolate, "hostmaster"), - OneByteString(node_isolate, soa_out->hostmaster)); - soa_record->Set(FIXED_ONE_BYTE_STRING(node_isolate, "serial"), - Integer::New(soa_out->serial, node_isolate)); - soa_record->Set(FIXED_ONE_BYTE_STRING(node_isolate, "refresh"), - Integer::New(soa_out->refresh, node_isolate)); - soa_record->Set(FIXED_ONE_BYTE_STRING(node_isolate, "retry"), - Integer::New(soa_out->retry, node_isolate)); - soa_record->Set(FIXED_ONE_BYTE_STRING(node_isolate, "expire"), - Integer::New(soa_out->expire, node_isolate)); - soa_record->Set(FIXED_ONE_BYTE_STRING(node_isolate, "minttl"), - Integer::New(soa_out->minttl, node_isolate)); + soa_record->Set(env()->nsname_string(), + OneByteString(env()->isolate(), soa_out->nsname)); + soa_record->Set(env()->hostmaster_string(), + OneByteString(env()->isolate(), soa_out->hostmaster)); + soa_record->Set(env()->serial_string(), + Integer::New(soa_out->serial, env()->isolate())); + soa_record->Set(env()->refresh_string(), + Integer::New(soa_out->refresh, env()->isolate())); + soa_record->Set(env()->retry_string(), + Integer::New(soa_out->retry, env()->isolate())); + soa_record->Set(env()->expire_string(), + Integer::New(soa_out->expire, env()->isolate())); + soa_record->Set(env()->minttl_string(), + Integer::New(soa_out->minttl, env()->isolate())); ares_free_data(soa_out); @@ -803,7 +791,7 @@ class GetHostByAddrWrap: public QueryWrap { void Parse(struct hostent* host) { HandleScope handle_scope(env()->isolate()); Context::Scope context_scope(env()->context()); - this->CallOnComplete(HostentToNames(host)); + this->CallOnComplete(HostentToNames(env(), host)); } }; @@ -825,10 +813,10 @@ class GetHostByNameWrap: public QueryWrap { protected: void Parse(struct hostent* host) { - HandleScope scope(node_isolate); + HandleScope scope(env()->isolate()); - Local addresses = HostentToAddresses(host); - Local family = Integer::New(host->h_addrtype, node_isolate); + Local addresses = HostentToAddresses(env(), host); + Local family = Integer::New(host->h_addrtype, env()->isolate()); this->CallOnComplete(addresses, family); } @@ -865,8 +853,8 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { Context::Scope context_scope(env->context()); Local argv[] = { - Integer::New(status, node_isolate), - Null(node_isolate) + Integer::New(status, env->isolate()), + Null(env->isolate()) }; if (status == 0) { @@ -906,7 +894,7 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { continue; // Create JavaScript string - Local s = OneByteString(node_isolate, ip); + Local s = OneByteString(env->isolate(), ip); results->Set(n, s); n++; } @@ -933,7 +921,7 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { continue; // Create JavaScript string - Local s = OneByteString(node_isolate, ip); + Local s = OneByteString(env->isolate(), ip); results->Set(n, s); n++; } @@ -956,7 +944,8 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { static void IsIP(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); String::AsciiValue ip(args[0]); char address_buffer[sizeof(struct in6_addr)]; @@ -1041,7 +1030,7 @@ static void GetServers(const FunctionCallbackInfo& args) { int err = uv_inet_ntop(cur->family, caddr, ip, sizeof(ip)); assert(err == 0); - Local addr = OneByteString(node_isolate, ip); + Local addr = OneByteString(env->isolate(), ip); server_array->Set(i, addr); } @@ -1121,9 +1110,10 @@ static void SetServers(const FunctionCallbackInfo& args) { static void StrError(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); const char* errmsg = ares_strerror(args[0]->Int32Value()); - args.GetReturnValue().Set(OneByteString(node_isolate, errmsg)); + args.GetReturnValue().Set(OneByteString(env->isolate(), errmsg)); } @@ -1169,12 +1159,12 @@ static void Initialize(Handle target, NODE_SET_METHOD(target, "getServers", GetServers); NODE_SET_METHOD(target, "setServers", SetServers); - target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "AF_INET"), - Integer::New(AF_INET, node_isolate)); - target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "AF_INET6"), - Integer::New(AF_INET6, node_isolate)); - target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "AF_UNSPEC"), - Integer::New(AF_UNSPEC, node_isolate)); + target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "AF_INET"), + Integer::New(AF_INET, env->isolate())); + target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "AF_INET6"), + Integer::New(AF_INET6, env->isolate())); + target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "AF_UNSPEC"), + Integer::New(AF_UNSPEC, env->isolate())); } } // namespace cares_wrap diff --git a/src/env-inl.h b/src/env-inl.h index 7d0a9ddb4e..bcc7995fa3 100644 --- a/src/env-inl.h +++ b/src/env-inl.h @@ -23,6 +23,7 @@ #define SRC_ENV_INL_H_ #include "env.h" +#include "node.h" #include "util.h" #include "util-inl.h" #include "uv.h" @@ -364,6 +365,57 @@ inline Environment::IsolateData* Environment::isolate_data() const { return isolate_data_; } +// this would have been a template function were it not for the fact that g++ +// sometimes fails to resolve it... +#define THROW_ERROR(fun) \ + do { \ + v8::HandleScope scope(isolate); \ + v8::ThrowException(fun(OneByteString(isolate, errmsg))); \ + } \ + while (0) + +inline void Environment::ThrowError(v8::Isolate* isolate, const char* errmsg) { + THROW_ERROR(v8::Exception::Error); +} + +inline void Environment::ThrowTypeError(v8::Isolate* isolate, + const char* errmsg) { + THROW_ERROR(v8::Exception::TypeError); +} + +inline void Environment::ThrowRangeError(v8::Isolate* isolate, + const char* errmsg) { + THROW_ERROR(v8::Exception::RangeError); +} + +inline void Environment::ThrowError(const char* errmsg) { + ThrowError(isolate(), errmsg); +} + +inline void Environment::ThrowTypeError(const char* errmsg) { + ThrowTypeError(isolate(), errmsg); +} + +inline void Environment::ThrowRangeError(const char* errmsg) { + ThrowRangeError(isolate(), errmsg); +} + +inline void Environment::ThrowErrnoException(int errorno, + const char* syscall, + const char* message, + const char* path) { + v8::ThrowException( + ErrnoException(isolate(), errorno, syscall, message, path)); +} + +inline void Environment::ThrowUVException(int errorno, + const char* syscall, + const char* message, + const char* path) { + v8::ThrowException( + UVException(isolate(), errorno, syscall, message, path)); +} + #define V(PropertyName, StringValue) \ inline \ v8::Local Environment::IsolateData::PropertyName() const { \ diff --git a/src/env.h b/src/env.h index f04403d770..73a810dc70 100644 --- a/src/env.h +++ b/src/env.h @@ -54,6 +54,7 @@ namespace node { #define PER_ISOLATE_STRING_PROPERTIES(V) \ V(address_string, "address") \ V(args_string, "args") \ + V(argv_string, "argv") \ V(async_queue_string, "_asyncQueue") \ V(async, "async") \ V(atime_string, "atime") \ @@ -62,21 +63,35 @@ namespace node { V(blocks_string, "blocks") \ V(buffer_string, "buffer") \ V(bytes_string, "bytes") \ + V(bytes_parsed_string, "bytesParsed") \ + V(byte_length_string, "byteLength") \ V(callback_string, "callback") \ V(change_string, "change") \ V(close_string, "close") \ V(code_string, "code") \ V(ctime_string, "ctime") \ V(cwd_string, "cwd") \ + V(debug_port_string, "debugPort") \ + V(debug_string, "debug") \ V(detached_string, "detached") \ V(dev_string, "dev") \ V(disposed_string, "_disposed") \ V(domain_string, "domain") \ + V(exchange_string, "exchange") \ + V(idle_string, "idle") \ + V(irq_string, "irq") \ V(enter_string, "enter") \ V(env_pairs_string, "envPairs") \ + V(env_string, "env") \ V(errno_string, "errno") \ V(error_string, "error") \ + V(events_string, "_events") \ + V(exec_argv_string, "execArgv") \ + V(exec_path_string, "execPath") \ + V(exiting_string, "_exiting") \ + V(exit_code_string, "exitCode") \ V(exit_string, "exit") \ + V(expire_string, "expire") \ V(exponent_string, "exponent") \ V(exports_string, "exports") \ V(ext_key_usage_string, "ext_key_usage") \ @@ -86,30 +101,42 @@ namespace node { V(file_string, "file") \ V(fingerprint_string, "fingerprint") \ V(flags_string, "flags") \ + V(fsevent_string, "FSEvent") \ V(gid_string, "gid") \ V(handle_string, "handle") \ V(headers_string, "headers") \ V(heap_size_limit_string, "heap_size_limit") \ V(heap_total_string, "heapTotal") \ V(heap_used_string, "heapUsed") \ + V(hostmaster_string, "hostmaster") \ V(ignore_string, "ignore") \ V(immediate_callback_string, "_immediateCallback") \ V(inherit_string, "inherit") \ V(ino_string, "ino") \ V(input_string, "input") \ + V(internal_string, "internal") \ V(ipv4_string, "IPv4") \ + V(ipv6_lc_string, "ipv6") \ V(ipv6_string, "IPv6") \ V(issuer_string, "issuer") \ V(kill_signal_string, "killSignal") \ + V(mac_string, "mac") \ V(mark_sweep_compact_string, "mark-sweep-compact") \ V(max_buffer_string, "maxBuffer") \ V(message_string, "message") \ V(method_string, "method") \ + V(minttl_string, "minttl") \ V(mode_string, "mode") \ + V(model_string, "model") \ V(modulus_string, "modulus") \ V(mtime_string, "mtime") \ V(name_string, "name") \ + V(need_imm_cb_string, "_needImmediateCallback") \ + V(netmask_string, "netmask") \ + V(nice_string, "nice") \ V(nlink_string, "nlink") \ + V(nsname_string, "nsname") \ + V(offset_string, "offset") \ V(onchange_string, "onchange") \ V(onclienthello_string, "onclienthello") \ V(oncomplete_string, "oncomplete") \ @@ -127,17 +154,33 @@ namespace node { V(onsignal_string, "onsignal") \ V(onstop_string, "onstop") \ V(output_string, "output") \ + V(order_string, "order") \ + V(owner_string, "owner") \ + V(parse_error_string, "Parse Error") \ V(path_string, "path") \ + V(pbkdf2_error_string, "PBKDF2 Error") \ V(pid_string, "pid") \ V(pipe_string, "pipe") \ V(port_string, "port") \ + V(preference_string, "preference") \ + V(priority_string, "priority") \ V(processed_string, "processed") \ + V(prototype_string, "prototype") \ V(rdev_string, "rdev") \ V(readable_string, "readable") \ + V(received_shutdown_string, "receivedShutdown") \ + V(refresh_string, "refresh") \ + V(regexp_string, "regexp") \ V(rename_string, "rename") \ + V(replacement_string, "replacement") \ + V(retry_string, "retry") \ V(rss_string, "rss") \ + V(serial_string, "serial") \ V(scavenge_string, "scavenge") \ + V(scopeid_string, "scopeid") \ + V(sent_shutdown_string, "sentShutdown") \ V(serial_number_string, "serialNumber") \ + V(service_string, "service") \ V(servername_string, "servername") \ V(session_id_string, "sessionId") \ V(should_keep_alive_string, "shouldKeepAlive") \ @@ -146,6 +189,7 @@ namespace node { V(smalloc_p_string, "_smalloc_p") \ V(sni_context_err_string, "Invalid SNI context") \ V(sni_context_string, "sni_context") \ + V(speed_string, "speed") \ V(stack_string, "stack") \ V(status_code_string, "statusCode") \ V(status_message_string, "statusMessage") \ @@ -153,27 +197,43 @@ namespace node { V(stdio_string, "stdio") \ V(subject_string, "subject") \ V(subjectaltname_string, "subjectaltname") \ + V(sys_string, "sys") \ V(syscall_string, "syscall") \ + V(tick_callback_string, "_tickCallback") \ + V(tick_domain_cb_string, "_tickDomainCallback") \ + V(tick_info_string, "_tickInfo") \ V(timeout_string, "timeout") \ + V(times_string, "times") \ V(timestamp_string, "timestamp") \ + V(title_string, "title") \ + V(tls_npn_string, "tls_npn") \ + V(tls_sni_string, "tls_sni") \ + V(tls_string, "tls") \ V(tls_ticket_string, "tlsTicket") \ V(total_heap_size_executable_string, "total_heap_size_executable") \ V(total_heap_size_string, "total_heap_size") \ V(total_physical_size_string, "total_physical_size") \ V(type_string, "type") \ V(uid_string, "uid") \ + V(unknown_string, "") \ V(upgrade_string, "upgrade") \ V(url_string, "url") \ V(used_heap_size_string, "used_heap_size") \ + V(user_string, "user") \ + V(uv_string, "uv") \ V(valid_from_string, "valid_from") \ V(valid_to_string, "valid_to") \ V(verify_error_string, "verifyError") \ V(version_major_string, "versionMajor") \ V(version_minor_string, "versionMinor") \ V(version_string, "version") \ + V(weight_string, "weight") \ V(windows_verbatim_arguments_string, "windowsVerbatimArguments") \ + V(wrap_string, "wrap") \ V(writable_string, "writable") \ V(write_queue_size_string, "writeQueueSize") \ + V(x_forwarded_string, "x-forwarded-for") \ + V(zero_return_string, "ZERO_RETURN") \ #define ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V) \ V(async_listener_run_function, v8::Function) \ @@ -331,6 +391,23 @@ class Environment { inline bool printed_error() const; inline void set_printed_error(bool value); + inline void ThrowError(const char* errmsg); + inline void ThrowTypeError(const char* errmsg); + inline void ThrowRangeError(const char* errmsg); + inline void ThrowErrnoException(int errorno, + const char* syscall = NULL, + const char* message = NULL, + const char* path = NULL); + inline void ThrowUVException(int errorno, + const char* syscall = NULL, + const char* message = NULL, + const char* path = NULL); + + // Convenience methods for contextify + inline static void ThrowError(v8::Isolate* isolate, const char* errmsg); + inline static void ThrowTypeError(v8::Isolate* isolate, const char* errmsg); + inline static void ThrowRangeError(v8::Isolate* isolate, const char* errmsg); + // Strings are shared across shared contexts. The getters simply proxy to // the per-isolate primitive. #define V(PropertyName, StringValue) \ diff --git a/src/fs_event_wrap.cc b/src/fs_event_wrap.cc index 28b20ac47c..ee28c44afb 100644 --- a/src/fs_event_wrap.cc +++ b/src/fs_event_wrap.cc @@ -81,14 +81,16 @@ FSEventWrap::~FSEventWrap() { void FSEventWrap::Initialize(Handle target, Handle unused, Handle context) { + Environment* env = Environment::GetCurrent(context); + Local t = FunctionTemplate::New(New); t->InstanceTemplate()->SetInternalFieldCount(1); - t->SetClassName(FIXED_ONE_BYTE_STRING(node_isolate, "FSEvent")); + t->SetClassName(env->fsevent_string()); NODE_SET_PROTOTYPE_METHOD(t, "start", Start); NODE_SET_PROTOTYPE_METHOD(t, "close", Close); - target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "FSEvent"), t->GetFunction()); + target->Set(env->fsevent_string(), t->GetFunction()); } @@ -101,12 +103,13 @@ void FSEventWrap::New(const FunctionCallbackInfo& args) { void FSEventWrap::Start(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); FSEventWrap* wrap = Unwrap(args.This()); if (args.Length() < 1 || !args[0]->IsString()) { - return ThrowTypeError("Bad arguments"); + return env->ThrowTypeError("Bad arguments"); } String::Utf8Value path(args[0]); @@ -158,7 +161,7 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename, // unreasonable, right? Still, we should revisit this before v1.0. Local event_string; if (status) { - event_string = String::Empty(node_isolate); + event_string = String::Empty(env->isolate()); } else if (events & UV_RENAME) { event_string = env->rename_string(); } else if (events & UV_CHANGE) { @@ -169,13 +172,13 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename, } Local argv[] = { - Integer::New(status, node_isolate), + Integer::New(status, env->isolate()), event_string, - Null(node_isolate) + Null(env->isolate()) }; if (filename != NULL) { - argv[2] = OneByteString(node_isolate, filename); + argv[2] = OneByteString(env->isolate(), filename); } wrap->MakeCallback(env->onchange_string(), ARRAY_SIZE(argv), argv); @@ -183,7 +186,8 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename, void FSEventWrap::Close(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); FSEventWrap* wrap = Unwrap(args.This()); diff --git a/src/handle_wrap.cc b/src/handle_wrap.cc index b7ee48da18..3df1e497d3 100644 --- a/src/handle_wrap.cc +++ b/src/handle_wrap.cc @@ -44,7 +44,8 @@ extern QUEUE handle_wrap_queue; void HandleWrap::Ref(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); HandleWrap* wrap = Unwrap(args.This()); @@ -56,7 +57,8 @@ void HandleWrap::Ref(const FunctionCallbackInfo& args) { void HandleWrap::Unref(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); HandleWrap* wrap = Unwrap(args.This()); @@ -68,7 +70,8 @@ void HandleWrap::Unref(const FunctionCallbackInfo& args) { void HandleWrap::Close(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); HandleWrap* wrap = Unwrap(args.This()); @@ -76,7 +79,6 @@ void HandleWrap::Close(const FunctionCallbackInfo& args) { if (wrap == NULL || wrap->handle__ == NULL) return; - Environment* env = wrap->env(); assert(!wrap->persistent().IsEmpty()); uv_close(wrap->handle__, OnClose); wrap->handle__ = NULL; @@ -96,7 +98,7 @@ HandleWrap::HandleWrap(Environment* env, flags_(0), handle__(handle) { handle__->data = this; - HandleScope scope(node_isolate); + HandleScope scope(env->isolate()); Wrap(object, this); QUEUE_INSERT_TAIL(&handle_wrap_queue, &handle_wrap_queue_); } @@ -109,10 +111,9 @@ HandleWrap::~HandleWrap() { void HandleWrap::OnClose(uv_handle_t* handle) { - HandleScope scope(node_isolate); - HandleWrap* wrap = static_cast(handle->data); Environment* env = wrap->env(); + HandleScope scope(env->isolate()); // The wrap object should still be there. assert(wrap->persistent().IsEmpty() == false); diff --git a/src/node.cc b/src/node.cc index 434682fad6..7c0bb33926 100644 --- a/src/node.cc +++ b/src/node.cc @@ -142,8 +142,7 @@ static double prog_start_time; static bool debugger_running; static uv_async_t dispatch_debug_messages_async; -// Declared in node_internals.h -Isolate* node_isolate = NULL; +static Isolate* node_isolate = NULL; class ArrayBufferAllocator : public ArrayBuffer::Allocator { @@ -187,8 +186,8 @@ void ArrayBufferAllocator::Free(void* data, size_t length) { static void CheckImmediate(uv_check_t* handle, int status) { - HandleScope scope(node_isolate); Environment* env = Environment::from_immediate_check_handle(handle); + HandleScope scope(env->isolate()); Context::Scope context_scope(env->context()); MakeCallback(env, env->process_object(), env->immediate_callback_string()); } @@ -677,45 +676,46 @@ const char *signo_string(int signo) { } -Local ErrnoException(int errorno, +Local ErrnoException(Isolate* isolate, + int errorno, const char *syscall, const char *msg, const char *path) { - Environment* env = Environment::GetCurrent(node_isolate); + Environment* env = Environment::GetCurrent(isolate); Local e; - Local estring = OneByteString(node_isolate, errno_string(errorno)); + Local estring = OneByteString(env->isolate(), errno_string(errorno)); if (msg == NULL || msg[0] == '\0') { msg = strerror(errorno); } - Local message = OneByteString(node_isolate, msg); + Local message = OneByteString(env->isolate(), msg); Local cons1 = - String::Concat(estring, FIXED_ONE_BYTE_STRING(node_isolate, ", ")); + String::Concat(estring, FIXED_ONE_BYTE_STRING(env->isolate(), ", ")); Local cons2 = String::Concat(cons1, message); if (path) { Local cons3 = - String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, " '")); + String::Concat(cons2, FIXED_ONE_BYTE_STRING(env->isolate(), " '")); Local cons4 = - String::Concat(cons3, String::NewFromUtf8(node_isolate, path)); + String::Concat(cons3, String::NewFromUtf8(env->isolate(), path)); Local cons5 = - String::Concat(cons4, FIXED_ONE_BYTE_STRING(node_isolate, "'")); + String::Concat(cons4, FIXED_ONE_BYTE_STRING(env->isolate(), "'")); e = Exception::Error(cons5); } else { e = Exception::Error(cons2); } Local obj = e->ToObject(); - obj->Set(env->errno_string(), Integer::New(errorno, node_isolate)); + obj->Set(env->errno_string(), Integer::New(errorno, env->isolate())); obj->Set(env->code_string(), estring); if (path != NULL) { - obj->Set(env->path_string(), String::NewFromUtf8(node_isolate, path)); + obj->Set(env->path_string(), String::NewFromUtf8(env->isolate(), path)); } if (syscall != NULL) { - obj->Set(env->syscall_string(), OneByteString(node_isolate, syscall)); + obj->Set(env->syscall_string(), OneByteString(env->isolate(), syscall)); } return e; @@ -723,19 +723,20 @@ Local ErrnoException(int errorno, // hack alert! copy of ErrnoException, tuned for uv errors -Local UVException(int errorno, +Local UVException(Isolate* isolate, + int errorno, const char *syscall, const char *msg, const char *path) { - Environment* env = Environment::GetCurrent(node_isolate); + Environment* env = Environment::GetCurrent(isolate); if (!msg || !msg[0]) msg = uv_strerror(errorno); - Local estring = OneByteString(node_isolate, uv_err_name(errorno)); - Local message = OneByteString(node_isolate, msg); + Local estring = OneByteString(env->isolate(), uv_err_name(errorno)); + Local message = OneByteString(env->isolate(), msg); Local cons1 = - String::Concat(estring, FIXED_ONE_BYTE_STRING(node_isolate, ", ")); + String::Concat(estring, FIXED_ONE_BYTE_STRING(env->isolate(), ", ")); Local cons2 = String::Concat(cons1, message); Local e; @@ -745,23 +746,23 @@ Local UVException(int errorno, if (path) { #ifdef _WIN32 if (strncmp(path, "\\\\?\\UNC\\", 8) == 0) { - path_str = String::Concat(FIXED_ONE_BYTE_STRING(node_isolate, "\\\\"), - String::NewFromUtf8(node_isolate, path + 8)); + path_str = String::Concat(FIXED_ONE_BYTE_STRING(env->isolate(), "\\\\"), + String::NewFromUtf8(env->isolate(), path + 8)); } else if (strncmp(path, "\\\\?\\", 4) == 0) { - path_str = String::NewFromUtf8(node_isolate, path + 4); + path_str = String::NewFromUtf8(env->isolate(), path + 4); } else { - path_str = String::NewFromUtf8(node_isolate, path); + path_str = String::NewFromUtf8(env->isolate(), path); } #else - path_str = String::NewFromUtf8(node_isolate, path); + path_str = String::NewFromUtf8(env->isolate(), path); #endif Local cons3 = - String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, " '")); + String::Concat(cons2, FIXED_ONE_BYTE_STRING(env->isolate(), " '")); Local cons4 = String::Concat(cons3, path_str); Local cons5 = - String::Concat(cons4, FIXED_ONE_BYTE_STRING(node_isolate, "'")); + String::Concat(cons4, FIXED_ONE_BYTE_STRING(env->isolate(), "'")); e = Exception::Error(cons5); } else { e = Exception::Error(cons2); @@ -769,7 +770,7 @@ Local UVException(int errorno, Local obj = e->ToObject(); // TODO(piscisaureus) errno should probably go - obj->Set(env->errno_string(), Integer::New(errorno, node_isolate)); + obj->Set(env->errno_string(), Integer::New(errorno, env->isolate())); obj->Set(env->code_string(), estring); if (path != NULL) { @@ -777,7 +778,7 @@ Local UVException(int errorno, } if (syscall != NULL) { - obj->Set(env->syscall_string(), OneByteString(node_isolate, syscall)); + obj->Set(env->syscall_string(), OneByteString(env->isolate(), syscall)); } return e; @@ -809,43 +810,51 @@ static const char *winapi_strerror(const int errorno) { } -Local WinapiErrnoException(int errorno, +Local WinapiErrnoException(Environment* env, + int errorno, const char* syscall, const char* msg, const char* path) { - Environment* env = Environment::GetCurrent(node_isolate); - Local e; if (!msg || !msg[0]) { msg = winapi_strerror(errorno); } - Local message = OneByteString(node_isolate, msg); + Local message = OneByteString(env->isolate(), msg); if (path) { Local cons1 = - String::Concat(message, FIXED_ONE_BYTE_STRING(node_isolate, " '")); + String::Concat(message, FIXED_ONE_BYTE_STRING(env->isolate(), " '")); Local cons2 = - String::Concat(cons1, String::NewFromUtf8(node_isolate, path)); + String::Concat(cons1, String::NewFromUtf8(env->isolate(), path)); Local cons3 = - String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, "'")); + String::Concat(cons2, FIXED_ONE_BYTE_STRING(env->isolate(), "'")); e = Exception::Error(cons3); } else { e = Exception::Error(message); } Local obj = e->ToObject(); - obj->Set(env->errno_string(), Integer::New(errorno, node_isolate)); + obj->Set(env->errno_string(), Integer::New(errorno, env->isolate())); if (path != NULL) { - obj->Set(env->path_string(), String::NewFromUtf8(node_isolate, path)); + obj->Set(env->path_string(), String::NewFromUtf8(env->isolate(), path)); } if (syscall != NULL) { - obj->Set(env->syscall_string(), OneByteString(node_isolate, syscall)); + obj->Set(env->syscall_string(), OneByteString(env->isolate(), syscall)); } return e; } + + +Local WinapiErrnoException(int errorno, + const char* syscall, + const char* msg, + const char* path) { + Environment* env = Environment::GetCurrent(Isolate::GetCurrent()); + return WinapiErrnoException(env, errorno, syscall, msg, path); +} #endif @@ -882,11 +891,10 @@ void SetupDomainUse(const FunctionCallbackInfo& args) { return; env->set_using_domains(true); - HandleScope scope(node_isolate); + HandleScope scope(env->isolate()); Local process_object = env->process_object(); - Local tick_callback_function_key = - FIXED_ONE_BYTE_STRING(node_isolate, "_tickDomainCallback"); + Local tick_callback_function_key = env->tick_domain_cb_string(); Local tick_callback_function = process_object->Get(tick_callback_function_key).As(); @@ -895,8 +903,7 @@ void SetupDomainUse(const FunctionCallbackInfo& args) { abort(); } - process_object->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_tickCallback"), - tick_callback_function); + process_object->Set(env->tick_callback_string(), tick_callback_function); env->set_tick_callback_function(tick_callback_function); assert(args[0]->IsArray()); @@ -964,7 +971,7 @@ Handle MakeDomainCallback(Environment* env, env->async_listener_load_function()->Call(process, 1, &recv); if (try_catch.HasCaught()) - return Undefined(node_isolate); + return Undefined(env->isolate()); } } @@ -978,7 +985,7 @@ Handle MakeDomainCallback(Environment* env, if (domain->Get(env->disposed_string())->IsTrue()) { // domain has been disposed of. - return Undefined(node_isolate); + return Undefined(env->isolate()); } Local enter = @@ -987,7 +994,7 @@ Handle MakeDomainCallback(Environment* env, enter->Call(domain, 0, NULL); if (try_catch.HasCaught()) { - return Undefined(node_isolate); + return Undefined(env->isolate()); } } } @@ -995,7 +1002,7 @@ Handle MakeDomainCallback(Environment* env, Local ret = callback->Call(recv, argc, argv); if (try_catch.HasCaught()) { - return Undefined(node_isolate); + return Undefined(env->isolate()); } if (has_domain) { @@ -1005,7 +1012,7 @@ Handle MakeDomainCallback(Environment* env, exit->Call(domain, 0, NULL); if (try_catch.HasCaught()) { - return Undefined(node_isolate); + return Undefined(env->isolate()); } } @@ -1013,7 +1020,7 @@ Handle MakeDomainCallback(Environment* env, env->async_listener_unload_function()->Call(process, 1, &recv); if (try_catch.HasCaught()) - return Undefined(node_isolate); + return Undefined(env->isolate()); } Environment::TickInfo* tick_info = env->tick_info(); @@ -1040,7 +1047,7 @@ Handle MakeDomainCallback(Environment* env, if (try_catch.HasCaught()) { tick_info->set_last_threw(true); - return Undefined(node_isolate); + return Undefined(env->isolate()); } return ret; @@ -1069,20 +1076,20 @@ Handle MakeCallback(Environment* env, if (has_async_queue) { env->async_listener_load_function()->Call(process, 1, &recv); if (try_catch.HasCaught()) - return Undefined(node_isolate); + return Undefined(env->isolate()); } Local ret = callback->Call(recv, argc, argv); if (try_catch.HasCaught()) { - return Undefined(node_isolate); + return Undefined(env->isolate()); } if (has_async_queue) { env->async_listener_unload_function()->Call(process, 1, &recv); if (try_catch.HasCaught()) - return Undefined(node_isolate); + return Undefined(env->isolate()); } Environment::TickInfo* tick_info = env->tick_info(); @@ -1105,7 +1112,7 @@ Handle MakeCallback(Environment* env, if (try_catch.HasCaught()) { tick_info->set_last_threw(true); - return Undefined(node_isolate); + return Undefined(env->isolate()); } return ret; @@ -1141,16 +1148,17 @@ Handle MakeCallback(Environment* env, const char* method, int argc, Handle argv[]) { - Local method_string = OneByteString(node_isolate, method); + Local method_string = OneByteString(env->isolate(), method); return MakeCallback(env, recv, method_string, argc, argv); } -Handle MakeCallback(Handle recv, +Handle MakeCallback(Isolate* isolate, + Handle recv, const char* method, int argc, Handle argv[]) { - HandleScope handle_scope(node_isolate); // FIXME(bnoordhuis) Isolate-ify. + HandleScope handle_scope(isolate); Local context = recv->CreationContext(); Environment* env = Environment::GetCurrent(context); Context::Scope context_scope(context); @@ -1158,11 +1166,12 @@ Handle MakeCallback(Handle recv, } -Handle MakeCallback(Handle recv, +Handle MakeCallback(Isolate* isolate, + Handle recv, Handle symbol, int argc, Handle argv[]) { - HandleScope handle_scope(node_isolate); // FIXME(bnoordhuis) Isolate-ify. + HandleScope handle_scope(isolate); Local context = recv->CreationContext(); Environment* env = Environment::GetCurrent(context); Context::Scope context_scope(context); @@ -1170,11 +1179,12 @@ Handle MakeCallback(Handle recv, } -Handle MakeCallback(Handle recv, +Handle MakeCallback(Isolate* isolate, + Handle recv, Handle callback, int argc, Handle argv[]) { - HandleScope handle_scope(node_isolate); // FIXME(bnoordhuis) Isolate-ify. + HandleScope handle_scope(isolate); Local context = recv->CreationContext(); Environment* env = Environment::GetCurrent(context); Context::Scope context_scope(context); @@ -1196,8 +1206,10 @@ Handle MakeDomainCallback(Handle recv, } -enum encoding ParseEncoding(Handle encoding_v, enum encoding _default) { - HandleScope scope(node_isolate); +enum encoding ParseEncoding(Isolate* isolate, + Handle encoding_v, + enum encoding _default) { + HandleScope scope(isolate); if (!encoding_v->IsString()) return _default; @@ -1243,15 +1255,21 @@ enum encoding ParseEncoding(Handle encoding_v, enum encoding _default) { } } -Local Encode(const void *buf, size_t len, enum encoding encoding) { - return StringBytes::Encode(static_cast(buf), +Local Encode(Isolate* isolate, + const void* buf, + size_t len, + enum encoding encoding) { + return StringBytes::Encode(isolate, + static_cast(buf), len, encoding); } // Returns -1 if the handle was not valid for decoding -ssize_t DecodeBytes(v8::Handle val, enum encoding encoding) { - HandleScope scope(node_isolate); +ssize_t DecodeBytes(Isolate* isolate, + Handle val, + enum encoding encoding) { + HandleScope scope(isolate); if (val->IsArray()) { fprintf(stderr, "'raw' encoding (array of integers) has been removed. " @@ -1260,7 +1278,7 @@ ssize_t DecodeBytes(v8::Handle val, enum encoding encoding) { return -1; } - return StringBytes::Size(val, encoding); + return StringBytes::Size(isolate, val, encoding); } #ifndef MIN @@ -1268,11 +1286,12 @@ ssize_t DecodeBytes(v8::Handle val, enum encoding encoding) { #endif // Returns number of bytes written. -ssize_t DecodeWrite(char *buf, +ssize_t DecodeWrite(Isolate* isolate, + char* buf, size_t buflen, - v8::Handle val, + Handle val, enum encoding encoding) { - return StringBytes::Write(buf, buflen, val, encoding, NULL); + return StringBytes::Write(isolate, buf, buflen, val, encoding, NULL); } void AppendExceptionLine(Environment* env, @@ -1459,7 +1478,7 @@ static Local ExecuteString(Environment* env, static void GetActiveRequests(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + HandleScope scope(args.GetIsolate()); Local ary = Array::New(); QUEUE* q = NULL; @@ -1479,13 +1498,14 @@ static void GetActiveRequests(const FunctionCallbackInfo& args) { // Non-static, friend of HandleWrap. Could have been a HandleWrap method but // implemented here for consistency with GetActiveRequests(). void GetActiveHandles(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); Local ary = Array::New(); QUEUE* q = NULL; int i = 0; - Local owner_sym = FIXED_ONE_BYTE_STRING(node_isolate, "owner"); + Local owner_sym = env->owner_string(); QUEUE_FOREACH(q, &handle_wrap_queue) { HandleWrap* w = CONTAINER_OF(q, HandleWrap, handle_wrap_queue_); @@ -1508,22 +1528,25 @@ static void Abort(const FunctionCallbackInfo& args) { static void Chdir(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); if (args.Length() != 1 || !args[0]->IsString()) { - return ThrowError("Bad argument."); // FIXME(bnoordhuis) ThrowTypeError? + // FIXME(bnoordhuis) ThrowTypeError? + return env->ThrowError("Bad argument."); } String::Utf8Value path(args[0]); int err = uv_chdir(*path); if (err) { - return ThrowUVException(err, "uv_chdir"); + return env->ThrowUVException(err, "uv_chdir"); } } static void Cwd(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); #ifdef _WIN32 /* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */ char buf[MAX_PATH * 4 + 1]; @@ -1533,25 +1556,26 @@ static void Cwd(const FunctionCallbackInfo& args) { int err = uv_cwd(buf, ARRAY_SIZE(buf) - 1); if (err) { - return ThrowUVException(err, "uv_cwd"); + return env->ThrowUVException(err, "uv_cwd"); } buf[ARRAY_SIZE(buf) - 1] = '\0'; - Local cwd = String::NewFromUtf8(node_isolate, buf); + Local cwd = String::NewFromUtf8(env->isolate(), buf); args.GetReturnValue().Set(cwd); } static void Umask(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); uint32_t old; if (args.Length() < 1 || args[0]->IsUndefined()) { old = umask(0); umask(static_cast(old)); } else if (!args[0]->IsInt32() && !args[0]->IsString()) { - return ThrowTypeError("argument must be an integer or octal string."); + return env->ThrowTypeError("argument must be an integer or octal string."); } else { int oct; if (args[0]->IsInt32()) { @@ -1564,7 +1588,7 @@ static void Umask(const FunctionCallbackInfo& args) { for (int i = 0; i < str.length(); i++) { char c = (*str)[i]; if (c > '7' || c < '0') { - return ThrowTypeError("invalid octal string"); + return env->ThrowTypeError("invalid octal string"); } oct *= 8; oct += c - '0'; @@ -1692,50 +1716,53 @@ static void GetGid(const FunctionCallbackInfo& args) { static void SetGid(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); if (!args[0]->IsUint32() && !args[0]->IsString()) { - return ThrowTypeError("setgid argument must be a number or a string"); + return env->ThrowTypeError("setgid argument must be a number or a string"); } gid_t gid = gid_by_name(args[0]); if (gid == gid_not_found) { - return ThrowError("setgid group id does not exist"); + return env->ThrowError("setgid group id does not exist"); } if (setgid(gid)) { - return ThrowErrnoException(errno, "setgid"); + return env->ThrowErrnoException(errno, "setgid"); } } static void SetUid(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); if (!args[0]->IsUint32() && !args[0]->IsString()) { - return ThrowTypeError("setuid argument must be a number or a string"); + return env->ThrowTypeError("setuid argument must be a number or a string"); } uid_t uid = uid_by_name(args[0]); if (uid == uid_not_found) { - return ThrowError("setuid user id does not exist"); + return env->ThrowError("setuid user id does not exist"); } if (setuid(uid)) { - return ThrowErrnoException(errno, "setuid"); + return env->ThrowErrnoException(errno, "setuid"); } } static void GetGroups(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); int ngroups = getgroups(0, NULL); if (ngroups == -1) { - return ThrowErrnoException(errno, "getgroups"); + return env->ThrowErrnoException(errno, "getgroups"); } gid_t* groups = new gid_t[ngroups]; @@ -1744,7 +1771,7 @@ static void GetGroups(const FunctionCallbackInfo& args) { if (ngroups == -1) { delete[] groups; - return ThrowErrnoException(errno, "getgroups"); + return env->ThrowErrnoException(errno, "getgroups"); } Local groups_list = Array::New(ngroups); @@ -1752,7 +1779,7 @@ static void GetGroups(const FunctionCallbackInfo& args) { gid_t egid = getegid(); for (int i = 0; i < ngroups; i++) { - groups_list->Set(i, Integer::New(groups[i], node_isolate)); + groups_list->Set(i, Integer::New(groups[i], env->isolate())); if (groups[i] == egid) seen_egid = true; } @@ -1760,7 +1787,7 @@ static void GetGroups(const FunctionCallbackInfo& args) { delete[] groups; if (seen_egid == false) { - groups_list->Set(ngroups, Integer::New(egid, node_isolate)); + groups_list->Set(ngroups, Integer::New(egid, env->isolate())); } args.GetReturnValue().Set(groups_list); @@ -1768,10 +1795,11 @@ static void GetGroups(const FunctionCallbackInfo& args) { static void SetGroups(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); if (!args[0]->IsArray()) { - return ThrowTypeError("argument 1 must be an array"); + return env->ThrowTypeError("argument 1 must be an array"); } Local groups_list = args[0].As(); @@ -1783,7 +1811,7 @@ static void SetGroups(const FunctionCallbackInfo& args) { if (gid == gid_not_found) { delete[] groups; - return ThrowError("group name not found"); + return env->ThrowError("group name not found"); } groups[i] = gid; @@ -1793,20 +1821,21 @@ static void SetGroups(const FunctionCallbackInfo& args) { delete[] groups; if (rc == -1) { - return ThrowErrnoException(errno, "setgroups"); + return env->ThrowErrnoException(errno, "setgroups"); } } static void InitGroups(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); if (!args[0]->IsUint32() && !args[0]->IsString()) { - return ThrowTypeError("argument 1 must be a number or a string"); + return env->ThrowTypeError("argument 1 must be a number or a string"); } if (!args[1]->IsUint32() && !args[1]->IsString()) { - return ThrowTypeError("argument 2 must be a number or a string"); + return env->ThrowTypeError("argument 2 must be a number or a string"); } String::Utf8Value arg0(args[0]); @@ -1823,7 +1852,7 @@ static void InitGroups(const FunctionCallbackInfo& args) { } if (user == NULL) { - return ThrowError("initgroups user not found"); + return env->ThrowError("initgroups user not found"); } extra_group = gid_by_name(args[1]); @@ -1831,7 +1860,7 @@ static void InitGroups(const FunctionCallbackInfo& args) { if (extra_group == gid_not_found) { if (must_free) free(user); - return ThrowError("initgroups extra group not found"); + return env->ThrowError("initgroups extra group not found"); } int rc = initgroups(user, extra_group); @@ -1841,7 +1870,7 @@ static void InitGroups(const FunctionCallbackInfo& args) { } if (rc) { - return ThrowErrnoException(errno, "initgroups"); + return env->ThrowErrnoException(errno, "initgroups"); } } @@ -1849,13 +1878,15 @@ static void InitGroups(const FunctionCallbackInfo& args) { void Exit(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); exit(args[0]->IntegerValue()); } static void Uptime(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); double uptime; if (uv_uptime(&uptime)) return; @@ -1864,26 +1895,26 @@ static void Uptime(const FunctionCallbackInfo& args) { void MemoryUsage(const FunctionCallbackInfo& args) { - HandleScope handle_scope(args.GetIsolate()); Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); size_t rss; int err = uv_resident_set_memory(&rss); if (err) { - return ThrowUVException(err, "uv_resident_set_memory"); + return env->ThrowUVException(err, "uv_resident_set_memory"); } // V8 memory usage HeapStatistics v8_heap_stats; - node_isolate->GetHeapStatistics(&v8_heap_stats); + env->isolate()->GetHeapStatistics(&v8_heap_stats); Local heap_total = - Integer::NewFromUnsigned(v8_heap_stats.total_heap_size(), node_isolate); + Integer::NewFromUnsigned(v8_heap_stats.total_heap_size(), env->isolate()); Local heap_used = - Integer::NewFromUnsigned(v8_heap_stats.used_heap_size(), node_isolate); + Integer::NewFromUnsigned(v8_heap_stats.used_heap_size(), env->isolate()); Local info = Object::New(); - info->Set(env->rss_string(), Number::New(node_isolate, rss)); + info->Set(env->rss_string(), Number::New(env->isolate(), rss)); info->Set(env->heap_total_string(), heap_total); info->Set(env->heap_used_string(), heap_used); @@ -1892,10 +1923,11 @@ void MemoryUsage(const FunctionCallbackInfo& args) { void Kill(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); if (args.Length() != 2) { - return ThrowError("Bad argument."); + return env->ThrowError("Bad argument."); } int pid = args[0]->IntegerValue(); @@ -1913,14 +1945,16 @@ void Kill(const FunctionCallbackInfo& args) { // and nanoseconds, to avoid any integer overflow possibility. // Pass in an Array from a previous hrtime() call to instead get a time diff. void Hrtime(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); uint64_t t = uv_hrtime(); if (args.Length() > 0) { // return a time diff tuple if (!args[0]->IsArray()) { - return ThrowTypeError("process.hrtime() only accepts an Array tuple."); + return env->ThrowTypeError( + "process.hrtime() only accepts an Array tuple."); } Local inArray = Local::Cast(args[0]); uint64_t seconds = inArray->Get(0)->Uint32Value(); @@ -1929,8 +1963,8 @@ void Hrtime(const FunctionCallbackInfo& args) { } Local tuple = Array::New(2); - tuple->Set(0, Integer::NewFromUnsigned(t / NANOS_PER_SEC, node_isolate)); - tuple->Set(1, Integer::NewFromUnsigned(t % NANOS_PER_SEC, node_isolate)); + tuple->Set(0, Integer::NewFromUnsigned(t / NANOS_PER_SEC, env->isolate())); + tuple->Set(1, Integer::NewFromUnsigned(t % NANOS_PER_SEC, env->isolate())); args.GetReturnValue().Set(tuple); } @@ -1973,7 +2007,7 @@ void DLOpen(const FunctionCallbackInfo& args) { uv_lib_t lib; if (args.Length() < 2) { - ThrowError("process.dlopen takes exactly 2 arguments."); + env->ThrowError("process.dlopen takes exactly 2 arguments."); return; } @@ -1989,7 +2023,7 @@ void DLOpen(const FunctionCallbackInfo& args) { // Windows needs to add the filename into the error message errmsg = String::Concat(errmsg, args[1]->ToString()); #endif // _WIN32 - ThrowException(Exception::Error(errmsg)); + env->isolate()->ThrowException(Exception::Error(errmsg)); return; } @@ -2002,7 +2036,7 @@ void DLOpen(const FunctionCallbackInfo& args) { modpending = NULL; if (mp == NULL) { - ThrowError("Module did not self-register."); + env->ThrowError("Module did not self-register."); return; } if (mp->nm_version != NODE_MODULE_VERSION) { @@ -2011,11 +2045,11 @@ void DLOpen(const FunctionCallbackInfo& args) { sizeof(errmsg), "Module version mismatch. Expected %d, got %d.", NODE_MODULE_VERSION, mp->nm_version); - ThrowError(errmsg); + env->ThrowError(errmsg); return; } if (mp->nm_flags & NM_F_BUILTIN) { - ThrowError("Built-in module self-registered."); + env->ThrowError("Built-in module self-registered."); return; } @@ -2028,7 +2062,7 @@ void DLOpen(const FunctionCallbackInfo& args) { } else if (mp->nm_register_func != NULL) { mp->nm_register_func(exports, module, mp->nm_priv); } else { - ThrowError("Module has no declared entry point."); + env->ThrowError("Module has no declared entry point."); return; } @@ -2055,10 +2089,12 @@ NO_RETURN void FatalError(const char* location, const char* message) { } -void FatalException(Handle error, Handle message) { - HandleScope scope(node_isolate); +void FatalException(Isolate* isolate, + Handle error, + Handle message) { + HandleScope scope(isolate); - Environment* env = Environment::GetCurrent(node_isolate); + Environment* env = Environment::GetCurrent(isolate); Local process_object = env->process_object(); Local fatal_exception_string = env->fatal_exception_string(); Local fatal_exception_function = @@ -2093,18 +2129,18 @@ void FatalException(Handle error, Handle message) { } -void FatalException(const TryCatch& try_catch) { - HandleScope scope(node_isolate); +void FatalException(Isolate* isolate, const TryCatch& try_catch) { + HandleScope scope(isolate); // TODO(bajtos) do not call FatalException if try_catch is verbose // (requires V8 API to expose getter for try_catch.is_verbose_) - FatalException(try_catch.Exception(), try_catch.Message()); + FatalException(isolate, try_catch.Exception(), try_catch.Message()); } void OnMessage(Handle message, Handle error) { // The current version of V8 sends messages for errors only // (thus `error` is always set). - FatalException(error, message); + FatalException(Isolate::GetCurrent(), error, message); } @@ -2130,7 +2166,7 @@ static void Binding(const FunctionCallbackInfo& args) { Local modules = env->module_load_list_array(); uint32_t l = modules->Length(); - modules->Set(l, OneByteString(node_isolate, buf)); + modules->Set(l, OneByteString(env->isolate(), buf)); node_module* mod = get_builtin_module(*module_v); if (mod != NULL) { @@ -2148,10 +2184,10 @@ static void Binding(const FunctionCallbackInfo& args) { cache->Set(module, exports); } else if (!strcmp(*module_v, "natives")) { exports = Object::New(); - DefineJavaScript(exports); + DefineJavaScript(env, exports); cache->Set(module, exports); } else { - return ThrowError("No such module"); + return env->ThrowError("No such module"); } args.GetReturnValue().Set(exports); @@ -2160,17 +2196,19 @@ static void Binding(const FunctionCallbackInfo& args) { static void ProcessTitleGetter(Local property, const PropertyCallbackInfo& info) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(info.GetIsolate()); + HandleScope scope(env->isolate()); char buffer[512]; uv_get_process_title(buffer, sizeof(buffer)); - info.GetReturnValue().Set(String::NewFromUtf8(node_isolate, buffer)); + info.GetReturnValue().Set(String::NewFromUtf8(env->isolate(), buffer)); } static void ProcessTitleSetter(Local property, Local value, const PropertyCallbackInfo& info) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(info.GetIsolate()); + HandleScope scope(env->isolate()); String::Utf8Value title(value); // TODO(piscisaureus): protect with a lock uv_set_process_title(*title); @@ -2179,12 +2217,13 @@ static void ProcessTitleSetter(Local property, static void EnvGetter(Local property, const PropertyCallbackInfo& info) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(info.GetIsolate()); + HandleScope scope(env->isolate()); #ifdef __POSIX__ String::Utf8Value key(property); const char* val = getenv(*key); if (val) { - return info.GetReturnValue().Set(String::NewFromUtf8(node_isolate, val)); + return info.GetReturnValue().Set(String::NewFromUtf8(env->isolate(), val)); } #else // _WIN32 String::Value key(property); @@ -2198,7 +2237,7 @@ static void EnvGetter(Local property, if ((result > 0 || GetLastError() == ERROR_SUCCESS) && result < ARRAY_SIZE(buffer)) { const uint16_t* two_byte_buffer = reinterpret_cast(buffer); - Local rc = String::NewFromTwoByte(node_isolate, two_byte_buffer); + Local rc = String::NewFromTwoByte(env->isolate(), two_byte_buffer); return info.GetReturnValue().Set(rc); } #endif @@ -2211,7 +2250,8 @@ static void EnvGetter(Local property, static void EnvSetter(Local property, Local value, const PropertyCallbackInfo& info) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(info.GetIsolate()); + HandleScope scope(env->isolate()); #ifdef __POSIX__ String::Utf8Value key(property); String::Utf8Value val(value); @@ -2232,7 +2272,8 @@ static void EnvSetter(Local property, static void EnvQuery(Local property, const PropertyCallbackInfo& info) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(info.GetIsolate()); + HandleScope scope(env->isolate()); int32_t rc = -1; // Not found unless proven otherwise. #ifdef __POSIX__ String::Utf8Value key(property); @@ -2259,7 +2300,8 @@ static void EnvQuery(Local property, static void EnvDeleter(Local property, const PropertyCallbackInfo& info) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(info.GetIsolate()); + HandleScope scope(env->isolate()); bool rc = true; #ifdef __POSIX__ String::Utf8Value key(property); @@ -2281,29 +2323,30 @@ static void EnvDeleter(Local property, static void EnvEnumerator(const PropertyCallbackInfo& info) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(info.GetIsolate()); + HandleScope scope(env->isolate()); #ifdef __POSIX__ int size = 0; while (environ[size]) size++; - Local env = Array::New(size); + Local envarr = Array::New(size); for (int i = 0; i < size; ++i) { const char* var = environ[i]; const char* s = strchr(var, '='); const int length = s ? s - var : strlen(var); - Local name = String::NewFromUtf8(node_isolate, + Local name = String::NewFromUtf8(env->isolate(), var, String::kNormalString, length); - env->Set(i, name); + envarr->Set(i, name); } #else // _WIN32 WCHAR* environment = GetEnvironmentStringsW(); if (environment == NULL) return; // This should not happen. - Local env = Array::New(); + Local envarr = Array::New(); WCHAR* p = environment; int i = 0; while (*p != NULL) { @@ -2320,51 +2363,51 @@ static void EnvEnumerator(const PropertyCallbackInfo& info) { } const uint16_t* two_byte_buffer = reinterpret_cast(p); const size_t two_byte_buffer_len = s - p; - Local value = String::NewFromTwoByte(node_isolate, + Local value = String::NewFromTwoByte(env->isolate(), two_byte_buffer, String::kNormalString, two_byte_buffer_len); - env->Set(i++, value); + envarr->Set(i++, value); p = s + wcslen(s) + 1; } FreeEnvironmentStringsW(environment); #endif - info.GetReturnValue().Set(env); + info.GetReturnValue().Set(envarr); } -static Handle GetFeatures() { - HandleScope scope(node_isolate); +static Handle GetFeatures(Environment* env) { + HandleScope scope(env->isolate()); Local obj = Object::New(); #if defined(DEBUG) && DEBUG - Local debug = True(node_isolate); + Local debug = True(env->isolate()); #else - Local debug = False(node_isolate); + Local debug = False(env->isolate()); #endif // defined(DEBUG) && DEBUG - obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "debug"), debug); + obj->Set(env->debug_string(), debug); - obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "uv"), True(node_isolate)); + obj->Set(env->uv_string(), True(env->isolate())); // TODO(bnoordhuis) ping libuv - obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "ipv6"), True(node_isolate)); + obj->Set(env->ipv6_lc_string(), True(env->isolate())); #ifdef OPENSSL_NPN_NEGOTIATED - Local tls_npn = True(node_isolate); + Local tls_npn = True(env->isolate()); #else - Local tls_npn = False(node_isolate); + Local tls_npn = False(env->isolate()); #endif - obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls_npn"), tls_npn); + obj->Set(env->tls_npn_string(), tls_npn); #ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB - Local tls_sni = True(node_isolate); + Local tls_sni = True(env->isolate()); #else - Local tls_sni = False(node_isolate); + Local tls_sni = False(env->isolate()); #endif - obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls_sni"), tls_sni); + obj->Set(env->tls_sni_string(), tls_sni); - obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls"), + obj->Set(env->tls_string(), Boolean::New(get_builtin_module("crypto") != NULL)); return scope.Close(obj); @@ -2373,7 +2416,8 @@ static Handle GetFeatures() { static void DebugPortGetter(Local property, const PropertyCallbackInfo& info) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(info.GetIsolate()); + HandleScope scope(env->isolate()); info.GetReturnValue().Set(debug_port); } @@ -2381,7 +2425,8 @@ static void DebugPortGetter(Local property, static void DebugPortSetter(Local property, Local value, const PropertyCallbackInfo& info) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(info.GetIsolate()); + HandleScope scope(env->isolate()); debug_port = value->NumberValue(); } @@ -2469,7 +2514,7 @@ void StopProfilerIdleNotifier(const FunctionCallbackInfo& args) { #define READONLY_PROPERTY(obj, str, var) \ do { \ - obj->Set(OneByteString(node_isolate, str), var, v8::ReadOnly); \ + obj->Set(OneByteString(env->isolate(), str), var, v8::ReadOnly); \ } while (0) @@ -2478,18 +2523,18 @@ void SetupProcessObject(Environment* env, const char* const* argv, int exec_argc, const char* const* exec_argv) { - HandleScope scope(node_isolate); + HandleScope scope(env->isolate()); Local process = env->process_object(); - process->SetAccessor(FIXED_ONE_BYTE_STRING(node_isolate, "title"), + process->SetAccessor(env->title_string(), ProcessTitleGetter, ProcessTitleSetter); // process.version READONLY_PROPERTY(process, "version", - FIXED_ONE_BYTE_STRING(node_isolate, NODE_VERSION)); + FIXED_ONE_BYTE_STRING(env->isolate(), NODE_VERSION)); // process.moduleLoadList READONLY_PROPERTY(process, @@ -2505,26 +2550,27 @@ void SetupProcessObject(Environment* env, NODE_STRINGIFY(HTTP_PARSER_VERSION_MINOR); READONLY_PROPERTY(versions, "http_parser", - FIXED_ONE_BYTE_STRING(node_isolate, http_parser_version)); + FIXED_ONE_BYTE_STRING(env->isolate(), http_parser_version)); // +1 to get rid of the leading 'v' READONLY_PROPERTY(versions, "node", - OneByteString(node_isolate, NODE_VERSION + 1)); + OneByteString(env->isolate(), NODE_VERSION + 1)); READONLY_PROPERTY(versions, "v8", - OneByteString(node_isolate, V8::GetVersion())); + OneByteString(env->isolate(), V8::GetVersion())); READONLY_PROPERTY(versions, "uv", - OneByteString(node_isolate, uv_version_string())); + OneByteString(env->isolate(), uv_version_string())); READONLY_PROPERTY(versions, "zlib", - FIXED_ONE_BYTE_STRING(node_isolate, ZLIB_VERSION)); + FIXED_ONE_BYTE_STRING(env->isolate(), ZLIB_VERSION)); const char node_modules_version[] = NODE_STRINGIFY(NODE_MODULE_VERSION); - READONLY_PROPERTY(versions, - "modules", - FIXED_ONE_BYTE_STRING(node_isolate, node_modules_version)); + READONLY_PROPERTY( + versions, + "modules", + FIXED_ONE_BYTE_STRING(env->isolate(), node_modules_version)); #if HAVE_OPENSSL // Stupid code to slice out the version string. @@ -2545,31 +2591,31 @@ void SetupProcessObject(Environment* env, READONLY_PROPERTY( versions, "openssl", - OneByteString(node_isolate, &OPENSSL_VERSION_TEXT[i], j - i)); + OneByteString(env->isolate(), &OPENSSL_VERSION_TEXT[i], j - i)); } #endif // process.arch - READONLY_PROPERTY(process, "arch", OneByteString(node_isolate, ARCH)); + READONLY_PROPERTY(process, "arch", OneByteString(env->isolate(), ARCH)); // process.platform READONLY_PROPERTY(process, "platform", - OneByteString(node_isolate, PLATFORM)); + OneByteString(env->isolate(), PLATFORM)); // process.argv Local arguments = Array::New(argc); for (int i = 0; i < argc; ++i) { - arguments->Set(i, String::NewFromUtf8(node_isolate, argv[i])); + arguments->Set(i, String::NewFromUtf8(env->isolate(), argv[i])); } - process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "argv"), arguments); + process->Set(env->argv_string(), arguments); // process.execArgv Local exec_arguments = Array::New(exec_argc); for (int i = 0; i < exec_argc; ++i) { - exec_arguments->Set(i, String::NewFromUtf8(node_isolate, exec_argv[i])); + exec_arguments->Set(i, String::NewFromUtf8(env->isolate(), exec_argv[i])); } - process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "execArgv"), exec_arguments); + process->Set(env->exec_argv_string(), exec_arguments); // create process.env Local process_env_template = ObjectTemplate::New(); @@ -2580,12 +2626,11 @@ void SetupProcessObject(Environment* env, EnvEnumerator, Object::New()); Local process_env = process_env_template->NewInstance(); - process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "env"), process_env); + process->Set(env->env_string(), process_env); - READONLY_PROPERTY(process, "pid", Integer::New(getpid(), node_isolate)); - READONLY_PROPERTY(process, "features", GetFeatures()); - process->SetAccessor( - FIXED_ONE_BYTE_STRING(node_isolate, "_needImmediateCallback"), + READONLY_PROPERTY(process, "pid", Integer::New(getpid(), env->isolate())); + READONLY_PROPERTY(process, "features", GetFeatures(env)); + process->SetAccessor(env->need_imm_cb_string(), NeedImmediateCallbackGetter, NeedImmediateCallbackSetter); @@ -2593,50 +2638,49 @@ void SetupProcessObject(Environment* env, if (eval_string) { READONLY_PROPERTY(process, "_eval", - String::NewFromUtf8(node_isolate, eval_string)); + String::NewFromUtf8(env->isolate(), eval_string)); } // -p, --print if (print_eval) { - READONLY_PROPERTY(process, "_print_eval", True(node_isolate)); + READONLY_PROPERTY(process, "_print_eval", True(env->isolate())); } // -i, --interactive if (force_repl) { - READONLY_PROPERTY(process, "_forceRepl", True(node_isolate)); + READONLY_PROPERTY(process, "_forceRepl", True(env->isolate())); } // --no-deprecation if (no_deprecation) { - READONLY_PROPERTY(process, "noDeprecation", True(node_isolate)); + READONLY_PROPERTY(process, "noDeprecation", True(env->isolate())); } // --throw-deprecation if (throw_deprecation) { - READONLY_PROPERTY(process, "throwDeprecation", True(node_isolate)); + READONLY_PROPERTY(process, "throwDeprecation", True(env->isolate())); } // --trace-deprecation if (trace_deprecation) { - READONLY_PROPERTY(process, "traceDeprecation", True(node_isolate)); + READONLY_PROPERTY(process, "traceDeprecation", True(env->isolate())); } size_t exec_path_len = 2 * PATH_MAX; char* exec_path = new char[exec_path_len]; Local exec_path_value; if (uv_exepath(exec_path, &exec_path_len) == 0) { - exec_path_value = String::NewFromUtf8(node_isolate, + exec_path_value = String::NewFromUtf8(env->isolate(), exec_path, String::kNormalString, exec_path_len); } else { - exec_path_value = String::NewFromUtf8(node_isolate, argv[0]); + exec_path_value = String::NewFromUtf8(env->isolate(), argv[0]); } - process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "execPath"), - exec_path_value); + process->Set(env->exec_path_string(), exec_path_value); delete[] exec_path; - process->SetAccessor(FIXED_ONE_BYTE_STRING(node_isolate, "debugPort"), + process->SetAccessor(env->debug_port_string(), DebugPortGetter, DebugPortSetter); @@ -2693,10 +2737,10 @@ void SetupProcessObject(Environment* env, env->tick_info()->fields(), kExternalUnsignedIntArray, env->tick_info()->fields_count()); - process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_tickInfo"), tick_info_obj); + process->Set(env->tick_info_string(), tick_info_obj); // pre-set _events object for faster emit checks - process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_events"), Object::New()); + process->Set(env->events_string(), Object::New()); } @@ -2719,7 +2763,8 @@ static void SignalExit(int signal) { // when debugging the stream.Writable class or the process.nextTick // function, it is useful to bypass JavaScript entirely. static void RawDebug(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); assert(args.Length() == 1 && args[0]->IsString() && "must be called with a single string"); @@ -2731,7 +2776,7 @@ static void RawDebug(const FunctionCallbackInfo& args) { void Load(Environment* env) { - HandleScope handle_scope(node_isolate); + HandleScope handle_scope(env->isolate()); // Compile, execute the src/node.js file. (Which was included as static C // string in node_natives.h. 'natve_node' is the string containing that @@ -2748,7 +2793,7 @@ void Load(Environment* env) { try_catch.SetVerbose(false); Local script_name = FIXED_ONE_BYTE_STRING(env->isolate(), "node.js"); - Local f_value = ExecuteString(env, MainSource(), script_name); + Local f_value = ExecuteString(env, MainSource(env), script_name); if (try_catch.HasCaught()) { ReportException(env, try_catch); exit(10); @@ -2768,11 +2813,11 @@ void Load(Environment* env) { Local global = env->context()->Global(); #if defined HAVE_DTRACE || defined HAVE_ETW - InitDTrace(global); + InitDTrace(env, global); #endif #if defined HAVE_PERFCTR - InitPerfCounters(global); + InitPerfCounters(env, global); #endif // Enable handling of uncaught exceptions @@ -2977,9 +3022,8 @@ static void DispatchMessagesDebugAgentCallback() { // Called from the main thread. -static void EnableDebug(bool wait_connect) { +static void EnableDebug(Isolate* isolate, bool wait_connect) { assert(debugger_running == false); - Isolate* isolate = node_isolate; // TODO(bnoordhuis) Multi-isolate support. Isolate::Scope isolate_scope(isolate); HandleScope handle_scope(isolate); v8::Debug::SetDebugMessageDispatchHandler(DispatchMessagesDebugAgentCallback, @@ -3015,7 +3059,7 @@ static void EnableDebug(bool wait_connect) { static void DispatchDebugMessagesAsyncCallback(uv_async_t* handle, int status) { if (debugger_running == false) { fprintf(stderr, "Starting debugger agent.\n"); - EnableDebug(false); + EnableDebug(node_isolate, false); } Isolate::Scope isolate_scope(node_isolate); v8::Debug::ProcessDebugMessages(); @@ -3056,10 +3100,11 @@ static void RegisterSignalHandler(int signal, void (*handler)(int signal)) { void DebugProcess(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); if (args.Length() != 1) { - return ThrowError("Invalid number of arguments."); + return env->ThrowError("Invalid number of arguments."); } pid_t pid; @@ -3068,7 +3113,7 @@ void DebugProcess(const FunctionCallbackInfo& args) { pid = args[0]->IntegerValue(); r = kill(pid, SIGUSR1); if (r != 0) { - return ThrowErrnoException(errno, "kill"); + return env->ThrowErrnoException(errno, "kill"); } } @@ -3143,7 +3188,8 @@ static int RegisterDebugSignalHandler() { static void DebugProcess(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); DWORD pid; HANDLE process = NULL; HANDLE thread = NULL; @@ -3152,7 +3198,7 @@ static void DebugProcess(const FunctionCallbackInfo& args) { LPTHREAD_START_ROUTINE* handler = NULL; if (args.Length() != 1) { - ThrowError("Invalid number of arguments."); + env->ThrowError("Invalid number of arguments."); goto out; } @@ -3164,20 +3210,23 @@ static void DebugProcess(const FunctionCallbackInfo& args) { FALSE, pid); if (process == NULL) { - ThrowException(WinapiErrnoException(GetLastError(), "OpenProcess")); + env->ThrowException( + WinapiErrnoException(env, GetLastError(), "OpenProcess")); goto out; } if (GetDebugSignalHandlerMappingName(pid, mapping_name, ARRAY_SIZE(mapping_name)) < 0) { - ThrowErrnoException(errno, "sprintf"); + env->ThrowErrnoException(errno, "sprintf"); goto out; } mapping = OpenFileMappingW(FILE_MAP_READ, FALSE, mapping_name); if (mapping == NULL) { - ThrowException(WinapiErrnoException(GetLastError(), "OpenFileMappingW")); + env->ThrowException(WinapiErrnoException(env, + GetLastError(), + "OpenFileMappingW")); goto out; } @@ -3188,7 +3237,8 @@ static void DebugProcess(const FunctionCallbackInfo& args) { 0, sizeof *handler)); if (handler == NULL || *handler == NULL) { - ThrowException(WinapiErrnoException(GetLastError(), "MapViewOfFile")); + env->ThrowException( + WinapiErrnoException(env, GetLastError(), "MapViewOfFile")); goto out; } @@ -3200,13 +3250,17 @@ static void DebugProcess(const FunctionCallbackInfo& args) { 0, NULL); if (thread == NULL) { - ThrowException(WinapiErrnoException(GetLastError(), "CreateRemoteThread")); + env->ThrowException(WinapiErrnoException(env, + GetLastError(), + "CreateRemoteThread")); goto out; } // Wait for the thread to terminate if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) { - ThrowException(WinapiErrnoException(GetLastError(), "WaitForSingleObject")); + env->ThrowException(WinapiErrnoException(env, + GetLastError(), + "WaitForSingleObject")); goto out; } @@ -3224,7 +3278,7 @@ static void DebugProcess(const FunctionCallbackInfo& args) { static void DebugPause(const FunctionCallbackInfo& args) { - v8::Debug::DebugBreak(node_isolate); + v8::Debug::DebugBreak(args.GetIsolate()); } @@ -3329,7 +3383,7 @@ void Init(int* argc, // If the --debug flag was specified then initialize the debug thread. if (use_debug_agent) { - EnableDebug(debug_wait_connect); + EnableDebug(node_isolate, debug_wait_connect); } else { RegisterDebugSignalHandler(); } @@ -3373,15 +3427,14 @@ int EmitExit(Environment* env) { HandleScope handle_scope(env->isolate()); Context::Scope context_scope(env->context()); Local process_object = env->process_object(); - process_object->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_exiting"), - True(node_isolate)); + process_object->Set(env->exiting_string(), True(env->isolate())); - Handle exitCode = FIXED_ONE_BYTE_STRING(node_isolate, "exitCode"); + Handle exitCode = env->exit_code_string(); int code = process_object->Get(exitCode)->IntegerValue(); Local args[] = { - FIXED_ONE_BYTE_STRING(node_isolate, "exit"), - Integer::New(code, node_isolate) + env->exit_string(), + Integer::New(code, env->isolate()) }; MakeCallback(env, process_object, "emit", ARRAY_SIZE(args), args); diff --git a/src/node.h b/src/node.h index c07b3ae3de..3c48c79805 100644 --- a/src/node.h +++ b/src/node.h @@ -61,19 +61,47 @@ #include "v8.h" // NOLINT(build/include_order) #include "node_version.h" // NODE_MODULE_VERSION +#define NODE_DEPRECATED(msg, fn) V8_DEPRECATED(msg, fn) + // Forward-declare these functions now to stop MSVS from becoming // terminally confused when it's done in node_internals.h namespace node { -NODE_EXTERN v8::Local ErrnoException(int errorno, +NODE_EXTERN v8::Local ErrnoException(v8::Isolate* isolate, + int errorno, const char* syscall = NULL, const char* message = NULL, const char* path = NULL); -NODE_EXTERN v8::Local UVException(int errorno, +NODE_EXTERN v8::Local UVException(v8::Isolate* isolate, + int errorno, const char* syscall = NULL, const char* message = NULL, const char* path = NULL); +NODE_DEPRECATED("Use UVException(isolate, ...)", + inline v8::Local ErrnoException( + int errorno, + const char* syscall = NULL, + const char* message = NULL, + const char* path = NULL) { + return ErrnoException(v8::Isolate::GetCurrent(), + errorno, + syscall, + message, + path); +}) + +inline v8::Local UVException(int errorno, + const char* syscall = NULL, + const char* message = NULL, + const char* path = NULL) { + return UVException(v8::Isolate::GetCurrent(), + errorno, + syscall, + message, + path); +} + /* * MakeCallback doesn't have a HandleScope. That means the callers scope * will retain ownership of created handles from MakeCallback and related. @@ -187,27 +215,79 @@ inline void NODE_SET_PROTOTYPE_METHOD(v8::Handle recv, #define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD enum encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER}; -enum encoding ParseEncoding(v8::Handle encoding_v, +enum encoding ParseEncoding(v8::Isolate* isolate, + v8::Handle encoding_v, enum encoding _default = BINARY); -NODE_EXTERN void FatalException(const v8::TryCatch& try_catch); - -NODE_EXTERN v8::Local Encode(const void *buf, size_t len, +NODE_DEPRECATED("Use ParseEncoding(isolate, ...)", + inline enum encoding ParseEncoding( + v8::Handle encoding_v, + enum encoding _default = BINARY) { + return ParseEncoding(v8::Isolate::GetCurrent(), encoding_v, _default); +}) + +NODE_EXTERN void FatalException(v8::Isolate* isolate, + const v8::TryCatch& try_catch); + +NODE_DEPRECATED("Use FatalException(isolate, ...)", + inline void FatalException(const v8::TryCatch& try_catch) { + return FatalException(v8::Isolate::GetCurrent(), try_catch); +}) + +NODE_EXTERN v8::Local Encode(v8::Isolate* isolate, + const void* buf, + size_t len, enum encoding encoding = BINARY); +NODE_DEPRECATED("Use Encode(isolate, ...)", + inline v8::Local Encode( + const void* buf, + size_t len, + enum encoding encoding = BINARY) { + return Encode(v8::Isolate::GetCurrent(), buf, len, encoding); +}) // Returns -1 if the handle was not valid for decoding -NODE_EXTERN ssize_t DecodeBytes(v8::Handle, +NODE_EXTERN ssize_t DecodeBytes(v8::Isolate* isolate, + v8::Handle, enum encoding encoding = BINARY); +NODE_DEPRECATED("Use DecodeBytes(isolate, ...)", + inline ssize_t DecodeBytes( + v8::Handle val, + enum encoding encoding = BINARY) { + return DecodeBytes(v8::Isolate::GetCurrent(), val, encoding); +}) // returns bytes written. -NODE_EXTERN ssize_t DecodeWrite(char *buf, +NODE_EXTERN ssize_t DecodeWrite(v8::Isolate* isolate, + char* buf, size_t buflen, v8::Handle, enum encoding encoding = BINARY); +NODE_DEPRECATED("Use DecodeWrite(isolate, ...)", + inline ssize_t DecodeWrite(char* buf, + size_t buflen, + v8::Handle val, + enum encoding encoding = BINARY) { + return DecodeWrite(v8::Isolate::GetCurrent(), buf, buflen, val, encoding); +}) #ifdef _WIN32 -NODE_EXTERN v8::Local WinapiErrnoException(int errorno, - const char *syscall = NULL, const char *msg = "", +NODE_EXTERN v8::Local WinapiErrnoException( + v8::Isolate* isolate, + int errorno, + const char *syscall = NULL, + const char *msg = "", const char *path = NULL); + +NODE_DEPRECATED("Use WinapiErrnoException(isolate, ...)", + inline v8::Local WinapiErrnoException(int errorno, + const char *syscall = NULL, const char *msg = "", + const char *path = NULL) { + return WinapiErrnoException(v8::Isolate::GetCurrent(), + errorno, + syscall, + msg, + path); +}) #endif const char *signo_string(int errorno); diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 5a3803e978..8304c4f02f 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -37,7 +37,9 @@ #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define CHECK_NOT_OOB(r) \ - do { if (!(r)) return ThrowRangeError("out of range index"); } while (0) + do { \ + if (!(r)) return env->ThrowRangeError("out of range index"); \ + } while (0) #define ARGS_THIS(argT) \ Local obj = argT; \ @@ -66,6 +68,7 @@ using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Handle; using v8::HandleScope; +using v8::Isolate; using v8::Local; using v8::Number; using v8::Object; @@ -113,23 +116,22 @@ size_t Length(Handle obj) { } -Local New(Handle string, enum encoding enc) { - HandleScope scope(node_isolate); +Local New(Isolate* isolate, Handle string, enum encoding enc) { + HandleScope scope(isolate); - size_t length = StringBytes::Size(string, enc); + size_t length = StringBytes::Size(isolate, string, enc); Local buf = New(length); char* data = Buffer::Data(buf); - StringBytes::Write(data, length, string, enc); + StringBytes::Write(isolate, data, length, string, enc); return scope.Close(buf); } -Local New(size_t length) { - HandleScope handle_scope(node_isolate); - Environment* env = Environment::GetCurrent(node_isolate); - Local obj = Buffer::New(env, length); +Local New(Isolate* isolate, size_t length) { + HandleScope handle_scope(isolate); + Local obj = Buffer::New(Environment::GetCurrent(isolate), length); return handle_scope.Close(obj); } @@ -137,11 +139,11 @@ Local New(size_t length) { // TODO(trevnorris): these have a flaw by needing to call the Buffer inst then // Alloc. continue to look for a better architecture. Local New(Environment* env, size_t length) { - HandleScope scope(node_isolate); + HandleScope scope(env->isolate()); assert(length <= kMaxLength); - Local arg = Uint32::NewFromUnsigned(length, node_isolate); + Local arg = Uint32::NewFromUnsigned(length, env->isolate()); Local obj = env->buffer_constructor_function()->NewInstance(1, &arg); // TODO(trevnorris): done like this to handle HasInstance since only checks @@ -155,15 +157,15 @@ Local New(Environment* env, size_t length) { } else { data = NULL; } - smalloc::Alloc(obj, data, length); + smalloc::Alloc(env, obj, data, length); return scope.Close(obj); } -Local New(const char* data, size_t length) { - HandleScope handle_scope(node_isolate); - Environment* env = Environment::GetCurrent(node_isolate); +Local New(Isolate* isolate, const char* data, size_t length) { + Environment* env = Environment::GetCurrent(isolate); + HandleScope handle_scope(env->isolate()); Local obj = Buffer::New(env, data, length); return handle_scope.Close(obj); } @@ -173,11 +175,11 @@ Local New(const char* data, size_t length) { // but for consistency w/ the other should use data. And a copy version renamed // to something else. Local New(Environment* env, const char* data, size_t length) { - HandleScope scope(node_isolate); + HandleScope scope(env->isolate()); assert(length <= kMaxLength); - Local arg = Uint32::NewFromUnsigned(length, node_isolate); + Local arg = Uint32::NewFromUnsigned(length, env->isolate()); Local obj = env->buffer_constructor_function()->NewInstance(1, &arg); // TODO(trevnorris): done like this to handle HasInstance since only checks @@ -193,18 +195,19 @@ Local New(Environment* env, const char* data, size_t length) { new_data = NULL; } - smalloc::Alloc(obj, new_data, length); + smalloc::Alloc(env, obj, new_data, length); return scope.Close(obj); } -Local New(char* data, +Local New(Isolate* isolate, + char* data, size_t length, smalloc::FreeCallback callback, void* hint) { - HandleScope handle_scope(node_isolate); - Environment* env = Environment::GetCurrent(node_isolate); + Environment* env = Environment::GetCurrent(isolate); + HandleScope handle_scope(env->isolate()); Local obj = Buffer::New(env, data, length, callback, hint); return handle_scope.Close(obj); } @@ -215,36 +218,36 @@ Local New(Environment* env, size_t length, smalloc::FreeCallback callback, void* hint) { - HandleScope scope(node_isolate); + HandleScope scope(env->isolate()); assert(length <= kMaxLength); - Local arg = Uint32::NewFromUnsigned(length, node_isolate); + Local arg = Uint32::NewFromUnsigned(length, env->isolate()); Local obj = env->buffer_constructor_function()->NewInstance(1, &arg); - smalloc::Alloc(obj, data, length, callback, hint); + smalloc::Alloc(env, obj, data, length, callback, hint); return scope.Close(obj); } -Local Use(char* data, uint32_t length) { - HandleScope handle_scope(node_isolate); - Environment* env = Environment::GetCurrent(node_isolate); +Local Use(Isolate* isolate, char* data, uint32_t length) { + Environment* env = Environment::GetCurrent(isolate); + HandleScope handle_scope(env->isolate()); Local obj = Buffer::Use(env, data, length); return handle_scope.Close(obj); } Local Use(Environment* env, char* data, uint32_t length) { - HandleScope scope(node_isolate); + HandleScope scope(env->isolate()); assert(length <= kMaxLength); - Local arg = Uint32::NewFromUnsigned(length, node_isolate); + Local arg = Uint32::NewFromUnsigned(length, env->isolate()); Local obj = env->buffer_constructor_function()->NewInstance(1, &arg); - smalloc::Alloc(obj, data, length); + smalloc::Alloc(env, obj, data, length); return scope.Close(obj); } @@ -252,13 +255,14 @@ Local Use(Environment* env, char* data, uint32_t length) { template void StringSlice(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); ARGS_THIS(args.This()) SLICE_START_END(args[0], args[1], obj_length) args.GetReturnValue().Set( - StringBytes::Encode(obj_data + start, length, encoding)); + StringBytes::Encode(env->isolate(), obj_data + start, length, encoding)); } @@ -294,12 +298,13 @@ void Base64Slice(const FunctionCallbackInfo& args) { // bytesCopied = buffer.copy(target[, targetStart][, sourceStart][, sourceEnd]); void Copy(const FunctionCallbackInfo &args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); Local target = args[0]->ToObject(); if (!HasInstance(target)) - return ThrowTypeError("first arg should be a Buffer"); + return env->ThrowTypeError("first arg should be a Buffer"); ARGS_THIS(args.This()) size_t target_length = target->GetIndexedPropertiesExternalArrayDataLength(); @@ -318,7 +323,7 @@ void Copy(const FunctionCallbackInfo &args) { return args.GetReturnValue().Set(0); if (source_start > obj_length) - return ThrowRangeError("out of range index"); + return env->ThrowRangeError("out of range index"); if (source_end - source_start > target_length - target_start) source_end = source_start + target_length - target_start; @@ -334,7 +339,8 @@ void Copy(const FunctionCallbackInfo &args) { // buffer.fill(value[, start][, end]); void Fill(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); ARGS_THIS(args.This()) SLICE_START_END(args[1], args[2], obj_length) @@ -379,17 +385,18 @@ void Fill(const FunctionCallbackInfo& args) { template void StringWrite(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); ARGS_THIS(args.This()) if (!args[0]->IsString()) - return ThrowTypeError("Argument must be a string"); + return env->ThrowTypeError("Argument must be a string"); Local str = args[0]->ToString(); if (encoding == HEX && str->Length() % 2 != 0) - return ThrowTypeError("Invalid hex string"); + return env->ThrowTypeError("Invalid hex string"); size_t offset; size_t max_length; @@ -406,9 +413,10 @@ void StringWrite(const FunctionCallbackInfo& args) { max_length = max_length / 2; if (offset >= obj_length) - return ThrowRangeError("Offset is out of bounds"); + return env->ThrowRangeError("Offset is out of bounds"); - uint32_t written = StringBytes::Write(obj_data + offset, + uint32_t written = StringBytes::Write(env->isolate(), + obj_data + offset, max_length, str, encoding, @@ -459,6 +467,7 @@ static inline void Swizzle(char* start, unsigned int len) { template void ReadFloatGeneric(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args.GetIsolate()); bool doAssert = !args[1]->BooleanValue(); size_t offset; @@ -467,7 +476,7 @@ void ReadFloatGeneric(const FunctionCallbackInfo& args) { if (doAssert) { size_t len = Length(args.This()); if (offset + sizeof(T) > len || offset + sizeof(T) < offset) - return ThrowRangeError("Trying to read beyond buffer length"); + return env->ThrowRangeError("Trying to read beyond buffer length"); } union NoAlias { @@ -508,20 +517,21 @@ void ReadDoubleBE(const FunctionCallbackInfo& args) { template uint32_t WriteFloatGeneric(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args.GetIsolate()); bool doAssert = !args[2]->BooleanValue(); T val = static_cast(args[0]->NumberValue()); size_t offset; if (!ParseArrayIndex(args[1], 0, &offset)) { - ThrowRangeError("out of range index"); + env->ThrowRangeError("out of range index"); return 0; } if (doAssert) { size_t len = Length(args.This()); if (offset + sizeof(T) > len || offset + sizeof(T) < offset) { - ThrowRangeError("Trying to write beyond buffer length"); + env->ThrowRangeError("Trying to write beyond buffer length"); return 0; } } @@ -562,7 +572,8 @@ void WriteDoubleBE(const FunctionCallbackInfo& args) { void ToArrayBuffer(const FunctionCallbackInfo& args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); ARGS_THIS(args.This()); void* adata = malloc(obj_length); @@ -581,30 +592,30 @@ void ToArrayBuffer(const FunctionCallbackInfo& args) { void ByteLength(const FunctionCallbackInfo &args) { - HandleScope scope(node_isolate); + Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); if (!args[0]->IsString()) - return ThrowTypeError("Argument must be a string"); + return env->ThrowTypeError("Argument must be a string"); Local s = args[0]->ToString(); - enum encoding e = ParseEncoding(args[1], UTF8); + enum encoding e = ParseEncoding(env->isolate(), args[1], UTF8); - uint32_t size = StringBytes::Size(s, e); + uint32_t size = StringBytes::Size(env->isolate(), s, e); args.GetReturnValue().Set(size); } // pass Buffer object to load prototype methods void SetupBufferJS(const FunctionCallbackInfo& args) { - HandleScope handle_scope(args.GetIsolate()); Environment* env = Environment::GetCurrent(args.GetIsolate()); + HandleScope scope(env->isolate()); assert(args[0]->IsFunction()); Local bv = args[0].As(); env->set_buffer_constructor_function(bv); - Local proto_v = - bv->Get(FIXED_ONE_BYTE_STRING(node_isolate, "prototype")); + Local proto_v = bv->Get(env->prototype_string()); assert(proto_v->IsObject()); @@ -640,15 +651,15 @@ void SetupBufferJS(const FunctionCallbackInfo& args) { NODE_SET_METHOD(proto, "fill", Fill); // for backwards compatibility - proto->Set(FIXED_ONE_BYTE_STRING(node_isolate, "offset"), - Uint32::New(0, node_isolate), + proto->Set(env->offset_string(), + Uint32::New(0, env->isolate()), v8::ReadOnly); assert(args[1]->IsObject()); Local internal = args[1].As(); - internal->Set(FIXED_ONE_BYTE_STRING(node_isolate, "byteLength"), + internal->Set(env->byte_length_string(), FunctionTemplate::New(ByteLength)->GetFunction()); } diff --git a/src/node_buffer.h b/src/node_buffer.h index 9b004bdc82..1d58c309bc 100644 --- a/src/node_buffer.h +++ b/src/node_buffer.h @@ -43,22 +43,52 @@ NODE_EXTERN size_t Length(v8::Handle val); NODE_EXTERN size_t Length(v8::Handle val); // public constructor -NODE_EXTERN v8::Local New(size_t length); +NODE_EXTERN v8::Local New(v8::Isolate* isolate, size_t length); +NODE_DEPRECATED("Use New(isolate, ...)", + inline v8::Local New(size_t length) { + return New(v8::Isolate::GetCurrent(), length); +}) // public constructor from string -NODE_EXTERN v8::Local New(v8::Handle string, +NODE_EXTERN v8::Local New(v8::Isolate* isolate, + v8::Handle string, enum encoding enc = UTF8); +NODE_DEPRECATED("Use New(isolate, ...)", + inline v8::Local New(v8::Handle string, + enum encoding enc = UTF8) { + return New(v8::Isolate::GetCurrent(), string, enc); +}) // public constructor - data is copied // TODO(trevnorris): should be something like Copy() -NODE_EXTERN v8::Local New(const char* data, size_t len); +NODE_EXTERN v8::Local New(v8::Isolate* isolate, + const char* data, + size_t len); +NODE_DEPRECATED("Use New(isolate, ...)", + inline v8::Local New(const char* data, size_t len) { + return New(v8::Isolate::GetCurrent(), data, len); +}) // public constructor - data is used, callback is passed data on object gc -NODE_EXTERN v8::Local New(char* data, +NODE_EXTERN v8::Local New(v8::Isolate* isolate, + char* data, size_t length, smalloc::FreeCallback callback, void* hint); +NODE_DEPRECATED("Use New(isolate, ...)", + inline v8::Local New(char* data, + size_t length, + smalloc::FreeCallback callback, + void* hint) { + return New(v8::Isolate::GetCurrent(), data, length, callback, hint); +}) // public constructor - data is used. // TODO(trevnorris): should be New() for consistency -NODE_EXTERN v8::Local Use(char* data, uint32_t len); +NODE_EXTERN v8::Local Use(v8::Isolate* isolate, + char* data, + uint32_t len); +NODE_DEPRECATED("Use Use(isolate, ...)", + inline v8::Local Use(char* data, uint32_t len) { + return Use(v8::Isolate::GetCurrent(), data, len); +}) // This is verbose to be explicit with inline commenting static inline bool IsWithinBounds(size_t off, size_t len, size_t max) { diff --git a/src/node_contextify.cc b/src/node_contextify.cc index 4ef6126a46..05d2b11c3f 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -128,7 +128,7 @@ class ContextifyContext { // should be fixed properly in V8, and this copy function should be // removed once there is a better way. void CopyProperties() { - HandleScope scope(node_isolate); + HandleScope scope(env()->isolate()); Local context = PersistentToLocal(env()->isolate(), context_); Local global = context->Global()->GetPrototype()->ToObject(); @@ -155,7 +155,7 @@ class ContextifyContext { // which doesn't faithfully capture the full range of configurations // that can be done using Object.defineProperty. if (clone_property_method.IsEmpty()) { - Local code = FIXED_ONE_BYTE_STRING(node_isolate, + Local code = FIXED_ONE_BYTE_STRING(env()->isolate(), "(function cloneProperty(source, key, target) {\n" " if (key === 'Proxy') return;\n" " try {\n" @@ -167,7 +167,7 @@ class ContextifyContext { " }\n" "})"); - Local fname = FIXED_ONE_BYTE_STRING(node_isolate, + Local fname = FIXED_ONE_BYTE_STRING(env()->isolate(), "binding:script"); Local