Skip to content

Commit

Permalink
Add string class that uses ExternalAsciiStringResource.
Browse files Browse the repository at this point in the history
Change the natives to use this class instead of creating completely new
strings. Reduces memory usage by about 1 MB.
  • Loading branch information
thughes authored and ry committed Mar 8, 2011
1 parent 81d3de7 commit 74954ce
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 13 deletions.
1 change: 1 addition & 0 deletions cmake/node_build.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
7 changes: 4 additions & 3 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <node_stdio.h>
#include <node_javascript.h>
#include <node_version.h>
#include <node_string.h>
#ifdef HAVE_OPENSSL
# include <node_crypto.h>
#endif
Expand Down Expand Up @@ -1269,7 +1270,7 @@ static void ReportException(TryCatch &try_catch, bool show_line) {
}

// 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;
TryCatch try_catch;

Expand Down Expand Up @@ -2036,8 +2037,8 @@ static void Load(int argc, char *argv[]) {

TryCatch try_catch;

Local<Value> f_value = ExecuteString(String::New(MainSource()),
String::New("node.js"));
Local<Value> f_value = ExecuteString(MainSource(),
IMMUTABLE_STRING("node.js"));
if (try_catch.HasCaught()) {
ReportException(try_catch, true);
exit(10);
Expand Down
11 changes: 4 additions & 7 deletions src/node_javascript.cc
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include <v8.h>
#include "node.h"
#include "node_natives.h"
#include "node_string.h"
#include <string.h>
#include <strings.h>

using namespace v8;

namespace node {

const char* MainSource() {
return node_native;
Handle<String> MainSource() {
return BUILTIN_ASCII_ARRAY(node_native, sizeof(node_native)-1);
}

void DefineJavaScript(v8::Handle<v8::Object> target) {
Expand All @@ -18,14 +19,10 @@ void DefineJavaScript(v8::Handle<v8::Object> target) {
for (int i = 0; natives[i].name; i++) {
if (natives[i].source != node_native) {
Local<String> name = String::New(natives[i].name);
// TODO: Use ExternalAsciiStringResource for source
// Might need to do some assertions in js2c about chars > 128
Local<String> source = String::New(natives[i].source);
Handle<String> source = BUILTIN_ASCII_ARRAY(natives[i].source, natives[i].source_len);
target->Set(name, source);
}
}
}



} // namespace node
2 changes: 1 addition & 1 deletion src/node_javascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
namespace node {

void DefineJavaScript(v8::Handle<v8::Object> target);
const char* MainSource();
v8::Handle<v8::String> MainSource();

} // namespace node
18 changes: 18 additions & 0 deletions src/node_string.cc
Original file line number Diff line number Diff line change
@@ -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 changes: 42 additions & 0 deletions src/node_string.h
Original file line number Diff line number Diff line change
@@ -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_
5 changes: 3 additions & 2 deletions tools/js2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -220,6 +220,7 @@ def ReadMacros(lines):
struct _native {
const char* name;
const char* source;
size_t source_len;
};
static const struct _native natives[] = {
Expand All @@ -236,7 +237,7 @@ def ReadMacros(lines):


NATIVE_DECLARATION = """\
{ "%(id)s", %(id)s_native },
{ "%(id)s", %(id)s_native, sizeof(%(id)s_native)-1 },
"""

SOURCE_DECLARATION = """\
Expand Down
1 change: 1 addition & 0 deletions wscript
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down

0 comments on commit 74954ce

Please sign in to comment.