Skip to content

Commit 96d333a

Browse files
authored
Merge pull request #1577 from c410-f3r/getters-setters-ts
Fix getter and setter for TS
2 parents cf2a42c + e7e8ae1 commit 96d333a

File tree

4 files changed

+104
-22
lines changed

4 files changed

+104
-22
lines changed

crates/cli-support/src/js/mod.rs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -601,15 +601,16 @@ impl<'a> Context<'a> {
601601
},
602602
wasm_bindgen_shared::free_function(&name),
603603
));
604-
ts_dst.push_str(" free(): void;");
604+
ts_dst.push_str(" free(): void;\n");
605605
dst.push_str(&class.contents);
606606
ts_dst.push_str(&class.typescript);
607607

608608
let mut fields = class.typescript_fields.keys().collect::<Vec<_>>();
609609
fields.sort(); // make sure we have deterministic output
610610
for name in fields {
611-
let (ty, readonly) = &class.typescript_fields[name];
612-
if *readonly {
611+
let (ty, has_setter) = &class.typescript_fields[name];
612+
ts_dst.push_str(" ");
613+
if !has_setter {
613614
ts_dst.push_str("readonly ");
614615
}
615616
ts_dst.push_str(name);
@@ -1890,20 +1891,24 @@ impl<'a> Context<'a> {
18901891
let (js, ts, raw_docs) = j2r
18911892
.process(&descriptor, &export.arg_names)?
18921893
.finish("", &format!("wasm.{}", wasm_name));
1893-
let ret_ty = j2r.ret_ty.clone();
1894-
let exported = require_class(&mut self.exported_classes, class);
18951894
let docs = format_doc_comments(&export.comments, Some(raw_docs));
18961895
match export.kind {
18971896
AuxExportKind::Getter { .. } => {
1898-
exported.push_field(&docs, name, &js, Some(&ret_ty), true);
1897+
let ret_ty = j2r.ret_ty.clone();
1898+
let exported = require_class(&mut self.exported_classes, class);
1899+
exported.push_getter(&docs, name, &js, &ret_ty);
18991900
}
19001901
AuxExportKind::Setter { .. } => {
1901-
exported.push_field(&docs, name, &js, None, false);
1902+
let arg_ty = &j2r.js_arguments[0].type_.clone();
1903+
let exported = require_class(&mut self.exported_classes, class);
1904+
exported.push_setter(&docs, name, &js, &arg_ty);
19021905
}
19031906
AuxExportKind::StaticFunction { .. } => {
1907+
let exported = require_class(&mut self.exported_classes, class);
19041908
exported.push(&docs, name, "static ", &js, &ts);
19051909
}
19061910
_ => {
1911+
let exported = require_class(&mut self.exported_classes, class);
19071912
exported.push(&docs, name, "", &js, &ts);
19081913
}
19091914
}
@@ -2155,30 +2160,38 @@ impl ExportedClass {
21552160
self.typescript.push_str("\n");
21562161
}
21572162

2158-
/// Used for adding a field to a class, mainly to ensure that TypeScript
2163+
/// Used for adding a getter to a class, mainly to ensure that TypeScript
21592164
/// generation is handled specially.
2160-
///
2161-
/// Note that the `ts` is optional and it's expected to just be the field
2162-
/// type, not the full signature. It's currently only available on getters,
2163-
/// but there currently has to always be at least a getter.
2164-
fn push_field(&mut self, docs: &str, field: &str, js: &str, ts: Option<&str>, getter: bool) {
2165+
fn push_getter(&mut self, docs: &str, field: &str, js: &str, ret_ty: &str) {
2166+
self.push_accessor(docs, field, js, "get ", ret_ty);
2167+
}
2168+
2169+
/// Used for adding a setter to a class, mainly to ensure that TypeScript
2170+
/// generation is handled specially.
2171+
fn push_setter(&mut self, docs: &str, field: &str, js: &str, ret_ty: &str) {
2172+
let has_setter = self.push_accessor(docs, field, js, "set ", ret_ty);
2173+
*has_setter = true;
2174+
}
2175+
2176+
fn push_accessor(
2177+
&mut self,
2178+
docs: &str,
2179+
field: &str,
2180+
js: &str,
2181+
prefix: &str,
2182+
ret_ty: &str
2183+
) -> &mut bool {
21652184
self.contents.push_str(docs);
2166-
if getter {
2167-
self.contents.push_str("get ");
2168-
} else {
2169-
self.contents.push_str("set ");
2170-
}
2185+
self.contents.push_str(prefix);
21712186
self.contents.push_str(field);
21722187
self.contents.push_str(js);
21732188
self.contents.push_str("\n");
21742189
let (ty, has_setter) = self
21752190
.typescript_fields
21762191
.entry(field.to_string())
21772192
.or_insert_with(Default::default);
2178-
if let Some(ts) = ts {
2179-
*ty = ts.to_string();
2180-
}
2181-
*has_setter = *has_setter || !getter;
2193+
*ty = ret_ty.to_string();
2194+
has_setter
21822195
}
21832196
}
21842197

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
#[wasm_bindgen]
4+
pub struct ColorWithGetter { r: f64, _g: f64, _b: f64, _a: u8 }
5+
6+
#[wasm_bindgen]
7+
impl ColorWithGetter {
8+
#[wasm_bindgen(getter)]
9+
pub fn r(&self) -> f64 {
10+
self.r
11+
}
12+
}
13+
14+
#[wasm_bindgen]
15+
pub struct ColorWithSetter { r: f64, _g: f64, _b: f64, a: u8 }
16+
17+
#[wasm_bindgen]
18+
impl ColorWithSetter {
19+
#[wasm_bindgen(setter)]
20+
pub fn set_r(&mut self, r: f64) {
21+
self.r = r;
22+
self.a = if self.r > 1.0 {
23+
255
24+
}
25+
else if self.r < 0.0 {
26+
0
27+
}
28+
else {
29+
(self.r * 255.0) as u8
30+
};
31+
}
32+
}
33+
34+
#[wasm_bindgen]
35+
pub struct ColorWithGetterAndSetter { r: f64, _g: f64, _b: f64, a: u8 }
36+
37+
#[wasm_bindgen]
38+
impl ColorWithGetterAndSetter {
39+
#[wasm_bindgen(getter)]
40+
pub fn r(&self) -> f64 {
41+
self.r
42+
}
43+
44+
#[wasm_bindgen(setter)]
45+
pub fn set_r(&mut self, r: f64) {
46+
self.r = r;
47+
self.a = if self.r > 1.0 {
48+
255
49+
}
50+
else if self.r < 0.0 {
51+
0
52+
}
53+
else {
54+
(self.r * 255.0) as u8
55+
};
56+
}
57+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as wbg from '../pkg/typescript_tests';
2+
3+
const colorWithGetter: wbg.ColorWithGetter = new wbg.ColorWithGetter;
4+
const _a = colorWithGetter.r;
5+
6+
const colorWithSetter: wbg.ColorWithSetter = new wbg.ColorWithSetter;
7+
colorWithSetter.r = 1;
8+
9+
const colorWithGetterAndSetter: wbg.ColorWithGetterAndSetter = new wbg.ColorWithGetterAndSetter;
10+
colorWithGetterAndSetter.r = 1;
11+
const _b = colorWithGetterAndSetter.r;

crates/typescript-tests/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod custom_section;
2+
mod getters_setters;
23
mod opt_args_and_ret;
34
mod simple_fn;
45
mod simple_struct;

0 commit comments

Comments
 (0)