-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1577 from c410-f3r/getters-setters-ts
Fix getter and setter for TS
- Loading branch information
Showing
4 changed files
with
104 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod custom_section; | ||
mod getters_setters; | ||
mod opt_args_and_ret; | ||
mod simple_fn; | ||
mod simple_struct; |