JSctypes: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
m (Updated the link to the devmo page)
 
(10 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Note|This article is outdated. See the devmo pages for the current version.https://developer.mozilla.org/en/JavaScript_code_modules/ctypes.jsm}}
== Overview ==
== Overview ==


Line 16: Line 18:


<pre>
<pre>
const nsINativeTypes = Components.interfaces.nsINativeTypes;
/* Load JS Ctypes Javascript module */
const MB_OK = 3;
Components.utils.import("resource://gre/modules/ctypes.jsm");
 
function messageBox() {
/* Load windows api dll */
  // create the XPCOM js-ctypes instance
var lib = ctypes.open("C:\\WINDOWS\\system32\\user32.dll");
  var library = Components.classes["@developer.mozilla.org/js-ctypes;1"]
 
                          .createInstance(nsINativeTypes);
/* Declare the signature of the function we are going to call */
var msgBox = lib.declare("MessageBoxW",
                        ctypes.stdcall_abi,
                        ctypes.int32_t,
                        ctypes.int32_t,
                        ctypes.ustring,
                        ctypes.ustring,
                        ctypes.int32_t);
var MB_OK = 3;
 
/* Do it! */
var ret = msgBox(0, "Hello world", "title", MB_OK);
 
/* Display the returned value */
alert("MessageBox result : "+ret);
 
lib.close();
</pre>


  // load the native shared library
Instead of defining the whole path, you may also just give the file name.
  library.open("user32.dll");


  // declare the native method
<pre>
  var msgBox = library.declare("MessageBoxW",          /* function name */
var lib = ctypes.open("user32.dll");
                              nsINativeTypes.STDCALL,  /* call type */
</pre>
                              nsINativeTypes.INT32,    /* return type */
                              nsINativeTypes.INT32,    /* arg 1 */
                              nsINativeTypes.WSTRING,  /* arg 2 */
                              nsINativeTypes.WSTRING,  /* arg 3 */
                              nsINativeTypes.INT32);   /* arg 4 */


  // call the native method
Or even without the extension.
  var ret = msgBox(0, "This is the coolest message", "My Title", MB_OK);


  // test the return value
<pre>
  alert(ret);
var lib = ctypes.open("user32");
}
</pre>
</pre>
If the full path is not given, Windows uses the following search order to locate the DLL:
#The current directory of the task that is using the DLL.
#The \WINNT directory.
#The \WINNT\SYSTEM directory.
#The \WINNT\SYSTEM32 directory.
#The directory of the executable for the task that is using the DLL.
#A directory listed in the PATH environment variable.
(taken from http://support.microsoft.com/kb/164501/)


{{Note|This functionality cannot be accessed from JavaScript used in web content. Only JavaScript that runs with chrome privileges (extensions and Firefox UI for example) can use js-ctypes.}}
{{Note|This functionality cannot be accessed from JavaScript used in web content. Only JavaScript that runs with chrome privileges (extensions and Firefox UI for example) can use js-ctypes.}}
Line 48: Line 69:
== Technical Notes and Limitations ==
== Technical Notes and Limitations ==


The js-ctypes code uses the same [http://sourceware.org/libffi/ libffi] library as the Python ctypes module. It is currently known to work on Windows and Linux. We need to start the Mac implementation. We also need to add support for struct and more primitive types.
The js-ctypes code uses the same [http://sourceware.org/libffi/ libffi] library as the Python ctypes module. It is currently known to work on Windows, Mac and Linux.


== Getting the Code ==
== Getting the Code ==

Latest revision as of 07:45, 22 October 2009

Note: This article is outdated. See the devmo pages for the current version.https://developer.mozilla.org/en/JavaScript_code_modules/ctypes.jsm

Overview

js-ctypes is a foreign-function library for Mozilla’s privileged JavaScript. It provides C-compatible data types and allows JS code to call functions in shared libraries (dll, so, dylib) and implement callback functions. The interface and implementation are modeled on the Python ctypes module.

The main objective for the library is to help developers avoid building binary (C++) XPCOM components when only a simple wrapper is needed. Such situations include:

  1. Developer wants functionality not built into the Mozilla platform, but easily supported by the native OS.
  2. Developer wants to use a 3rd party library in an extension or XUL app.
  3. Developer has methods in native code for performance reasons.

The usual answer to these problems is creating a binary (C++) component to act as a wrapper so the native features can be exposed to JavaScript via XPCOM. However, the process of building binary XPCOM components is significantly harder than JavaScript components. The macros and memory management rules of binary components are harder than JavaScript, but its really the compiler and linker flags, IDL, makefiles and SDK versioning that make the process painful. The build process must be repeated for each platform too. For many cases, its just too much work.

The goal of js-ctypes is to allow developers to declare methods in binary libraries and then expose those methods as callable JavaScript functions. Developers can then create pure JavaScript library wrappers around binary libraries - without making binary XPCOM wrappers.

Example Usage

/* Load JS Ctypes Javascript module */
Components.utils.import("resource://gre/modules/ctypes.jsm");

/* Load windows api dll */
var lib = ctypes.open("C:\\WINDOWS\\system32\\user32.dll");

/* Declare the signature of the function we are going to call */
var msgBox = lib.declare("MessageBoxW",
                         ctypes.stdcall_abi,
                         ctypes.int32_t,
                         ctypes.int32_t,
                         ctypes.ustring,
                         ctypes.ustring,
                         ctypes.int32_t);
var MB_OK = 3;

/* Do it! */
var ret = msgBox(0, "Hello world", "title", MB_OK);

/* Display the returned value */
alert("MessageBox result : "+ret);

lib.close();

Instead of defining the whole path, you may also just give the file name.

var lib = ctypes.open("user32.dll");

Or even without the extension.

var lib = ctypes.open("user32");

If the full path is not given, Windows uses the following search order to locate the DLL:

  1. The current directory of the task that is using the DLL.
  2. The \WINNT directory.
  3. The \WINNT\SYSTEM directory.
  4. The \WINNT\SYSTEM32 directory.
  5. The directory of the executable for the task that is using the DLL.
  6. A directory listed in the PATH environment variable.

(taken from http://support.microsoft.com/kb/164501/)

Note: This functionality cannot be accessed from JavaScript used in web content. Only JavaScript that runs with chrome privileges (extensions and Firefox UI for example) can use js-ctypes.

Technical Notes and Limitations

The js-ctypes code uses the same libffi library as the Python ctypes module. It is currently known to work on Windows, Mac and Linux.

Getting the Code

The code can be checked out using Subversion:

svn co http://svn.mozilla.org/projects/js-ctypes/trunk/

It can also be viewed online here.

Build Instructions

Until js-ctypes makes its way into the Mozilla tree, building it will be a bit of a chore.

You'll first want to check out and build the Mozilla tree. See the Build Documentation for information on how to do this. The rest of these instructions assume that the path to your mozilla tree source is called PATH_TO_MOZILLA and that your objdir is in PATH_TO_OBJDIR.

You'll first want to create a directory called PATH_TO_MOZILLA/toolkit/components/js-ctypes and put the svn checkout there.

Then, from your objdir root, run the following command:

PATH_TO_MOZILLA/build/autoconf/make-makefile toolkit/components/js-ctypes

This should build the necessary Makefiles in your objdir, creating directories as necessary.

Now enter the PATH_TO_OBJDIR/toolkit/components/js-ctypes directory and run make.

Once that's done, you should be able to enter PATH_TO_OBJDIR/dist/bin and use xpcshell to play around with the component (see the example usage code above).

Contributing

Contributions are welcome! We have a Bugzilla component (see bugs, file bug). Please feel free to join #labs on irc.mozilla.org if you have any other questions.