From 9633642e6ed7ff494b0b610cdd62dc9eea13c5b3 Mon Sep 17 00:00:00 2001 From: Jonathan Sundqvist Date: Mon, 25 Jun 2018 21:41:28 +0200 Subject: [PATCH] Add abs to Math --- src/js.rs | 12 ++++++++++++ tests/all/js_globals/Math.rs | 31 +++++++++++++++++++++++++++++++ tests/all/js_globals/mod.rs | 1 + 3 files changed, 44 insertions(+) create mode 100644 tests/all/js_globals/Math.rs diff --git a/src/js.rs b/src/js.rs index f82573c63..e50b3b403 100644 --- a/src/js.rs +++ b/src/js.rs @@ -231,6 +231,18 @@ extern { 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. #[wasm_bindgen] extern { diff --git a/tests/all/js_globals/Math.rs b/tests/all/js_globals/Math.rs new file mode 100644 index 000000000..98992f457 --- /dev/null +++ b/tests/all/js_globals/Math.rs @@ -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() +} diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index a5d242466..3fd54d808 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -8,6 +8,7 @@ mod ArrayIterator; mod JsFunction; mod JsString; mod Number; +mod Math; #[test] #[cfg(feature = "std")]