Skip to content

Instantly share code, notes, and snippets.

@goldsborough
Created October 21, 2015 22:16
Show Gist options
  • Save goldsborough/1b538c08dbb1ee74a908 to your computer and use it in GitHub Desktop.
Save goldsborough/1b538c08dbb1ee74a908 to your computer and use it in GitHub Desktop.
Include a JavaScript file in Google V8.
void include(const v8::FunctionCallbackInfo<v8::Value>& file)
{
auto isolate = file.GetIsolate();
if (file.Length() != 1)
{
auto message = v8::String::NewFromUtf8(isolate,
"Invalid number "
"of arguments!");
v8::Exception::SyntaxError(message);
}
std::ifstream stream(*v8::String::Utf8Value(file[0]));
std::string source;
std::string input;
while (std::getline(stream, input))
{
source += input + "\n";
}
auto string = v8::String::NewFromUtf8(isolate, source.c_str());
auto script = v8::Script::Compile(string);
script->Run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment