看看JS-ctypes的(https://developer.mozilla.org/en-US/docs/Mozilla/js-ctypes)
小例子:
Components.utils.import("resource://gre/modules/FileUtils.jsm");
Components.utils.import("resource://gre/modules/ctypes.jsm")
// path to C++ lib (/home/username/.thunderbird/PROFILE/extensions/EXTNAME/components/lib.so)
var libPath = FileUtils.getFile("ProfD", ["extensions", "EXTNAME", "components", "lib.so"]);
var lib = ctypes.open(libPath.path);
var libFunction = lib.declare("concatStrings", // function name in C++ code
ctypes.default_abi,
ctypes.char.ptr, // return value
ctypes.char.ptr, // param1
ctypes.char.ptr // param2
);
var ret = libFunction("abc", "efg");
lib.close()
另外要注意,C++編譯器名字改編由於函數重載使你的函數名稱可能是 'concatStrings'在C++代碼中,但是在彙編中它可能像'_123concatStrings'。爲了防止這種聲明你的功能,如:
extern "C" const char * concatStrings (const char * str1, const char * str2);