我已经在外部JavaScript文件sample.js中用JavaScript编写了一个函数。例如:
function getNum()
{
return 123;
}
是否可以使用JavaScriptCore从C调用getNum函数?如果可以,怎么办?
我可以使用以下步骤从JavaScript函数中调用C函数“ HelloCallback”:
JSObjectRef globalObject = JSContextGetGlobalObject(global_context);
JSStringRef helloFunctionName = JSStringCreateWithUTF8CString("hello");
// make function object
JSObjectRef functionObject = JSObjectMakeFunctionWithCallback(
global_context, helloFunctionName, &HelloCallback);
// set it as proprty of global object
JSObjectSetProperty(global_context,globalObject,
helloFunctionName,functionObject,kJSPropertyAttributeNone, NULL );
// make javascript(using sample.js file)
JSStringRef statement = JSStringCreateWithUTF8CString("/home/amit/Desktop/sampleWebkitgtkExample/sample.js");
// evaluate it
JSEvaluateScript( global_context,statement,NULL,NULL,1, NULL );
C函数“ HelloCallback”在C文件中的定义如下:
JSValueRef HelloCallback( JSContextRef ctx
, JSObjectRef function
, JSObjectRef thisObject
, size_t argumentCount
, const JSValueRef arguments[]
, JSValueRef* exception) {
// here do the C++ stuff
printf("\nHello from C function\n");
return JSValueMakeUndefined(ctx);}
sample.js文件包含以下示例代码:
function getnum()
{
return 123;
}
hello(); // calling C function
因此,如果我想从C调用getnum()函数,那我们该怎么做呢?
我确实是这样的:
JSStringRef statement = JSStringCreateWithUTF8CString("/home/amit/Desktop/sampleWebkitgtkExample/sample.js");
JSEvaluateScript( global_context,statement,NULL,NULL,1, NULL );
JSStringRef script1 = JSStringCreateWithUTF8CString("getnum()");
JSValueRef x = JSEvaluateScript(global_context, script1, NULL, NULL, 0, NULL);
printf("\n value = %d \n",x);
但是我没有得到返回的值。我在哪里出错了?
我确实喜欢这样,现在我可以调用getnum函数并获取返回的值:
char* scriptUTF8 = createStringWithContentsOfFile("/home/amit/Desktop/sampleWebkitgtkExample/sample.js");
JSStringRef script = JSStringCreateWithUTF8CString(scriptUTF8);
JSValueRef result = JSEvaluateScript(global_context, script, NULL, NULL, 1, &exception);
if (JSValueIsUndefined(global_context, result))
printf("PASS: Test script executed successfully.\n");
else {
printf("FAIL: Test script returned unexcpected value:\n");
JSStringRef exceptionIString = JSValueToStringCopy(global_context,
exception, NULL);
JSStringRelease(exceptionIString);
}
JSStringRef script1 = JSStringCreateWithUTF8CString("getnum()");
JSValueRef x = JSEvaluateScript(global_context, script1, NULL, NULL, 1, NULL);
printf("\nvalue = %f \n",JSValueToNumber(global_context,x,NULL));
createStringWithContentsOfFile函数的定义如下:
static char* createStringWithContentsOfFile(const char* fileName)
{
char* buffer;
size_t buffer_size = 0;
size_t buffer_capacity = 1024;
buffer = (char*)malloc(buffer_capacity);
FILE* f = fopen(fileName, "r");
if (!f) {
fprintf(stderr, "Could not open file: %s\n", fileName);
return 0;
}
while (!feof(f) && !ferror(f)) {
buffer_size += fread(buffer + buffer_size, 1, buffer_capacity -
buffer_size, f);
if (buffer_size == buffer_capacity) { // guarantees space for
trailing '\0'
buffer_capacity *= 2;
buffer = (char*)realloc(buffer, buffer_capacity);
assert(buffer);
}
assert(buffer_size < buffer_capacity);
}
fclose(f);
buffer[buffer_size] = '\0';
return buffer; }