Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change script source type and compiler api to improve code. #1

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions src/nwjc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,24 @@ int main(int argc, char** argv) {
}
fclose(file);
Local<String> source_str = String::NewFromUtf8(isolate, chars);
Local<String> filename = String::NewFromUtf8(isolate, argv[1]);
TryCatch try_catch;

i::Isolate* iso = reinterpret_cast<i::Isolate*>(isolate);
i::Handle<i::String> orig_source = iso->factory()
->NewStringFromUtf8(i::CStrVector(chars)).ToHandleChecked();

i::ScriptData* cache = NULL;
i::Handle<i::SharedFunctionInfo> orig = i::Compiler::CompileScript(
orig_source, i::Handle<i::String>(), 0, 0, false,
i::Handle<i::Context>(iso->native_context()), NULL, &cache,
v8::ScriptCompiler::kProduceCodeCache, i::NOT_NATIVES_CODE);

TryCatch try_catch;
ScriptCompiler::Source script_source(source_str, ScriptOrigin(v8::Undefined(isolate)));
ScriptCompiler::CompileUnbound(isolate, &script_source,
v8::ScriptCompiler::kProduceCodeCache);
if (try_catch.HasCaught()) {
fprintf(stderr, "Failure compiling '%s' (see above)\n", argv[1]);
exit(1);
}
uint8_t* buffer = i::NewArray<uint8_t>(cache->length());
i::MemCopy(buffer, cache->data(), cache->length());

SnapshotWriter writer(argv[2]);
writer.WriteSnapshot(buffer, cache->length());
if (script_source.GetCachedData()) {
int length = script_source.GetCachedData()->length;
uint8_t* cache = new uint8_t[length];
memcpy(cache, script_source.GetCachedData()->data, length);

SnapshotWriter writer(argv[2]);
writer.WriteSnapshot(cache, length);
}
}

V8::Dispose();
Expand Down