mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-14 05:21:24 +00:00
Fix getter and setter
This commit is contained in:
57
crates/typescript-tests/src/getters_setters.rs
Normal file
57
crates/typescript-tests/src/getters_setters.rs
Normal file
@ -0,0 +1,57 @@
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct ColorWithGetter { r: f64, _g: f64, _b: f64, _a: u8 }
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl ColorWithGetter {
|
||||
#[wasm_bindgen(getter)]
|
||||
pub fn r(&self) -> f64 {
|
||||
self.r
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct ColorWithSetter { r: f64, _g: f64, _b: f64, a: u8 }
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl ColorWithSetter {
|
||||
#[wasm_bindgen(setter)]
|
||||
pub fn set_r(&mut self, r: f64) {
|
||||
self.r = r;
|
||||
self.a = if self.r > 1.0 {
|
||||
255
|
||||
}
|
||||
else if self.r < 0.0 {
|
||||
0
|
||||
}
|
||||
else {
|
||||
(self.r * 255.0) as u8
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct ColorWithGetterAndSetter { r: f64, _g: f64, _b: f64, a: u8 }
|
||||
|
||||
#[wasm_bindgen]
|
||||
impl ColorWithGetterAndSetter {
|
||||
#[wasm_bindgen(getter)]
|
||||
pub fn r(&self) -> f64 {
|
||||
self.r
|
||||
}
|
||||
|
||||
#[wasm_bindgen(setter)]
|
||||
pub fn set_r(&mut self, r: f64) {
|
||||
self.r = r;
|
||||
self.a = if self.r > 1.0 {
|
||||
255
|
||||
}
|
||||
else if self.r < 0.0 {
|
||||
0
|
||||
}
|
||||
else {
|
||||
(self.r * 255.0) as u8
|
||||
};
|
||||
}
|
||||
}
|
11
crates/typescript-tests/src/getters_setters.ts
Normal file
11
crates/typescript-tests/src/getters_setters.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import * as wbg from '../pkg/typescript_tests';
|
||||
|
||||
const colorWithGetter: wbg.ColorWithGetter = new wbg.ColorWithGetter;
|
||||
const _a = colorWithGetter.r;
|
||||
|
||||
const colorWithSetter: wbg.ColorWithSetter = new wbg.ColorWithSetter;
|
||||
colorWithSetter.r = 1;
|
||||
|
||||
const colorWithGetterAndSetter: wbg.ColorWithGetterAndSetter = new wbg.ColorWithGetterAndSetter;
|
||||
colorWithGetterAndSetter.r = 1;
|
||||
const _b = colorWithGetterAndSetter.r;
|
@ -1,4 +1,5 @@
|
||||
mod custom_section;
|
||||
mod getters_setters;
|
||||
mod opt_args_and_ret;
|
||||
mod simple_fn;
|
||||
mod simple_struct;
|
||||
|
Reference in New Issue
Block a user