Skip to content

Commit f85a71d

Browse files
authored
embind: Run test_embind with TS generation enabled. (#20102)
To get this running: - Map all integers to a JS number. - Add missing c++ string to JS string mappings. - Stub out embind_tsgen_test_embind which is not needed. - Add missing function dependency. - Skip tests for unbound types. - Remove invalid constructor with unique_ptr argument.
1 parent 12bb5c9 commit f85a71d

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

src/embind/embind_ts.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ var LibraryEmbind = {
1414
this.name = name;
1515
}
1616
},
17+
$IntegerType: class IntegerType {
18+
constructor(typeId) {
19+
this.typeId = typeId;
20+
}
21+
},
1722
$Argument: class Argument {
1823
constructor(name, type) {
1924
this.name = name;
@@ -193,21 +198,22 @@ var LibraryEmbind = {
193198
const jsString = 'ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string';
194199
this.builtInToJsName = new Map([
195200
['bool', 'boolean'],
196-
['char', 'number'],
197-
['unsigned char', 'number'],
198-
['int', 'number'],
199-
['unsigned int', 'number'],
200-
['unsigned long', 'number'],
201201
['float', 'number'],
202202
['double', 'number'],
203203
['void', 'void'],
204204
['std::string', jsString],
205205
['std::basic_string<unsigned char>', jsString],
206+
['std::wstring', jsString],
207+
['std::u16string', jsString],
208+
['std::u32string', jsString],
206209
['emscripten::val', 'any'],
207210
]);
208211
}
209212

210213
typeToJsName(type) {
214+
if (type instanceof IntegerType) {
215+
return 'number';
216+
}
211217
if (type instanceof PrimitiveType) {
212218
if (!this.builtInToJsName.has(type.name)) {
213219
throw new Error(`Missing primitive type to TS type for '${type.name}'`);
@@ -247,6 +253,10 @@ var LibraryEmbind = {
247253
name = readLatin1String(name);
248254
registerType(id, new PrimitiveType(id, name));
249255
},
256+
$registerIntegerType__deps: ['$registerType', '$IntegerType'],
257+
$registerIntegerType: (id) => {
258+
registerType(id, new IntegerType(id));
259+
},
250260
$createFunctionDefinition__deps: ['$FunctionDefinition', '$heap32VectorToArray', '$readLatin1String', '$Argument', '$whenDependentTypesAreResolved'],
251261
$createFunctionDefinition: (name, argCount, rawArgTypesAddr, hasThis, cb) => {
252262
const argTypes = heap32VectorToArray(argCount, rawArgTypesAddr);
@@ -277,9 +287,9 @@ var LibraryEmbind = {
277287
_embind_register_bool: (rawType, name, trueValue, falseValue) => {
278288
registerPrimitiveType(rawType, name);
279289
},
280-
_embind_register_integer__deps: ['$registerPrimitiveType'],
290+
_embind_register_integer__deps: ['$registerIntegerType'],
281291
_embind_register_integer: (primitiveType, name, size, minRange, maxRange) => {
282-
registerPrimitiveType(primitiveType, name);
292+
registerIntegerType(primitiveType, name);
283293
},
284294
_embind_register_bigint: (primitiveType, name, size, minRange, maxRange) => {
285295
registerPrimitiveType(primitiveType, name);
@@ -333,7 +343,7 @@ var LibraryEmbind = {
333343
);
334344

335345
},
336-
_embind_register_class_constructor__deps: ['$whenDependentTypesAreResolved'],
346+
_embind_register_class_constructor__deps: ['$whenDependentTypesAreResolved', '$createFunctionDefinition'],
337347
_embind_register_class_constructor: function(
338348
rawClassType,
339349
argCount,
@@ -428,6 +438,8 @@ var LibraryEmbind = {
428438
return [];
429439
});
430440
},
441+
// Stub function. This is called a when extending an object and not needed for TS generation.
442+
_embind_create_inheriting_constructor: (constructorName, wrapperType, properties) => {},
431443
_embind_register_enum__deps: ['$readLatin1String', '$EnumDefinition', '$moduleDefinitions'],
432444
_embind_register_enum: function(rawType, name, size, isSigned) {
433445
name = readLatin1String(name);
@@ -559,7 +571,7 @@ var LibraryEmbind = {
559571
$embindEmitTypes__postset: 'addOnInit(embindEmitTypes);',
560572
$embindEmitTypes: () => {
561573
for (const typeId in awaitingDependencies) {
562-
throwBindingError(`Missing type definition for '${getTypeName(typeId)}'`);
574+
throwBindingError(`Missing binding for type: '${getTypeName(typeId)}' typeId: ${typeId}`);
563575
}
564576
const printer = new TsPrinter(moduleDefinitions);
565577
printer.print();

test/embind/embind_test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2173,7 +2173,6 @@ EMSCRIPTEN_BINDINGS(tests) {
21732173
function("embind_test_accept_big_class_instance", &embind_test_accept_big_class_instance);
21742174

21752175
class_<UniquePtrToConstructor>("UniquePtrToConstructor")
2176-
.constructor<std::unique_ptr<int>>()
21772176
.function("getValue", &UniquePtrToConstructor::getValue)
21782177
;
21792178

@@ -2669,6 +2668,7 @@ struct BoundClass {
26692668
UnboundClass property;
26702669
};
26712670

2671+
#ifndef SKIP_UNBOUND_TYPES
26722672
EMSCRIPTEN_BINDINGS(incomplete) {
26732673
constant("hasUnboundTypeNames", emscripten::has_unbound_type_names);
26742674

@@ -2694,6 +2694,7 @@ EMSCRIPTEN_BINDINGS(incomplete) {
26942694
.property("property", &BoundClass::property)
26952695
;
26962696
}
2697+
#endif
26972698

26982699
class Noncopyable {
26992700
Noncopyable(const Noncopyable&) = delete;

test/test_other.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,6 +2915,12 @@ def test_embind_tsgen(self):
29152915
self.assertNotExists('out.js')
29162916
self.assertFileContents(test_file('other/embind_tsgen.d.ts'), actual)
29172917

2918+
def test_embind_tsgen_test_embind(self):
2919+
self.run_process([EMCC, test_file('embind/embind_test.cpp'),
2920+
'-lembind', '--embind-emit-tsd', 'embind_tsgen_test_embind.d.ts',
2921+
'-DSKIP_UNBOUND_TYPES']) # TypeScript generation requires all type to be bound.
2922+
self.assertExists('embind_tsgen_test_embind.d.ts')
2923+
29182924
def test_embind_tsgen_val(self):
29192925
# Check that any dependencies from val still works with TS generation enabled.
29202926
self.run_process([EMCC, test_file('other/embind_tsgen_val.cpp'),

0 commit comments

Comments
 (0)