//函数定义
static jsValue js_callAsFunction(jsExecState es, jsValue object, jsValue* args, int argCount)
{
wchar_t text[1025] = { 0 };
wchar_t title[1025] = { 0 };
if (argCount >= 1)
wcsncpy(text, jsToTempStringW(es, jsArg(es, 0)), 1024);
if (argCount >= 2)
wcsncpy(title, jsToTempStringW(es, jsArg(es, 1)), 1024);
MessageBoxW(NULL, text, title[0] ? title : NULL, MB_OK);
return jsInt(0);
}
static void js_releaseFunction(jsData* data)
{
delete data;
}
static void bindToGlobal(jsExecState es)
{
jsData* data = new jsData();
memset(data, 0, sizeof(jsData));
strcpy(data->typeName, "Function");
data->callAsFunction = js_callAsFunction;
data->finalize = js_releaseFunction;
jsValue func = jsFunction(es, data);
jsSetGlobal(es, "popupFunc", func);
}
static void bindToObject(jsExecState es, jsValue obj)
{
jsData* data = new jsData();
memset(data, 0, sizeof(jsData));
strcpy(data->typeName, "Function");
data->callAsFunction = js_callAsFunction;
data->finalize = js_releaseFunction;
jsValue func = jsFunction(es, data);
jsSet(es, obj, "popupObj", func);
}
//函数调用
jsExecState es = wkeGlobalExec(m_pBrowserWindow);
bindToGlobal(es);
jsExecState es = wkeGlobalExec(m_pBrowserWindow);
jsValue f = jsGetGlobal(es, "popupFunc");
jsValue val = jsString(es, "hello");
jsValue callRet = jsCallGlobal(es, f, &val, 1);
const wchar_t* jsv = jsToStringW(es, callRet);