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:

JAVASCRIPT:

  1.  
  2. const nsINativeType = Components.interfaces.nsINativeType;
  3.  
  4. function msgbox() {
  5.   var library = Components.classes[”@mozilla.org/js-ctypes;1″]
  6.                           .createInstance(Components.interfaces.nsINativeLibrary);
  7.   library.open(”user32.dll”); // Load the shared Windows DLL
  8.  
  9.   var messageBox = library.declare(
  10.       “MessageBoxW”,          // name of the exported method
  11.       nsINativeType.INT32,    // return type
  12.       nsINativeType.INT32,    // parent hwnd (its a Window’s thing)
  13.       nsINativeType.WSTRING,  // Unicode message string
  14.       nsINativeType.WSTRING,  // Unicode title string
  15.       nsINativeType.INT32     // bitflag for buttons to show
  16.   );
  17.  
  18.   var ret = messageBox(0, “This is the message”, “Msg Title 1″, 3);
  19.   alert(ret);
  20.  
  21.   // You can reuse the method
  22.   var ret = messageBox(0, “This is another message”, “Msg Title 2″, 3);
  23.   alert(ret);
  24. }
  25.  

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

Leave a Reply

You must be logged in to post a comment.



Site Navigation