From 2f9a7211e2d6ab044595db8acf750e56b177190c Mon Sep 17 00:00:00 2001 From: Lloyd Hilaiel Date: Thu, 17 Feb 2011 14:46:52 -0700 Subject: [PATCH] working example of how to use jsctypes to include native code in a module --- packages/chromeless-kit/lib/jsctypes/mylib.c | 5 +++++ packages/chromeless-kit/lib/jsctypes/mylib.js | 20 +++++++++++++++++++ toph/index.html | 4 ++++ 3 files changed, 29 insertions(+) create mode 100644 packages/chromeless-kit/lib/jsctypes/mylib.c create mode 100644 packages/chromeless-kit/lib/jsctypes/mylib.js create mode 100644 toph/index.html diff --git a/packages/chromeless-kit/lib/jsctypes/mylib.c b/packages/chromeless-kit/lib/jsctypes/mylib.c new file mode 100644 index 0000000..30ee26e --- /dev/null +++ b/packages/chromeless-kit/lib/jsctypes/mylib.c @@ -0,0 +1,5 @@ +const char * gettaStringFromNativeCode(void) +{ + return "This string has come a loooong way"; +} + diff --git a/packages/chromeless-kit/lib/jsctypes/mylib.js b/packages/chromeless-kit/lib/jsctypes/mylib.js new file mode 100644 index 0000000..8dd1183 --- /dev/null +++ b/packages/chromeless-kit/lib/jsctypes/mylib.js @@ -0,0 +1,20 @@ +const {Cc,Ci,Cm,Cr,Cu} = require("chrome"); + +const path = require("path"); + +/* Load JS Ctypes Javascript module */ +ctypes = {}; +Cu.import("resource://gre/modules/ctypes.jsm", ctypes); +ctypes = ctypes.ctypes; + +exports.getString = function() { + let pathToLib = require("url").toFilename(__url__); + pathToLib = path.join(path.dirname(pathToLib), 'libmylib.dylib'); + let lib = ctypes.open(pathToLib); + let getStr = lib.declare("gettaStringFromNativeCode", + ctypes.default_abi, + ctypes.char.ptr); + let rv = getStr().readString(); + lib.close(); + return rv; +}; diff --git a/toph/index.html b/toph/index.html new file mode 100644 index 0000000..ea6cf4c --- /dev/null +++ b/toph/index.html @@ -0,0 +1,4 @@ +