Ok, so you'll need to do:
include "jswrap_modules.h" // for jswrap_require /*JSON{ "type": "library", "class" : "foo" }*/ static JsVar *getFooModule() { JsVar *moduleName = jsvNewFromString("foo"); JsVar *m = jswrap_require(moduleName); jsvUnLock(moduleName); return m; } /*JSON{ "type" : "staticmethod", "class" : "foo", "name" : "bar", "generate" : "jswrap_foo_bar" }*/ void jswrap_foo_bar() { JsVar *module = getFooModule(); if (!module) return; // out of memory? jsiQueueObjectCallbacks(module, "#onbob", NULL, 0); jsvUnLock(module); }
and then this works:
>require("foo").on("bob",function() { print('Hello'); }); =undefined >require("foo").bar() =undefined Hello
It's a bit nasty. With the prototype chain branch I'm hoping to tidy it up so you'll be able to do:
/*JSON{ "type" : "method", // <----- changed "class" : "foo", "name" : "bar", "generate" : "jswrap_foo_bar" }*/ void jswrap_foo_bar(JsVar *module) { jsiQueueObjectCallbacks(module, "#onbob", NULL, 0); }
But right now I think the safest method is above, and should be easy enough to tweak later on.
@Gordon started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Ok, so you'll need to do:
and then this works:
It's a bit nasty. With the prototype chain branch I'm hoping to tidy it up so you'll be able to do:
But right now I think the safest method is above, and should be easy enough to tweak later on.