JS-CTYPES: Calling out to native code from XUL
Written by on September 25th, 2007 in Ajax News.
Mark Finkle has ported Python’s ctypes to JavaScript, and has created JS-CTYPES which allows you to declare and call exported methods from binary/shared libraries from Mozilla’s privileged JavaScript. The idea is to make dealing with XPCOM simpler.
You end up with code like this:
-
-
const nsINativeType = Components.interfaces.nsINativeType;
-
-
function msgbox() {
-
var library = Components.classes[”@mozilla.org/js-ctypes;1″]
-
.createInstance(Components.interfaces.nsINativeLibrary);
-
library.open(”user32.dll”); // Load the shared Windows DLL
-
-
var messageBox = library.declare(
-
“MessageBoxW”, // name of the exported method
-
nsINativeType.INT32, // return type
-
nsINativeType.INT32, // parent hwnd (its a Window’s thing)
-
nsINativeType.WSTRING, // Unicode message string
-
nsINativeType.WSTRING, // Unicode title string
-
nsINativeType.INT32 // bitflag for buttons to show
-
);
-
-
var ret = messageBox(0, “This is the message”, “Msg Title 1″, 3);
-
alert(ret);
-
-
// You can reuse the method
-
var ret = messageBox(0, “This is another message”, “Msg Title 2″, 3);
-
alert(ret);
-
}
-
Of course, John Resig is working on FUEL which aims to make it easier to create add-ons easier:
FUEL is a JavaScript Library designed to help developers build extensions using terminology and interfaces that are familiar to them. FUEL is new in Firefox 3 and will be backported to Firefox 2 as well.
FUEL is about making it easier for extension developers to be productive, by minimizing some of the XPCOM formality and adding some “modern” JavaScript ideas. We want to start with areas that will provide the most benefit.
Source: Ajaxian
Original Article: http://feeds.feedburner.com/~r/ajaxian/~3/161033524/js-ctypes-calling-out-to-native-code-from-xul