Generates C++/JS wrappers for JavaScript on mbed. This makes it easy to consume C++ APIs from JerryScript.
-
Create a C++ application that uses the library - f.e. the example program that comes with the library.
-
Produce a debug build via:
$ mbed compile --profile ./mbed-os/tools/profiles/debug.json
-
Go to the build directory and run an object dump:
$ arm-none-eabi-objdump -Wi -g *.elf > symbols.txt
-
Run this tool to generate the wrapper - where ClassName is the name of the object you want to wrap, and HeaderFile is the
.h
file of the native library:$ npm install $ node generate.js symbols.txt ClassName --header-file NativeHeaderFile.h
This instructions only work for projects that build via Gulp. See mbed-js-example.
-
In the wrapper directory: add the native library via
mbed add http://path/to/lib
. -
In your mbed-js project, run
npm install PATH_TO_WRAPPER --save --ignore-scripts
. -
Compile the project with Gulp.
$ gulp --target=K64F
-
C++ APIs often look different than a JS API, passing in a pointer and the number of elements. This requires manual work.
-
Higher-level APIs work better than lower-level APIs. F.e. on C++ level use a
string
rather than achar*
. Same goes for arrays. -
If you want to have a function in JavaScript that then returns another object (rather than a primitive):
- Create wrappers for both objects (with
--library-name samelibname
parameter). - Make a shared
.lib
file (rather than 2 separate ones) where you declare both objects. - Include the
mbed-js-childname.h
header in your parent implementation (to expose thembed_js_wrap_native_object
function). - Probably remove the ctor for the child, unless you want JS users to be able to construct their own versions.
- That's it!
- Create wrappers for both objects (with
-
Enums are automatically exposed under the name of the C++ object that declared them (see the
_setup
calls). F.e.:typedef enum { HTTP_GET } http_method;
Becomes:
http_method.HTTP_GET
-
Templated functions require the
--js-class-name
parameter to be set.$ node generate.js mqtt-symbols.txt "Client<MQTTNetwork, Countdown, 100, 5>" --js-class-name MqqtClient
Callback<>
types. There ismbed::js::EventLoop::getInstance().wrapFunction
, but it does not handle arguments.- vector / array types.
- C++ APIs are often synchronous. Should have an easy method to make them async (by waiting on an RTOS thread).
- A whole bunch of primitives are not implemented yet.
- Include the header file where the native object is declared. This info does not seem to be in the
.elf
file.