mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-26 19:45:54 +03:00
Add abs to Math
This commit is contained in:
parent
ee31080b09
commit
9633642e6e
12
src/js.rs
12
src/js.rs
@ -231,6 +231,18 @@ extern {
|
|||||||
pub fn name(this: &JsFunction) -> JsString;
|
pub fn name(this: &JsFunction) -> JsString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Math
|
||||||
|
#[wasm_bindgen]
|
||||||
|
extern {
|
||||||
|
pub type Math;
|
||||||
|
/// The Math.abs() function returns the absolute value of a number, that is
|
||||||
|
/// Math.abs(x) = |x|
|
||||||
|
///
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/abs
|
||||||
|
#[wasm_bindgen(static_method_of = Math)]
|
||||||
|
pub fn abs(number: i32) -> Number;
|
||||||
|
}
|
||||||
|
|
||||||
// Number.
|
// Number.
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
extern {
|
extern {
|
||||||
|
31
tests/all/js_globals/Math.rs
Normal file
31
tests/all/js_globals/Math.rs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
|
use super::project;
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn abs() {
|
||||||
|
project()
|
||||||
|
.file("src/lib.rs", r#"
|
||||||
|
#![feature(proc_macro, wasm_custom_section)]
|
||||||
|
|
||||||
|
extern crate wasm_bindgen;
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn abs(number: i32) -> js::Number {
|
||||||
|
js::Math::abs(number)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.abs(-32), Math.abs(-32));
|
||||||
|
assert.equal(wasm.abs(32), 32));
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
@ -8,6 +8,7 @@ mod ArrayIterator;
|
|||||||
mod JsFunction;
|
mod JsFunction;
|
||||||
mod JsString;
|
mod JsString;
|
||||||
mod Number;
|
mod Number;
|
||||||
|
mod Math;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
Loading…
Reference in New Issue
Block a user