Tuesday, September 25th, 2007

JS-CTYPES: Calling out to native code from XUL

Category: Firefox, JavaScript, Library

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

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.

Related Content:

Posted by Dion Almaer at 6:43 am
1 Comment

+++--
3 rating from 21 votes

1 Comment »

Comments feed TrackBack URI

Maybe then FireBug will use some native calls to get the CSS and JS files from the cache, so it doesn’t have to reload them …on… …every… …page… …load…

Then maybe yslow will have real numbers to deal with on all pages but the first…

Comment by Steven — September 25, 2007

Leave a comment

You must be logged in to post a comment.