annotate V8_tests/Hello_World.cpp @ 13:77d9e57697b1

y
author Sara
date Sat, 15 Feb 2014 12:11:09 -0800
parents
children
rev   line source
seanhalle@6 1 #include <v8.h>
seanhalle@6 2
seanhalle@6 3 using namespace v8;
seanhalle@6 4
seanhalle@6 5 int main(int argc, char* argv[]) {
seanhalle@6 6 // Get the default Isolate created at startup.
seanhalle@6 7 Isolate* isolate = Isolate::GetCurrent();
seanhalle@6 8
seanhalle@6 9 // Create a stack-allocated handle scope.
seanhalle@6 10 HandleScope handle_scope(isolate);
seanhalle@6 11
seanhalle@6 12 // Create a new context.
seanhalle@6 13 Handle<Context> context = Context::New(isolate);
seanhalle@6 14
seanhalle@6 15 // Enter the context for compiling and running the hello world script.
seanhalle@6 16 Context::Scope context_scope(context);
seanhalle@6 17
seanhalle@6 18 // Create a string containing the JavaScript source code.
seanhalle@6 19 Handle<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
seanhalle@6 20
seanhalle@6 21 // Compile the source code.
seanhalle@6 22 Handle<Script> script = Script::Compile(source);
seanhalle@6 23
seanhalle@6 24 // Run the script to get the result.
seanhalle@6 25 Handle<Value> result = script->Run();
seanhalle@6 26
seanhalle@6 27 // Convert the result to an UTF8 string and print it.
seanhalle@6 28 String::Utf8Value utf8(result);
seanhalle@6 29 printf("%s\n", *utf8);
seanhalle@6 30 return 0;
seanhalle@6 31 }