mirror of
https://github.com/nodejs/node.git
synced 2025-05-07 23:52:40 +00:00
Add string class that uses ExternalAsciiStringResource.
Change the natives to use this class instead of creating completely new strings. Reduces memory usage by about 1 MB.
This commit is contained in:
parent
81d3de7e6d
commit
74954ce7d8
@ -62,6 +62,7 @@ set(node_sources
|
|||||||
src/node_script.cc
|
src/node_script.cc
|
||||||
src/node_os.cc
|
src/node_os.cc
|
||||||
src/node_dtrace.cc
|
src/node_dtrace.cc
|
||||||
|
src/node_string.cc
|
||||||
src/node_natives.h
|
src/node_natives.h
|
||||||
${node_extra_src})
|
${node_extra_src})
|
||||||
|
|
||||||
|
@ -48,6 +48,7 @@
|
|||||||
#include <node_stdio.h>
|
#include <node_stdio.h>
|
||||||
#include <node_javascript.h>
|
#include <node_javascript.h>
|
||||||
#include <node_version.h>
|
#include <node_version.h>
|
||||||
|
#include <node_string.h>
|
||||||
#ifdef HAVE_OPENSSL
|
#ifdef HAVE_OPENSSL
|
||||||
# include <node_crypto.h>
|
# include <node_crypto.h>
|
||||||
#endif
|
#endif
|
||||||
@ -1269,7 +1270,7 @@ static void ReportException(TryCatch &try_catch, bool show_line) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Executes a str within the current v8 context.
|
// Executes a str within the current v8 context.
|
||||||
Local<Value> ExecuteString(Local<String> source, Local<Value> filename) {
|
Local<Value> ExecuteString(Handle<String> source, Handle<Value> filename) {
|
||||||
HandleScope scope;
|
HandleScope scope;
|
||||||
TryCatch try_catch;
|
TryCatch try_catch;
|
||||||
|
|
||||||
@ -2036,8 +2037,8 @@ static void Load(int argc, char *argv[]) {
|
|||||||
|
|
||||||
TryCatch try_catch;
|
TryCatch try_catch;
|
||||||
|
|
||||||
Local<Value> f_value = ExecuteString(String::New(MainSource()),
|
Local<Value> f_value = ExecuteString(MainSource(),
|
||||||
String::New("node.js"));
|
IMMUTABLE_STRING("node.js"));
|
||||||
if (try_catch.HasCaught()) {
|
if (try_catch.HasCaught()) {
|
||||||
ReportException(try_catch, true);
|
ReportException(try_catch, true);
|
||||||
exit(10);
|
exit(10);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <v8.h>
|
#include <v8.h>
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
#include "node_natives.h"
|
#include "node_natives.h"
|
||||||
|
#include "node_string.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
|
|
||||||
@ -8,8 +9,8 @@ using namespace v8;
|
|||||||
|
|
||||||
namespace node {
|
namespace node {
|
||||||
|
|
||||||
const char* MainSource() {
|
Handle<String> MainSource() {
|
||||||
return node_native;
|
return BUILTIN_ASCII_ARRAY(node_native, sizeof(node_native)-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DefineJavaScript(v8::Handle<v8::Object> target) {
|
void DefineJavaScript(v8::Handle<v8::Object> target) {
|
||||||
@ -18,14 +19,10 @@ void DefineJavaScript(v8::Handle<v8::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::New(natives[i].name);
|
Local<String> name = String::New(natives[i].name);
|
||||||
// TODO: Use ExternalAsciiStringResource for source
|
Handle<String> source = BUILTIN_ASCII_ARRAY(natives[i].source, natives[i].source_len);
|
||||||
// Might need to do some assertions in js2c about chars > 128
|
|
||||||
Local<String> source = String::New(natives[i].source);
|
|
||||||
target->Set(name, source);
|
target->Set(name, source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace node
|
} // namespace node
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
namespace node {
|
namespace node {
|
||||||
|
|
||||||
void DefineJavaScript(v8::Handle<v8::Object> target);
|
void DefineJavaScript(v8::Handle<v8::Object> target);
|
||||||
const char* MainSource();
|
v8::Handle<v8::String> MainSource();
|
||||||
|
|
||||||
} // namespace node
|
} // namespace node
|
||||||
|
18
src/node_string.cc
Normal file
18
src/node_string.cc
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#include "node_string.h"
|
||||||
|
|
||||||
|
namespace node {
|
||||||
|
|
||||||
|
using namespace v8;
|
||||||
|
|
||||||
|
Handle<String> ImmutableAsciiSource::CreateFromLiteral(
|
||||||
|
const char *string_literal,
|
||||||
|
size_t length) {
|
||||||
|
HandleScope scope;
|
||||||
|
|
||||||
|
Local<String> ret = String::NewExternal(new ImmutableAsciiSource(
|
||||||
|
string_literal,
|
||||||
|
length));
|
||||||
|
return scope.Close(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
42
src/node_string.h
Normal file
42
src/node_string.h
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
#ifndef SRC_NODE_STRING_H_
|
||||||
|
#define SRC_NODE_STRING_H_
|
||||||
|
|
||||||
|
#include <v8.h>
|
||||||
|
|
||||||
|
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<v8::String> 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_
|
@ -53,7 +53,7 @@ def ToCArray(filename, lines):
|
|||||||
|
|
||||||
value = ord(chr)
|
value = ord(chr)
|
||||||
|
|
||||||
if value > 128:
|
if value >= 128:
|
||||||
print 'non-ascii value ' + filename + ':' + str(row) + ':' + str(col)
|
print 'non-ascii value ' + filename + ':' + str(row) + ':' + str(col)
|
||||||
sys.exit(1);
|
sys.exit(1);
|
||||||
|
|
||||||
@ -220,6 +220,7 @@ namespace node {
|
|||||||
struct _native {
|
struct _native {
|
||||||
const char* name;
|
const char* name;
|
||||||
const char* source;
|
const char* source;
|
||||||
|
size_t source_len;
|
||||||
};
|
};
|
||||||
|
|
||||||
static const struct _native natives[] = {
|
static const struct _native natives[] = {
|
||||||
@ -236,7 +237,7 @@ static const struct _native natives[] = {
|
|||||||
|
|
||||||
|
|
||||||
NATIVE_DECLARATION = """\
|
NATIVE_DECLARATION = """\
|
||||||
{ "%(id)s", %(id)s_native },
|
{ "%(id)s", %(id)s_native, sizeof(%(id)s_native)-1 },
|
||||||
"""
|
"""
|
||||||
|
|
||||||
SOURCE_DECLARATION = """\
|
SOURCE_DECLARATION = """\
|
||||||
|
Loading…
Reference in New Issue
Block a user