Browse Source

src,tools: allow utf-8 in built-in js source code

PR-URL: https://github.com/nodejs/node/pull/5418
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
v5.x
Ben Noordhuis 9 years ago
committed by Rod Vagg
parent
commit
17d14f3346
  1. 13
      src/node_javascript.cc
  2. 23
      tools/js2c.py

13
src/node_javascript.cc

@ -13,11 +13,14 @@ namespace node {
using v8::HandleScope; using v8::HandleScope;
using v8::Local; using v8::Local;
using v8::NewStringType;
using v8::Object; using v8::Object;
using v8::String; using v8::String;
Local<String> MainSource(Environment* env) { Local<String> MainSource(Environment* env) {
return OneByteString(env->isolate(), node_native, sizeof(node_native) - 1); return String::NewFromUtf8(
env->isolate(), reinterpret_cast<const char*>(node_native),
NewStringType::kNormal, sizeof(node_native) - 1).ToLocalChecked();
} }
void DefineJavaScript(Environment* env, Local<Object> target) { void DefineJavaScript(Environment* env, Local<Object> target) {
@ -26,10 +29,10 @@ void DefineJavaScript(Environment* env, Local<Object> target) {
for (int i = 0; natives[i].name; i++) { for (int i = 0; natives[i].name; i++) {
if (natives[i].source != node_native) { if (natives[i].source != node_native) {
Local<String> name = String::NewFromUtf8(env->isolate(), natives[i].name); Local<String> name = String::NewFromUtf8(env->isolate(), natives[i].name);
Local<String> source = String::NewFromUtf8(env->isolate(), Local<String> source =
natives[i].source, String::NewFromUtf8(
String::kNormalString, env->isolate(), reinterpret_cast<const char*>(natives[i].source),
natives[i].source_len); NewStringType::kNormal, natives[i].source_len).ToLocalChecked();
target->Set(name, source); target->Set(name, source);
} }
} }

23
tools/js2c.py

@ -42,24 +42,7 @@ import jsmin
def ToCArray(filename, lines): def ToCArray(filename, lines):
result = [] return ','.join(str(ord(c)) for c in lines + '\0')
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)
def CompressScript(lines, do_jsmin): def CompressScript(lines, do_jsmin):
@ -220,7 +203,7 @@ namespace node {
struct _native { struct _native {
const char* name; const char* name;
const char* source; const unsigned char* source;
size_t source_len; size_t source_len;
}; };
@ -242,7 +225,7 @@ NATIVE_DECLARATION = """\
""" """
SOURCE_DECLARATION = """\ SOURCE_DECLARATION = """\
const char %(escaped_id)s_native[] = { %(data)s }; const unsigned char %(escaped_id)s_native[] = { %(data)s };
""" """

Loading…
Cancel
Save