将二进制组件引用为js-ctypes

问题描述

| 我在ѭ0中注册了一个二进制组件:
binary-component components/linux/myLib.so abi=Linux_x86-gcc3
现在,我想将其路径传递给
ctypes.open()
。我的问题是:如何引用二进制分量,以便可以将其传递给
ctypes.open()
?     

解决方法

        chrome.manifest中列出的二进制组件应该是XPCOM组件。另一方面,您的则是常规库,无需在清单中列出它-而是一种非常“手动”的方法。您的代码需要检查nsIXULRuntime.XPCOMABI(请参阅https://developer.mozilla.org/En/NsIXULRuntime)以查看该平台是否兼容。然后,您需要获取库文件的位置,如下所示:
Components.utils.import(\"resource://gre/modules/AddonManager.jsm\");
AddonManager.getAddonByID(\"myAddon@foo.com\",function(addon)
{
    var uri = addon.getResourceURI(\"components/linux/myLib.so\");
    if (uri instanceof Components.interfaces.nsIFileURL)
    {
        ctypes.open(uri.file.path);
        ...
    }
});
当然,getAddonByID()的第一个参数需要替换为您的加载项的ID。并且这里的假设是您的加载项未打包安装(在install.rdf中指定了5),因为否则将不会在磁盘上加载任何文件。     ,        您可以使用\“ resource \\”来在插件中引用普通的二进制文件: 将此添加到您的清单:
resource YOUR-ADDON-LIB path/to/libaddon.so ABI=Linux_x86-gcc3
resource YOUR-ADDON-LIB path/to/addon.dll ABI=WINNT_x86-msvc
\“ ABI \”指令将在不同平台下提供正确的lib路径。 在您的javascript文件中,您可以这样引用lib路径:
const ioService = Cc[\"@mozilla.org/network/io-service;1\"].getService(Ci.nsIIOService);
var uri = ioService.newURI(\'resource://YOUR-ADDON-LIB\',null,null);
if (uri instanceof Components.interfaces.nsIFileURL)
{
    var lib = ctypes.open(uri.file.path);
    /// ...
}
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...