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"
#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
@ -57,14 +45,7 @@ int main(int argc, char* argv[]) {
}
}
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();

View file

@ -18,7 +18,9 @@ public:
JSShell() {}
virtual ~JSShell();
virtual ~JSShell() = 0;
static JSShell* Create();
bool ImportModule(const std::string& name);

View file

@ -34,7 +34,7 @@ protected:
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);
@ -106,7 +106,7 @@ bool JSCShell::DisposeEngine() {
JSValueRef JSCShell::Print(JSContextRef context, JSObjectRef object,
JSObjectRef globalobj, size_t argc,
const JSValueRef args[], JSValueRef* ex) {
const JSValueRef args[], JSValueRef* ex) {
if (argc > 0)
{
JSStringRef string = JSValueToStringCopy(context, args[0], NULL);
@ -160,6 +160,6 @@ void JSCShell::PrintError(JSContextRef ctx, JSValueRef err, const std::string& n
std::cerr << name << ":" << line << ":" << errMsg << std::endl;
}
JSShell* create_jsc_shell() {
JSShell* JSShell::Create() {
return new JSCShell();
}

View file

@ -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();
}