Simplify creation of a Javascript shell.
This commit is contained in:
parent
57980975a0
commit
058a27bf32
4 changed files with 53 additions and 70 deletions
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue