Simplify creation of a Javascript shell.

This commit is contained in:
Oliver Buchtala 2012-11-17 02:12:12 +01:00
commit 058a27bf32
4 changed files with 53 additions and 70 deletions

View file

@ -7,14 +7,6 @@
#include "js_shell.h" #include "js_shell.h"
#ifdef USE_JSC
extern JSShell* create_jsc_shell();
#endif
#ifdef USE_V8
extern JSShell* create_v8_shell();
#endif
void print_usage() { void print_usage() {
std::cout << "javascript [-i] [-jsc|-v8] [-l module] <js-file>" << std::endl; std::cout << "javascript [-i] [-jsc|-v8] [-l module] <js-file>" << std::endl;
} }
@ -37,16 +29,12 @@ int main(int argc, char* argv[]) {
std::string module_name(argv[idx]); std::string module_name(argv[idx]);
module_names.push_back(module_name); module_names.push_back(module_name);
} else if(strcmp(argv[idx], "-v8") == 0) { } else if(strcmp(argv[idx], "-v8") == 0) {
#ifdef USE_V8 #ifndef USE_V8
shell = create_v8_shell();
#else
std::cerr << "V8 support is not enabled" << std::endl; std::cerr << "V8 support is not enabled" << std::endl;
exit(-1); exit(-1);
#endif #endif
} else if(strcmp(argv[idx], "-jsc") == 0) { } else if(strcmp(argv[idx], "-jsc") == 0) {
#ifdef USE_JSC #ifndef USE_JSC
shell = create_jsc_shell();
#else
std::cerr << "JSC support is not enabled" << std::endl; std::cerr << "JSC support is not enabled" << std::endl;
exit(-1); exit(-1);
#endif #endif
@ -56,16 +44,9 @@ int main(int argc, char* argv[]) {
scriptPath = argv[idx]; scriptPath = argv[idx];
} }
} }
if (shell == 0) { shell = JSShell::Create();
#ifdef USE_JSC
shell = create_jsc_shell();
#else
std::cerr << "JSC support is not enabled" << std::endl;
exit(-1);
#endif
}
bool failed = false; bool failed = false;
for(std::vector<std::string>::iterator it = module_names.begin(); for(std::vector<std::string>::iterator it = module_names.begin();
it != module_names.end(); ++it) { it != module_names.end(); ++it) {
@ -94,6 +75,6 @@ int main(int argc, char* argv[]) {
} }
delete shell; delete shell;
return 0; return 0;
} }

View file

@ -18,10 +18,12 @@ public:
JSShell() {} JSShell() {}
virtual ~JSShell(); virtual ~JSShell() = 0;
static JSShell* Create();
bool ImportModule(const std::string& name); bool ImportModule(const std::string& name);
virtual bool RunScript(const std::string& scriptPath); virtual bool RunScript(const std::string& scriptPath);
virtual bool RunShell(); virtual bool RunShell();
@ -31,7 +33,7 @@ protected:
virtual bool RegisterModule(HANDLE library, const std::string& module_name) = 0; virtual bool RegisterModule(HANDLE library, const std::string& module_name) = 0;
virtual bool InitializeEngine() = 0; virtual bool InitializeEngine() = 0;
virtual bool ExecuteScript(const std::string& source, const std::string& name) = 0; virtual bool ExecuteScript(const std::string& source, const std::string& name) = 0;
virtual bool DisposeEngine() = 0; virtual bool DisposeEngine() = 0;
@ -39,7 +41,7 @@ protected:
static std::string ReadFile(const std::string& fileName); static std::string ReadFile(const std::string& fileName);
protected: protected:
std::vector<HANDLE> loaded_modules; std::vector<HANDLE> loaded_modules;
}; };

View file

@ -19,7 +19,7 @@ typedef int (*JSCIntializer)(JSGlobalContextRef context);
public: public:
JSCShell() {}; JSCShell() {};
virtual ~JSCShell(); virtual ~JSCShell();
protected: protected:
@ -27,14 +27,14 @@ protected:
virtual bool RegisterModule(HANDLE library, const std::string& module_name); virtual bool RegisterModule(HANDLE library, const std::string& module_name);
virtual bool InitializeEngine(); virtual bool InitializeEngine();
virtual bool ExecuteScript(const std::string& source, const std::string& name); virtual bool ExecuteScript(const std::string& source, const std::string& name);
virtual bool DisposeEngine(); virtual bool DisposeEngine();
private: private:
static JSValueRef Print(JSContextRef context,JSObjectRef object, JSObjectRef globalobj, size_t argc, const JSValueRef args[], JSValueRef* ex); static JSValueRef Print(JSContextRef context,JSObjectRef object, JSObjectRef globalobj, size_t argc, const JSValueRef args[], JSValueRef* ex);
static bool RegisterFunction(JSGlobalContextRef context, JSObjectRef object, const char* functionName, JSObjectCallAsFunctionCallback cbFunction); static bool RegisterFunction(JSGlobalContextRef context, JSObjectRef object, const char* functionName, JSObjectCallAsFunctionCallback cbFunction);
@ -43,7 +43,7 @@ private:
private: private:
std::vector<JSCIntializer> module_initializers; std::vector<JSCIntializer> module_initializers;
JSGlobalContextRef context; JSGlobalContextRef context;
}; };
@ -56,7 +56,7 @@ JSCShell::~JSCShell() {
bool JSCShell::RegisterModule(HANDLE library, const std::string& module_name) { bool JSCShell::RegisterModule(HANDLE library, const std::string& module_name) {
std::string symname = std::string(module_name).append("_initialize"); std::string symname = std::string(module_name).append("_initialize");
JSCIntializer init_function = reinterpret_cast<JSCIntializer>((long) LOAD_SYMBOL(library, symname.c_str())); JSCIntializer init_function = reinterpret_cast<JSCIntializer>((long) LOAD_SYMBOL(library, symname.c_str()));
if(init_function == 0) return false; if(init_function == 0) return false;
@ -64,7 +64,7 @@ bool JSCShell::RegisterModule(HANDLE library, const std::string& module_name) {
return true; return true;
} }
bool JSCShell::InitializeEngine() { bool JSCShell::InitializeEngine() {
if(context != 0) { if(context != 0) {
JSGlobalContextRelease(context); JSGlobalContextRelease(context);
context = 0; context = 0;
@ -81,7 +81,7 @@ bool JSCShell::InitializeEngine() {
if(!init_function(context)) { if(!init_function(context)) {
return false; return false;
} }
} }
return true; return true;
} }
@ -104,9 +104,9 @@ bool JSCShell::DisposeEngine() {
return true; return true;
} }
JSValueRef JSCShell::Print(JSContextRef context, JSObjectRef object, JSValueRef JSCShell::Print(JSContextRef context, JSObjectRef object,
JSObjectRef globalobj, size_t argc, JSObjectRef globalobj, size_t argc,
const JSValueRef args[], JSValueRef* ex) { const JSValueRef args[], JSValueRef* ex) {
if (argc > 0) if (argc > 0)
{ {
JSStringRef string = JSValueToStringCopy(context, args[0], NULL); JSStringRef string = JSValueToStringCopy(context, args[0], NULL);
@ -114,18 +114,18 @@ JSValueRef JSCShell::Print(JSContextRef context, JSObjectRef object,
char *stringUTF8 = new char[numChars]; char *stringUTF8 = new char[numChars];
JSStringGetUTF8CString(string, stringUTF8, numChars); JSStringGetUTF8CString(string, stringUTF8, numChars);
printf("%s\n", stringUTF8); printf("%s\n", stringUTF8);
delete[] stringUTF8; delete[] stringUTF8;
} }
return JSValueMakeUndefined(context); return JSValueMakeUndefined(context);
} }
bool JSCShell::RegisterFunction(JSGlobalContextRef context, JSObjectRef object, bool JSCShell::RegisterFunction(JSGlobalContextRef context, JSObjectRef object,
const char* functionName, JSObjectCallAsFunctionCallback callback) { const char* functionName, JSObjectCallAsFunctionCallback callback) {
JSStringRef js_functionName = JSStringCreateWithUTF8CString(functionName); JSStringRef js_functionName = JSStringCreateWithUTF8CString(functionName);
JSObjectSetProperty(context, object, js_functionName, JSObjectSetProperty(context, object, js_functionName,
JSObjectMakeFunctionWithCallback(context, js_functionName, callback), JSObjectMakeFunctionWithCallback(context, js_functionName, callback),
kJSPropertyAttributeNone, NULL); kJSPropertyAttributeNone, NULL);
JSStringRelease(js_functionName); JSStringRelease(js_functionName);
return true; return true;
@ -133,10 +133,10 @@ bool JSCShell::RegisterFunction(JSGlobalContextRef context, JSObjectRef object,
void JSCShell::PrintError(JSContextRef ctx, JSValueRef err, const std::string& name) { void JSCShell::PrintError(JSContextRef ctx, JSValueRef err, const std::string& name) {
char *buffer; char *buffer;
JSStringRef string = JSValueToStringCopy(ctx, err, 0); JSStringRef string = JSValueToStringCopy(ctx, err, 0);
size_t length = JSStringGetLength(string); size_t length = JSStringGetLength(string);
buffer = new char[length+1]; buffer = new char[length+1];
JSStringGetUTF8CString(string, buffer, length+1); JSStringGetUTF8CString(string, buffer, length+1);
std::string errMsg(buffer); std::string errMsg(buffer);
@ -144,7 +144,7 @@ void JSCShell::PrintError(JSContextRef ctx, JSValueRef err, const std::string& n
delete[] buffer; delete[] buffer;
JSObjectRef errObj = JSValueToObject(ctx, err, 0); JSObjectRef errObj = JSValueToObject(ctx, err, 0);
if(errObj == 0) { if(errObj == 0) {
std::cerr << errMsg << std::endl; std::cerr << errMsg << std::endl;
return; return;
@ -154,12 +154,12 @@ void JSCShell::PrintError(JSContextRef ctx, JSValueRef err, const std::string& n
// though, it happened that this was always "" // though, it happened that this was always ""
JSStringRef lineKey = JSStringCreateWithUTF8CString("line"); JSStringRef lineKey = JSStringCreateWithUTF8CString("line");
JSValueRef jsLine = JSObjectGetProperty(ctx, errObj, lineKey, 0); JSValueRef jsLine = JSObjectGetProperty(ctx, errObj, lineKey, 0);
int line = (int) JSValueToNumber(ctx, jsLine, 0); int line = (int) JSValueToNumber(ctx, jsLine, 0);
JSStringRelease(lineKey); JSStringRelease(lineKey);
std::cerr << name << ":" << line << ":" << errMsg << std::endl; std::cerr << name << ":" << line << ":" << errMsg << std::endl;
} }
JSShell* create_jsc_shell() { JSShell* JSShell::Create() {
return new JSCShell(); return new JSCShell();
} }

View file

@ -15,9 +15,9 @@ class V8Shell: public JSShell {
public: public:
V8Shell(); V8Shell();
virtual ~V8Shell(); virtual ~V8Shell();
virtual bool RunScript(const std::string& scriptPath); virtual bool RunScript(const std::string& scriptPath);
virtual bool RunShell(); virtual bool RunShell();
@ -28,11 +28,11 @@ protected:
virtual bool RegisterModule(HANDLE library, const std::string& module_name); virtual bool RegisterModule(HANDLE library, const std::string& module_name);
virtual bool InitializeEngine(); virtual bool InitializeEngine();
virtual bool ExecuteScript(const std::string& source, const std::string& name); virtual bool ExecuteScript(const std::string& source, const std::string& name);
virtual bool DisposeEngine(); virtual bool DisposeEngine();
private: private:
v8::Persistent<v8::Context> CreateShellContext(); v8::Persistent<v8::Context> CreateShellContext();
@ -46,13 +46,13 @@ private:
static v8::Handle<v8::Value> Version(const v8::Arguments& args); static v8::Handle<v8::Value> Version(const v8::Arguments& args);
static const char* ToCString(const v8::String::Utf8Value& value); static const char* ToCString(const v8::String::Utf8Value& value);
void ExtendEngine(); void ExtendEngine();
protected: protected:
std::vector<V8ExtensionRegistrar> module_initializers; std::vector<V8ExtensionRegistrar> module_initializers;
v8::Persistent<v8::Context> context; v8::Persistent<v8::Context> context;
}; };
@ -75,7 +75,7 @@ V8Shell::~V8Shell() {
bool V8Shell::RegisterModule(HANDLE library, const std::string& module_name) { bool V8Shell::RegisterModule(HANDLE library, const std::string& module_name) {
std::string symname = std::string(module_name).append("_initialize"); std::string symname = std::string(module_name).append("_initialize");
V8ExtensionRegistrar init_function = reinterpret_cast<V8ExtensionRegistrar>((long) LOAD_SYMBOL(library, symname.c_str())); V8ExtensionRegistrar init_function = reinterpret_cast<V8ExtensionRegistrar>((long) LOAD_SYMBOL(library, symname.c_str()));
if(init_function == 0) return false; if(init_function == 0) return false;
@ -88,10 +88,10 @@ bool V8Shell::RunScript(const std::string& scriptPath) {
if (!context.IsEmpty()) { if (!context.IsEmpty()) {
context.Dispose(); context.Dispose();
} }
std::string source = ReadFile(scriptPath); std::string source = ReadFile(scriptPath);
context = CreateShellContext(); context = CreateShellContext();
if (context.IsEmpty()) { if (context.IsEmpty()) {
printf("Could not create context.\n"); printf("Could not create context.\n");
return false; return false;
@ -104,11 +104,11 @@ bool V8Shell::RunScript(const std::string& scriptPath) {
if(!ExecuteScript(source, scriptPath)) { if(!ExecuteScript(source, scriptPath)) {
return false; return false;
} }
context->Exit(); context->Exit();
context.Dispose(); context.Dispose();
v8::V8::Dispose(); v8::V8::Dispose();
return true; return true;
} }
@ -117,18 +117,18 @@ bool V8Shell::RunShell() {
if (!context.IsEmpty()) { if (!context.IsEmpty()) {
context.Dispose(); context.Dispose();
} }
context = CreateShellContext(); context = CreateShellContext();
if (context.IsEmpty()) { if (context.IsEmpty()) {
printf("Could not create context.\n"); printf("Could not create context.\n");
return false; return false;
} }
context->Enter(); context->Enter();
v8::Context::Scope context_scope(context); v8::Context::Scope context_scope(context);
ExtendEngine(); ExtendEngine();
static const int kBufferSize = 1024; static const int kBufferSize = 1024;
while (true) { while (true) {
char buffer[kBufferSize]; char buffer[kBufferSize];
@ -152,13 +152,13 @@ bool V8Shell::InitializeEngine() {
} }
void V8Shell::ExtendEngine() { void V8Shell::ExtendEngine() {
// register extensions // register extensions
for(std::vector<V8ExtensionRegistrar>::iterator it=module_initializers.begin(); for(std::vector<V8ExtensionRegistrar>::iterator it=module_initializers.begin();
it != module_initializers.end(); ++it) { it != module_initializers.end(); ++it) {
(*it)(context); (*it)(context);
} }
} }
bool V8Shell::ExecuteScript(const std::string& source, const std::string& name) { bool V8Shell::ExecuteScript(const std::string& source, const std::string& name) {
@ -195,7 +195,7 @@ bool V8Shell::DisposeEngine() {
v8::Persistent<v8::Context> V8Shell::CreateShellContext() { v8::Persistent<v8::Context> V8Shell::CreateShellContext() {
v8::HandleScope scope; v8::HandleScope scope;
// Create a template for the global object. // Create a template for the global object.
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
@ -205,7 +205,7 @@ v8::Persistent<v8::Context> V8Shell::CreateShellContext() {
global->Set(v8::String::New("version"), v8::FunctionTemplate::New(V8Shell::Version)); global->Set(v8::String::New("version"), v8::FunctionTemplate::New(V8Shell::Version));
v8::Persistent<v8::Context> _context = v8::Context::New(NULL, global); v8::Persistent<v8::Context> _context = v8::Context::New(NULL, global);
return _context; return _context;
} }
@ -282,6 +282,6 @@ const char* V8Shell::ToCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>"; return *value ? *value : "<string conversion failed>";
} }
JSShell* create_v8_shell() { JSShell* JSShell::Create() {
return new V8Shell(); return new V8Shell();
} }