-
Notifications
You must be signed in to change notification settings - Fork 55
Binding type for dicts #3
Comments
In particular, while one would have the generic ability to create an object (through imported One could imagine a compound argument/return host binding that says "create a dict with A rough sketch as to how this might look in C++ is: // generated from .idl file:
template <class... Field> struct PASS_AS_SCALARS dict { ... };
template <class... Field> dict<FieldT...> make_dict(Field&&...);
struct WEBIDL_DICT_FIELD x { int32_t value; x(int32_t v) : value(v) {} };
struct WEBIDL_DICT_FIELD y { int32_t value; y(int32_t v) : value(v) {} };
template <class... Field> void WEBIDL_IMPORT foo(dict<Field...>);
// called like:
foo(make_dict(x(1), y(2))); assuming |
How would that approach work for optional keys? e.g. you can do |
Would sorted key order still work if we add a key? |
I misunderstood Luke's proposal. Something not clear to me in the spec is that you can import a binding multiple times with different annotations. Luke is suggesting some complex annotation syntax for dictionaries which corresponds to "construct a dictionary, the first argument is set to To handle optional keys, you import the binding multiple times with differing annotations. This has the obvious downside that I can't specify whether a key is missing or not at runtime, but that might just be better off done by the |
@magcius Exactly right. If a @annevk Here we are not relying on any sort of static order; the binding is mapping parameters to (a subset of) dictionary key names. Just as a concrete example, if the dictionary args { a:double, b:double, x:long, y:long, z:double }
void foo(args); you could write the following C++ (as described in my first comment): foo(make_dict(x(1), a(2.0));
foo(make_dict(a(1.0), y(2), b(3.0)); and you'd get two wasm imports: (import "" "foo" (func $foo1 (param i32) (param f64)))
(import "" "foo" (func $foo2 (param f64) (param i32) (param f64))) and each would have a host binding (making up .wat syntax on the fly, so bear with me): (binding $foo1 (dictionary (key "x") (key "a")))
(binding $foo2 (dictionary (key "a") (key "y") (key "b"))) and this would basically capture what in JS you'd write as: foo({x:1, a:2});
foo({a:1, y:2, b:3}); |
Closing as out-of-date: these concepts don't map to the current proposal, which has evolved a lot since this issue was opened. Some relevant discussion continues in #61 |
Generalize Module Types to Module Linking
Passing in dicts is a common Web API pattern.
We should consider making this efficient.
We probably need to take into account the idea that several versions of the set of keys might exist.
The text was updated successfully, but these errors were encountered: