E4042

E4042#

Invalid stub type.

When defineing a FFI, the stub type must be a valid type, namely they can be represented natively at corresponding backends.

For example, you cannot use FixedArray[T] and String in FFI definition on WASM linear backend.

Erroneous Example#

On WASM linear backend:

fn ffi(array : FixedArray[Int]) -> Unit = "mod" "ffi"
//             ^~~~~~~~~~~~~~~ Error: Invalid stub type.

Suggestion#

The way to pass non-native types through the FFI interface depends on both the backend of the MoonBit compiler and the runtime you are using. For example, if you wish to pass an array of int to the JS host with the WASM linear backend, you can pass the int one by one to the host function and store the array on the JS side.

extern type JsArray
fn make_js_array() -> JsArray = "mod" "make_js_array"
fn push_js_array(array : JsArray, value : Int) -> Unit = "mod" "push_js_array"