std::string can be called from JavaScript with a JavaScript string, and the argument will be automatically converted; similarly, a Turbo Module method which returns std::string will return a JavaScript string to the JavaScript environment. Some of these, such as std::string and int32_t, are basic C++ types. Others are specific to Vega, and are primarily available as part of the com::amazon::kepler::turbomodule namespace.
Types map
The following table shows the mapping between TypeScript types and their C++ counterparts. This is the mapping used by the codegen tool. Some of these types are not part of base TypeScript and need to be imported from@amazon-devices/keplerscript-turbomodule-api.
int64_t might have loss of precision due to Number.MAX_SAFE_INTEGER. You can use BigInt for large integers where the precision is necessary.
[2] A homogeneous array is the one whose elements are of the same type.
[3] BigInt is supported for signed and unsigned 64-bit integers, as direct method parameter and return types. For more details on supported usages, see BigInt
[4] JsonContainer will be removed after the gaps in JSObject are addressed. If you need to use it, you can import JsonContainer type from @amazon-devices/keplerscript-turbomodule-api for a generic Object, or define a custom type alias such as
Number.MAX_SAFE_INTEGER, use a std::string representation for the Turbo Module’s parameter and return types. You can convert the string to more appropriate types within the JavaScript and C++ implementations.
JSArray
For homogeneous (T[]) arrays, you can use . For other arrays, you can use JSArray.
A JSArray is an std::vector which accepts a variety of element types including null pointer, boolean, various int, double, float, std::string, JSArray, and JSObject. Its usage is therefore similar to std::vector.
Example
JSObject
AJSObject is an std::map with std::string keys which accepts a variety of value types including null pointer, boolean, various int, double, float, std::string, JSArray, and JSObject. Its usage is therefore similar to std::map.
JSObject provides a static parseJson method. The parseJson method accepts an std::string JSON string and returns the equivalent JSObject.
Example
Proxy JSObject and JSArray
Vega Turbo Modules support the proxy typesproxy::JSObject and proxy::JSArray in addition to JSObject and JSArray types. One of the key features of the proxy types is dynamic access and modification. You can read from and write to JavaScript objects and arrays directly from your C++ code, without having to create copies.
However, there are a few important things to keep in mind when working with these proxy types:
- Thread Considerations: The proxy types must be used within the JSThread context and the scope of a Turbo Module method invocation. They can’t be stored or accessed from arbitrary threads. Thread safety checks are only performed in Debug mode, so be cautious when working with these proxy objects in a multi-threaded environment.
-
Native construction:
You can natively construct new proxy items, but you’ll need to obtain the required JSRuntime as a method parameter from your Turbo Module or HostCallback methods.
Warning: Don’t use default constructors. the default constructors for these proxy types are only maintained to support current internal usage in constructs such as
std::tupleandstd::variant. You shouldn’t rely on these default constructors, as they require a JSThread context and will be removed in a future release.
JSRuntime
The JSRuntime type represents a JavaScript runtime environment. It’s a native-only type that can be requested as a parameter by any Turbo Module or HostCallback method. It is typically used for creating new proxy items in C++.
Note: JSRuntime cannot be constructed from a non-JSThread.
proxy::JSObject
The proxy::JSObject type allows you to create a JavaScript object that can be passed to the native code, and have its properties and methods dynamically accessed and modified from the native side without creating a copy.
Note: Access to proxy::JSObject is restricted to the JSThread context and must occur within the scope of a Turbo Module method invocation. Thread safety checks are only performed in Debug mode.
proxy::JSArray
The proxy::JSArray type allows you to create a JavaScript array that you can pass to the native code, and have its elements dynamically accessed and modified from the native side.
Note: Access to proxy::JSArray is restricted to the JSThread context and must occur within the scope of a Turbo Module method invocation. Thread safety checks are only performed in Debug mode.
Promise
Promises should be executed on a separate thread. APromise is constructed with a PromiseOperation as input. A PromiseOperation is a function which accepts an and returns void.
A Promise has resolve and reject methods. A Promise can be rejected with an Error or std::string, and resolved with any of the C++ Turbo Module types from the types map.
Example
Callback
A callback should be executed on a separate thread. ACallback object can only be a parameter value, representing a JavaScript callback passed to C++. See HostCallback for callbacks as a return type.
The only method exposed by the Callback object is invoke which calls the JavaScript callback with the specified arguments, and returns the result to C++.
Example
HostCallback
HostCallback can only be a return value, used to return a C++ callback to the JavaScript environment. See Callback for callbacks as a parameter type.
Example
Callback using a HostCallback.
ArrayBuffer
AnArrayBuffer represents a generic, fixed-length raw binary data buffer.
Example
BigInt
ABigInt represents an integer. In JavaScript, these are used for integers which exceed Number.MAX_SAFE_INTEGER or Number.MIN_SAFE_INTEGER. In Vega, Turbo Modules support BigInt with int64_t- and uint64_t-based getter and constructor methods.
BigInt in method parameter and returns, as well as Callbacks and Promises, but only as a raw value and not as a part of a JSArray or JSObject.
A JavaScript BigInt which is too large to be represented by the Vega Turbo Module BigInt type (for example, it cannot be represented by int64_t or uint64_t) will fail an assertion, as do all invalid parameters for Vega Turbo Modules. For more information, see the error handling in “Advanced Turbo Module topics”.
utils::json::JsonContainer
Use ofJsonContainer for the JSObject type is being replaced, but there are currently still some feature gaps. For a full list of supported types, see the types map.
Example
NativeObject
In most cases, when you are passing data from your native code to JavaScript, a JSONContainer or JSObject will suffice. However, if you need a native object whose methods can be invoked from JavaScript, you need to create an object that implementscom::amazon::kepler::turbomodule::NativeObject.
Example

