From: Ben Noordhuis Date: Wed, 7 Aug 2013 19:50:41 +0000 (+0200) Subject: src: use v8::String::NewFrom*() functions X-Git-Tag: v0.11.6~77 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f674b09f40d22915e15b6968aafc5d25ac8178a2;p=platform%2Fupstream%2Fnodejs.git src: use v8::String::NewFrom*() functions * Change calls to String::New() and String::NewSymbol() to their respective one-byte, two-byte and UTF-8 counterparts. * Add a FIXED_ONE_BYTE_STRING macro that takes a string literal and turns it into a v8::Local. * Add helper functions that make v8::String::NewFromOneByte() easier to work with. Said function expects a `const uint8_t*` but almost every call site deals with `const char*` or `const unsigned char*`. Helps us avoid doing reinterpret_casts all over the place. * Code that handles file system paths keeps using UTF-8 for backwards compatibility reasons. At least now the use of UTF-8 is explicit. * Remove v8::String::NewSymbol() entirely. Almost all call sites were effectively minor de-optimizations. If you create a string only once, there is no point in making it a symbol. If you are create the same string repeatedly, it should probably be cached in a persistent handle. --- diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index d782ad6..746dc02 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -206,7 +206,7 @@ static Local HostentToAddresses(struct hostent* host) { for (int i = 0; host->h_addr_list[i]; ++i) { uv_inet_ntop(host->h_addrtype, host->h_addr_list[i], ip, sizeof(ip)); - Local address = String::New(ip); + Local address = OneByteString(node_isolate, ip); addresses->Set(Integer::New(i, node_isolate), address); } @@ -219,7 +219,7 @@ static Local HostentToNames(struct hostent* host) { Local names = Array::New(); for (int i = 0; host->h_aliases[i]; ++i) { - Local address = String::New(host->h_aliases[i]); + Local address = OneByteString(node_isolate, host->h_aliases[i]); names->Set(Integer::New(i, node_isolate), address); } @@ -426,7 +426,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, String::New(host->h_name)); + result->Set(0, OneByteString(node_isolate, host->h_name)); ares_free_hostent(host); this->CallOnComplete(result); @@ -456,14 +456,18 @@ class QueryMxWrap: public QueryWrap { } Local mx_records = Array::New(); - Local exchange_symbol = String::NewSymbol("exchange"); - Local priority_symbol = String::NewSymbol("priority"); + Local exchange_symbol = + FIXED_ONE_BYTE_STRING(node_isolate, "exchange"); + Local priority_symbol = + FIXED_ONE_BYTE_STRING(node_isolate, "priority"); + int i = 0; for (struct ares_mx_reply* mx_current = mx_start; mx_current; mx_current = mx_current->next) { Local mx_record = Object::New(); - mx_record->Set(exchange_symbol, String::New(mx_current->host)); + mx_record->Set(exchange_symbol, + OneByteString(node_isolate, mx_current->host)); mx_record->Set(priority_symbol, Integer::New(mx_current->priority, node_isolate)); mx_records->Set(Integer::New(i++, node_isolate), mx_record); @@ -528,7 +532,7 @@ class QueryTxtWrap: public QueryWrap { struct ares_txt_reply *current = txt_out; for (int i = 0; current; ++i, current = current->next) { - Local txt = String::New(reinterpret_cast(current->txt)); + Local txt = OneByteString(node_isolate, current->txt); txt_records->Set(Integer::New(i, node_isolate), txt); } @@ -566,16 +570,22 @@ class QuerySrvWrap: public QueryWrap { } Local srv_records = Array::New(); - Local name_symbol = String::NewSymbol("name"); - Local port_symbol = String::NewSymbol("port"); - Local priority_symbol = String::NewSymbol("priority"); - Local weight_symbol = String::NewSymbol("weight"); + 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"); + int i = 0; for (struct ares_srv_reply* srv_current = srv_start; srv_current; srv_current = srv_current->next) { Local srv_record = Object::New(); - srv_record->Set(name_symbol, String::New(srv_current->host)); + srv_record->Set(name_symbol, + OneByteString(node_isolate, srv_current->host)); srv_record->Set(port_symbol, Integer::New(srv_current->port, node_isolate)); srv_record->Set(priority_symbol, @@ -620,12 +630,18 @@ class QueryNaptrWrap: public QueryWrap { } Local naptr_records = Array::New(); - Local flags_symbol = String::NewSymbol("flags"); - Local service_symbol = String::NewSymbol("service"); - Local regexp_symbol = String::NewSymbol("regexp"); - Local replacement_symbol = String::NewSymbol("replacement"); - Local order_symbol = String::NewSymbol("order"); - Local preference_symbol = String::NewSymbol("preference"); + 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"); int i = 0; for (ares_naptr_reply* naptr_current = naptr_start; @@ -634,13 +650,14 @@ class QueryNaptrWrap: public QueryWrap { Local naptr_record = Object::New(); naptr_record->Set(flags_symbol, - String::New(reinterpret_cast(naptr_current->flags))); + OneByteString(node_isolate, naptr_current->flags)); naptr_record->Set(service_symbol, - String::New(reinterpret_cast(naptr_current->service))); + OneByteString(node_isolate, naptr_current->service)); naptr_record->Set(regexp_symbol, - String::New(reinterpret_cast(naptr_current->regexp))); + OneByteString(node_isolate, naptr_current->regexp)); naptr_record->Set(replacement_symbol, - String::New(naptr_current->replacement)); + OneByteString(node_isolate, + naptr_current->replacement)); naptr_record->Set(order_symbol, Integer::New(naptr_current->order, node_isolate)); naptr_record->Set(preference_symbol, @@ -814,7 +831,7 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { continue; // Create JavaScript string - Local s = String::New(ip); + Local s = OneByteString(node_isolate, ip); results->Set(n, s); n++; } @@ -841,7 +858,7 @@ void AfterGetAddrInfo(uv_getaddrinfo_t* req, int status, struct addrinfo* res) { continue; // Create JavaScript string - Local s = String::New(ip); + Local s = OneByteString(node_isolate, ip); results->Set(n, s); n++; } @@ -943,7 +960,7 @@ static void GetServers(const FunctionCallbackInfo& args) { int err = uv_inet_ntop(cur->family, caddr, ip, sizeof(ip)); assert(err == 0); - Local addr = String::New(ip); + Local addr = OneByteString(node_isolate, ip); server_array->Set(i, addr); } @@ -1024,7 +1041,7 @@ static void SetServers(const FunctionCallbackInfo& args) { static void StrError(const FunctionCallbackInfo& args) { HandleScope scope(node_isolate); const char* errmsg = ares_strerror(args[0]->Int32Value()); - args.GetReturnValue().Set(String::New(errmsg)); + args.GetReturnValue().Set(OneByteString(node_isolate, errmsg)); } @@ -1069,14 +1086,14 @@ static void Initialize(Handle target) { NODE_SET_METHOD(target, "getServers", GetServers); NODE_SET_METHOD(target, "setServers", SetServers); - target->Set(String::NewSymbol("AF_INET"), + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "AF_INET"), Integer::New(AF_INET, node_isolate)); - target->Set(String::NewSymbol("AF_INET6"), + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "AF_INET6"), Integer::New(AF_INET6, node_isolate)); - target->Set(String::NewSymbol("AF_UNSPEC"), + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "AF_UNSPEC"), Integer::New(AF_UNSPEC, node_isolate)); - oncomplete_sym = String::New("oncomplete"); + oncomplete_sym = FIXED_ONE_BYTE_STRING(node_isolate, "oncomplete"); } } // namespace cares_wrap diff --git a/src/fs_event_wrap.cc b/src/fs_event_wrap.cc index b8d24c0..8c10cca 100644 --- a/src/fs_event_wrap.cc +++ b/src/fs_event_wrap.cc @@ -75,16 +75,16 @@ void FSEventWrap::Initialize(Handle target) { Local t = FunctionTemplate::New(New); t->InstanceTemplate()->SetInternalFieldCount(1); - t->SetClassName(String::NewSymbol("FSEvent")); + t->SetClassName(FIXED_ONE_BYTE_STRING(node_isolate, "FSEvent")); NODE_SET_PROTOTYPE_METHOD(t, "start", Start); NODE_SET_PROTOTYPE_METHOD(t, "close", Close); - target->Set(String::New("FSEvent"), t->GetFunction()); + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "FSEvent"), t->GetFunction()); - change_sym = String::New("change"); - onchange_sym = String::New("onchange"); - rename_sym = String::New("rename"); + change_sym = FIXED_ONE_BYTE_STRING(node_isolate, "change"); + onchange_sym = FIXED_ONE_BYTE_STRING(node_isolate, "onchange"); + rename_sym = FIXED_ONE_BYTE_STRING(node_isolate, "rename"); } @@ -161,7 +161,7 @@ void FSEventWrap::OnEvent(uv_fs_event_t* handle, const char* filename, }; if (filename != NULL) { - argv[2] = String::New(filename); + argv[2] = OneByteString(node_isolate, filename); } MakeCallback(wrap->object(), onchange_sym, ARRAY_SIZE(argv), argv); diff --git a/src/handle_wrap.cc b/src/handle_wrap.cc index 1eb0dc5..ce6ac5c 100644 --- a/src/handle_wrap.cc +++ b/src/handle_wrap.cc @@ -76,7 +76,9 @@ void HandleWrap::Close(const FunctionCallbackInfo& args) { wrap->handle__ = NULL; if (args[0]->IsFunction()) { - if (close_sym.IsEmpty() == true) close_sym = String::New("close"); + if (close_sym.IsEmpty() == true) { + close_sym = FIXED_ONE_BYTE_STRING(node_isolate, "close"); + } wrap->object()->Set(close_sym, args[0]); wrap->flags_ |= kCloseCallback; } diff --git a/src/node.cc b/src/node.cc index 649fd92..8c1653a 100644 --- a/src/node.cc +++ b/src/node.cc @@ -242,7 +242,8 @@ static void CheckImmediate(uv_check_t* handle, int status) { HandleScope scope(node_isolate); if (immediate_callback_sym.IsEmpty()) { - immediate_callback_sym = String::New("_immediateCallback"); + immediate_callback_sym = + FIXED_ONE_BYTE_STRING(node_isolate, "_immediateCallback"); } MakeCallback(process_p, immediate_callback_sym, 0, NULL); @@ -738,26 +739,30 @@ Local ErrnoException(int errorno, const char *msg, const char *path) { Local e; - Local estring = String::NewSymbol(errno_string(errorno)); + Local estring = OneByteString(node_isolate, errno_string(errorno)); if (msg == NULL || msg[0] == '\0') { msg = strerror(errorno); } - Local message = String::NewSymbol(msg); + Local message = OneByteString(node_isolate, msg); - Local cons1 = String::Concat(estring, String::NewSymbol(", ")); + Local cons1 = + String::Concat(estring, FIXED_ONE_BYTE_STRING(node_isolate, ", ")); Local cons2 = String::Concat(cons1, message); if (syscall_symbol.IsEmpty()) { - syscall_symbol = String::New("syscall"); - errno_symbol = String::New("errno"); - errpath_symbol = String::New("path"); - code_symbol = String::New("code"); + syscall_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "syscall"); + errno_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "errno"); + errpath_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "path"); + code_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "code"); } if (path) { - Local cons3 = String::Concat(cons2, String::NewSymbol(" '")); - Local cons4 = String::Concat(cons3, String::New(path)); - Local cons5 = String::Concat(cons4, String::NewSymbol("'")); + Local cons3 = + String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, " '")); + Local cons4 = + String::Concat(cons3, String::NewFromUtf8(node_isolate, path)); + Local cons5 = + String::Concat(cons4, FIXED_ONE_BYTE_STRING(node_isolate, "'")); e = Exception::Error(cons5); } else { e = Exception::Error(cons2); @@ -767,8 +772,8 @@ Local ErrnoException(int errorno, obj->Set(errno_symbol, Integer::New(errorno, node_isolate)); obj->Set(code_symbol, estring); - if (path) obj->Set(errpath_symbol, String::New(path)); - if (syscall) obj->Set(syscall_symbol, String::NewSymbol(syscall)); + if (path) obj->Set(errpath_symbol, String::NewFromUtf8(node_isolate, path)); + if (syscall) obj->Set(syscall_symbol, OneByteString(node_isolate, syscall)); return e; } @@ -779,18 +784,19 @@ Local UVException(int errorno, const char *msg, const char *path) { if (syscall_symbol.IsEmpty()) { - syscall_symbol = String::New("syscall"); - errno_symbol = String::New("errno"); - errpath_symbol = String::New("path"); - code_symbol = String::New("code"); + syscall_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "syscall"); + errno_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "errno"); + errpath_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "path"); + code_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "code"); } if (!msg || !msg[0]) msg = uv_strerror(errorno); - Local estring = String::NewSymbol(uv_err_name(errorno)); - Local message = String::NewSymbol(msg); - Local cons1 = String::Concat(estring, String::NewSymbol(", ")); + Local estring = OneByteString(node_isolate, uv_err_name(errorno)); + Local message = OneByteString(node_isolate, msg); + Local cons1 = + String::Concat(estring, FIXED_ONE_BYTE_STRING(node_isolate, ", ")); Local cons2 = String::Concat(cons1, message); Local e; @@ -800,19 +806,23 @@ Local UVException(int errorno, if (path) { #ifdef _WIN32 if (strncmp(path, "\\\\?\\UNC\\", 8) == 0) { - path_str = String::Concat(String::New("\\\\"), String::New(path + 8)); + path_str = String::Concat(FIXED_ONE_BYTE_STRING(node_isolate, "\\\\"), + String::NewFromUtf8(node_isolate, path + 8)); } else if (strncmp(path, "\\\\?\\", 4) == 0) { - path_str = String::New(path + 4); + path_str = String::NewFromUtf8(node_isolate, path + 4); } else { - path_str = String::New(path); + path_str = String::NewFromUtf8(node_isolate, path); } #else - path_str = String::New(path); + path_str = String::NewFromUtf8(node_isolate, path); #endif - Local cons3 = String::Concat(cons2, String::NewSymbol(" '")); - Local cons4 = String::Concat(cons3, path_str); - Local cons5 = String::Concat(cons4, String::NewSymbol("'")); + Local cons3 = + String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, " '")); + Local cons4 = + String::Concat(cons3, path_str); + Local cons5 = + String::Concat(cons4, FIXED_ONE_BYTE_STRING(node_isolate, "'")); e = Exception::Error(cons5); } else { e = Exception::Error(cons2); @@ -824,7 +834,7 @@ Local UVException(int errorno, obj->Set(errno_symbol, Integer::New(errorno, node_isolate)); obj->Set(code_symbol, estring); if (path) obj->Set(errpath_symbol, path_str); - if (syscall) obj->Set(syscall_symbol, String::NewSymbol(syscall)); + if (syscall) obj->Set(syscall_symbol, OneByteString(node_isolate, syscall)); return e; } @@ -862,19 +872,22 @@ Local WinapiErrnoException(int errorno, if (!msg || !msg[0]) { msg = winapi_strerror(errorno); } - Local message = String::NewSymbol(msg); + Local message = OneByteString(node_isolate, msg); if (syscall_symbol.IsEmpty()) { - syscall_symbol = String::New("syscall"); - errno_symbol = String::New("errno"); - errpath_symbol = String::New("path"); - code_symbol = String::New("code"); + syscall_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "syscall"); + errno_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "errno"); + errpath_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "path"); + code_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "code"); } if (path) { - Local cons1 = String::Concat(message, String::NewSymbol(" '")); - Local cons2 = String::Concat(cons1, String::New(path)); - Local cons3 = String::Concat(cons2, String::NewSymbol("'")); + Local cons1 = + String::Concat(message, FIXED_ONE_BYTE_STRING(node_isolate, " '")); + Local cons2 = + String::Concat(cons1, String::NewFromUtf8(node_isolate, path)); + Local cons3 = + String::Concat(cons2, FIXED_ONE_BYTE_STRING(node_isolate, "'")); e = Exception::Error(cons3); } else { e = Exception::Error(message); @@ -883,8 +896,8 @@ Local WinapiErrnoException(int errorno, Local obj = e->ToObject(); obj->Set(errno_symbol, Integer::New(errorno, node_isolate)); - if (path) obj->Set(errpath_symbol, String::New(path)); - if (syscall) obj->Set(syscall_symbol, String::NewSymbol(syscall)); + if (path) obj->Set(errpath_symbol, String::NewFromUtf8(node_isolate, path)); + if (syscall) obj->Set(syscall_symbol, OneByteString(node_isolate, syscall)); return e; } #endif @@ -895,8 +908,10 @@ void SetupDomainUse(const FunctionCallbackInfo& args) { HandleScope scope(node_isolate); using_domains = true; Local process = PersistentToLocal(node_isolate, process_p); - Local tdc_v = process->Get(String::New("_tickDomainCallback")); - Local ndt_v = process->Get(String::New("_nextDomainTick")); + Local tdc_v = + process->Get(FIXED_ONE_BYTE_STRING(node_isolate, "_tickDomainCallback")); + Local ndt_v = + process->Get(FIXED_ONE_BYTE_STRING(node_isolate, "_nextDomainTick")); if (!tdc_v->IsFunction()) { fprintf(stderr, "process._tickDomainCallback assigned to non-function\n"); abort(); @@ -907,8 +922,8 @@ void SetupDomainUse(const FunctionCallbackInfo& args) { } Local tdc = tdc_v.As(); Local ndt = ndt_v.As(); - process->Set(String::New("_tickCallback"), tdc); - process->Set(String::New("nextTick"), ndt); + process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_tickCallback"), tdc); + process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "nextTick"), ndt); process_tickCallback.Reset(node_isolate, tdc); } @@ -922,9 +937,9 @@ MakeDomainCallback(const Handle object, // lazy load domain specific symbols if (enter_symbol.IsEmpty()) { - enter_symbol = String::New("enter"); - exit_symbol = String::New("exit"); - disposed_symbol = String::New("_disposed"); + enter_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "enter"); + exit_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "exit"); + disposed_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "_disposed"); } Local domain_v = object->Get(domain_symbol); @@ -1008,7 +1023,8 @@ MakeCallback(const Handle object, // lazy load no domain next tick callbacks if (process_tickCallback.IsEmpty()) { - Local cb_v = process->Get(String::New("_tickCallback")); + Local cb_v = + process->Get(FIXED_ONE_BYTE_STRING(node_isolate, "_tickCallback")); if (!cb_v->IsFunction()) { fprintf(stderr, "process._tickCallback assigned to non-function\n"); abort(); @@ -1069,8 +1085,8 @@ MakeCallback(const Handle object, Handle argv[]) { HandleScope scope(node_isolate); - Handle ret = - MakeCallback(object, String::NewSymbol(method), argc, argv); + Local method_string = OneByteString(node_isolate, method); + Handle ret = MakeCallback(object, method_string, argc, argv); return scope.Close(ret); } @@ -1219,7 +1235,8 @@ static void ReportException(Handle er, Handle message) { DisplayExceptionLine(message); - Local trace_value(er->ToObject()->Get(String::New("stack"))); + Local trace_value( + er->ToObject()->Get(FIXED_ONE_BYTE_STRING(node_isolate, "stack"))); String::Utf8Value trace(trace_value); // range errors have a trace member set to undefined @@ -1229,18 +1246,27 @@ static void ReportException(Handle er, Handle message) { // this really only happens for RangeErrors, since they're the only // kind that won't have all this info in the trace, or when non-Error // objects are thrown manually. - bool isErrorObject = er->IsObject() && - !(er->ToObject()->Get(String::New("message"))->IsUndefined()) && - !(er->ToObject()->Get(String::New("name"))->IsUndefined()); + Local message; + Local name; - if (isErrorObject) { - String::Utf8Value name(er->ToObject()->Get(String::New("name"))); - fprintf(stderr, "%s: ", *name); + if (er->IsObject()) { + Local err_obj = er.As(); + message = err_obj->Get(FIXED_ONE_BYTE_STRING(node_isolate, "message")); + name = err_obj->Get(FIXED_ONE_BYTE_STRING(node_isolate, "name")); } - String::Utf8Value msg(!isErrorObject ? er - : er->ToObject()->Get(String::New("message"))); - fprintf(stderr, "%s\n", *msg); + if (message.IsEmpty() || + message->IsUndefined() || + name.IsEmpty() || + name->IsUndefined()) { + // Not an error object. Just print as-is. + String::Utf8Value message(er); + fprintf(stderr, "%s\n", *message); + } else { + String::Utf8Value name_string(name); + String::Utf8Value message_string(message); + fprintf(stderr, "%s: %s\n", *name_string, *message_string); + } } fflush(stderr); @@ -1303,7 +1329,7 @@ void GetActiveHandles(const FunctionCallbackInfo& args) { QUEUE* q = NULL; int i = 0; - Local owner_sym = String::New("owner"); + Local owner_sym = FIXED_ONE_BYTE_STRING(node_isolate, "owner"); QUEUE_FOREACH(q, &handle_wrap_queue) { HandleWrap* w = container_of(q, HandleWrap, handle_wrap_queue_); @@ -1353,7 +1379,7 @@ static void Cwd(const FunctionCallbackInfo& args) { } buf[ARRAY_SIZE(buf) - 1] = '\0'; - Local cwd = String::New(buf); + Local cwd = String::NewFromUtf8(node_isolate, buf); args.GetReturnValue().Set(cwd); } @@ -1687,9 +1713,9 @@ void MemoryUsage(const FunctionCallbackInfo& args) { Local info = Object::New(); if (rss_symbol.IsEmpty()) { - rss_symbol = String::New("rss"); - heap_total_symbol = String::New("heapTotal"); - heap_used_symbol = String::New("heapUsed"); + rss_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "rss"); + heap_total_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "heapTotal"); + heap_used_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "heapUsed"); } info->Set(rss_symbol, Number::New(rss)); @@ -1770,12 +1796,12 @@ void DLOpen(const FunctionCallbackInfo& args) { String::Utf8Value filename(args[1]); // Cast if (exports_symbol.IsEmpty()) { - exports_symbol = String::New("exports"); + exports_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "exports"); } Local exports = module->Get(exports_symbol)->ToObject(); if (uv_dlopen(*filename, &lib)) { - Local errmsg = String::New(uv_dlerror(&lib)); + Local errmsg = OneByteString(node_isolate, uv_dlerror(&lib)); #ifdef _WIN32 // Windows needs to add the filename into the error message errmsg = String::Concat(errmsg, args[1]->ToString()); @@ -1870,8 +1896,10 @@ NO_RETURN void FatalError(const char* location, const char* message) { void FatalException(Handle error, Handle message) { HandleScope scope(node_isolate); - if (fatal_exception_symbol.IsEmpty()) - fatal_exception_symbol = String::New("_fatalException"); + if (fatal_exception_symbol.IsEmpty()) { + fatal_exception_symbol = + FIXED_ONE_BYTE_STRING(node_isolate, "_fatalException"); + } Local process = PersistentToLocal(node_isolate, process_p); Local fatal_v = process->Get(fatal_exception_symbol); @@ -1943,7 +1971,7 @@ static void Binding(const FunctionCallbackInfo& args) { Local modules = PersistentToLocal(node_isolate, module_load_list); uint32_t l = modules->Length(); - modules->Set(l, String::New(buf)); + modules->Set(l, OneByteString(node_isolate, buf)); if ((modp = get_builtin_module(*module_v)) != NULL) { exports = Object::New(); @@ -1972,7 +2000,7 @@ static void ProcessTitleGetter(Local property, HandleScope scope(node_isolate); char buffer[512]; uv_get_process_title(buffer, sizeof(buffer)); - info.GetReturnValue().Set(String::New(buffer)); + info.GetReturnValue().Set(String::NewFromUtf8(node_isolate, buffer)); } @@ -1993,7 +2021,7 @@ static void EnvGetter(Local property, String::Utf8Value key(property); const char* val = getenv(*key); if (val) { - return info.GetReturnValue().Set(String::New(val)); + return info.GetReturnValue().Set(String::NewFromUtf8(node_isolate, val)); } #else // _WIN32 String::Value key(property); @@ -2006,8 +2034,9 @@ static void EnvGetter(Local property, // not found. if ((result > 0 || GetLastError() == ERROR_SUCCESS) && result < ARRAY_SIZE(buffer)) { - return info.GetReturnValue().Set( - String::New(reinterpret_cast(buffer), result)); + const uint16_t* two_byte_buffer = reinterpret_cast(buffer); + Local rc = String::NewFromTwoByte(node_isolate, two_byte_buffer); + return info.GetReturnValue().Set(rc); } #endif // Not found. Fetch from prototype. @@ -2097,7 +2126,11 @@ static void EnvEnumerator(const PropertyCallbackInfo& info) { const char* var = environ[i]; const char* s = strchr(var, '='); const int length = s ? s - var : strlen(var); - env->Set(i, String::New(var, length)); + Local name = String::NewFromUtf8(node_isolate, + var, + String::kNormalString, + length); + env->Set(i, name); } #else // _WIN32 WCHAR* environment = GetEnvironmentStringsW(); @@ -2117,7 +2150,8 @@ static void EnvEnumerator(const PropertyCallbackInfo& info) { if (!s) { s = p + wcslen(p); } - env->Set(i++, String::New(reinterpret_cast(p), s - p)); + const uint16_t* two_byte_buffer = reinterpret_cast(p); + env->Set(i++, TWO_BYTE_STRING(node_isolate, two_byte_buffer, s - p)); p = s + wcslen(s) + 1; } FreeEnvironmentStringsW(environment); @@ -2137,15 +2171,18 @@ static Handle GetFeatures() { Local debug = False(node_isolate); #endif // defined(DEBUG) && DEBUG - obj->Set(String::NewSymbol("debug"), debug); + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "debug"), debug); - obj->Set(String::NewSymbol("uv"), True(node_isolate)); + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "uv"), True(node_isolate)); // TODO(bnoordhuis) ping libuv - obj->Set(String::NewSymbol("ipv6"), True(node_isolate)); - obj->Set(String::NewSymbol("tls_npn"), Boolean::New(use_npn)); - obj->Set(String::NewSymbol("tls_sni"), Boolean::New(use_sni)); - obj->Set(String::NewSymbol("tls"), - Boolean::New(get_builtin_module("crypto") != NULL)); + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "ipv6"), True(node_isolate)); + + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls_npn"), + Boolean::New(use_npn)); + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls_sni"), + Boolean::New(use_sni)); + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "tls"), + Boolean::New(get_builtin_module("crypto") != NULL)); return scope.Close(obj); } @@ -2201,7 +2238,7 @@ static void NeedImmediateCallbackSetter(Local property, #define READONLY_PROPERTY(obj, str, var) \ do { \ - obj->Set(String::New(str), var, v8::ReadOnly); \ + obj->Set(OneByteString(node_isolate, str), var, v8::ReadOnly); \ } while (0) @@ -2211,7 +2248,8 @@ Handle SetupProcessObject(int argc, char *argv[]) { int i, j; Local process_template = FunctionTemplate::New(); - process_template->SetClassName(String::New("process")); + process_template->SetClassName( + FIXED_ONE_BYTE_STRING(node_isolate, "process")); Local process = process_template->GetFunction()->NewInstance(); assert(process.IsEmpty() == false); @@ -2219,12 +2257,14 @@ Handle SetupProcessObject(int argc, char *argv[]) { process_p.Reset(node_isolate, process); - process->SetAccessor(String::New("title"), + process->SetAccessor(FIXED_ONE_BYTE_STRING(node_isolate, "title"), ProcessTitleGetter, ProcessTitleSetter); // process.version - READONLY_PROPERTY(process, "version", String::New(NODE_VERSION)); + READONLY_PROPERTY(process, + "version", + FIXED_ONE_BYTE_STRING(node_isolate, NODE_VERSION)); // process.moduleLoadList Local modules = Array::New(); @@ -2234,17 +2274,33 @@ Handle SetupProcessObject(int argc, char *argv[]) { // process.versions Local versions = Object::New(); READONLY_PROPERTY(process, "versions", versions); - READONLY_PROPERTY(versions, "http_parser", String::New( - NODE_STRINGIFY(HTTP_PARSER_VERSION_MAJOR) "." - NODE_STRINGIFY(HTTP_PARSER_VERSION_MINOR))); + + const char http_parser_version[] = NODE_STRINGIFY(HTTP_PARSER_VERSION_MAJOR) + "." + NODE_STRINGIFY(HTTP_PARSER_VERSION_MINOR); + READONLY_PROPERTY(versions, + "http_parser", + FIXED_ONE_BYTE_STRING(node_isolate, http_parser_version)); + // +1 to get rid of the leading 'v' - READONLY_PROPERTY(versions, "node", String::New(NODE_VERSION+1)); - READONLY_PROPERTY(versions, "v8", String::New(V8::GetVersion())); - READONLY_PROPERTY(versions, "uv", String::New(uv_version_string())); - READONLY_PROPERTY(versions, "zlib", String::New(ZLIB_VERSION)); + READONLY_PROPERTY(versions, + "node", + OneByteString(node_isolate, NODE_VERSION + 1)); + READONLY_PROPERTY(versions, + "v8", + OneByteString(node_isolate, V8::GetVersion())); + READONLY_PROPERTY(versions, + "uv", + OneByteString(node_isolate, uv_version_string())); + READONLY_PROPERTY(versions, + "zlib", + FIXED_ONE_BYTE_STRING(node_isolate, ZLIB_VERSION)); + + const char node_modules_version[] = NODE_STRINGIFY(NODE_MODULE_VERSION); READONLY_PROPERTY(versions, "modules", - String::New(NODE_STRINGIFY(NODE_MODULE_VERSION))); + FIXED_ONE_BYTE_STRING(node_isolate, node_modules_version)); + #if HAVE_OPENSSL // Stupid code to slice out the version string. int c, l = strlen(OPENSSL_VERSION_TEXT); @@ -2258,37 +2314,36 @@ Handle SetupProcessObject(int argc, char *argv[]) { break; } } - READONLY_PROPERTY(versions, - "openssl", - String::New(&OPENSSL_VERSION_TEXT[i], j - i)); + READONLY_PROPERTY( + versions, + "openssl", + OneByteString(node_isolate, &OPENSSL_VERSION_TEXT[i], j - i)); #endif // process.arch - READONLY_PROPERTY(process, "arch", String::New(ARCH)); + READONLY_PROPERTY(process, "arch", OneByteString(node_isolate, ARCH)); // process.platform - READONLY_PROPERTY(process, "platform", String::New(PLATFORM)); + READONLY_PROPERTY(process, + "platform", + OneByteString(node_isolate, PLATFORM)); // process.argv Local arguments = Array::New(argc - option_end_index + 1); - arguments->Set(Integer::New(0, node_isolate), String::New(argv[0])); + arguments->Set(0, String::NewFromUtf8(node_isolate, argv[0])); for (j = 1, i = option_end_index; i < argc; j++, i++) { - Local arg = String::New(argv[i]); - arguments->Set(Integer::New(j, node_isolate), arg); + arguments->Set(j, String::NewFromUtf8(node_isolate, argv[i])); } - // assign it - process->Set(String::NewSymbol("argv"), arguments); + process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "argv"), arguments); // process.execArgv - Local execArgv = Array::New(option_end_index - 1); + Local exec_argv = Array::New(option_end_index - 1); for (j = 1, i = 0; j < option_end_index; j++, i++) { - execArgv->Set(Integer::New(i, node_isolate), String::New(argv[j])); + exec_argv->Set(i, String::NewFromUtf8(node_isolate, argv[j])); } - // assign it - process->Set(String::NewSymbol("execArgv"), execArgv); - + process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "execArgv"), exec_argv); // create process.env Local envTemplate = ObjectTemplate::New(); @@ -2299,17 +2354,20 @@ Handle SetupProcessObject(int argc, char *argv[]) { EnvEnumerator, Object::New()); Local env = envTemplate->NewInstance(); - process->Set(String::NewSymbol("env"), env); + process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "env"), env); READONLY_PROPERTY(process, "pid", Integer::New(getpid(), node_isolate)); READONLY_PROPERTY(process, "features", GetFeatures()); - process->SetAccessor(String::New("_needImmediateCallback"), - NeedImmediateCallbackGetter, - NeedImmediateCallbackSetter); + process->SetAccessor( + FIXED_ONE_BYTE_STRING(node_isolate, "_needImmediateCallback"), + NeedImmediateCallbackGetter, + NeedImmediateCallbackSetter); // -e, --eval if (eval_string) { - READONLY_PROPERTY(process, "_eval", String::New(eval_string)); + READONLY_PROPERTY(process, + "_eval", + String::NewFromUtf8(node_isolate, eval_string)); } // -p, --print @@ -2337,21 +2395,25 @@ Handle SetupProcessObject(int argc, char *argv[]) { READONLY_PROPERTY(process, "traceDeprecation", True(node_isolate)); } - size_t size = 2*PATH_MAX; - char* execPath = new char[size]; - if (uv_exepath(execPath, &size) != 0) { - // as a last ditch effort, fallback on argv[0] ? - process->Set(String::NewSymbol("execPath"), String::New(argv[0])); + 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, + String::kNormalString, + exec_path_len); } else { - process->Set(String::NewSymbol("execPath"), String::New(execPath, size)); + exec_path_value = String::NewFromUtf8(node_isolate, argv[0]); } - delete [] execPath; + process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "execPath"), + exec_path_value); + delete[] exec_path; - process->SetAccessor(String::New("debugPort"), + process->SetAccessor(FIXED_ONE_BYTE_STRING(node_isolate, "debugPort"), DebugPortGetter, DebugPortSetter); - // define various internal methods NODE_SET_METHOD(process, "_getActiveRequests", GetActiveRequests); NODE_SET_METHOD(process, "_getActiveHandles", GetActiveHandles); @@ -2396,10 +2458,10 @@ Handle SetupProcessObject(int argc, char *argv[]) { info_box->SetIndexedPropertiesToExternalArrayData(&tick_infobox, kExternalUnsignedIntArray, 4); - process->Set(String::NewSymbol("_tickInfoBox"), info_box); + process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_tickInfoBox"), info_box); // pre-set _events object for faster emit checks - process->Set(String::NewSymbol("_events"), Object::New()); + process->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_events"), Object::New()); return scope.Close(process); } @@ -2422,8 +2484,8 @@ static void SignalExit(int signal) { void Load(Handle process_l) { HandleScope handle_scope(node_isolate); - process_symbol = String::New("process"); - domain_symbol = String::New("domain"); + process_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "process"); + domain_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "domain"); // 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 @@ -2439,7 +2501,8 @@ void Load(Handle process_l) { // are not safe to ignore. try_catch.SetVerbose(false); - Local f_value = ExecuteString(MainSource(), String::New("node.js")); + Local script_name = FIXED_ONE_BYTE_STRING(node_isolate, "node.js"); + Local f_value = ExecuteString(MainSource(), script_name); if (try_catch.HasCaught()) { ReportException(try_catch); exit(10); @@ -2632,8 +2695,12 @@ static void DispatchMessagesDebugAgentCallback() { static void EmitDebugEnabledAsyncCallback(uv_async_t* handle, int status) { HandleScope handle_scope(node_isolate); Local obj = Object::New(); - obj->Set(String::New("cmd"), String::New("NODE_DEBUG_ENABLED")); - Local args[] = { String::New("internalMessage"), obj }; + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "cmd"), + FIXED_ONE_BYTE_STRING(node_isolate, "NODE_DEBUG_ENABLED")); + Local args[] = { + FIXED_ONE_BYTE_STRING(node_isolate, "internalMessage"), + obj + }; MakeCallback(process_p, "emit", ARRAY_SIZE(args), args); } @@ -3001,8 +3068,12 @@ void AtExit(void (*cb)(void* arg), void* arg) { void EmitExit(v8::Handle process_l) { // process.emit('exit') - process_l->Set(String::NewSymbol("_exiting"), True(node_isolate)); - Local args[] = { String::New("exit"), Integer::New(0, node_isolate) }; + process_l->Set(FIXED_ONE_BYTE_STRING(node_isolate, "_exiting"), + True(node_isolate)); + Local args[] = { + FIXED_ONE_BYTE_STRING(node_isolate, "exit"), + Integer::New(0, node_isolate) + }; MakeCallback(process_l, "emit", ARRAY_SIZE(args), args); } diff --git a/src/node.h b/src/node.h index f41495c..d7996fb 100644 --- a/src/node.h +++ b/src/node.h @@ -129,20 +129,30 @@ void EmitExit(v8::Handle process); #define NODE_UNIXTIME_V8(t) v8::Date::New(1000*static_cast(t)) #define NODE_V8_UNIXTIME(v) (static_cast((v)->NumberValue())/1000.0); -#define NODE_DEFINE_CONSTANT(target, constant) \ - (target)->Set(v8::String::NewSymbol(#constant), \ - v8::Number::New(constant), \ - static_cast( \ - v8::ReadOnly|v8::DontDelete)) +// Used to be a macro, hence the uppercase name. +#define NODE_DEFINE_CONSTANT(target, constant) \ + do { \ + v8::Isolate* isolate = v8::Isolate::GetCurrent(); \ + v8::Local constant_name = \ + v8::String::NewFromUtf8(isolate, #constant); \ + v8::Local constant_value = \ + v8::Number::New(isolate, static_cast(constant)); \ + v8::PropertyAttribute constant_attributes = \ + static_cast(v8::ReadOnly | v8::DontDelete); \ + (target)->Set(constant_name, constant_value, constant_attributes); \ + } \ + while (0) // Used to be a macro, hence the uppercase name. template inline void NODE_SET_METHOD(const TypeName& recv, const char* name, v8::FunctionCallback callback) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::HandleScope handle_scope(isolate); v8::Local t = v8::FunctionTemplate::New(callback); v8::Local fn = t->GetFunction(); - v8::Local fn_name = v8::String::New(name); + v8::Local fn_name = v8::String::NewFromUtf8(isolate, name); fn->SetName(fn_name); recv->Set(fn_name, fn); } @@ -153,8 +163,11 @@ inline void NODE_SET_METHOD(const TypeName& recv, inline void NODE_SET_PROTOTYPE_METHOD(v8::Handle recv, const char* name, v8::FunctionCallback callback) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + v8::HandleScope handle_scope(isolate); v8::Local t = v8::FunctionTemplate::New(callback); - recv->PrototypeTemplate()->Set(v8::String::New(name), t->GetFunction()); + recv->PrototypeTemplate()->Set(v8::String::NewFromUtf8(isolate, name), + t->GetFunction()); } #define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 480b275..b69ff63 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -559,13 +559,14 @@ void SetupBufferJS(const FunctionCallbackInfo& args) { Local bv = args[0].As(); p_buffer_fn.Reset(node_isolate, bv); - Local proto_v = bv->Get(String::New("prototype")); + Local proto_v = + bv->Get(FIXED_ONE_BYTE_STRING(node_isolate, "prototype")); assert(proto_v->IsObject()); Local proto = proto_v.As(); - bv->Set(String::New("byteLength"), + bv->Set(FIXED_ONE_BYTE_STRING(node_isolate, "byteLength"), FunctionTemplate::New(ByteLength)->GetFunction()); NODE_SET_METHOD(proto, "asciiSlice", AsciiSlice); @@ -596,14 +597,16 @@ void SetupBufferJS(const FunctionCallbackInfo& args) { NODE_SET_METHOD(proto, "fill", Fill); // for backwards compatibility - proto->Set(String::New("offset"), Uint32::New(0, node_isolate), v8::ReadOnly); + proto->Set(FIXED_ONE_BYTE_STRING(node_isolate, "offset"), + Uint32::New(0, node_isolate), + v8::ReadOnly); } void Initialize(Handle target) { HandleScope scope(node_isolate); - target->Set(String::New("setupBufferJS"), + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "setupBufferJS"), FunctionTemplate::New(SetupBufferJS)->GetFunction()); } diff --git a/src/node_counters.cc b/src/node_counters.cc index 111d282..1350a08 100644 --- a/src/node_counters.cc +++ b/src/node_counters.cc @@ -114,7 +114,7 @@ void InitPerfCounters(Handle target) { }; for (int i = 0; i < ARRAY_SIZE(tab); i++) { - Local key = String::New(tab[i].name); + Local key = OneByteString(node_isolate, tab[i].name); Local val = FunctionTemplate::New(tab[i].func)->GetFunction(); target->Set(key, val); } diff --git a/src/node_crypto.cc b/src/node_crypto.cc index a4687a9..ff30f88 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -179,7 +179,7 @@ void SecureContext::Initialize(Handle target) { secure_context_constructor.Reset(node_isolate, t); t->InstanceTemplate()->SetInternalFieldCount(1); - t->SetClassName(String::NewSymbol("SecureContext")); + t->SetClassName(FIXED_ONE_BYTE_STRING(node_isolate, "SecureContext")); NODE_SET_PROTOTYPE_METHOD(t, "init", SecureContext::Init); NODE_SET_PROTOTYPE_METHOD(t, "setKey", SecureContext::SetKey); @@ -198,7 +198,8 @@ void SecureContext::Initialize(Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "getTicketKeys", SecureContext::GetTicketKeys); NODE_SET_PROTOTYPE_METHOD(t, "setTicketKeys", SecureContext::SetTicketKeys); - target->Set(String::NewSymbol("SecureContext"), t->GetFunction()); + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "SecureContext"), + t->GetFunction()); } @@ -312,7 +313,7 @@ int SecureContext::NewSessionCallback(SSL* s, SSL_SESSION* sess) { }; if (onnewsession_sym.IsEmpty()) { - onnewsession_sym = String::New("onnewsession"); + onnewsession_sym = FIXED_ONE_BYTE_STRING(node_isolate, "onnewsession"); } MakeCallback(p->handle(node_isolate), @@ -647,11 +648,12 @@ void SecureContext::SetSessionIdContext( bio = BIO_new(BIO_s_mem()); if (bio == NULL) { - message = String::New("SSL_CTX_set_session_id_context error"); + message = FIXED_ONE_BYTE_STRING(node_isolate, + "SSL_CTX_set_session_id_context error"); } else { ERR_print_errors(bio); BIO_get_mem_ptr(bio, &mem); - message = String::New(mem->data, mem->length); + message = OneByteString(node_isolate, mem->data, mem->length); BIO_free_all(bio); } @@ -800,9 +802,9 @@ void Connection::OnClientHello(void* arg, Connection* c = static_cast(arg); if (onclienthello_sym.IsEmpty()) - onclienthello_sym = String::New("onclienthello"); + onclienthello_sym = FIXED_ONE_BYTE_STRING(node_isolate, "onclienthello"); if (sessionid_sym.IsEmpty()) - sessionid_sym = String::New("sessionId"); + sessionid_sym = FIXED_ONE_BYTE_STRING(node_isolate, "sessionId"); Local obj = Object::New(); obj->Set(sessionid_sym, @@ -858,8 +860,10 @@ int Connection::HandleBIOError(BIO *bio, const char* func, int rv) { ERR_error_string_n(rv, ssl_error_buf, sizeof(ssl_error_buf)); HandleScope scope(node_isolate); - Local e = Exception::Error(String::New(ssl_error_buf)); - handle(node_isolate)->Set(String::New("error"), e); + Local exception = + Exception::Error(OneByteString(node_isolate, ssl_error_buf)); + handle(node_isolate)->Set( + FIXED_ONE_BYTE_STRING(node_isolate, "error"), exception); DEBUG_PRINT("[%p] BIO: %s failed: (%d) %s\n", ssl_, @@ -898,8 +902,10 @@ int Connection::HandleSSLError(const char* func, return 0; } else if (err == SSL_ERROR_ZERO_RETURN) { - handle(node_isolate)->Set(String::New("error"), - Exception::Error(String::New("ZERO_RETURN"))); + Local exception = + Exception::Error(FIXED_ONE_BYTE_STRING(node_isolate, "ZERO_RETURN")); + handle(node_isolate)->Set( + FIXED_ONE_BYTE_STRING(node_isolate, "error"), exception); return rv; } else if ((err == SSL_ERROR_SYSCALL) && (ss == kIgnoreSyscall)) { @@ -920,8 +926,10 @@ int Connection::HandleSSLError(const char* func, if ((bio = BIO_new(BIO_s_mem()))) { ERR_print_errors(bio); BIO_get_mem_ptr(bio, &mem); - Local e = Exception::Error(String::New(mem->data, mem->length)); - handle(node_isolate)->Set(String::New("error"), e); + Local exception = + Exception::Error(OneByteString(node_isolate, mem->data, mem->length)); + handle(node_isolate)->Set( + FIXED_ONE_BYTE_STRING(node_isolate, "error"), exception); BIO_free_all(bio); } @@ -937,8 +945,9 @@ void Connection::ClearError() { HandleScope scope(node_isolate); // We should clear the error in JS-land - assert( - handle(node_isolate)->Get(String::New("error"))->BooleanValue() == false); + Local error_key = FIXED_ONE_BYTE_STRING(node_isolate, "error"); + Local error = handle(node_isolate)->Get(error_key); + assert(error->BooleanValue() == false); #endif // NDEBUG } @@ -949,12 +958,15 @@ void Connection::SetShutdownFlags() { int flags = SSL_get_shutdown(ssl_); if (flags & SSL_SENT_SHUTDOWN) { - handle(node_isolate)->Set(String::New("sentShutdown"), True(node_isolate)); + Local sent_shutdown_key = + FIXED_ONE_BYTE_STRING(node_isolate, "sentShutdown"); + handle(node_isolate)->Set(sent_shutdown_key, True(node_isolate)); } if (flags & SSL_RECEIVED_SHUTDOWN) { - handle(node_isolate)->Set(String::New("receivedShutdown"), - True(node_isolate)); + Local received_shutdown_key = + FIXED_ONE_BYTE_STRING(node_isolate, "receivedShutdown"); + handle(node_isolate)->Set(received_shutdown_key, True(node_isolate)); } } @@ -964,7 +976,7 @@ void Connection::Initialize(Handle target) { Local t = FunctionTemplate::New(Connection::New); t->InstanceTemplate()->SetInternalFieldCount(1); - t->SetClassName(String::NewSymbol("Connection")); + t->SetClassName(FIXED_ONE_BYTE_STRING(node_isolate, "Connection")); NODE_SET_PROTOTYPE_METHOD(t, "encIn", Connection::EncIn); NODE_SET_PROTOTYPE_METHOD(t, "clearOut", Connection::ClearOut); @@ -1006,7 +1018,8 @@ void Connection::Initialize(Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "setSNICallback", Connection::SetSNICallback); #endif - target->Set(String::NewSymbol("Connection"), t->GetFunction()); + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "Connection"), + t->GetFunction()); } @@ -1109,10 +1122,11 @@ int Connection::SelectNextProtoCallback_(SSL *s, p->selectedNPNProto_.Reset(node_isolate, Null(node_isolate)); break; case OPENSSL_NPN_NEGOTIATED: - p->selectedNPNProto_.Reset( - node_isolate, - String::New(reinterpret_cast(*out), *outlen)); - break; + { + Local string = OneByteString(node_isolate, *out, *outlen); + p->selectedNPNProto_.Reset(node_isolate, string); + break; + } case OPENSSL_NPN_NO_OVERLAP: p->selectedNPNProto_.Reset(node_isolate, False(node_isolate)); break; @@ -1133,7 +1147,8 @@ int Connection::SelectSNIContextCallback_(SSL *s, int *ad, void* arg) { const char* servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name); if (servername) { - p->servername_.Reset(node_isolate, String::New(servername)); + p->servername_.Reset(node_isolate, + OneByteString(node_isolate, servername)); // Call the SNI callback and use its return value as context if (!p->sniObject_.IsEmpty()) { @@ -1247,7 +1262,8 @@ void Connection::SSLInfoCallback(const SSL *ssl_, int where, int ret) { HandleScope scope(node_isolate); Connection* c = static_cast(SSL_get_app_data(ssl)); if (onhandshakestart_sym.IsEmpty()) { - onhandshakestart_sym = String::New("onhandshakestart"); + onhandshakestart_sym = + FIXED_ONE_BYTE_STRING(node_isolate, "onhandshakestart"); } MakeCallback(c->handle(node_isolate), onhandshakestart_sym, 0, NULL); } @@ -1255,7 +1271,8 @@ void Connection::SSLInfoCallback(const SSL *ssl_, int where, int ret) { HandleScope scope(node_isolate); Connection* c = static_cast(SSL_get_app_data(ssl)); if (onhandshakedone_sym.IsEmpty()) { - onhandshakedone_sym = String::New("onhandshakedone"); + onhandshakedone_sym = + FIXED_ONE_BYTE_STRING(node_isolate, "onhandshakedone"); } MakeCallback(c->handle(node_isolate), onhandshakedone_sym, 0, NULL); } @@ -1483,14 +1500,16 @@ void Connection::GetPeerCertificate(const FunctionCallbackInfo& args) { if (X509_NAME_print_ex(bio, X509_get_subject_name(peer_cert), 0, X509_NAME_FLAGS) > 0) { BIO_get_mem_ptr(bio, &mem); - info->Set(subject_symbol, String::New(mem->data, mem->length)); + info->Set(subject_symbol, + OneByteString(node_isolate, mem->data, mem->length)); } (void) BIO_reset(bio); if (X509_NAME_print_ex(bio, X509_get_issuer_name(peer_cert), 0, X509_NAME_FLAGS) > 0) { BIO_get_mem_ptr(bio, &mem); - info->Set(issuer_symbol, String::New(mem->data, mem->length)); + info->Set(issuer_symbol, + OneByteString(node_isolate, mem->data, mem->length)); } (void) BIO_reset(bio); @@ -1506,7 +1525,8 @@ void Connection::GetPeerCertificate(const FunctionCallbackInfo& args) { assert(rv == 1); BIO_get_mem_ptr(bio, &mem); - info->Set(subjectaltname_symbol, String::New(mem->data, mem->length)); + info->Set(subjectaltname_symbol, + OneByteString(node_isolate, mem->data, mem->length)); (void) BIO_reset(bio); } @@ -1517,12 +1537,14 @@ void Connection::GetPeerCertificate(const FunctionCallbackInfo& args) { NULL != (rsa = EVP_PKEY_get1_RSA(pkey))) { BN_print(bio, rsa->n); BIO_get_mem_ptr(bio, &mem); - info->Set(modulus_symbol, String::New(mem->data, mem->length) ); + info->Set(modulus_symbol, + OneByteString(node_isolate, mem->data, mem->length)); (void) BIO_reset(bio); BN_print(bio, rsa->e); BIO_get_mem_ptr(bio, &mem); - info->Set(exponent_symbol, String::New(mem->data, mem->length) ); + info->Set(exponent_symbol, + OneByteString(node_isolate, mem->data, mem->length)); (void) BIO_reset(bio); } @@ -1537,12 +1559,14 @@ void Connection::GetPeerCertificate(const FunctionCallbackInfo& args) { ASN1_TIME_print(bio, X509_get_notBefore(peer_cert)); BIO_get_mem_ptr(bio, &mem); - info->Set(valid_from_symbol, String::New(mem->data, mem->length)); + info->Set(valid_from_symbol, + OneByteString(node_isolate, mem->data, mem->length)); (void) BIO_reset(bio); ASN1_TIME_print(bio, X509_get_notAfter(peer_cert)); BIO_get_mem_ptr(bio, &mem); - info->Set(valid_to_symbol, String::New(mem->data, mem->length)); + info->Set(valid_to_symbol, + OneByteString(node_isolate, mem->data, mem->length)); BIO_free_all(bio); unsigned int md_size, i; @@ -1563,7 +1587,7 @@ void Connection::GetPeerCertificate(const FunctionCallbackInfo& args) { fingerprint[0] = '\0'; } - info->Set(fingerprint_symbol, String::New(fingerprint)); + info->Set(fingerprint_symbol, OneByteString(node_isolate, fingerprint)); } STACK_OF(ASN1_OBJECT) *eku = (STACK_OF(ASN1_OBJECT) *)X509_get_ext_d2i( @@ -1575,7 +1599,8 @@ void Connection::GetPeerCertificate(const FunctionCallbackInfo& args) { for (int i = 0; i < sk_ASN1_OBJECT_num(eku); i++) { memset(buf, 0, sizeof(buf)); OBJ_obj2txt(buf, sizeof(buf) - 1, sk_ASN1_OBJECT_value(eku, i), 1); - ext_key_usage->Set(Integer::New(i, node_isolate), String::New(buf)); + ext_key_usage->Set(Integer::New(i, node_isolate), + OneByteString(node_isolate, buf)); } sk_ASN1_OBJECT_pop_free(eku, ASN1_OBJECT_free); @@ -1748,8 +1773,10 @@ void Connection::VerifyError(const FunctionCallbackInfo& args) { // We requested a certificate and they did not send us one. // Definitely an error. // XXX is this the right error message? - return args.GetReturnValue().Set( - Exception::Error(String::New("UNABLE_TO_GET_ISSUER_CERT"))); + Local error_message = + FIXED_ONE_BYTE_STRING(node_isolate, "UNABLE_TO_GET_ISSUER_CERT"); + Local exception = Exception::Error(error_message); + return args.GetReturnValue().Set(exception); } X509_free(peer_cert); @@ -1762,115 +1789,121 @@ void Connection::VerifyError(const FunctionCallbackInfo& args) { break; case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: - s = String::New("UNABLE_TO_GET_ISSUER_CERT"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "UNABLE_TO_GET_ISSUER_CERT"); break; case X509_V_ERR_UNABLE_TO_GET_CRL: - s = String::New("UNABLE_TO_GET_CRL"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "UNABLE_TO_GET_CRL"); break; case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: - s = String::New("UNABLE_TO_DECRYPT_CERT_SIGNATURE"); + s = FIXED_ONE_BYTE_STRING(node_isolate, + "UNABLE_TO_DECRYPT_CERT_SIGNATURE"); break; case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE: - s = String::New("UNABLE_TO_DECRYPT_CRL_SIGNATURE"); + s = FIXED_ONE_BYTE_STRING(node_isolate, + "UNABLE_TO_DECRYPT_CRL_SIGNATURE"); break; case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: - s = String::New("UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY"); + s = FIXED_ONE_BYTE_STRING(node_isolate, + "UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY"); break; case X509_V_ERR_CERT_SIGNATURE_FAILURE: - s = String::New("CERT_SIGNATURE_FAILURE"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "CERT_SIGNATURE_FAILURE"); break; case X509_V_ERR_CRL_SIGNATURE_FAILURE: - s = String::New("CRL_SIGNATURE_FAILURE"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "CRL_SIGNATURE_FAILURE"); break; case X509_V_ERR_CERT_NOT_YET_VALID: - s = String::New("CERT_NOT_YET_VALID"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "CERT_NOT_YET_VALID"); break; case X509_V_ERR_CERT_HAS_EXPIRED: - s = String::New("CERT_HAS_EXPIRED"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "CERT_HAS_EXPIRED"); break; case X509_V_ERR_CRL_NOT_YET_VALID: - s = String::New("CRL_NOT_YET_VALID"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "CRL_NOT_YET_VALID"); break; case X509_V_ERR_CRL_HAS_EXPIRED: - s = String::New("CRL_HAS_EXPIRED"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "CRL_HAS_EXPIRED"); break; case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: - s = String::New("ERROR_IN_CERT_NOT_BEFORE_FIELD"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "ERROR_IN_CERT_NOT_BEFORE_FIELD"); break; case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: - s = String::New("ERROR_IN_CERT_NOT_AFTER_FIELD"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "ERROR_IN_CERT_NOT_AFTER_FIELD"); break; case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD: - s = String::New("ERROR_IN_CRL_LAST_UPDATE_FIELD"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "ERROR_IN_CRL_LAST_UPDATE_FIELD"); break; case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD: - s = String::New("ERROR_IN_CRL_NEXT_UPDATE_FIELD"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "ERROR_IN_CRL_NEXT_UPDATE_FIELD"); break; case X509_V_ERR_OUT_OF_MEM: - s = String::New("OUT_OF_MEM"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "OUT_OF_MEM"); break; case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: - s = String::New("DEPTH_ZERO_SELF_SIGNED_CERT"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "DEPTH_ZERO_SELF_SIGNED_CERT"); break; case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: - s = String::New("SELF_SIGNED_CERT_IN_CHAIN"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "SELF_SIGNED_CERT_IN_CHAIN"); break; case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: - s = String::New("UNABLE_TO_GET_ISSUER_CERT_LOCALLY"); + s = FIXED_ONE_BYTE_STRING(node_isolate, + "UNABLE_TO_GET_ISSUER_CERT_LOCALLY"); break; case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: - s = String::New("UNABLE_TO_VERIFY_LEAF_SIGNATURE"); + s = FIXED_ONE_BYTE_STRING(node_isolate, + "UNABLE_TO_VERIFY_LEAF_SIGNATURE"); break; case X509_V_ERR_CERT_CHAIN_TOO_LONG: - s = String::New("CERT_CHAIN_TOO_LONG"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "CERT_CHAIN_TOO_LONG"); break; case X509_V_ERR_CERT_REVOKED: - s = String::New("CERT_REVOKED"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "CERT_REVOKED"); break; case X509_V_ERR_INVALID_CA: - s = String::New("INVALID_CA"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "INVALID_CA"); break; case X509_V_ERR_PATH_LENGTH_EXCEEDED: - s = String::New("PATH_LENGTH_EXCEEDED"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "PATH_LENGTH_EXCEEDED"); break; case X509_V_ERR_INVALID_PURPOSE: - s = String::New("INVALID_PURPOSE"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "INVALID_PURPOSE"); break; case X509_V_ERR_CERT_UNTRUSTED: - s = String::New("CERT_UNTRUSTED"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "CERT_UNTRUSTED"); break; case X509_V_ERR_CERT_REJECTED: - s = String::New("CERT_REJECTED"); + s = FIXED_ONE_BYTE_STRING(node_isolate, "CERT_REJECTED"); break; default: - s = String::New(X509_verify_cert_error_string(x509_verify_error)); + s = OneByteString(node_isolate, + X509_verify_cert_error_string(x509_verify_error)); break; } @@ -1893,9 +1926,9 @@ void Connection::GetCurrentCipher(const FunctionCallbackInfo& args) { if (c == NULL) return; Local info = Object::New(); const char* cipher_name = SSL_CIPHER_get_name(c); - info->Set(name_symbol, String::New(cipher_name)); + info->Set(name_symbol, OneByteString(node_isolate, cipher_name)); const char* cipher_version = SSL_CIPHER_get_version(c); - info->Set(version_symbol, String::New(cipher_version)); + info->Set(version_symbol, OneByteString(node_isolate, cipher_version)); args.GetReturnValue().Set(info); } @@ -1929,7 +1962,7 @@ void Connection::GetNegotiatedProto(const FunctionCallbackInfo& args) { } args.GetReturnValue().Set( - String::New(reinterpret_cast(npn_proto), npn_proto_len)); + OneByteString(node_isolate, npn_proto, npn_proto_len)); } else { args.GetReturnValue().Set(ss->selectedNPNProto_); } @@ -1974,7 +2007,7 @@ void Connection::SetSNICallback(const FunctionCallbackInfo& args) { } Local obj = Object::New(); - obj->Set(String::New("onselect"), args[0]); + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "onselect"), args[0]); ss->sniObject_.Reset(node_isolate, obj); } #endif @@ -1993,7 +2026,8 @@ void CipherBase::Initialize(Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "final", Final); NODE_SET_PROTOTYPE_METHOD(t, "setAutoPadding", SetAutoPadding); - target->Set(String::NewSymbol("CipherBase"), t->GetFunction()); + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "CipherBase"), + t->GetFunction()); } @@ -2232,7 +2266,7 @@ void Hmac::Initialize(v8::Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "update", HmacUpdate); NODE_SET_PROTOTYPE_METHOD(t, "digest", HmacDigest); - target->Set(String::NewSymbol("Hmac"), t->GetFunction()); + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "Hmac"), t->GetFunction()); } @@ -2363,7 +2397,7 @@ void Hash::Initialize(v8::Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "update", HashUpdate); NODE_SET_PROTOTYPE_METHOD(t, "digest", HashDigest); - target->Set(String::NewSymbol("Hash"), t->GetFunction()); + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "Hash"), t->GetFunction()); } @@ -2473,7 +2507,7 @@ void Sign::Initialize(v8::Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "update", SignUpdate); NODE_SET_PROTOTYPE_METHOD(t, "sign", SignFinal); - target->Set(String::NewSymbol("Sign"), t->GetFunction()); + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "Sign"), t->GetFunction()); } @@ -2618,7 +2652,7 @@ void Verify::Initialize(v8::Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "update", VerifyUpdate); NODE_SET_PROTOTYPE_METHOD(t, "verify", VerifyFinal); - target->Set(String::NewSymbol("Verify"), t->GetFunction()); + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "Verify"), t->GetFunction()); } @@ -2826,7 +2860,8 @@ void DiffieHellman::Initialize(v8::Handle target) { NODE_SET_PROTOTYPE_METHOD(t, "setPublicKey", SetPublicKey); NODE_SET_PROTOTYPE_METHOD(t, "setPrivateKey", SetPrivateKey); - target->Set(String::NewSymbol("DiffieHellman"), t->GetFunction()); + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "DiffieHellman"), + t->GetFunction()); Local t2 = FunctionTemplate::New(DiffieHellmanGroup); t2->InstanceTemplate()->SetInternalFieldCount(1); @@ -2838,7 +2873,8 @@ void DiffieHellman::Initialize(v8::Handle target) { NODE_SET_PROTOTYPE_METHOD(t2, "getPublicKey", GetPublicKey); NODE_SET_PROTOTYPE_METHOD(t2, "getPrivateKey", GetPrivateKey); - target->Set(String::NewSymbol("DiffieHellmanGroup"), t2->GetFunction()); + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "DiffieHellmanGroup"), + t2->GetFunction()); } @@ -3201,7 +3237,8 @@ void EIO_PBKDF2After(pbkdf2_req* req, Local argv[2]) { argv[1] = Encode(req->key, req->keylen, BUFFER); memset(req->key, 0, req->keylen); } else { - argv[0] = Exception::Error(String::New("PBKDF2 error")); + argv[0] = Exception::Error( + FIXED_ONE_BYTE_STRING(node_isolate, "PBKDF2 error")); argv[1] = Undefined(node_isolate); } @@ -3302,7 +3339,7 @@ void PBKDF2(const FunctionCallbackInfo& args) { if (args[4]->IsFunction()) { Local obj = Object::New(); - obj->Set(String::New("ondone"), args[4]); + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "ondone"), args[4]); req->obj.Reset(node_isolate, obj); uv_queue_work(uv_default_loop(), &req->work_req, @@ -3373,7 +3410,7 @@ void RandomBytesCheck(RandomBytesRequest* req, Local argv[2]) { if (req->error_ != (unsigned long) -1) ERR_error_string_n(req->error_, errmsg, sizeof errmsg); - argv[0] = Exception::Error(String::New(errmsg)); + argv[0] = Exception::Error(OneByteString(node_isolate, errmsg)); argv[1] = Null(node_isolate); } else { argv[0] = Null(node_isolate); @@ -3417,7 +3454,7 @@ void RandomBytes(const FunctionCallbackInfo& args) { if (args[1]->IsFunction()) { Local obj = Object::New(); - obj->Set(String::New("ondone"), args[1]); + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "ondone"), args[1]); req->obj_.Reset(node_isolate, obj); uv_queue_work(uv_default_loop(), @@ -3458,7 +3495,7 @@ void GetSSLCiphers(const FunctionCallbackInfo& args) { for (int i = 0; i < sk_SSL_CIPHER_num(ciphers); ++i) { SSL_CIPHER* cipher = sk_SSL_CIPHER_value(ciphers, i); - arr->Set(i, String::New(SSL_CIPHER_get_name(cipher))); + arr->Set(i, OneByteString(node_isolate, SSL_CIPHER_get_name(cipher))); } SSL_free(ssl); @@ -3474,7 +3511,7 @@ static void array_push_back(const TypeName* md, const char* to, void* arg) { Local& arr = *static_cast*>(arg); - arr->Set(arr->Length(), String::New(from)); + arr->Set(arr->Length(), OneByteString(node_isolate, from)); } @@ -3534,17 +3571,17 @@ void InitCrypto(Handle target) { NODE_SET_METHOD(target, "getCiphers", GetCiphers); NODE_SET_METHOD(target, "getHashes", GetHashes); - subject_symbol = String::New("subject"); - issuer_symbol = String::New("issuer"); - valid_from_symbol = String::New("valid_from"); - valid_to_symbol = String::New("valid_to"); - subjectaltname_symbol = String::New("subjectaltname"); - modulus_symbol = String::New("modulus"); - exponent_symbol = String::New("exponent"); - fingerprint_symbol = String::New("fingerprint"); - name_symbol = String::New("name"); - version_symbol = String::New("version"); - ext_key_usage_symbol = String::New("ext_key_usage"); + subject_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "subject"); + issuer_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "issuer"); + valid_from_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "valid_from"); + valid_to_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "valid_to"); + subjectaltname_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "subjectaltname"); + modulus_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "modulus"); + exponent_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "exponent"); + fingerprint_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "fingerprint"); + name_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "name"); + version_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "version"); + ext_key_usage_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "ext_key_usage"); } } // namespace crypto diff --git a/src/node_dtrace.cc b/src/node_dtrace.cc index 9d7048b..de2fcea 100644 --- a/src/node_dtrace.cc +++ b/src/node_dtrace.cc @@ -77,7 +77,7 @@ using v8::Value; return ThrowError( \ "expected object for " #obj " to contain string member " #member); \ } \ - String::Utf8Value _##member(obj->Get(String::New(#member))); \ + String::Utf8Value _##member(obj->Get(OneByteString(node_isolate, #member))); \ if ((*(const char **)valp = *_##member) == NULL) \ *(const char **)valp = ""; @@ -86,14 +86,14 @@ using v8::Value; return ThrowError( \ "expected object for " #obj " to contain integer member " #member); \ } \ - *valp = obj->Get(String::New(#member))->ToInteger()->Value(); + *valp = obj->Get(OneByteString(node_isolate, #member))->ToInteger()->Value(); #define SLURP_OBJECT(obj, member, valp) \ if (!(obj)->IsObject()) { \ return ThrowError( \ "expected object for " #obj " to contain object member " #member); \ } \ - *valp = Local::Cast(obj->Get(String::New(#member))); + *valp = Local::Cast(obj->Get(OneByteString(node_isolate, #member))); #define SLURP_CONNECTION(arg, conn) \ if (!(arg)->IsObject()) { \ @@ -102,7 +102,7 @@ using v8::Value; } \ node_dtrace_connection_t conn; \ Local _##conn = Local::Cast(arg); \ - Local _handle = (_##conn)->Get(String::New("_handle")); \ + Local _handle = (_##conn)->Get(FIXED_ONE_BYTE_STRING(node_isolate, "_handle")); \ if (_handle->IsObject()) { \ SLURP_INT(_handle.As(), fd, &conn.fd); \ } else { \ @@ -221,7 +221,7 @@ void DTRACE_HTTP_SERVER_REQUEST(const FunctionCallbackInfo& args) { "expected object for request to contain string member headers"); } - Local strfwdfor = headers->Get(String::New("x-forwarded-for")); + Local strfwdfor = headers->Get(FIXED_ONE_BYTE_STRING(node_isolate, "x-forwarded-for")); String::Utf8Value fwdfor(strfwdfor); if (!strfwdfor->IsString() || (req.forwardedFor = *fwdfor) == NULL) @@ -332,7 +332,7 @@ void InitDTrace(Handle target) { }; for (unsigned int i = 0; i < ARRAY_SIZE(tab); i++) { - Local key = String::New(tab[i].name); + Local key = OneByteString(node_isolate, tab[i].name); Local val = FunctionTemplate::New(tab[i].func)->GetFunction(); target->Set(key, val); } diff --git a/src/node_file.cc b/src/node_file.cc index 9aa944b..f09973f 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -169,7 +169,8 @@ static void After(uv_fs_t *req) { break; case UV_FS_READLINK: - argv[1] = String::New(static_cast(req->ptr)); + argv[1] = String::NewFromUtf8(node_isolate, + static_cast(req->ptr)); break; case UV_FS_READ: @@ -185,7 +186,7 @@ static void After(uv_fs_t *req) { Local names = Array::New(nnames); for (int i = 0; i < nnames; i++) { - Local name = String::New(namebuf); + Local name = String::NewFromUtf8(node_isolate, namebuf); names->Set(Integer::New(i, node_isolate), name); #ifndef NDEBUG namebuf += strlen(namebuf); @@ -206,7 +207,7 @@ static void After(uv_fs_t *req) { } if (oncomplete_sym.IsEmpty()) { - oncomplete_sym = String::New("oncomplete"); + oncomplete_sym = FIXED_ONE_BYTE_STRING(node_isolate, "oncomplete"); } MakeCallback(req_wrap->object(), oncomplete_sym, argc, argv); @@ -290,19 +291,19 @@ Local BuildStatsObject(const uv_stat_t* s) { HandleScope scope(node_isolate); if (dev_symbol.IsEmpty()) { - dev_symbol = String::New("dev"); - ino_symbol = String::New("ino"); - mode_symbol = String::New("mode"); - nlink_symbol = String::New("nlink"); - uid_symbol = String::New("uid"); - gid_symbol = String::New("gid"); - rdev_symbol = String::New("rdev"); - size_symbol = String::New("size"); - blksize_symbol = String::New("blksize"); - blocks_symbol = String::New("blocks"); - atime_symbol = String::New("atime"); - mtime_symbol = String::New("mtime"); - ctime_symbol = String::New("ctime"); + dev_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "dev"); + ino_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "ino"); + mode_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "mode"); + nlink_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "nlink"); + uid_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "uid"); + gid_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "gid"); + rdev_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "rdev"); + size_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "size"); + blksize_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "blksize"); + blocks_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "blocks"); + atime_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "atime"); + mtime_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "mtime"); + ctime_symbol = FIXED_ONE_BYTE_STRING(node_isolate, "ctime"); } Local constructor = @@ -481,8 +482,9 @@ static void ReadLink(const FunctionCallbackInfo& args) { ASYNC_CALL(readlink, args[1], *path) } else { SYNC_CALL(readlink, *path, *path) - args.GetReturnValue().Set( - String::New(static_cast(SYNC_REQ.ptr))); + const char* link_path = static_cast(SYNC_REQ.ptr); + Local rc = String::NewFromUtf8(node_isolate, link_path); + args.GetReturnValue().Set(rc); } } @@ -621,7 +623,7 @@ static void ReadDir(const FunctionCallbackInfo& args) { Local names = Array::New(nnames); for (int i = 0; i < nnames; i++) { - Local name = String::New(namebuf); + Local name = String::NewFromUtf8(node_isolate, namebuf); names->Set(Integer::New(i, node_isolate), name); #ifndef NDEBUG namebuf += strlen(namebuf); @@ -1005,12 +1007,12 @@ void InitFs(Handle target) { // Initialize the stats object Local constructor = FunctionTemplate::New()->GetFunction(); - target->Set(String::NewSymbol("Stats"), constructor); + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "Stats"), constructor); stats_constructor.Reset(node_isolate, constructor); File::Initialize(target); - oncomplete_sym = String::New("oncomplete"); + oncomplete_sym = FIXED_ONE_BYTE_STRING(node_isolate, "oncomplete"); StatWatcher::Initialize(target); } diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc index 5261f26..72fda5e 100644 --- a/src/node_http_parser.cc +++ b/src/node_http_parser.cc @@ -179,7 +179,7 @@ struct StringPtr { Local ToString() const { if (str_) - return String::New(str_, size_); + return OneByteString(node_isolate, str_, size_); else return String::Empty(node_isolate); } @@ -433,10 +433,12 @@ class Parser : public ObjectWrap { if (!parser->parser_.upgrade && nparsed != buffer_len) { enum http_errno err = HTTP_PARSER_ERRNO(&parser->parser_); - Local e = Exception::Error(String::NewSymbol("Parse Error")); + Local e = Exception::Error( + FIXED_ONE_BYTE_STRING(node_isolate, "Parse Error")); Local obj = e->ToObject(); - obj->Set(String::NewSymbol("bytesParsed"), nparsed_obj); - obj->Set(String::NewSymbol("code"), String::New(http_errno_name(err))); + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "bytesParsed"), nparsed_obj); + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "code"), + OneByteString(node_isolate, http_errno_name(err))); args.GetReturnValue().Set(e); } else { args.GetReturnValue().Set(nparsed_obj); @@ -459,10 +461,13 @@ class Parser : public ObjectWrap { if (rv != 0) { enum http_errno err = HTTP_PARSER_ERRNO(&parser->parser_); - Local e = Exception::Error(String::NewSymbol("Parse Error")); + Local e = Exception::Error( + FIXED_ONE_BYTE_STRING(node_isolate, "Parse Error")); Local obj = e->ToObject(); - obj->Set(String::NewSymbol("bytesParsed"), Integer::New(0, node_isolate)); - obj->Set(String::NewSymbol("code"), String::New(http_errno_name(err))); + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "bytesParsed"), + Integer::New(0, node_isolate)); + obj->Set(FIXED_ONE_BYTE_STRING(node_isolate, "code"), + OneByteString(node_isolate, http_errno_name(err))); args.GetReturnValue().Set(e); } } @@ -547,38 +552,45 @@ void InitHttpParser(Handle target) { Local t = FunctionTemplate::New(Parser::New); t->InstanceTemplate()->SetInternalFieldCount(1); - t->SetClassName(String::NewSymbol("HTTPParser")); + t->SetClassName(FIXED_ONE_BYTE_STRING(node_isolate, "HTTPParser")); - t->Set(String::NewSymbol("REQUEST"), + t->Set(FIXED_ONE_BYTE_STRING(node_isolate, "REQUEST"), Integer::New(HTTP_REQUEST, node_isolate)); - t->Set(String::NewSymbol("RESPONSE"), + t->Set(FIXED_ONE_BYTE_STRING(node_isolate, "RESPONSE"), Integer::New(HTTP_RESPONSE, node_isolate)); NODE_SET_PROTOTYPE_METHOD(t, "execute", Parser::Execute); NODE_SET_PROTOTYPE_METHOD(t, "finish", Parser::Finish); NODE_SET_PROTOTYPE_METHOD(t, "reinitialize", Parser::Reinitialize); - target->Set(String::NewSymbol("HTTPParser"), t->GetFunction()); + target->Set(FIXED_ONE_BYTE_STRING(node_isolate, "HTTPParser"), + t->GetFunction()); - on_headers_sym = String::New("onHeaders"); - on_headers_complete_sym = String::New("onHeadersComplete"); - on_body_sym = String::New("onBody"); - on_message_complete_sym = String::New("onMessageComplete"); + on_headers_sym = + FIXED_ONE_BYTE_STRING(node_isolate, "onHeaders"); + on_headers_complete_sym = + FIXED_ONE_BYTE_STRING(node_isolate, "onHeadersComplete"); + on_body_sym = + FIXED_ONE_BYTE_STRING(node_isolate, "onBody"); + on_message_complete_sym = + FIXED_ONE_BYTE_STRING(node_isolate, "onMessageComplete"); -#define X(num, name, string) name##_sym = String::New(#string); +#define X(num, name, string) \ + name ## _sym = OneByteString(node_isolate, #string); HTTP_METHOD_MAP(X) #undef X - unknown_method_sym = String::New("UNKNOWN_METHOD"); - - method_sym = String::New("method"); - status_code_sym = String::New("statusCode"); - http_version_sym = String::New("httpVersion"); - version_major_sym = String::New("versionMajor"); - version_minor_sym = String::New("versionMinor"); - should_keep_alive_sym = String::New("shouldKeepAlive"); - upgrade_sym = String::New("upgrade"); - headers_sym = String::New("headers"); - url_sym = String::New("url"); + unknown_method_sym = FIXED_ONE_BYTE_STRING(node_isolate, "UNKNOWN_METHOD"); + + method_sym = FIXED_ONE_BYTE_STRING(node_isolate, "method"); + status_code_sym = FIXED_ONE_BYTE_STRING(node_isolate, "statusCode"); + http_version_sym = FIXED_ONE_BYTE_STRING(node_isolate, "httpVersion"); + version_major_sym = FIXED_ONE_BYTE_STRING(node_isolate, "versionMajor"); + version_minor_sym = FIXED_ONE_BYTE_STRING(node_isolate, "versionMinor"); + should_keep_alive_sym = + FIXED_ONE_BYTE_STRING(node_isolate, "shouldKeepAlive"); + upgrade_sym = FIXED_ONE_BYTE_STRING(node_isolate, "upgrade"); + headers_sym = FIXED_ONE_BYTE_STRING(node_isolate, "headers"); + url_sym = FIXED_ONE_BYTE_STRING(node_isolate, "url"); settings.on_message_begin = Parser::on_message_begin; settings.on_url = Parser::on_url; diff --git a/src/node_internals.h b/src/node_internals.h index d9db29c..082368e 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -25,8 +25,12 @@ #include "v8.h" #include +#include #include +#define FIXED_ONE_BYTE_STRING(isolate, string) \ + (node::OneByteString((isolate), (string), sizeof(string) - 1)) + struct sockaddr; namespace node { @@ -95,6 +99,20 @@ inline v8::Local NewInstance( int argc = 0, v8::Handle* argv = NULL); +// Convenience wrapper around v8::String::NewFromOneByte(). +inline v8::Local OneByteString(v8::Isolate* isolate, + const char* data, + int length = -1); + +// For the people that compile with -funsigned-char. +inline v8::Local OneByteString(v8::Isolate* isolate, + const signed char* data, + int length = -1); + +inline v8::Local OneByteString(v8::Isolate* isolate, + const unsigned char* data, + int length = -1); + // Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object. // Sets address and port properties on the info object and returns it. // If |info| is omitted, a new object is returned. @@ -157,7 +175,7 @@ inline static int snprintf(char* buf, unsigned int len, const char* fmt, ...) { #define THROW_ERROR(fun) \ do { \ v8::HandleScope scope(node_isolate); \ - v8::ThrowException(fun(v8::String::New(errmsg))); \ + v8::ThrowException(fun(OneByteString(node_isolate, errmsg))); \ } \ while (0) @@ -333,6 +351,33 @@ inline v8::Local NewInstance( return constructor_handle->NewInstance(argc, argv); } +inline v8::Local OneByteString(v8::Isolate* isolate, + const char* data, + int length) { + return v8::String::NewFromOneByte(isolate, + reinterpret_cast(data), + v8::String::kNormalString, + length); +} + +inline v8::Local OneByteString(v8::Isolate* isolate, + const signed char* data, + int length) { + return v8::String::NewFromOneByte(isolate, + reinterpret_cast(data), + v8::String::kNormalString, + length); +} + +inline v8::Local OneByteString(v8::Isolate* isolate, + const unsigned char* data, + int length) { + return v8::String::NewFromOneByte(isolate, + reinterpret_cast(data), + v8::String::kNormalString, + length); +} + } // namespace node #endif // SRC_NODE_INTERNALS_H_ diff --git a/src/node_javascript.cc b/src/node_javascript.cc index b37caa3..deddbe2 100644 --- a/src/node_javascript.cc +++ b/src/node_javascript.cc @@ -37,7 +37,7 @@ using v8::Object; using v8::String; Handle MainSource() { - return String::New(node_native, sizeof(node_native) - 1); + return OneByteString(node_isolate, node_native, sizeof(node_native) - 1); } void DefineJavaScript(Handle target) { @@ -45,9 +45,11 @@ void DefineJavaScript(Handle target) { for (int i = 0; natives[i].name; i++) { if (natives[i].source != node_native) { - Local name = String::New(natives[i].name); - Handle source = String::New(natives[i].source, - natives[i].source_len); + Local name = String::NewFromUtf8(node_isolate, natives[i].name); + Handle source = String::NewFromUtf8(node_isolate, + natives[i].source, + String::kNormalString, + natives[i].source_len); target->Set(name, source); } } diff --git a/src/node_os.cc b/src/node_os.cc index cbce5cc..4e79127 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -59,7 +59,7 @@ using v8::Value; static void GetEndianness(const FunctionCallbackInfo& args) { HandleScope scope(node_isolate); const char* rval = IsBigEndian() ? "BE" : "LE"; - args.GetReturnValue().Set(String::New(rval)); + args.GetReturnValue().Set(OneByteString(node_isolate, rval)); } @@ -77,7 +77,7 @@ static void GetHostname(const FunctionCallbackInfo& args) { } buf[sizeof(buf) - 1] = '\0'; - args.GetReturnValue().Set(String::New(buf)); + args.GetReturnValue().Set(OneByteString(node_isolate, buf)); } @@ -95,7 +95,7 @@ static void GetOSType(const FunctionCallbackInfo& args) { rval ="Windows_NT"; #endif // __POSIX__ - args.GetReturnValue().Set(String::New(rval)); + args.GetReturnValue().Set(OneByteString(node_isolate, rval)); } @@ -125,7 +125,7 @@ static void GetOSRelease(const FunctionCallbackInfo& args) { rval = release; #endif // __POSIX__ - args.GetReturnValue().Set(String::New(rval)); + args.GetReturnValue().Set(OneByteString(node_isolate, rval)); } @@ -142,21 +142,23 @@ static void GetCPUInfo(const FunctionCallbackInfo& args) { uv_cpu_info_t* ci = cpu_infos + i; Local times_info = Object::New(); - times_info->Set(String::New("user"), + times_info->Set(FIXED_ONE_BYTE_STRING(node_isolate, "user"), Number::New(node_isolate, ci->cpu_times.user)); - times_info->Set(String::New("nice"), + times_info->Set(FIXED_ONE_BYTE_STRING(node_isolate, "nice"), Number::New(node_isolate, ci->cpu_times.nice)); - times_info->Set(String::New("sys"), + times_info->Set(FIXED_ONE_BYTE_STRING(node_isolate, "sys"), Number::New(node_isolate, ci->cpu_times.sys)); - times_info->Set(String::New("idle"), + times_info->Set(FIXED_ONE_BYTE_STRING(node_isolate, "idle"), Number::New(node_isolate, ci->cpu_times.idle)); - times_info->Set(String::New("irq"), + times_info->Set(FIXED_ONE_BYTE_STRING(node_isolate, "irq"), Number::New(node_isolate, ci->cpu_times.irq)); Local cpu_info = Object::New(); - cpu_info->Set(String::New("model"), String::New(ci->model)); - cpu_info->Set(String::New("speed"), Number::New(node_isolate, ci->speed)); - cpu_info->Set(String::New("times"), times_info); + cpu_info->Set(FIXED_ONE_BYTE_STRING(node_isolate, "model"), + OneByteString(node_isolate, ci->model)); + cpu_info->Set(FIXED_ONE_BYTE_STRING(node_isolate, "speed"), + Number::New(node_isolate, ci->speed)); + cpu_info->Set(FIXED_ONE_BYTE_STRING(node_isolate, "times"), times_info); (*cpus)->Set(i, cpu_info); } @@ -221,7 +223,7 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo& args) { ret = Object::New(); for (i = 0; i < count; i++) { - name = String::New(interfaces[i].name); + name = OneByteString(node_isolate, interfaces[i].name); if (ret->Has(name)) { ifarr = Local::Cast(ret->Get(name)); } else { @@ -242,24 +244,27 @@ static void GetInterfaceAddresses(const FunctionCallbackInfo& args) { if (interfaces[i].address.address4.sin_family == AF_INET) { uv_ip4_name(&interfaces[i].address.address4, ip, sizeof(ip)); uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask)); - family = String::New("IPv4"); + family = FIXED_ONE_BYTE_STRING(node_isolate, "IPv4"); } else if (interfaces[i].address.address4.sin_family == AF_INET6) { uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip)); uv_ip6_name(&interfaces[i].netmask.netmask6, netmask, sizeof(netmask)); - family = String::New("IPv6"); + family = FIXED_ONE_BYTE_STRING(node_isolate, "IPv6"); } else { strncpy(ip, "", INET6_ADDRSTRLEN); - family = String::New(""); + family = FIXED_ONE_BYTE_STRING(node_isolate, ""); } o = Object::New(); - o->Set(String::New("address"), String::New(ip)); - o->Set(String::New("netmask"), String::New(netmask)); - o->Set(String::New("family"), family); - o->Set(String::New("mac"), String::New(mac)); + o->Set(FIXED_ONE_BYTE_STRING(node_isolate, "address"), + OneByteString(node_isolate, ip)); + o->Set(FIXED_ONE_BYTE_STRING(node_isolate, "netmask"), + OneByteString(node_isolate, netmask)); + o->Set(FIXED_ONE_BYTE_STRING(node_isolate, "family"), family); + o->Set(FIXED_ONE_BYTE_STRING(node_isolate, "mac"), + FIXED_ONE_BYTE_STRING(node_isolate, mac)); const bool internal = interfaces[i].is_internal; - o->Set(String::New("internal"), + o->Set(FIXED_ONE_BYTE_STRING(node_isolate, "internal"), internal ? True(node_isolate) : False(node_isolate)); ifarr->Set(ifarr->Length(), o); diff --git a/src/node_script.cc b/src/node_script.cc index d155c51..2795cef 100644 --- a/src/node_script.cc +++ b/src/node_script.cc @@ -118,9 +118,10 @@ void CloneObject(Handle recv, "}); \n"; Local script_source = - String::New(raw_script_source, sizeof(raw_script_source) - 1); - Local