A question about BindingType and fallible conversion

31 views
Skip to first unread message

Søren Gammelmark

unread,
Apr 3, 2025, 2:42:45 PMApr 3
to emscripten-discuss
Hi all,

I came across the BindingType template and tried to use it to convert an integer to my own Id-type. Given for example

class Id {
public:
  explicit Id(int v) : value(v) {}
  int toInt() const { return value; }
private:
  int value;

}

I can add the following template specializations

template <> struct TypeID<Id> {
static constexpr TYPEID get() {
return TypeID<int>::get();
}
};

template <> struct BindingType<Id> {
using WireType = int;
static WireType toWireType(const Id& v) {
return v.toInt();
}
static Id fromWireType(WireType v) {
return Id(v);
}
};

and get a transparent conversion for functions calling/returning Id-types. I have two questions:

1) Is this a supported way to use BindingType? (asking because it is defined in 'Internal')
2) What if the conversion is not infallible? Maybe.I only want to accept numbers larger than 0 or similar. I would like the JS-side to fail with an exception (similar to throwBindingError if I try to pass a null as a string).

I would like something like this be able to deal with JS-native types on the Java-script side and to avoid writing all the boilerplate to convert between the JS-native types and the C++ types.

Hope this makes sense, Any feedback appreciated.
Søren

Eduard Mironenko

unread,
Apr 8, 2025, 6:14:06 AMApr 8
to emscripte...@googlegroups.com
Do we get paid for answering these questions or is there any portal that offers tasks that you can get paid for completing!? 

--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-disc...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/emscripten-discuss/77411b31-6bd3-48f1-a016-09caa81d5651n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages