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"
|
#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();
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ protected:
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
|
|
@ -106,7 +106,7 @@ bool JSCShell::DisposeEngine() {
|
||||||
|
|
||||||
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);
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue