Created
September 3, 2008 08:29
-
-
Save ishikawa/8559 to your computer and use it in GitHub Desktop.
The simplest shell program for v8 javascript engine
This file contains 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 <v8.h> | |
using namespace v8; | |
int main(int argc, const char **argv) { | |
// Create a scope and environment | |
HandleScope scope; | |
Handle<Context> context = Context::New(); | |
Context::Scope context_scope(context); | |
// Reading a source code | |
std::stringbuf buffer; | |
std::cin.get(buffer, EOF); | |
Handle<String> source = String::New(buffer.str().c_str()); | |
// Register try/cache handler for error reporting | |
TryCatch try_catch; | |
// Compile the source code. | |
Handle<Script> script = Script::Compile(source); | |
if (script.IsEmpty()) { | |
// Print errors that happened during compilation. | |
String::AsciiValue error(try_catch.Exception()); | |
std::cout << *error << std::endl; | |
return -1; | |
} | |
// Running script | |
Handle<Value> result = script->Run(); | |
if (result.IsEmpty()) { | |
// Print errors that happened during execution. | |
String::AsciiValue error(try_catch.Exception()); | |
std::cout << *error << std::endl; | |
return -1; | |
} | |
// Convert the result to an ASCII string and print it. | |
String::AsciiValue ascii(result); | |
std::cout << *ascii << std::endl; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment