Skip to content

Instantly share code, notes, and snippets.

@ghaiklor
Last active February 1, 2022 13:25
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ghaiklor/8000ff81a90d8b96dd6e to your computer and use it in GitHub Desktop.
Save ghaiklor/8000ff81a90d8b96dd6e to your computer and use it in GitHub Desktop.
Simple example how V8 can compile JavaScript source and run it
// Create a new context.
Local<Context> context = Context::New(isolate);
// Enter the context for compiling and running the hello world script.
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Local<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
// Compile the source code.
Local<Script> script = Script::Compile(source);
// Run the script to get the result.
Local<Value> result = script->Run();
// Convert the result to an UTF8 string and print it.
String::Utf8Value utf8(result);
printf("%s\n", *utf8);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment