|
| 1 | +# Customizing import behavior |
| 2 | + |
| 3 | +The `#[wasm_bindgen]` macro supports a good amount of configuration for |
| 4 | +controlling precisely how imports are imported and what they map to in JS. This |
| 5 | +section is intended to hopefully be an exhaustive reference of the |
| 6 | +possibilities! |
| 7 | + |
| 8 | +* `catch` - this attribute allows catching a JS exception. This can be attached |
| 9 | + to any imported function and the function must return a `Result` where the |
| 10 | + `Err` payload is a `JsValue`, like so: |
| 11 | + |
| 12 | + ```rust |
| 13 | + #[wasm_bindgen] |
| 14 | + extern { |
| 15 | + #[wasm_bindgen(catch)] |
| 16 | + fn foo() -> Result<(), JsValue>; |
| 17 | + } |
| 18 | + ``` |
| 19 | + |
| 20 | + If the imported function throws an exception then `Err` will be returned with |
| 21 | + the exception that was raised, and otherwise `Ok` is returned with the result |
| 22 | + of the function. |
| 23 | + |
| 24 | + By default `wasm-bindgen` will take no action when wasm calls a JS function |
| 25 | + which ends up throwing an exception. The wasm spec right now doesn't support |
| 26 | + stack unwinding and as a result Rust code **will not execute destructors**. |
| 27 | + This can unfortunately cause memory leaks in Rust right now, but as soon as |
| 28 | + wasm implements catching exceptions we'll be sure to add support as well! |
| 29 | + |
| 30 | +* `constructor` - this is used to indicate that the function being bound should |
| 31 | + actually translate to a `new` constructor in JS. The final argument must be a |
| 32 | + type that's imported from JS, and it's what'll get used in JS: |
| 33 | + |
| 34 | + ```rust |
| 35 | + #[wasm_bindgen] |
| 36 | + extern { |
| 37 | + type Foo; |
| 38 | + #[wasm_bindgen(constructor)] |
| 39 | + fn new() -> Foo; |
| 40 | + } |
| 41 | + ``` |
| 42 | + |
| 43 | + This will attach the `new` function to the `Foo` type (implied by |
| 44 | + `constructor`) and in JS when this function is called it will be equivalent to |
| 45 | + `new Foo()`. |
| 46 | + |
| 47 | +* `method` - this is the gateway to adding methods to imported objects or |
| 48 | + otherwise accessing properties on objects via methods and such. This should be |
| 49 | + done for doing the equivalent of expressions like `foo.bar()` in JS. |
| 50 | + |
| 51 | + ```rust |
| 52 | + #[wasm_bindgen] |
| 53 | + extern { |
| 54 | + type Foo; |
| 55 | + #[wasm_bindgen(method)] |
| 56 | + fn work(this: &Foo); |
| 57 | + } |
| 58 | + ``` |
| 59 | + |
| 60 | + The first argument of a `method` annotation must be a borrowed reference (not |
| 61 | + mutable, shared) to the type that the method is attached to. In this case |
| 62 | + we'll be able to call this method like `foo.work()` in JS (where `foo` has |
| 63 | + type `Foo`). |
| 64 | + |
| 65 | + In JS this invocation will correspond to accessing `Foo.prototype.work` and |
| 66 | + then calling that when the import is called. Note that `method` by default |
| 67 | + implies going through `prototype` to get a function pointer. |
| 68 | + |
| 69 | +* `js_namespace` - this attribute indicates that the JS type is accessed through |
| 70 | + a particular namespace. For example the `WebAssembly.Module` APIs are all |
| 71 | + accessed through the `WebAssembly` namespace. The `js_namespace` can be |
| 72 | + applied to any import and whenever the generated JS attempts to reference a |
| 73 | + name (like a class or function name) it'll be accessed through this namespace. |
| 74 | + |
| 75 | + ```rust |
| 76 | + #[wasm_bindgen] |
| 77 | + extern { |
| 78 | + #[wasm_bindgen(js_namespace = console)] |
| 79 | + fn log(s: &str); |
| 80 | + } |
| 81 | + ``` |
| 82 | + |
| 83 | + This is an example of how to bind `console.log(x)` in Rust. The `log` function |
| 84 | + will be available in the Rust module and will be invoked as `console.log` in |
| 85 | + JS. |
| 86 | + |
| 87 | +* `getter` and `setter` - these two attributes can be combined with `method` to |
| 88 | + indicate that this is a getter or setter method. A `getter`-tagged function by |
| 89 | + default accesses the JS property with the same name as the getter function. A |
| 90 | + `setter`'s function name is currently required to start with "set\_" and the |
| 91 | + property it accesses is the suffix after "set\_". |
| 92 | + |
| 93 | + ```rust |
| 94 | + #[wasm_bindgen] |
| 95 | + extern { |
| 96 | + type Foo; |
| 97 | + #[wasm_bindgen(method, getter)] |
| 98 | + fn property(this: &Foo) -> u32; |
| 99 | + #[wasm_bindgen(method, setter)] |
| 100 | + fn set_property(this: &Foo, val: u32); |
| 101 | + } |
| 102 | + ``` |
| 103 | + |
| 104 | + Here we're importing the `Foo` type and defining the ability to access each |
| 105 | + object's `property` property. The first function here is a getter and will be |
| 106 | + available in Rust as `foo.property()`, and the latter is the setter which is |
| 107 | + accessible as `foo.set_property(2)`. Note that both functions have a `this` |
| 108 | + argument as they're tagged with `method`. |
| 109 | + |
| 110 | + Finally, you can also pass an argument to the `getter` and `setter` |
| 111 | + properties to configure what property is accessed. When the property is |
| 112 | + explicitly specified then there is no restriction on the method name. For |
| 113 | + example the below is equivalent to the above: |
| 114 | + |
| 115 | + ```rust |
| 116 | + #[wasm_bindgen] |
| 117 | + extern { |
| 118 | + type Foo; |
| 119 | + #[wasm_bindgen(method, getter = property)] |
| 120 | + fn assorted_method_name(this: &Foo) -> u32; |
| 121 | + #[wasm_bindgen(method, setter = "property")] |
| 122 | + fn some_other_method_name(this: &Foo, val: u32); |
| 123 | + } |
| 124 | + ``` |
| 125 | + |
| 126 | + Properties in JS are accessed through `Object.getOwnPropertyDescriptor`. Note |
| 127 | + that this typically only works for class-like-defined properties which aren't |
| 128 | + just attached properties on any old object. For accessing any old property on |
| 129 | + an object we can use... |
| 130 | + |
| 131 | +* `structural` - this is a flag to `method` annotations which indicates that the |
| 132 | + method being accessed (or property with getters/setters) should be accessed in |
| 133 | + a structural fashion. For example methods are *not* accessed through |
| 134 | + `prototype` and properties are accessed on the object directly rather than |
| 135 | + through `Object.getOwnPropertyDescriptor`. |
| 136 | + |
| 137 | + ```rust |
| 138 | + #[wasm_bindgen] |
| 139 | + extern { |
| 140 | + type Foo; |
| 141 | + #[wasm_bindgen(method, structural)] |
| 142 | + fn bar(this: &Foo); |
| 143 | + #[wasm_bindgen(method, getter, structural)] |
| 144 | + fn baz(this: &Foo) -> u32; |
| 145 | + } |
| 146 | + ``` |
| 147 | + |
| 148 | + The type here, `Foo`, is not required to exist in JS (it's not referenced). |
| 149 | + Instead wasm-bindgen will generate shims that will access the passed in JS |
| 150 | + value's `bar` property to or the `baz` property (depending on the function). |
| 151 | + |
| 152 | +* `js_name = foo` - this can be used to bind to a different function in JS than |
| 153 | + the identifier that's defined in Rust. For example you can also define |
| 154 | + multiple signatures for a polymorphic function in JS as well: |
| 155 | + |
| 156 | + ```rust |
| 157 | + #[wasm_bindgen] |
| 158 | + extern { |
| 159 | + type Foo; |
| 160 | + #[wasm_bindgen(js_namespace = console, js_name = log)] |
| 161 | + fn log_string(s: &str); |
| 162 | + #[wasm_bindgen(js_namespace = console, js_name = log)] |
| 163 | + fn log_u32(n: u32); |
| 164 | + #[wasm_bindgen(js_namespace = console, js_name = log)] |
| 165 | + fn log_many(a: u32, b: JsValue); |
| 166 | + } |
| 167 | + ``` |
| 168 | + |
| 169 | + All of these functions will call `console.log` in JS, but each identifier |
| 170 | + will have only one signature in Rust. |
0 commit comments