仔细检查你的发现。duk_pcall
对我来说,失败时 TOS 上有一个错误对象:
result = duk_pcall(_ctx, static_cast<duk_idx_t>(argCount));
if (result != DUK_EXEC_SUCCESS)
checkForErrors();
错误检查是:
/**
* Checks if the TOS contains a JS error object and throws a C++ exception for it. This is particularly useful
* when executing code in safe mode (pcall).
*/
void ScriptingContext::checkForErrors() const {
if (duk_is_error(_ctx, -1)) {
// See if there's a stack trace. That includes the actual error message so we can use
// that as exception message. Otherwise just get what is on stack top.
std::string error;
if (duk_has_prop_string(_ctx, -1, "stack")) {
duk_get_prop_string(_ctx, -1, "stack"); // Puts stack trace on the stack.
error = duk_require_string(_ctx, -1);
} else {
error = duk_safe_to_string(_ctx, -1);
}
duk_pop(_ctx); // Remove error from stack.
throw std::runtime_error(error);
}
}