Refactored custom javascript engines to support primitive 'require' statements.
This commit is contained in:
parent
740da9c733
commit
4794fa1884
5 changed files with 203 additions and 131 deletions
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#include "js_shell.h"
|
||||
|
||||
typedef int (*V8ExtensionRegistrar) (v8::Handle<v8::Object>);
|
||||
typedef int (*V8ExtensionInitializer) (v8::Handle<v8::Object> module);
|
||||
|
||||
class V8Shell: public JSShell {
|
||||
|
||||
|
|
@ -25,36 +25,33 @@ public:
|
|||
|
||||
protected:
|
||||
|
||||
virtual bool RegisterModule(HANDLE library, const std::string& module_name);
|
||||
|
||||
virtual bool InitializeEngine();
|
||||
|
||||
virtual bool ExecuteScript(const std::string& source, const std::string& name);
|
||||
virtual bool ExecuteScript(const std::string& source, const std::string& scriptPath);
|
||||
|
||||
virtual bool DisposeEngine();
|
||||
|
||||
private:
|
||||
|
||||
v8::Handle<v8::Value> Import(const std::string& moduleName);
|
||||
|
||||
v8::Persistent<v8::Context> CreateShellContext();
|
||||
|
||||
void ReportException(v8::TryCatch* handler);
|
||||
|
||||
static v8::Handle<v8::Value> Print(const v8::Arguments& args);
|
||||
|
||||
static v8::Handle<v8::Value> Require(const v8::Arguments& args);
|
||||
|
||||
static v8::Handle<v8::Value> Quit(const v8::Arguments& args);
|
||||
|
||||
static v8::Handle<v8::Value> Version(const v8::Arguments& args);
|
||||
|
||||
static const char* ToCString(const v8::String::Utf8Value& value);
|
||||
|
||||
void ExtendEngine();
|
||||
|
||||
protected:
|
||||
|
||||
std::vector<V8ExtensionRegistrar> module_initializers;
|
||||
|
||||
v8::Persistent<v8::Context> context;
|
||||
|
||||
};
|
||||
|
||||
#ifdef __GNUC__
|
||||
|
|
@ -73,16 +70,6 @@ V8Shell::~V8Shell() {
|
|||
v8::V8::Dispose();
|
||||
}
|
||||
|
||||
bool V8Shell::RegisterModule(HANDLE library, const std::string& module_name) {
|
||||
std::string symname = std::string(module_name).append("_initialize");
|
||||
|
||||
V8ExtensionRegistrar init_function = reinterpret_cast<V8ExtensionRegistrar>((long) LOAD_SYMBOL(library, symname.c_str()));
|
||||
if(init_function == 0) return false;
|
||||
|
||||
module_initializers.push_back(init_function);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool V8Shell::RunScript(const std::string& scriptPath) {
|
||||
|
||||
if (!context.IsEmpty()) {
|
||||
|
|
@ -97,9 +84,16 @@ bool V8Shell::RunScript(const std::string& scriptPath) {
|
|||
return false;
|
||||
}
|
||||
context->Enter();
|
||||
//v8::Context::Scope context_scope(context);
|
||||
v8::HandleScope scope;
|
||||
|
||||
v8::Context::Scope context_scope(context);
|
||||
ExtendEngine();
|
||||
// Store a pointer to this shell for later use
|
||||
v8::Handle<v8::Object> global = context->Global();
|
||||
v8::Local<v8::External> __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;", "<console>");
|
||||
|
||||
if(!ExecuteScript(source, scriptPath)) {
|
||||
return false;
|
||||
|
|
@ -127,7 +121,8 @@ bool V8Shell::RunShell() {
|
|||
context->Enter();
|
||||
|
||||
v8::Context::Scope context_scope(context);
|
||||
ExtendEngine();
|
||||
|
||||
ExecuteScript("var console = {}; console.log = print;", "<console>");
|
||||
|
||||
static const int kBufferSize = 1024;
|
||||
while (true) {
|
||||
|
|
@ -152,45 +147,26 @@ bool V8Shell::InitializeEngine() {
|
|||
return true;
|
||||
}
|
||||
|
||||
void V8Shell::ExtendEngine() {
|
||||
|
||||
v8::HandleScope scope;
|
||||
v8::Local<v8::Object> global = context->Global();
|
||||
|
||||
// register extensions
|
||||
for(std::vector<V8ExtensionRegistrar>::iterator it=module_initializers.begin();
|
||||
it != module_initializers.end(); ++it) {
|
||||
(*it)(global);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool V8Shell::ExecuteScript(const std::string& source, const std::string& name) {
|
||||
v8::HandleScope handle_scope;
|
||||
v8::TryCatch try_catch;
|
||||
v8::Handle<v8::Script> 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<v8::Value> result = script->Run();
|
||||
|
||||
// Print errors that happened during execution.
|
||||
if (try_catch.HasCaught()) {
|
||||
ReportException(&try_catch);
|
||||
return false;
|
||||
} else {
|
||||
v8::Handle<v8::Value> result = script->Run();
|
||||
if (result.IsEmpty()) {
|
||||
assert(try_catch.HasCaught());
|
||||
// Print errors that happened during execution.
|
||||
ReportException(&try_catch);
|
||||
return false;
|
||||
} else {
|
||||
assert(!try_catch.HasCaught());
|
||||
if (!result->IsUndefined()) {
|
||||
// If all went well and the result wasn't undefined then print
|
||||
// the returned value.
|
||||
//v8::String::Utf8Value str(result);
|
||||
//const char* cstr = V8Shell::ToCString(str);
|
||||
//printf("%s\n", cstr);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -207,6 +183,7 @@ v8::Persistent<v8::Context> V8Shell::CreateShellContext() {
|
|||
// 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));
|
||||
|
||||
v8::Persistent<v8::Context> _context = v8::Context::New(NULL, global);
|
||||
|
|
@ -214,6 +191,26 @@ v8::Persistent<v8::Context> V8Shell::CreateShellContext() {
|
|||
return _context;
|
||||
}
|
||||
|
||||
v8::Handle<v8::Value> 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<V8ExtensionInitializer>((long) LOAD_SYMBOL(library, symname.c_str()));
|
||||
|
||||
if(init_function == 0) {
|
||||
printf("Could not find initializer function.");
|
||||
return v8::Undefined();
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> module = v8::Object::New();
|
||||
init_function(module);
|
||||
return scope.Close(module);
|
||||
}
|
||||
|
||||
v8::Handle<v8::Value> V8Shell::Print(const v8::Arguments& args) {
|
||||
bool first = true;
|
||||
|
|
@ -233,6 +230,27 @@ v8::Handle<v8::Value> V8Shell::Print(const v8::Arguments& args) {
|
|||
return v8::Undefined();
|
||||
}
|
||||
|
||||
v8::Handle<v8::Value> 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<v8::Object> global = v8::Context::GetCurrent()->Global();
|
||||
v8::Local<v8::Value> hidden = global->GetHiddenValue(v8::String::New("__shell__"));
|
||||
v8::Local<v8::External> __shell__ = v8::Local<v8::External>::Cast(hidden);
|
||||
V8Shell* _this = (V8Shell*) (long) __shell__->Value();
|
||||
|
||||
v8::Handle<v8::Value> module = _this->Import(moduleName);
|
||||
|
||||
return scope.Close(module);
|
||||
}
|
||||
|
||||
v8::Handle<v8::Value> V8Shell::Quit(const v8::Arguments& args) {
|
||||
int exit_code = args[0]->Int32Value();
|
||||
fflush(stdout);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue