JSctypes: Difference between revisions

Jump to navigation Jump to search
2,454 bytes added ,  2 March 2008
update page to actual js-ctype content
m (Add "preliminary documentation" template)
(update page to actual js-ctype content)
Line 1: Line 1:
= JSctypes: a foreign-function library for the SpiderMonkey JavaScript Engine =
== Overview ==


JSctypes is a foreign-function library for the SpiderMonkey JavaScript engine used by Mozilla. It provides C-compatible data types and allows JS code to call functions in shared libraries (DLLs) and implement callback functions. The interface and implementation are modeled on the [http://docs.python.org/lib/module-ctypes.html Python ctypes module].
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 [http://docs.python.org/lib/module-ctypes.html Python ctypes module].


JSctypes can be used to provide pure-JavaScript wrappers for binary libraries.
The main objective for the library is to help developers avoid building binary (C++) [http://developer.mozilla.org/en/docs/XPCOM XPCOM] components when only a simple wrapper is needed. Such situations include:


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


== JSctypes reference ==
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 [http://developer.mozilla.org/en/docs/How_to_Build_an_XPCOM_Component_in_Javascript 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.


To import JSctypes into a chrome script or JS component:
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.


<pre>Components.utils.import("resource://jsctypes/jsctypes.js");</pre>
== Example Usage ==


This will import a single JSCtypes object into your script namespace.
<pre>
const nsINativeTypes = Components.interfaces.nsINativeTypes;
const MB_OK = 3;
function messageBox() {
  // create the XPCOM js-ctypes instance
  var library = Components.classes["@developer.mozilla.org/js-ctypes;1"]
                          .createInstance(nsINativeTypes);


* [[JSctypes/reference/Libraries|Finding and loading shared libraries]]
  // load the native shared library
* [[JSctypes/reference/Types|Data types]]
  library.open("user32.dll");
** Fundamental data types
 
** Structured data types
  // declare the native method
** Arrays and pointers
  var msgBox = library.declare("MessageBoxW",          /* function name */
* [[JSctypes/reference/Functions|Functions]]
                              nsINativeTypes.STDCALL,  /* call type */
** Non-prototyped functions
                              nsINativeTypes.INT32,    /* return type */
** Function prototypes
                              nsINativeTypes.INT32,    /* arg 1 */
** Prototyped functions
                              nsINativeTypes.WSTRING,  /* arg 2 */
** Implementing Callback Functions
                              nsINativeTypes.WSTRING,  /* arg 3 */
* [[JSctypes/reference/Utility functions|Utility functions]]
                              nsINativeTypes.INT32);  /* arg 4 */
 
  // call the native method
  var ret = msgBox(0, "This is the coolest message", "My Title", MB_OK);
 
  // test the return value
  alert(ret);
}
</pre>
 
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 code is in SVN [http://viewvc.svn.mozilla.org/vc/projects/js-ctypes/ here] and we have a Bugzilla component too ([https://bugzilla.mozilla.org/buglist.cgi?query_format=advanced&product=Other+Applications&component=js-ctypes&resolution=---&chfieldto=Now see bugs], [https://bugzilla.mozilla.org/enter_bug.cgi?product=Other%20Applications&component=js-ctypes file bug]).
 
{{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.}}
canmove, Confirmed users, Bureaucrats and Sysops emeriti
2,798

edits

Navigation menu