Skip to content

Commit 9633642

Browse files
committed
Add abs to Math
1 parent ee31080 commit 9633642

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/js.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ extern {
231231
pub fn name(this: &JsFunction) -> JsString;
232232
}
233233

234+
// Math
235+
#[wasm_bindgen]
236+
extern {
237+
pub type Math;
238+
/// The Math.abs() function returns the absolute value of a number, that is
239+
/// Math.abs(x) = |x|
240+
///
241+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/abs
242+
#[wasm_bindgen(static_method_of = Math)]
243+
pub fn abs(number: i32) -> Number;
244+
}
245+
234246
// Number.
235247
#[wasm_bindgen]
236248
extern {

tests/all/js_globals/Math.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![allow(non_snake_case)]
2+
3+
use super::project;
4+
5+
6+
#[test]
7+
fn abs() {
8+
project()
9+
.file("src/lib.rs", r#"
10+
#![feature(proc_macro, wasm_custom_section)]
11+
12+
extern crate wasm_bindgen;
13+
use wasm_bindgen::prelude::*;
14+
use wasm_bindgen::js;
15+
16+
#[wasm_bindgen]
17+
pub fn abs(number: i32) -> js::Number {
18+
js::Math::abs(number)
19+
}
20+
"#)
21+
.file("test.ts", r#"
22+
import * as assert from "assert";
23+
import * as wasm from "./out";
24+
25+
export function test() {
26+
assert.equal(wasm.abs(-32), Math.abs(-32));
27+
assert.equal(wasm.abs(32), 32));
28+
}
29+
"#)
30+
.test()
31+
}

tests/all/js_globals/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod ArrayIterator;
88
mod JsFunction;
99
mod JsString;
1010
mod Number;
11+
mod Math;
1112

1213
#[test]
1314
#[cfg(feature = "std")]

0 commit comments

Comments
 (0)