diff --git a/src/lib/libembind_gen.js b/src/lib/libembind_gen.js index c5ee1d30774ae..6be4588fa755d 100644 --- a/src/lib/libembind_gen.js +++ b/src/lib/libembind_gen.js @@ -357,7 +357,7 @@ var LibraryEmbind = { out.push(`export type ${this.name} = {\n`); const outFields = []; for (const {name, type} of this.fields) { - outFields.push(` ${name}: ${nameMap(type)}`); + outFields.push(` ${name}${type instanceof OptionalType ? '?' : ''}: ${nameMap(type)}`); } out.push(outFields.join(',\n')) out.push('\n};\n\n'); diff --git a/test/other/embind_tsgen.cpp b/test/other/embind_tsgen.cpp index cc6dcfe9167ef..dc62482fad4f1 100644 --- a/test/other/embind_tsgen.cpp +++ b/test/other/embind_tsgen.cpp @@ -61,6 +61,7 @@ struct ValObj { Bar bar; std::string string; CallbackType callback; + std::optional optionalInt; ValObj() : callback(val::undefined()) {} }; @@ -209,6 +210,7 @@ EMSCRIPTEN_BINDINGS(Test) { value_object("ValObj") .field("string", &ValObj::string) .field("bar", &ValObj::bar) + .field("optionalInt", &ValObj::optionalInt) .field("callback", &ValObj::callback); function("getValObj", &getValObj); function("setValObj", &setValObj); diff --git a/test/other/embind_tsgen.d.ts b/test/other/embind_tsgen.d.ts index 90558824ff95f..115fa01adcbe2 100644 --- a/test/other/embind_tsgen.d.ts +++ b/test/other/embind_tsgen.d.ts @@ -96,6 +96,7 @@ export type ValArr = [ number, number, number ]; export type ValObj = { string: EmbindString, bar: Bar, + optionalInt?: number | undefined, callback: (message: string) => void }; diff --git a/test/other/embind_tsgen_ignore_1.d.ts b/test/other/embind_tsgen_ignore_1.d.ts index 58fc3931a8157..ddf08832a7f7d 100644 --- a/test/other/embind_tsgen_ignore_1.d.ts +++ b/test/other/embind_tsgen_ignore_1.d.ts @@ -107,6 +107,7 @@ export type ValArr = [ number, number, number ]; export type ValObj = { string: EmbindString, bar: Bar, + optionalInt?: number | undefined, callback: (message: string) => void }; diff --git a/test/other/embind_tsgen_ignore_2.d.ts b/test/other/embind_tsgen_ignore_2.d.ts index 94e114b1bcde6..0df9b9bac0d77 100644 --- a/test/other/embind_tsgen_ignore_2.d.ts +++ b/test/other/embind_tsgen_ignore_2.d.ts @@ -95,6 +95,7 @@ export type ValArr = [ number, number, number ]; export type ValObj = { string: EmbindString, bar: Bar, + optionalInt?: number | undefined, callback: (message: string) => void }; diff --git a/test/other/embind_tsgen_ignore_3.d.ts b/test/other/embind_tsgen_ignore_3.d.ts index 90558824ff95f..115fa01adcbe2 100644 --- a/test/other/embind_tsgen_ignore_3.d.ts +++ b/test/other/embind_tsgen_ignore_3.d.ts @@ -96,6 +96,7 @@ export type ValArr = [ number, number, number ]; export type ValObj = { string: EmbindString, bar: Bar, + optionalInt?: number | undefined, callback: (message: string) => void }; diff --git a/test/other/embind_tsgen_main.ts b/test/other/embind_tsgen_main.ts index 8b4588e85643c..3ea5b7938ec3a 100644 --- a/test/other/embind_tsgen_main.ts +++ b/test/other/embind_tsgen_main.ts @@ -25,6 +25,13 @@ import moduleFactory from './embind_tsgen.js'; callback: () => {} }); + module.setValObj({ + bar: module.Bar.valueOne, + string: "ABCD", + callback: () => {}, + optionalInt: 99 + }); + const valObj = module.getValObj(); // TODO: remove the cast below when better definitions are generated for value // objects. diff --git a/test/other/embind_tsgen_module.d.ts b/test/other/embind_tsgen_module.d.ts index 83c670f84db0c..2c304e8acdcda 100644 --- a/test/other/embind_tsgen_module.d.ts +++ b/test/other/embind_tsgen_module.d.ts @@ -96,6 +96,7 @@ export type ValArr = [ number, number, number ]; export type ValObj = { string: EmbindString, bar: Bar, + optionalInt?: number | undefined, callback: (message: string) => void };