Simplify creation of a Javascript shell.
This commit is contained in:
parent
57980975a0
commit
058a27bf32
4 changed files with 53 additions and 70 deletions
|
|
@ -7,14 +7,6 @@
|
|||
|
||||
#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() {
|
||||
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]);
|
||||
module_names.push_back(module_name);
|
||||
} else if(strcmp(argv[idx], "-v8") == 0) {
|
||||
#ifdef USE_V8
|
||||
shell = create_v8_shell();
|
||||
#else
|
||||
#ifndef USE_V8
|
||||
std::cerr << "V8 support is not enabled" << std::endl;
|
||||
exit(-1);
|
||||
#endif
|
||||
} else if(strcmp(argv[idx], "-jsc") == 0) {
|
||||
#ifdef USE_JSC
|
||||
shell = create_jsc_shell();
|
||||
#else
|
||||
#ifndef USE_JSC
|
||||
std::cerr << "JSC support is not enabled" << std::endl;
|
||||
exit(-1);
|
||||
#endif
|
||||
|
|
@ -56,16 +44,9 @@ int main(int argc, char* argv[]) {
|
|||
scriptPath = argv[idx];
|
||||
}
|
||||
}
|
||||
|
||||
if (shell == 0) {
|
||||
#ifdef USE_JSC
|
||||
shell = create_jsc_shell();
|
||||
#else
|
||||
std::cerr << "JSC support is not enabled" << std::endl;
|
||||
exit(-1);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
shell = JSShell::Create();
|
||||
|
||||
bool failed = false;
|
||||
for(std::vector<std::string>::iterator it = module_names.begin();
|
||||
it != module_names.end(); ++it) {
|
||||
|
|
@ -94,6 +75,6 @@ int main(int argc, char* argv[]) {
|
|||
}
|
||||
|
||||
delete shell;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,10 +18,12 @@ public:
|
|||
|
||||
JSShell() {}
|
||||
|
||||
virtual ~JSShell();
|
||||
|
||||
virtual ~JSShell() = 0;
|
||||
|
||||
static JSShell* Create();
|
||||
|
||||
bool ImportModule(const std::string& name);
|
||||
|
||||
|
||||
virtual bool RunScript(const std::string& scriptPath);
|
||||
|
||||
virtual bool RunShell();
|
||||
|
|
@ -31,7 +33,7 @@ protected:
|
|||
virtual bool RegisterModule(HANDLE library, const std::string& module_name) = 0;
|
||||
|
||||
virtual bool InitializeEngine() = 0;
|
||||
|
||||
|
||||
virtual bool ExecuteScript(const std::string& source, const std::string& name) = 0;
|
||||
|
||||
virtual bool DisposeEngine() = 0;
|
||||
|
|
@ -39,7 +41,7 @@ protected:
|
|||
static std::string ReadFile(const std::string& fileName);
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
std::vector<HANDLE> loaded_modules;
|
||||
|
||||
};
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ typedef int (*JSCIntializer)(JSGlobalContextRef context);
|
|||
public:
|
||||
|
||||
JSCShell() {};
|
||||
|
||||
|
||||
virtual ~JSCShell();
|
||||
|
||||
protected:
|
||||
|
|
@ -27,14 +27,14 @@ 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 DisposeEngine();
|
||||
|
||||
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);
|
||||
|
||||
|
|
@ -43,7 +43,7 @@ private:
|
|||
private:
|
||||
|
||||
std::vector<JSCIntializer> module_initializers;
|
||||
|
||||
|
||||
JSGlobalContextRef context;
|
||||
};
|
||||
|
||||
|
|
@ -56,7 +56,7 @@ JSCShell::~JSCShell() {
|
|||
|
||||
bool JSCShell::RegisterModule(HANDLE library, const std::string& module_name) {
|
||||
std::string symname = std::string(module_name).append("_initialize");
|
||||
|
||||
|
||||
JSCIntializer init_function = reinterpret_cast<JSCIntializer>((long) LOAD_SYMBOL(library, symname.c_str()));
|
||||
if(init_function == 0) return false;
|
||||
|
||||
|
|
@ -64,7 +64,7 @@ bool JSCShell::RegisterModule(HANDLE library, const std::string& module_name) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool JSCShell::InitializeEngine() {
|
||||
bool JSCShell::InitializeEngine() {
|
||||
if(context != 0) {
|
||||
JSGlobalContextRelease(context);
|
||||
context = 0;
|
||||
|
|
@ -81,7 +81,7 @@ bool JSCShell::InitializeEngine() {
|
|||
if(!init_function(context)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -104,9 +104,9 @@ bool JSCShell::DisposeEngine() {
|
|||
return true;
|
||||
}
|
||||
|
||||
JSValueRef JSCShell::Print(JSContextRef context, JSObjectRef object,
|
||||
JSObjectRef globalobj, size_t argc,
|
||||
const JSValueRef args[], JSValueRef* ex) {
|
||||
JSValueRef JSCShell::Print(JSContextRef context, JSObjectRef object,
|
||||
JSObjectRef globalobj, size_t argc,
|
||||
const JSValueRef args[], JSValueRef* ex) {
|
||||
if (argc > 0)
|
||||
{
|
||||
JSStringRef string = JSValueToStringCopy(context, args[0], NULL);
|
||||
|
|
@ -114,18 +114,18 @@ JSValueRef JSCShell::Print(JSContextRef context, JSObjectRef object,
|
|||
char *stringUTF8 = new char[numChars];
|
||||
JSStringGetUTF8CString(string, stringUTF8, numChars);
|
||||
printf("%s\n", stringUTF8);
|
||||
|
||||
|
||||
delete[] stringUTF8;
|
||||
}
|
||||
|
||||
|
||||
return JSValueMakeUndefined(context);
|
||||
}
|
||||
|
||||
bool JSCShell::RegisterFunction(JSGlobalContextRef context, JSObjectRef object,
|
||||
bool JSCShell::RegisterFunction(JSGlobalContextRef context, JSObjectRef object,
|
||||
const char* functionName, JSObjectCallAsFunctionCallback callback) {
|
||||
JSStringRef js_functionName = JSStringCreateWithUTF8CString(functionName);
|
||||
JSObjectSetProperty(context, object, js_functionName,
|
||||
JSObjectMakeFunctionWithCallback(context, js_functionName, callback),
|
||||
JSObjectMakeFunctionWithCallback(context, js_functionName, callback),
|
||||
kJSPropertyAttributeNone, NULL);
|
||||
JSStringRelease(js_functionName);
|
||||
return true;
|
||||
|
|
@ -133,10 +133,10 @@ bool JSCShell::RegisterFunction(JSGlobalContextRef context, JSObjectRef object,
|
|||
|
||||
void JSCShell::PrintError(JSContextRef ctx, JSValueRef err, const std::string& name) {
|
||||
char *buffer;
|
||||
|
||||
|
||||
JSStringRef string = JSValueToStringCopy(ctx, err, 0);
|
||||
size_t length = JSStringGetLength(string);
|
||||
|
||||
|
||||
buffer = new char[length+1];
|
||||
JSStringGetUTF8CString(string, buffer, length+1);
|
||||
std::string errMsg(buffer);
|
||||
|
|
@ -144,7 +144,7 @@ void JSCShell::PrintError(JSContextRef ctx, JSValueRef err, const std::string& n
|
|||
delete[] buffer;
|
||||
|
||||
JSObjectRef errObj = JSValueToObject(ctx, err, 0);
|
||||
|
||||
|
||||
if(errObj == 0) {
|
||||
std::cerr << errMsg << std::endl;
|
||||
return;
|
||||
|
|
@ -154,12 +154,12 @@ void JSCShell::PrintError(JSContextRef ctx, JSValueRef err, const std::string& n
|
|||
// though, it happened that this was always ""
|
||||
JSStringRef lineKey = JSStringCreateWithUTF8CString("line");
|
||||
JSValueRef jsLine = JSObjectGetProperty(ctx, errObj, lineKey, 0);
|
||||
int line = (int) JSValueToNumber(ctx, jsLine, 0);
|
||||
int line = (int) JSValueToNumber(ctx, jsLine, 0);
|
||||
JSStringRelease(lineKey);
|
||||
|
||||
std::cerr << name << ":" << line << ":" << errMsg << std::endl;
|
||||
}
|
||||
|
||||
JSShell* create_jsc_shell() {
|
||||
JSShell* JSShell::Create() {
|
||||
return new JSCShell();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,9 +15,9 @@ class V8Shell: public JSShell {
|
|||
|
||||
public:
|
||||
V8Shell();
|
||||
|
||||
|
||||
virtual ~V8Shell();
|
||||
|
||||
|
||||
virtual bool RunScript(const std::string& scriptPath);
|
||||
|
||||
virtual bool RunShell();
|
||||
|
|
@ -28,11 +28,11 @@ 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 DisposeEngine();
|
||||
|
||||
|
||||
private:
|
||||
|
||||
v8::Persistent<v8::Context> CreateShellContext();
|
||||
|
|
@ -46,13 +46,13 @@ private:
|
|||
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;
|
||||
|
||||
};
|
||||
|
|
@ -75,7 +75,7 @@ V8Shell::~V8Shell() {
|
|||
|
||||
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;
|
||||
|
||||
|
|
@ -88,10 +88,10 @@ bool V8Shell::RunScript(const std::string& scriptPath) {
|
|||
if (!context.IsEmpty()) {
|
||||
context.Dispose();
|
||||
}
|
||||
|
||||
|
||||
std::string source = ReadFile(scriptPath);
|
||||
|
||||
context = CreateShellContext();
|
||||
context = CreateShellContext();
|
||||
if (context.IsEmpty()) {
|
||||
printf("Could not create context.\n");
|
||||
return false;
|
||||
|
|
@ -104,11 +104,11 @@ bool V8Shell::RunScript(const std::string& scriptPath) {
|
|||
if(!ExecuteScript(source, scriptPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
context->Exit();
|
||||
context.Dispose();
|
||||
v8::V8::Dispose();
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -117,18 +117,18 @@ bool V8Shell::RunShell() {
|
|||
if (!context.IsEmpty()) {
|
||||
context.Dispose();
|
||||
}
|
||||
|
||||
context = CreateShellContext();
|
||||
|
||||
context = CreateShellContext();
|
||||
if (context.IsEmpty()) {
|
||||
printf("Could not create context.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
context->Enter();
|
||||
|
||||
v8::Context::Scope context_scope(context);
|
||||
ExtendEngine();
|
||||
|
||||
|
||||
static const int kBufferSize = 1024;
|
||||
while (true) {
|
||||
char buffer[kBufferSize];
|
||||
|
|
@ -152,13 +152,13 @@ bool V8Shell::InitializeEngine() {
|
|||
}
|
||||
|
||||
void V8Shell::ExtendEngine() {
|
||||
|
||||
|
||||
// register extensions
|
||||
for(std::vector<V8ExtensionRegistrar>::iterator it=module_initializers.begin();
|
||||
it != module_initializers.end(); ++it) {
|
||||
(*it)(context);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
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::HandleScope scope;
|
||||
|
||||
|
||||
// Create a template for the global object.
|
||||
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));
|
||||
|
||||
v8::Persistent<v8::Context> _context = v8::Context::New(NULL, global);
|
||||
|
||||
|
||||
return _context;
|
||||
}
|
||||
|
||||
|
|
@ -282,6 +282,6 @@ const char* V8Shell::ToCString(const v8::String::Utf8Value& value) {
|
|||
return *value ? *value : "<string conversion failed>";
|
||||
}
|
||||
|
||||
JSShell* create_v8_shell() {
|
||||
JSShell* JSShell::Create() {
|
||||
return new V8Shell();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue