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

View file

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

View file

@ -160,6 +160,6 @@ void JSCShell::PrintError(JSContextRef ctx, JSValueRef err, const std::string& n
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

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