Skip to content

Commit 680ae34

Browse files
authored
Fix: add missing int64 <-> bigint primitive mapping when generating TypeScript definitions (#20451)
This PR adds a missing mapping between int64_t <=> bigint that makes the typings generation fail for code that makes use of that primitive type.
1 parent bea4940 commit 680ae34

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

site/source/docs/porting/connecting_cpp_and_javascript/embind.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,13 +917,19 @@ Out of the box, *embind* provides converters for many standard C++ types:
917917
+---------------------+--------------------------------------------------------------------+
918918
| ``double`` | Number |
919919
+---------------------+--------------------------------------------------------------------+
920+
| ``int64_t`` | BigInt* |
921+
+---------------------+--------------------------------------------------------------------+
922+
| ``uint64_t`` | BigInt* |
923+
+---------------------+--------------------------------------------------------------------+
920924
| ``std::string`` | ArrayBuffer, Uint8Array, Uint8ClampedArray, Int8Array, or String |
921925
+---------------------+--------------------------------------------------------------------+
922926
| ``std::wstring`` | String (UTF-16 code units) |
923927
+---------------------+--------------------------------------------------------------------+
924928
| ``emscripten::val`` | anything |
925929
+---------------------+--------------------------------------------------------------------+
926930

931+
\*Requires BigInt support to be enabled with the `-sWASM_BIGINT` flag.
932+
927933
For convenience, *embind* provides factory functions to register
928934
``std::vector<T>`` (:cpp:func:`register_vector`) and ``std::map<K, V>``
929935
(:cpp:func:`register_map`) types:

src/embind/embind_ts.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ var LibraryEmbind = {
210210
['bool', 'boolean'],
211211
['float', 'number'],
212212
['double', 'number'],
213+
#if WASM_BIGINT
214+
['int64_t', 'bigint'],
215+
['uint64_t', 'bigint'],
216+
#endif
213217
['void', 'void'],
214218
['std::string', jsString],
215219
['std::basic_string<unsigned char>', jsString],

test/other/embind_tsgen_bigint.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <emscripten/bind.h>
2+
#include <emscripten/val.h>
3+
4+
using namespace emscripten;
5+
6+
uint64_t bigint_fn(int64_t) { return 0; }
7+
8+
EMSCRIPTEN_BINDINGS(Test) {
9+
function("bigintFn", &bigint_fn);
10+
}

test/other/embind_tsgen_bigint.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface MainModule {
2+
bigintFn(_0: bigint): bigint;
3+
}

test/test_other.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2990,6 +2990,15 @@ def test_embind_tsgen_val(self):
29902990
'-lembind', '--embind-emit-tsd', 'embind_tsgen_val.d.ts'])
29912991
self.assertExists('embind_tsgen_val.d.ts')
29922992

2993+
def test_embind_tsgen_bigint(self):
2994+
args = [EMCC, test_file('other/embind_tsgen_bigint.cpp'), '-lembind', '--embind-emit-tsd', 'embind_tsgen_bigint.d.ts']
2995+
# Check that TypeScript generation fails when code contains bigints but their support is not enabled
2996+
stderr = self.expect_fail(args)
2997+
self.assertContained("Missing primitive type to TS type for 'int64_t", stderr)
2998+
# Check that TypeScript generation works when bigint support is enabled
2999+
self.run_process(args + ['-sWASM_BIGINT'])
3000+
self.assertFileContents(test_file('other/embind_tsgen_bigint.d.ts'), read_file('embind_tsgen_bigint.d.ts'))
3001+
29933002
def test_emconfig(self):
29943003
output = self.run_process([emconfig, 'LLVM_ROOT'], stdout=PIPE).stdout.strip()
29953004
self.assertEqual(output, config.LLVM_ROOT)

0 commit comments

Comments
 (0)