JavaScript interoperabiity
Cheerp implements three advanced interoperability mechanisms to interface C++ code with pure JavaScript:
- Expose C++ classes and methods to JavaScript code
- Inlining JavaScript code within a C++ codebase.
- Use JavaScript methods and classes directly from C++
The CHEERP_OBJECT macro
A common use case for inline asm is to return a literal object to JavaScript:
This is usually much more performant than creating a new client::Object()
and populating it with the set_()
method.
The CHEERP_OBJECT
macro can be used to achieve the same result without the boilerplate and with a more elegant syntax:
Currently the macro has the following limitations:
- The maximum number of arguments is 16
- The arguments need to be global or local variable names, and the same names will be used for the js object fields
The client namespace
Cheerp treats every function and class inside the client
namespace as a declaration for something implemented by the browser or JavaScript. You are free to add new declarations there for functions implemented in JavaScript. For example:
And on the JavaScript side:
The cheerp::ArrayRef
class and cheerp::makeArrayRef
helpers
Cheerp provides an helper class to simplify access to JS array-like objects, such as client::Array
and client::Int32Array
. Itβs common to pass them as pointers, which makes it inconvenient to use the operator[]
to access their elements. The cheerp::ArrayRef
class can be wrapper around a pointer to use the operator[]
more naturally. cheerp::makeArrayRef
is an helper to automatically deduce the right template type for the cheerp::ArrayRef
class.