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

--emit-tsd to generate nullable types for pointer types #22182

Open
schultezb opened this issue Jul 3, 2024 · 0 comments · May be fixed by #22184
Open

--emit-tsd to generate nullable types for pointer types #22182

schultezb opened this issue Jul 3, 2024 · 0 comments · May be fixed by #22184
Assignees

Comments

@schultezb
Copy link

When binding functions that return pointers (either raw or smart pointers) to a type or accept them as a argument, those values can be nullptr on the c++ side. We think that the TS bindings generated through --emit-tsd should respect this. For instance:

struct Foo {};
Foo* foo1() { return new Foo(); };
void foo2(Foo* foo) {};

EMSCRIPTEN_BINDINGS(Bar)
{
	em::class_<Foo>("Foo");
	em::function("foo1", &foo1, em::allow_raw_pointers());
	em::function("foo2", &foo2, em::allow_raw_pointers());
}

will emit the following TS bindings:

export interface Foo {
  delete(): void;
}
interface EmbindModule {
  Foo: {};
  foo1(): Foo;
  foo2(_0: Foo): void;
}

however I would have expected

  foo1(): Foo | null;
  foo2(_0: Foo | null): void;

since C++'s nullptr is a valid input/output for foo1/2() and therefore the same counts for the JavaScript side.

Am I missing something?

Note that using std::optional<Foo> is not really an option because Foo is not necessarily a value type that can be copied around/easily wrapped into an optional.

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.62 (34c1aa36052b1882058f22aa1916437ba0872690)
clang version 19.0.0git (https:/github.com/llvm/llvm-project c00ada070207979f092be9046a02fcfff8b9f9ce)
Target: wasm32-unknown-emscripten
Thread model: posix
@brendandahl brendandahl linked a pull request Jul 3, 2024 that will close this issue
brendandahl added a commit to brendandahl/emscripten that referenced this issue Jul 3, 2024
Pointers should be allowed to be null in the TS definitions.

Fixes emscripten-core#22182
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants