Skip to content

Commit

Permalink
Use snapshot feature to load external scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustaf Sjöberg committed Feb 28, 2016
1 parent 07bdede commit 43e1476
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 24 deletions.
20 changes: 16 additions & 4 deletions c_src/erlang_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,24 @@ int main(int argc, char* argv[]) {
V8::Initialize();
V8::SetFlagsFromCommandLine(&argc, argv, true);

Isolate::CreateParams params;

char* source;
if (argc == 2) {
source = argv[1];
} else {
source = NULL;
}

StartupData snapshot = V8::CreateSnapshotDataBlob(source);

ArrayBufferAllocator allocator;
Isolate::CreateParams create_params;
create_params.array_buffer_allocator = &allocator;
Isolate* isolate = Isolate::New(create_params);
params.snapshot_blob = &snapshot;
params.array_buffer_allocator = &allocator;

Isolate* isolate = Isolate::New(params);

VM vm(platform, isolate, argc, argv);
VM vm(platform, isolate);
FTRACE("Initial VM: %p\n", &vm);
{
Isolate::Scope isolate_scope(isolate);
Expand Down
13 changes: 1 addition & 12 deletions c_src/vm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ void* TimeoutHandler(void *arg) {
return NULL;
}

VM::VM(Platform* platform_, Isolate* isolate_, int scriptc, char* scriptv[]) {
VM::VM(Platform* platform_, Isolate* isolate_) {
isolate = isolate_;
platform = platform_;

scripts = vector<string>(scriptv + 1, scriptv + scriptc);
}

bool VM::CreateContext(uint32_t ref) {
Expand All @@ -53,15 +51,6 @@ bool VM::CreateContext(uint32_t ref) {

Context::Scope context_scope(context);

// Initializing scripts for every new context. This is a
// temporary solution.
for (auto script : scripts) {
Local<String> source = String::NewFromUtf8(isolate,
script.c_str());
Local<Script> compiled = Script::Compile(source);
Local<Value> result = compiled->Run();
}

contexts[ref] = pcontext;
return true;
}
Expand Down
5 changes: 1 addition & 4 deletions c_src/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ class VM {
v8::Platform* platform;

std::map<uint32_t, v8::Persistent<v8::Context, v8::CopyablePersistentTraits<v8::Context>>> contexts;
std::vector<std::string> scripts;
v8::Persistent<v8::Context> lol;

public:
VM(v8::Platform* platform_, v8::Isolate* isolate_, int scriptc,
char* scriptv[]);
VM(v8::Platform* platform_, v8::Isolate* isolate_);

v8::Isolate* GetIsolate();

Expand Down
8 changes: 4 additions & 4 deletions src/erlang_v8_vm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ kill_port(#state{monitor_pid = Pid, port = Port} = State) ->
%% @doc Start port and port monitor.
start_port(#state{initial_source = Source} = State) ->
Executable = filename:join(priv_dir(), ?EXECUTABLE),
Opts = [{args, Source}|?SPAWN_OPTS],
Opts = [{args, [Source]}|?SPAWN_OPTS],
Port = open_port({spawn_executable, Executable}, Opts),
monitor_port(State#state{port = Port}).

Expand Down Expand Up @@ -240,18 +240,18 @@ priv_dir() ->

%% @doc Parse proplists/opts and populate a state record.
parse_opts(Opts) ->
lists:foldl(fun parse_opt/2, #state{initial_source = []}, Opts).
lists:foldl(fun parse_opt/2, #state{initial_source = <<>>}, Opts).

%% @doc Append source specified in source option.
parse_opt({source, S}, #state{initial_source = InitialSource} = State) ->
State#state{initial_source = [S|InitialSource]};
State#state{initial_source = <<InitialSource/binary, S/binary>>};

%% @doc Read contents of file option and append to state.
parse_opt({file, F}, #state{initial_source = InitialSource} = State) ->
%% Files should probably be read in the OS process instead to prevent
%% keeping multiple copies of the JS source code in memory.
{ok, S} = file:read_file(F),
State#state{initial_source = [S|InitialSource]};
State#state{initial_source = <<InitialSource/binary, S/binary>>};

%% @doc Invalid max source size
parse_opt({max_source_size, N}, _State) when N > ?MAX_SOURCE_SIZE ->
Expand Down

0 comments on commit 43e1476

Please sign in to comment.