Created
January 31, 2013 17:52
-
-
Save hadashiA/4684784 to your computer and use it in GitHub Desktop.
v8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sstream> | |
#include <string> | |
#include <v8.h> | |
using namespace std; | |
int main(int argc, const char **argv) { | |
// Create a scope and environment | |
v8::HandleScope scope; | |
v8::Handle<v8::Context> context = v8::Context::New(); | |
v8::Context::Scope context_scope(context); | |
// Reading a source code | |
stringbuf buffer; | |
cin.get(buffer, EOF); | |
v8::Handle<v8::String> source = v8::String::New(buffer.str().c_str()); | |
// Register try/cache handler for error reporting | |
v8::TryCatch try_catch; | |
// Compile the source code. | |
v8::Handle<v8::Script> script = v8::Script::Compile(source); | |
if (script.IsEmpty()) { | |
// Print errors that happened during compilation. | |
v8::String::AsciiValue error(try_catch.Exception()); | |
cout << *error << endl; | |
return -1; | |
} | |
// Running script | |
v8::Handle<v8::Value> result = script->Run(); | |
if (result.IsEmpty()) { | |
// Print errors that happened during execution. | |
v8::String::AsciiValue error(try_catch.Exception()); | |
cout << *error << endl; | |
return -1; | |
} | |
// Convert the result to an ASCII string and print it. | |
v8::String::AsciiValue ascii(result); | |
cout << *ascii << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment