Skip to content

Instantly share code, notes, and snippets.

@ishikawa
Created September 3, 2008 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ishikawa/8559 to your computer and use it in GitHub Desktop.
Save ishikawa/8559 to your computer and use it in GitHub Desktop.
The simplest shell program for v8 javascript engine
#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