2011-05-30 49 views
4

我在chrome.manifest注册的二进制组件:参考二进制组件JS-ctypes的

binary-component components/linux/myLib.so abi=Linux_x86-gcc3 

现在,我想它的路径传递给ctypes.open()。我的问题是:如何引用二进制组件,以便我可以将它传递给ctypes.open()

回答

6

chrome.manifest中列出的二进制组件应该是XPCOM组件。另一方面,你是一个普通的图书馆,不需要在清单中列出它 - 而是一种非常“手动”的方法。您的代码需要检查nsIXULRuntime.XPCOMABI(请参阅https://developer.mozilla.org/En/NsIXULRuntime)以查看平台是否兼容。然后,你需要让你的库文件的位置,这样的事情:需要由您的附加课程的ID被替换

Components.utils.import("resource://gre/modules/AddonManager.jsm"); 
AddonManager.getAddonByID("[email protected]", function(addon) 
{ 
    var uri = addon.getResourceURI("components/linux/myLib.so"); 
    if (uri instanceof Components.interfaces.nsIFileURL) 
    { 
     ctypes.open(uri.file.path); 
     ... 
    } 
}); 

的第一个参数getAddonByID()。此处的假设是,您的加载项已解压缩安装(在install.rdf中指定的<em:unpack>true</em:unpack>),否则将无法加载磁盘上的文件。

5

你可以使用“资源”来引用正常的二进制文件在你的插件: 添加到您的清单:

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”指令会给出不同的平台下,你的右手库路径。

在你的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); 
    /// ... 
}