From cdc7e025e0d3b9f18403c53d523f99cc91051e58 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 24 Feb 2016 21:57:34 +0100 Subject: [PATCH 1/3] src,tools: allow utf-8 in built-in js source code PR-URL: https://github.com/nodejs/node/pull/5418 Reviewed-By: Trevor Norris --- src/node_javascript.cc | 13 ++++++++----- tools/js2c.py | 23 +++-------------------- 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/src/node_javascript.cc b/src/node_javascript.cc index e8dc4ac4eaf2d2..6159cfede93810 100644 --- a/src/node_javascript.cc +++ b/src/node_javascript.cc @@ -13,11 +13,14 @@ namespace node { using v8::HandleScope; using v8::Local; +using v8::NewStringType; using v8::Object; using v8::String; Local MainSource(Environment* env) { - return OneByteString(env->isolate(), node_native, sizeof(node_native) - 1); + return String::NewFromUtf8( + env->isolate(), reinterpret_cast(node_native), + NewStringType::kNormal, sizeof(node_native) - 1).ToLocalChecked(); } void DefineJavaScript(Environment* env, Local target) { @@ -26,10 +29,10 @@ void DefineJavaScript(Environment* env, Local target) { for (int i = 0; natives[i].name; i++) { if (natives[i].source != node_native) { Local name = String::NewFromUtf8(env->isolate(), natives[i].name); - Local source = String::NewFromUtf8(env->isolate(), - natives[i].source, - String::kNormalString, - natives[i].source_len); + Local source = + String::NewFromUtf8( + env->isolate(), reinterpret_cast(natives[i].source), + NewStringType::kNormal, natives[i].source_len).ToLocalChecked(); target->Set(name, source); } } diff --git a/tools/js2c.py b/tools/js2c.py index ec9705ec6af640..9c5878a66818eb 100755 --- a/tools/js2c.py +++ b/tools/js2c.py @@ -42,24 +42,7 @@ def ToCArray(filename, lines): - result = [] - row = 1 - col = 0 - for chr in lines: - col += 1 - if chr == "\n" or chr == "\r": - row += 1 - col = 0 - - value = ord(chr) - - if value >= 128: - print 'non-ascii value ' + filename + ':' + str(row) + ':' + str(col) - sys.exit(1); - - result.append(str(value)) - result.append("0") - return ", ".join(result) + return ','.join(str(ord(c)) for c in lines + '\0') def CompressScript(lines, do_jsmin): @@ -220,7 +203,7 @@ def ReadMacros(lines): struct _native { const char* name; - const char* source; + const unsigned char* source; size_t source_len; }; @@ -242,7 +225,7 @@ def ReadMacros(lines): """ SOURCE_DECLARATION = """\ - const char %(escaped_id)s_native[] = { %(data)s }; + const unsigned char %(escaped_id)s_native[] = { %(data)s }; """ From 96adbe95037fcc572d34484ea5d7ecd82911fda6 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 24 Feb 2016 21:59:25 +0100 Subject: [PATCH 2/3] src,tools: drop nul byte from built-in source code PR-URL: https://github.com/nodejs/node/pull/5418 Reviewed-By: Trevor Norris --- src/node_javascript.cc | 2 +- tools/js2c.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/node_javascript.cc b/src/node_javascript.cc index 6159cfede93810..5ba6e1d0a276f3 100644 --- a/src/node_javascript.cc +++ b/src/node_javascript.cc @@ -20,7 +20,7 @@ using v8::String; Local MainSource(Environment* env) { return String::NewFromUtf8( env->isolate(), reinterpret_cast(node_native), - NewStringType::kNormal, sizeof(node_native) - 1).ToLocalChecked(); + NewStringType::kNormal, sizeof(node_native)).ToLocalChecked(); } void DefineJavaScript(Environment* env, Local target) { diff --git a/tools/js2c.py b/tools/js2c.py index 9c5878a66818eb..761e80be59f76e 100755 --- a/tools/js2c.py +++ b/tools/js2c.py @@ -42,7 +42,7 @@ def ToCArray(filename, lines): - return ','.join(str(ord(c)) for c in lines + '\0') + return ','.join(str(ord(c)) for c in lines) def CompressScript(lines, do_jsmin): @@ -221,7 +221,7 @@ def ReadMacros(lines): NATIVE_DECLARATION = """\ - { "%(id)s", %(escaped_id)s_native, sizeof(%(escaped_id)s_native)-1 }, + { "%(id)s", %(escaped_id)s_native, sizeof(%(escaped_id)s_native) }, """ SOURCE_DECLARATION = """\ From 33e51fe18c82e882fe65f77ab14cbaf0efee4e0f Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 24 Feb 2016 22:05:00 +0100 Subject: [PATCH 3/3] src,tools: remove null sentinel from source array PR-URL: https://github.com/nodejs/node/pull/5418 Reviewed-By: Trevor Norris --- src/node_javascript.cc | 15 +++++---------- tools/js2c.py | 8 +------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/node_javascript.cc b/src/node_javascript.cc index 5ba6e1d0a276f3..d5377047c0ec5c 100644 --- a/src/node_javascript.cc +++ b/src/node_javascript.cc @@ -4,11 +4,6 @@ #include "env.h" #include "env-inl.h" -#include -#if !defined(_MSC_VER) -#include -#endif - namespace node { using v8::HandleScope; @@ -26,13 +21,13 @@ Local MainSource(Environment* env) { void DefineJavaScript(Environment* env, Local target) { HandleScope scope(env->isolate()); - for (int i = 0; natives[i].name; i++) { - if (natives[i].source != node_native) { - Local name = String::NewFromUtf8(env->isolate(), natives[i].name); + for (auto native : natives) { + if (native.source != node_native) { + Local name = String::NewFromUtf8(env->isolate(), native.name); Local source = String::NewFromUtf8( - env->isolate(), reinterpret_cast(natives[i].source), - NewStringType::kNormal, natives[i].source_len).ToLocalChecked(); + env->isolate(), reinterpret_cast(native.source), + NewStringType::kNormal, native.source_len).ToLocalChecked(); target->Set(name, source); } } diff --git a/tools/js2c.py b/tools/js2c.py index 761e80be59f76e..3e9a1c39cc496f 100755 --- a/tools/js2c.py +++ b/tools/js2c.py @@ -207,13 +207,7 @@ def ReadMacros(lines): size_t source_len; }; -static const struct _native natives[] = { - -%(native_lines)s\ - - { NULL, NULL, 0 } /* sentinel */ - -}; +static const struct _native natives[] = { %(native_lines)s }; } #endif