Skip to content

Instantly share code, notes, and snippets.

@jcla1
Created January 7, 2012 14:47
Show Gist options
  • Save jcla1/1574928 to your computer and use it in GitHub Desktop.
Save jcla1/1574928 to your computer and use it in GitHub Desktop.
Exploring the V8 JS engine (Part 1 of 2)
#include <v8.h>
using namespace v8;
int main(int argc, char* argv[]) {
// Create a new context.
Persistent<Context> context = Context::New();
// Enter the created context for compiling and
// running the hello world program.
context->Enter();
// Create a stack-allocated handle scope.
HandleScope handle_scope;
// Create a string containing the JavaScript code
// to execute (notice the quotation).
Handle<String> source = String::New(" 'Hello World!'; ");
// Compile the Javascript code.
Handle<Script> script = Script::Compile(source);
// Run the script to get the result.
Handle<Value> result = script->Run();
// Get rid of the persistent context.
context.Dispose();
// Convert the result to an ASCII string and print it.
String::AsciiValue ascii(result);
printf("%s\n", *ascii);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment