Skip to content

Commit 2b426d4

Browse files
authored
embind: Fix the TypeScript definitions for std::string and std::wstring. (#20815)
- The return type for std::string is always a JS string. - std::wstring always takes in a JS string and returns a JS string. Fixes #20564
1 parent 865bde7 commit 2b426d4

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

src/embind/embind_gen.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var LibraryEmbind = {
5353
argOut.push(`${arg.name}: ${nameMap(arg.type)}`);
5454
}
5555
out.push(argOut.join(', '));
56-
out.push(`): ${nameMap(this.returnType)}`);
56+
out.push(`): ${nameMap(this.returnType, true)}`);
5757
}
5858

5959
printFunction(nameMap, out) {
@@ -298,37 +298,40 @@ var LibraryEmbind = {
298298
constructor(definitions) {
299299
this.definitions = definitions;
300300
const jsString = 'ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string';
301+
// The mapping is in the format of '<c++ name>' => ['toWireType', 'fromWireType']
302+
// or if the to/from wire types are the same use a single element.
301303
this.builtInToJsName = new Map([
302-
['bool', 'boolean'],
303-
['float', 'number'],
304-
['double', 'number'],
304+
['bool', ['boolean']],
305+
['float', ['number']],
306+
['double', ['number']],
305307
#if MEMORY64
306-
['long', 'bigint'],
307-
['unsigned long', 'bigint'],
308+
['long', ['bigint']],
309+
['unsigned long', ['bigint']],
308310
#endif
309311
#if WASM_BIGINT
310-
['int64_t', 'bigint'],
311-
['uint64_t', 'bigint'],
312+
['int64_t', ['bigint']],
313+
['uint64_t', ['bigint']],
312314
#endif
313-
['void', 'void'],
314-
['std::string', jsString],
315-
['std::basic_string<unsigned char>', jsString],
316-
['std::wstring', jsString],
317-
['std::u16string', jsString],
318-
['std::u32string', jsString],
319-
['emscripten::val', 'any'],
315+
['void', ['void']],
316+
['std::string', [jsString, 'string']],
317+
['std::basic_string<unsigned char>', [jsString, 'string']],
318+
['std::wstring', ['string']],
319+
['std::u16string', ['string']],
320+
['std::u32string', ['string']],
321+
['emscripten::val', ['any']],
320322
]);
321323
}
322324

323-
typeToJsName(type) {
325+
typeToJsName(type, isFromWireType = false) {
324326
if (type instanceof IntegerType) {
325327
return 'number';
326328
}
327329
if (type instanceof PrimitiveType) {
328330
if (!this.builtInToJsName.has(type.name)) {
329331
throw new Error(`Missing primitive type to TS type for '${type.name}'`);
330332
}
331-
return this.builtInToJsName.get(type.name)
333+
const [toWireType, fromWireType = toWireType] = this.builtInToJsName.get(type.name);
334+
return isFromWireType ? fromWireType : toWireType;
332335
}
333336
if (type instanceof PointerDefinition) {
334337
return this.typeToJsName(type.classType);

test/other/embind_tsgen.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ int function_with_callback_param(CallbackType ct) {
8989

9090
int global_fn(int, int) { return 0; }
9191

92+
std::string string_test(std::string arg) {
93+
return "hi";
94+
}
95+
96+
std::wstring wstring_test(std::wstring arg) {
97+
return L"hi";
98+
}
99+
92100
class BaseClass {
93101
public:
94102
virtual ~BaseClass() = default;
@@ -156,6 +164,9 @@ EMSCRIPTEN_BINDINGS(Test) {
156164

157165
function("global_fn", &global_fn);
158166

167+
function("string_test", &string_test);
168+
function("wstring_test", &wstring_test);
169+
159170
class_<ClassWithConstructor>("ClassWithConstructor")
160171
.constructor<int, const ValArr&>()
161172
.function("fn", &ClassWithConstructor::fn);

test/other/embind_tsgen.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,6 @@ export interface MainModule {
9191
smart_ptr_function(_0: ClassWithSmartPtrConstructor): number;
9292
smart_ptr_function_with_params(foo: ClassWithSmartPtrConstructor): number;
9393
function_with_callback_param(_0: (message: string) => void): number;
94+
string_test(_0: ArrayBuffer|Uint8Array|Uint8ClampedArray|Int8Array|string): string;
95+
wstring_test(_0: string): string;
9496
}

0 commit comments

Comments
 (0)