mozilla/r2d2b2g — pull request #747 by bkase (2013-07-26 23:22:24+00:00)
Title: Created js-message bridge between C and JS

This changeset adds a generic communication bridge from C to JS.

Why make a communication bridge?
Each time we need to add a C to JS communication we have to create a new function pointer, initialize it properly in JS (which means declaring the types for the function pointer), modify or create a new function in the native code that takes the function pointer as a parameter, and then assign the function pointer to some variable that is accessible in the scope that it will be called.
This bridge makes it so that we reuse the same function call to JS and we can switch on a channel name and extract arbitrary data out of the arguments.

This shouldn't actually be merged-- it's just an example of this function being called during ADB initialization. @mykmelez suggested that I create a pull-request just for this bridge before using it everywhere.

C communicates to JS with `MSG("channel", [type1, value1, ...])`
JS uses `JsMessage.prototype.unravel(type1, [type2, ...])` to extract data.

See https://github.com/bkase/r2d2b2g/compare/js-message?expand=1#L4R185 for an example of `MSG`
The `MSG` macro creates an anonymous structure for the given types and populates it with the proper values before passing it as a parameter to the generic `void * js_msg(void *)` function.
The macro is complex but it makes it really easy to pass a struct to JS without having to construct a struct manually in C. If JS gets an actual struct, we can use `ctypes.cast` and not have to worry about pointer offsets and sizes of the types passed in. `MSG` is implemented in _js-message.h_.

See https://github.com/bkase/r2d2b2g/compare/js-message?expand=1#L0R35 for an example of `JsMessage`'s `unravel`. This method takes the types of each of the elements (which we know based on the channel name) and returns an N-tuple of the data extracted from an anonymous struct constructed from the type information. `JsMessage` is implemented in _adb/js-message.js_

If this lands, I can clean up the old native code in a few places as well (since we can share one function pointer now).

@ochameau Can you look at this?