How to call native c function via firefox
Posted on 10 Mayıs 2015 in Programlama by admin
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 (DLL, SO, DYLIB, etc)
Standard OS Libraries
Custom Native File
For this method, a native file must be created. A native file for use on UNIX systems is a SO file, it is crated and a C function is written called add.
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 succesfully created. This native file can be placed in any folder of an addon and then loaded from js-ctypes.
Loading the Native File
It is important to note that custom native file’s cannot be loaded through chrome:// or resource:// URIs. They must be access through a File path (file://) or a JAR path (jar:). If the addon is an unpacked addon, such as bootstrap or Addon-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 pathJar_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 you do not 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_addonFolder = aData.installPath.path;
}
This can then be joined with your file name to obtain it’s path like this:
function startup(aData, aReason) {
var nsIFile_addonFolder = aData.installPath.path;
var nsIFile_myLib = nsIFile_addonFolder.clone();
nsIFile_addonFolder.append(‘mySubFolder’);
nsIFile_addonFolder.append(‘myCFunctionsForUnix.so’);
var osPath_myLib = nsIFile_myLib.path;
var filePath_myLib = OS.Path.toFileURI(osPath_myLib);
var jarPath_myLib = ‘jar:’ + filePath_myLib.replace(aData.id + ‘.xpi’, aData.id + ‘.xpi!’);
}
This can then be opened and the C functions within can then be used from js-ctypes.
var myLib = 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();
Copied from:
https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes/Using_js-ctypes/ctypes.open#Custom_Native_File