Add abs to Math

This commit is contained in:
Jonathan Sundqvist 2018-06-25 21:41:28 +02:00
parent ee31080b09
commit 9633642e6e
3 changed files with 44 additions and 0 deletions

View File

@ -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 {

View 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()
}

View File

@ -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")]