#include #include #include #include #include #include #include #include "js_shell.h" typedef int (*V8ExtensionInitializer) (v8::Handle module); class V8Shell: public JSShell { public: V8Shell(); virtual ~V8Shell(); virtual bool RunScript(const std::string& scriptPath); virtual bool RunShell(); protected: virtual bool InitializeEngine(); virtual bool ExecuteScript(const std::string& source, const std::string& scriptPath); virtual bool DisposeEngine(); private: v8::Handle Import(const std::string& moduleName); #if (V8_VERSION < 0x031900) v8::Persistent CreateShellContext(); #else v8::Local CreateShellContext(); #endif void ReportException(v8::TryCatch* handler); static v8::Handle Print(const v8::Arguments& args); static v8::Handle Require(const v8::Arguments& args); static v8::Handle Quit(const v8::Arguments& args); static v8::Handle Version(const v8::Arguments& args); static const char* ToCString(const v8::String::Utf8Value& value); }; #ifdef __GNUC__ #include #define LOAD_SYMBOL(handle, name) dlsym(handle, name) #else #error "implement dll loading" #endif V8Shell::V8Shell(){} V8Shell::~V8Shell() {} bool V8Shell::RunScript(const std::string& scriptPath) { std::string source = ReadFile(scriptPath); v8::HandleScope scope; #if (V8_VERSION < 0x031900) v8::Persistent context = CreateShellContext(); #else v8::Local context = CreateShellContext(); #endif if (context.IsEmpty()) { printf("Could not create context.\n"); return false; } context->Enter(); // Store a pointer to this shell for later use v8::Handle global = context->Global(); v8::Local __shell__ = v8::External::New((void*) (long) this); global->SetHiddenValue(v8::String::New("__shell__"), __shell__); // Node.js compatibility: make `print` available as `console.log()` ExecuteScript("var console = {}; console.log = print;", ""); bool success = ExecuteScript(source, scriptPath); // Cleanup context->Exit(); #if (V8_VERSION < 0x031710) context.Dispose(); #elif (V8_VERSION < 0x031900) context.Dispose(v8::Isolate::GetCurrent()); #else // context.Dispose(); #endif // v8::V8::Dispose(); return success; } bool V8Shell::RunShell() { v8::HandleScope scope; #if (V8_VERSION < 0x031900) v8::Persistent context = CreateShellContext(); #else v8::Local context = CreateShellContext(); #endif if (context.IsEmpty()) { printf("Could not create context.\n"); return false; } context->Enter(); v8::Context::Scope context_scope(context); ExecuteScript("var console = {}; console.log = print;", ""); static const int kBufferSize = 1024; while (true) { char buffer[kBufferSize]; printf("> "); char* str = fgets(buffer, kBufferSize, stdin); if (str == NULL) break; std::string source(str); ExecuteScript(source, "(shell)"); } printf("\n"); // Cleanup context->Exit(); #if (V8_VERSION < 0x031710) context.Dispose(); #elif (V8_VERSION < 0x031900) context.Dispose(v8::Isolate::GetCurrent()); #else // context.Dispose(); #endif // v8::V8::Dispose(); return true; } bool V8Shell::InitializeEngine() { return true; } bool V8Shell::ExecuteScript(const std::string& source, const std::string& name) { v8::HandleScope handle_scope; v8::TryCatch try_catch; v8::Handle script = v8::Script::Compile(v8::String::New(source.c_str()), v8::String::New(name.c_str())); // Stop if script is empty if (script.IsEmpty()) { // Print errors that happened during compilation. ReportException(&try_catch); return false; } v8::Handle result = script->Run(); // Print errors that happened during execution. if (try_catch.HasCaught()) { ReportException(&try_catch); return false; } else { return true; } } bool V8Shell::DisposeEngine() { return true; } #if (V8_VERSION < 0x031900) v8::Persistent V8Shell::CreateShellContext() { #else v8::Local V8Shell::CreateShellContext() { #endif v8::HandleScope scope; // Create a template for the global object. v8::Handle global = v8::ObjectTemplate::New(); // Bind global functions global->Set(v8::String::New("print"), v8::FunctionTemplate::New(V8Shell::Print)); global->Set(v8::String::New("quit"), v8::FunctionTemplate::New(V8Shell::Quit)); global->Set(v8::String::New("require"), v8::FunctionTemplate::New(V8Shell::Require)); global->Set(v8::String::New("version"), v8::FunctionTemplate::New(V8Shell::Version)); #if (V8_VERSION < 0x031900) v8::Persistent context = v8::Context::New(NULL, global); return context; #else // v8::Local context_ = v8::Context::New(v8::Isolate::GetCurrent(), NULL, global); // v8::Persistent context; // context.Reset(v8::Isolate::GetCurrent(), context_); v8::Local context = v8::Context::New(v8::Isolate::GetCurrent(), NULL, global); return scope.Close(context); #endif } v8::Handle V8Shell::Import(const std::string& module_path) { v8::HandleScope scope; HANDLE library; std::string module_name = LoadModule(module_path, &library); std::string symname = std::string(module_name).append("_initialize"); V8ExtensionInitializer init_function = reinterpret_cast((long) LOAD_SYMBOL(library, symname.c_str())); if(init_function == 0) { printf("Could not find initializer function."); return v8::Undefined(); } v8::Local module = v8::Object::New(); init_function(module); return scope.Close(module); } v8::Handle V8Shell::Print(const v8::Arguments& args) { bool first = true; for (int i = 0; i < args.Length(); i++) { v8::HandleScope handle_scope; if (first) { first = false; } else { printf(" "); } v8::String::Utf8Value str(args[i]); const char* cstr = V8Shell::ToCString(str); printf("%s", cstr); } printf("\n"); fflush(stdout); return v8::Undefined(); } v8::Handle V8Shell::Require(const v8::Arguments& args) { v8::HandleScope scope; if (args.Length() != 1) { printf("Illegal arguments for `require`"); }; v8::String::Utf8Value str(args[0]); const char* cstr = V8Shell::ToCString(str); std::string moduleName(cstr); v8::Local global = v8::Context::GetCurrent()->Global(); v8::Local hidden = global->GetHiddenValue(v8::String::New("__shell__")); v8::Local __shell__ = v8::Local::Cast(hidden); V8Shell* _this = (V8Shell*) (long) __shell__->Value(); v8::Handle module = _this->Import(moduleName); return scope.Close(module); } v8::Handle V8Shell::Quit(const v8::Arguments& args) { int exit_code = args[0]->Int32Value(); fflush(stdout); fflush(stderr); exit(exit_code); return v8::Undefined(); } v8::Handle V8Shell::Version(const v8::Arguments& args) { return v8::String::New(v8::V8::GetVersion()); } void V8Shell::ReportException(v8::TryCatch* try_catch) { v8::HandleScope handle_scope; v8::String::Utf8Value exception(try_catch->Exception()); const char* exception_string = V8Shell::ToCString(exception); v8::Handle message = try_catch->Message(); if (message.IsEmpty()) { // V8 didn't provide any extra information about this error; just // print the exception. printf("%s\n", exception_string); } else { // Print (filename):(line number): (message). v8::String::Utf8Value filename(message->GetScriptResourceName()); const char* filename_string = V8Shell::ToCString(filename); int linenum = message->GetLineNumber(); printf("%s:%i: %s\n", filename_string, linenum, exception_string); // Print line of source code. v8::String::Utf8Value sourceline(message->GetSourceLine()); const char* sourceline_string = V8Shell::ToCString(sourceline); printf("%s\n", sourceline_string); // Print wavy underline (GetUnderline is deprecated). int start = message->GetStartColumn(); for (int i = 0; i < start; i++) { printf(" "); } int end = message->GetEndColumn(); for (int i = start; i < end; i++) { printf("^"); } printf("\n"); v8::String::Utf8Value stack_trace(try_catch->StackTrace()); if (stack_trace.length() > 0) { const char* stack_trace_string = V8Shell::ToCString(stack_trace); printf("%s\n", stack_trace_string); } } } // Extracts a C string from a V8 Utf8Value. const char* V8Shell::ToCString(const v8::String::Utf8Value& value) { return *value ? *value : ""; } JSShell* V8Shell_Create() { return new V8Shell(); }