我正在尝试Duktape
用于 Android 和 JavaScript 绑定。
我按照这里的文档https://wiki.duktape.org/HowtoModules.html并按照步骤,我有以下调用代码:
DuktapeContext::DuktapeContext(JavaVM* javaVM)
: m_context(duk_create_heap(nullptr, nullptr, nullptr, &m_context, fatalErrorHandler))
, m_objectType(m_javaValues.getObjectType(getEnvFromJavaVM(javaVM))) {
if (!m_context) {
throw std::bad_alloc();
}
duk_module_duktape_init(m_context); //<~~~ adds support for `require()`
// Stash the JVM object in the context, so we can find our way back from a Duktape C callback.
duk_push_global_stash(m_context);
duk_push_pointer(m_context, javaVM);
duk_put_prop_string(m_context, -2, JAVA_VM_PROP_NAME);
duk_pop(m_context);
}
duk_module_duktape_init
是这样定义的(开箱即用,我没有编辑它):
/* Register `require` as a global function. */
duk_eval_string(ctx,
"(function(req){"
"var D=Object.defineProperty;"
"D(req,'name',{value:'require'});"
"D(this,'require',{value:req,writable:true,configurable:true});"
"D(Duktape,'modLoaded',{value:Object.create(null),writable:true,configurable:true});"
"})");
duk_push_c_function(ctx, duk__require, 1 /*nargs*/);
在我的javascript
文件sample.js
中,我有以下行:
var mod = require('tester');
其中,当通过 Duktape 评估脚本时,会引发异常Caused by: com.squareup.duktape.DuktapeException: TypeError: undefined not callable at JavaScript.eval
(require()
调用 的行)
我还必须实现Duktape.modSearch()
功能吗?我以为我可以通过简单地使用require()
?