Skip to content

Embind fails to bind a pointer property with getter & setter #24225

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

Open
GulgDev opened this issue Apr 30, 2025 · 0 comments · May be fixed by #24239
Open

Embind fails to bind a pointer property with getter & setter #24225

GulgDev opened this issue Apr 30, 2025 · 0 comments · May be fixed by #24239
Assignees

Comments

@GulgDev
Copy link

GulgDev commented Apr 30, 2025

Please include the following in your bug report:

Version of emscripten/emsdk:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 4.0.6 (1ddaae4d2d6dfbb678ecc193bc988820d1fc4633)
clang version 21.0.0git (https:/github.com/llvm/llvm-project 4775e6d9099467df9363e1a3cd5950cc3d2fde05)
Target: wasm32-unknown-emscripten
Thread model: posix
InstalledDir: C:\Users\user\Downloads\emsdk\upstream\bin

Failing command line in full:

C:\Users\user\Desktop\Projects\other\embind-property-repro> emcc main.cpp -lembind
In file included from main.cpp:1:
In file included from C:\Users\user\Downloads\emsdk\upstream\emscripten\cache\sysroot/include\emscripten/bind.h:26:
In file included from C:\Users\user\Downloads\emsdk\upstream\emscripten\cache\sysroot/include\emscripten/val.h:17:
C:\Users\user\Downloads\emsdk\upstream\emscripten\cache\sysroot/include\emscripten/wire.h:118:19: error: static assertion failed due to requirement '!std::is_pointer<Thing *>::value': Implicitly      
      binding raw pointers is illegal. Specify allow_raw_pointer<arg<?>>
  118 |     static_assert(!std::is_pointer<T*>::value, "Implicitly binding raw pointers is illegal.  Specify allow_raw_pointer<arg<?>>");
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
C:\Users\user\Downloads\emsdk\upstream\emscripten\cache\sysroot/include\emscripten/bind.h:1946:13: note: in instantiation of template class 'emscripten::internal::TypeID<Thing *>' requested here      
 1946 |             TypeID<typename GP::ReturnType>::get(),
      |             ^
main.cpp:31:10: note: in instantiation of function template specialization 'emscripten::class_<MyClass>::property<emscripten::internal::DeduceArgumentsTag, Thing *(MyClass::*)() const, void
      (MyClass::*)(Thing *), emscripten::allow_raw_pointers, emscripten::return_value_policy::reference, void>' requested here
   31 |         .property("thing", // error: Implicitly binding raw pointers is illegal.
      |          ^
1 error generated.
emcc: error: 'C:/Users/user/Downloads/emsdk/upstream/bin\clang.exe -target wasm32-unknown-emscripten -fignore-exceptions -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr --sysroot=C:\Users\user\Downloads\emsdk\upstream\emscripten\cache\sysroot -DEMSCRIPTEN -Xclang -iwithsysroot/include\fakesdl -Xclang -iwithsysroot/include\compat -c main.cpp -o C:\Users\user\AppData\Local\Temp\emscripten_temp_nw0nf3r9\main_0.o' failed (returned 1)
#include <emscripten/bind.h>

using namespace emscripten;

class Thing {
public:
    int value;

    Thing() : value(0) {}
};

class MyClass {
public:
    Thing *thing;

    Thing* getThing() const {
        return thing;
    }

    void setThing(Thing* thing) {
        this->thing = thing;
    }
};

EMSCRIPTEN_BINDINGS(Example) {
    class_<Thing>("Thing")
        .constructor()
        .property("value", &Thing::value);

    class_<MyClass>("MyClass")
        .property("thing", // error: Implicitly binding raw pointers is illegal.
                  &MyClass::getThing,
                  &MyClass::setThing,
                  allow_raw_pointers(),
                  return_value_policy::reference());
}

Repro: https://github.com/GulgDev/embind-property-repro

brendandahl added a commit to brendandahl/emscripten that referenced this issue May 1, 2025
…ion.

When using separate getter and setter functions for class properties the
setter function did not have policies applied to it. This caused setters
with raw pointers to fail. For now, use the supplied return value policy
to allow pointers. In the future we should probably add separate polices
for arguments, but that will be a much bigger change.

Note: I've added a lot more testing for properties and return value
polices. This has highlighted a few issues where we seem to be creating
extra copies.

Fixes: emscripten-core#24225
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