Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some information about 'j' and 'p' types and function signatures #22562

Merged
merged 7 commits into from
Oct 8, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -622,21 +622,46 @@ See `test_add_function in test/test_core.py`_ for an example.
You should build with ``-sALLOW_TABLE_GROWTH`` to allow new functions to be
added to the table. Otherwise by default the table has a fixed size.

.. note:: When using ``addFunction`` on LLVM Wasm backend, you need to provide
an additional second argument, a Wasm function signature string. Each
character within a signature string represents a type. The first character
represents the return type of a function, and remaining characters are for
parameter types.
When using ``addFunction`` on the LLVM Wasm backend, you need to provide
goldwaving marked this conversation as resolved.
Show resolved Hide resolved
an additional second argument, a Wasm function signature string, explained below.
See `test/interop/test_add_function_post.js <https://github.com/emscripten-core/emscripten/blob/main/test/interop/test_add_function_post.js>`_ for an example.


.. _interacting-with-code-function-signatures:

Function Signatures
===================

The LLVM Wasm backend requires a Wasm function signature string when using ``addFunction``
and in JavaScript libraries. Each character within a signature string represents a type. The first character
represents the return type of a function, and remaining characters are for
parameter types.

- ``'v'``: void type
- ``'i'``: 32-bit integer type
- ``'j'``: 64-bit integer type (currently does not exist in JavaScript)
- ``'j'``: 64-bit integer type (see note below)
- ``'f'``: 32-bit float type
- ``'d'``: 64-bit float type
- ``'p'``: 32-bit or 64-bit pointer (MEMORY64)

For example, if you add a function that takes an integer and does not return
anything, the signature is ``'vi'``.

``'j'`` must be used with the ``__i53abi`` decorator. It simplifies passing 64-bit numbers in JavaScript libraries. It cannot be used with ``addFunction``. Here is an example of a library function that sets the size of a file using a 64-bit value and returns
goldwaving marked this conversation as resolved.
Show resolved Hide resolved
an integer error code:

.. code-block:: c

extern "C" int _set_file_size(int handle, uint64_t size);

.. code-block:: javascript

For example, if you add a function that takes an integer and does not return
anything, you can do ``addFunction(your_function, 'vi');``. See
`test/interop/test_add_function_post.js <https://github.com/emscripten-core/emscripten/blob/main/test/interop/test_add_function_post.js>`_ for an example.
_set_file_size__i53abi: true, // Handle 64-bit
_set_file_size__sig: 'iij', // Function signature
_set_file_size: function(handle, size) { ... return error; }

Using ``-sWASM_BIGINT`` when linking is an alternative method of handling 64-bit types in libraries. ```Number()``` may be needed on the JavaScript side to convert it to a useable value.
See `settings reference <https://emscripten.org/docs/tools_reference/settings_reference.html?highlight=environment#wasm-bigint>`_.


.. _interacting-with-code-access-memory:
Expand Down
Loading