好的,这是一个基于我正在工作的代码拼凑而成的片段,它应该做一些类似于你的例子的事情
bool compileJS(duk_context *ctx, const char* programBody)
{
bool success = false;
// Compile the JS into bytecode
if (duk_pcompile_string(ctx, 0, programBody) != 0)
{
// Error in program code
printf("Compile failed\n");
printf("%s\n", duk_safe_to_string(ctx, -1));
}
else
{
// Actually evaluate it - this will push the compiled code into the global scope
duk_pcall(ctx, 0);
success = true;
}
duk_pop(ctx);
return success;
}
duk_bool_t runJSFunction(duk_context *ctx, const char* funcName, const char* arga, const char* argb)
{
duk_bool_t returnVal;
// Get a reference to the named JS function
if (duk_get_global_string(ctx, funcName))
{
// Function found, push the args
duk_push_string(ctx, arga);
duk_push_string(ctx, argb);
// Use pcall - this lets you catch and handle any errors
if (duk_pcall(ctx, 2) != DUK_EXEC_SUCCESS)
{
// An error occurred - display a stack trace
duk_get_prop_string(ctx, -1, "stack");
printf(duk_safe_to_string(ctx, -1));
}
else
{
// function executed successfully - get result
returnVal = duk_get_boolean(ctx, -1);
}
}
else
{
printf("JS function not found!\n");
returnVal = false;
}
duk_pop(ctx); // pop result
return returnVal;
}
void testJS()
{
duk_context *ctx = duk_create_heap_default();
const char* programBody = "function helloWorld(a,b) { return a.includes(b); }";
if (compileJS(ctx, programBody))
{
for (int i = 0; i < 10; ++i)
{
bool ret = runJSFunction(ctx, "helloWorld", "the", "th");
if (ret)
{
printf("You got it!\n");
}
else
{
printf("yeah nah!\n");
}
}
}
}
我把它分成三个函数,这样你就可以看到函数的初始化和编译(以及缓存)是如何与 JS 执行分开处理的。我还添加了基本的错误处理,如果您的 JS 代码无法编译或运行,它应该会显示错误。