diff --git a/cmake/node_build.cmake b/cmake/node_build.cmake index 295bb3e632..9d89432f00 100644 --- a/cmake/node_build.cmake +++ b/cmake/node_build.cmake @@ -62,6 +62,7 @@ set(node_sources src/node_script.cc src/node_os.cc src/node_dtrace.cc + src/node_string.cc src/node_natives.h ${node_extra_src}) diff --git a/src/node.cc b/src/node.cc index b05e9aca9a..884c4a41c4 100644 --- a/src/node.cc +++ b/src/node.cc @@ -48,6 +48,7 @@ #include #include #include +#include #ifdef HAVE_OPENSSL # include #endif @@ -1269,7 +1270,7 @@ static void ReportException(TryCatch &try_catch, bool show_line) { } // Executes a str within the current v8 context. -Local ExecuteString(Local source, Local filename) { +Local ExecuteString(Handle source, Handle filename) { HandleScope scope; TryCatch try_catch; @@ -2036,8 +2037,8 @@ static void Load(int argc, char *argv[]) { TryCatch try_catch; - Local f_value = ExecuteString(String::New(MainSource()), - String::New("node.js")); + Local f_value = ExecuteString(MainSource(), + IMMUTABLE_STRING("node.js")); if (try_catch.HasCaught()) { ReportException(try_catch, true); exit(10); diff --git a/src/node_javascript.cc b/src/node_javascript.cc index 95005eeb15..e0cc5b5c3e 100644 --- a/src/node_javascript.cc +++ b/src/node_javascript.cc @@ -1,6 +1,7 @@ #include #include "node.h" #include "node_natives.h" +#include "node_string.h" #include #include @@ -8,8 +9,8 @@ using namespace v8; namespace node { -const char* MainSource() { - return node_native; +Handle MainSource() { + return BUILTIN_ASCII_ARRAY(node_native, sizeof(node_native)-1); } void DefineJavaScript(v8::Handle target) { @@ -18,14 +19,10 @@ void DefineJavaScript(v8::Handle target) { for (int i = 0; natives[i].name; i++) { if (natives[i].source != node_native) { Local name = String::New(natives[i].name); - // TODO: Use ExternalAsciiStringResource for source - // Might need to do some assertions in js2c about chars > 128 - Local source = String::New(natives[i].source); + Handle source = BUILTIN_ASCII_ARRAY(natives[i].source, natives[i].source_len); target->Set(name, source); } } } - - } // namespace node diff --git a/src/node_javascript.h b/src/node_javascript.h index 4df3bfa31e..9d682bd694 100644 --- a/src/node_javascript.h +++ b/src/node_javascript.h @@ -3,6 +3,6 @@ namespace node { void DefineJavaScript(v8::Handle target); -const char* MainSource(); +v8::Handle MainSource(); } // namespace node diff --git a/src/node_string.cc b/src/node_string.cc new file mode 100644 index 0000000000..806d7afc90 --- /dev/null +++ b/src/node_string.cc @@ -0,0 +1,18 @@ +#include "node_string.h" + +namespace node { + +using namespace v8; + +Handle ImmutableAsciiSource::CreateFromLiteral( + const char *string_literal, + size_t length) { + HandleScope scope; + + Local ret = String::NewExternal(new ImmutableAsciiSource( + string_literal, + length)); + return scope.Close(ret); +} + +} diff --git a/src/node_string.h b/src/node_string.h new file mode 100644 index 0000000000..1854cc2933 --- /dev/null +++ b/src/node_string.h @@ -0,0 +1,42 @@ +#ifndef SRC_NODE_STRING_H_ +#define SRC_NODE_STRING_H_ + +#include + +namespace node { + +#define IMMUTABLE_STRING(string_literal) \ + ::node::ImmutableAsciiSource::CreateFromLiteral( \ + string_literal "", sizeof(string_literal) - 1) +#define BUILTIN_ASCII_ARRAY(array, len) \ + ::node::ImmutableAsciiSource::CreateFromLiteral(array, len) + +class ImmutableAsciiSource : public v8::String::ExternalAsciiStringResource { + public: + static v8::Handle CreateFromLiteral(const char *string_literal, + size_t length); + + ImmutableAsciiSource(const char *src, size_t src_len) + : buffer_(src), + buf_len_(src_len) { + } + + ~ImmutableAsciiSource() { + } + + const char *data() const { + return buffer_; + } + + size_t length() const { + return buf_len_; + } + + private: + const char *buffer_; + size_t buf_len_; +}; + +} // namespace node + +#endif // SRC_NODE_STRING_H_ diff --git a/tools/js2c.py b/tools/js2c.py index 8ef644ee19..fd8b25b1a4 100755 --- a/tools/js2c.py +++ b/tools/js2c.py @@ -53,7 +53,7 @@ def ToCArray(filename, lines): value = ord(chr) - if value > 128: + if value >= 128: print 'non-ascii value ' + filename + ':' + str(row) + ':' + str(col) sys.exit(1); @@ -220,6 +220,7 @@ namespace node { struct _native { const char* name; const char* source; + size_t source_len; }; static const struct _native natives[] = { @@ -236,7 +237,7 @@ static const struct _native natives[] = { NATIVE_DECLARATION = """\ - { "%(id)s", %(id)s_native }, + { "%(id)s", %(id)s_native, sizeof(%(id)s_native)-1 }, """ SOURCE_DECLARATION = """\ diff --git a/wscript b/wscript index 5f242b7225..dc23707ba8 100644 --- a/wscript +++ b/wscript @@ -801,6 +801,7 @@ def build(bld): src/node_script.cc src/node_os.cc src/node_dtrace.cc + src/node_string.cc """ if sys.platform.startswith("win32"):