ctypes.open 编辑

At the heart of js-ctypes is the ctypes.open() function. This must be called before any work can commence. There are two options:

Custom native file

For this method, a native file must be created. A native file for use on UNIX systems is an SO file, which is created and a C function called add() is written.

int add(int a, int b) {
  return a + b;
}

To make this a shared library, a native file which can be loaded and used from js-ctypes, compile it with these commands:

gcc -fPIC -c myCFuntionsForUnix.c
gcc -shared -o myCFuntionsForUnix.so myCFuntionsForUnix.o

A file named myCFuntionsForUnix.so is successfully created. This native file can be placed in any folder of an addon and then loaded from js-ctypes. They can also be loaded from the system library path.

Loading the native file

If your addon is restartless, you must set it to be unpacked. See: http://stackoverflow.com/questions/19382201/how-to-load-dll-from-sdk-addon-data-folder

It is important to note that custom native files cannot be loaded through chrome:// or resource:// URIs. They must be accessed through a local file path  (file://) or a JAR path (jar:). If the addon is an unpacked addon, such as bootstrap or Add-on SDK addons, the JAR path must be used. Therefore, the path has to be converted and then the converted path is used for loading with ctypes.open.

If the native file is located at chrome://youraddon/content/mySubFolder/myCFunctionsForUnix.so then it is converted to a File URI like this:

Components.utils.import("resource://gre/modules/Services.jsm");
var cr = Components.classes['@mozilla.org/chrome/chrome-registry;1'].getService(Components.interfaces.nsIChromeRegistry);

var chromeURI_myLib = Services.io.newURI('chrome://youraddon/content/mySubFolder/myCFunctionsForUnix.so', 'UTF-8', null);
var localFile_myLib = cr.convertChromeURL(chromeURI_myLib);
var jarPath_myLib = localFile_myLib.spec; // "jar:file:///C:/Users/Vayeate/AppData/Roaming/Mozilla/Firefox/Profiles/aecgxse.Unnamed%20Profile%201/extensions/youraddon@jetpack.xpi!/mySubFolder/myCFunctionsForUnix.so"
var filePath_myLib = localFilemyLib.path; // "file:///C:/Users/Vayeate/AppData/Roaming/Mozilla/Firefox/Profiles/aecgxse.Unnamed%20Profile%201/extensions/youraddon@jetpack.xpi!/mySubFolder/myCFunctionsForUnix.so"

If your add-on is a bootstrap add-on, then you don't need to use this method to convert a chrome:// path; instead, on startup procedure of the bootstrap add-on obtain the File and/or JAR path from installPath from the aData parameter.

function startup(aData, aReason) {
    var nsIFile_folder = aData.installPath; // if addon is unpacked, this will be the addon folder. if addon is packed, this will be addon xpi
}

This can then be joined with your file name to obtain it's path like this:

function startup(aData, aReason) {

    var jarPath_folder = 'jar:' + OS.Path.toFileURI(aData.installPath.path) + '!/'; // if unpacked is false in install.rdf this will look like: "jar:file:///C:/Users/Vayeate/AppData/Roaming/Mozilla/Firefox/Profiles/aksozfjt.Unnamed%20Profile%2010/extensions/AsyncZip@jetpack!/"
    var filePath_folder = aData.installPath.path; // if unpacked is set to false in install.rdf this will look like: "C:\Users\Vayeate\AppData\Roaming\Mozilla\Firefox\Profiles\aksozfjt.Unnamed Profile 10\extensions\AsyncZip@jetpack"

    var filePath_myLib = OS.Path.join(filePath_folder, 'mySubFolder', 'myCFunctionsForUnix.so');
    var jarPath_myLib = jarPath_folder + 'mySubFolder/myCFunctionsForUnix.so';
}

This can then be opened and the C functions within can be used from js-ctypes.

var lib = ctypes.open(filePath_myLib);

var add_with_c = lib.declare("add", ctypes.default_abi,
    ctypes.int,    // return type
    ctypes.int,    // a
    ctypes.int     // b
);

var rez = add_with_c(2, 5); // rez is 7

lib.close();

References

Standard OS Libraries

See Standard OS Libraries

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:65 次

字数:5627

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文