Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.