From 3e84b97de2511a760e31624533ad2fcc27abd1d6 Mon Sep 17 00:00:00 2001 From: Kevin Hoffman Date: Thu, 28 Jun 2018 12:46:53 -0400 Subject: [PATCH 1/2] Binding for Math.cos,cosh,exp,expml,fround,imul,log,log10,log1p,log2 --- .gitignore | 1 + src/js.rs | 59 ++++++++ tests/all/js_globals/Math.rs | 282 +++++++++++++++++++++++++++++++++++ 3 files changed, 342 insertions(+) diff --git a/.gitignore b/.gitignore index c4ab28c3..3e7a542c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea /target/ **/*.rs.bk Cargo.lock diff --git a/src/js.rs b/src/js.rs index fd16b23e..9028f640 100644 --- a/src/js.rs +++ b/src/js.rs @@ -387,12 +387,71 @@ extern { #[wasm_bindgen(static_method_of = Math)] pub fn clz32(x: i32) -> Number; + /// The Math.cos() static function returns the cosine of the specified angle, + /// which must be specified in radians. This value is length(adjacent)/length(hypotenuse). + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cos + #[wasm_bindgen(static_method_of = Math)] + pub fn cos(x: f32) -> Number; + + + /// The Math.cosh() function returns the hyperbolic cosine of a number, + /// that can be expressed using the constant e. + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cosh + #[wasm_bindgen(static_method_of = Math)] + pub fn cosh(x: f32) -> Number; + + /// The Math.exp() function returns e^x, where x is the argument, and e is Euler's number + /// (also known as Napier's constant), the base of the natural logarithms. + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/exp + #[wasm_bindgen(static_method_of = Math)] + pub fn exp(x: f32) -> Number; + + /// The Math.expm1() function returns e^x - 1, where x is the argument, and e the base of the + /// natural logarithms. + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/expm1 + #[wasm_bindgen(static_method_of = Math)] + pub fn expm1(x: f32) -> Number; + /// The Math.floor() function returns the largest integer less than or /// equal to a given number. /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor #[wasm_bindgen(static_method_of = Math)] pub fn floor(x: f32) -> Number; + + /// The Math.fround() function returns the nearest 32-bit single precision float representation + /// of a Number. + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround + #[wasm_bindgen(static_method_of = Math)] + pub fn fround(x: f32) -> Number; + + /// The Math.imul() function returns the result of the C-like 32-bit multiplication of the + /// two parameters. + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul + #[wasm_bindgen(static_method_of = Math)] + pub fn imul(x: i32, y: i32) -> Number; + + /// The Math.log() function returns the natural logarithm (base e) of a number. + /// The JavaScript Math.log() function is equivalent to ln(x) in mathematics. + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log + #[wasm_bindgen(static_method_of = Math)] + pub fn log(x: f32) -> Number; + + /// The Math.log10() function returns the base 10 logarithm of a number. + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10 + #[wasm_bindgen(static_method_of = Math)] + pub fn log10(x: f32) -> Number; + + /// The Math.log1p() function returns the natural logarithm (base e) of 1 + a number. + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log1p + #[wasm_bindgen(static_method_of = Math)] + pub fn log1p(x: f32) -> Number; + + /// The Math.log2() function returns the base 2 logarithm of a number. + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2 + #[wasm_bindgen(static_method_of = Math)] + pub fn log2(x: f32) -> Number; + } // Number. diff --git a/tests/all/js_globals/Math.rs b/tests/all/js_globals/Math.rs index df3038f4..e578bf82 100644 --- a/tests/all/js_globals/Math.rs +++ b/tests/all/js_globals/Math.rs @@ -78,6 +78,7 @@ fn acosh() { export function test() { assert.equal(wasm.acosh(1), 0); assert.equal(wasm.acosh(2), Math.acosh(2)); + assert.equal(wasm.acosh(2), Math.acosh(2)); } "#) .test() @@ -293,6 +294,117 @@ fn clz32() { .test() } +#[test] +fn cos() { + 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 cos(x: f32) -> js::Number { + js::Math::cos(x) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.cos(0), 1); + // other assertions failing due to rounding errors + } + "#) + .test() +} + +#[test] +fn cosh() { + 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 cosh(x: f32) -> js::Number { + js::Math::cosh(x) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.cosh(0), 1); + assert.equal(wasm.cosh(2), 3.7621956910836314); + } + "#) + .test() +} + +#[test] +fn exp() { + 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 exp(x: f32) -> js::Number { + js::Math::exp(x) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.exp(0), 1); + assert.equal(wasm.exp(-1), 0.36787944117144233); + assert.equal(wasm.exp(2), 7.38905609893065); + } + "#) + .test() +} + +#[test] +fn expm1() { + 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 expm1(x: f32) -> js::Number { + js::Math::expm1(x) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.expm1(0), 0); + assert.equal(wasm.expm1(1), 1.718281828459045); + assert.equal(wasm.expm1(-1), -0.6321205588285577); + assert.equal(wasm.expm1(2), 6.38905609893065); + } + "#) + .test() +} + #[test] fn floor() { project() @@ -319,3 +431,173 @@ fn floor() { "#) .test() } + +#[test] +fn fround() { + 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 fround(x: f32) -> js::Number { + js::Math::fround(x) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.fround(5.5), 5.5); + assert.equal(wasm.fround(5.05), 5.050000190734863); + assert.equal(wasm.fround(5), 5); + assert.equal(wasm.fround(-5.05), -5.050000190734863); + } + "#) + .test() +} + +#[test] +fn imul() { + 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 imul(x: i32, y:i32) -> js::Number { + js::Math::imul(x, y) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.imul(3, 4), 12); + assert.equal(wasm.imul(-5, 12), -60); + assert.equal(wasm.imul(0xffffffff, 5), Math.imul(0xffffffff, 5)); + } + "#) + .test() +} + +#[test] +fn log() { + 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 log(x: f32) -> js::Number { + js::Math::log(x) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.log(8) / wasm.log(2), 3); + assert.equal(wasm.log(625) / wasm.log(5), 4); + } + "#) + .test() +} + +#[test] +fn log10() { + 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 log10(x: f32) -> js::Number { + js::Math::log10(x) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.log10(100000), 5); + assert.equal(wasm.log10(1), 0); + assert.equal(wasm.log10(2), 0.3010299956639812); + } + "#) + .test() +} + +#[test] +fn log1p() { + 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 log1p(x: f32) -> js::Number { + js::Math::log1p(x) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.log1p(1), 0.6931471805599453); + assert.equal(wasm.log1p(0), 0); + assert.equal(wasm.log1p(-1), -Infinity); + assert(isNaN(wasm.log1p(-2))); + } + "#) + .test() +} + +#[test] +fn log2() { + 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 log2(x: f32) -> js::Number { + js::Math::log2(x) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.log2(3), 1.584962500721156); + assert.equal(wasm.log2(2), 1); + assert.equal(wasm.log2(1), 0); + assert.equal(wasm.log2(0), -Infinity); + } + "#) + .test() +} \ No newline at end of file From 81e68517f26f34ee8d2dd52aa2bf76e166855f37 Mon Sep 17 00:00:00 2001 From: Kevin Hoffman Date: Thu, 28 Jun 2018 15:05:10 -0400 Subject: [PATCH 2/2] Adding line separator in code docs above MDN URLs. --- src/js.rs | 11 ++++++++++- tests/all/js_globals/Math.rs | 3 +-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/js.rs b/src/js.rs index 9028f640..61ca9d52 100644 --- a/src/js.rs +++ b/src/js.rs @@ -389,6 +389,7 @@ extern { /// The Math.cos() static function returns the cosine of the specified angle, /// which must be specified in radians. This value is length(adjacent)/length(hypotenuse). + /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cos #[wasm_bindgen(static_method_of = Math)] pub fn cos(x: f32) -> Number; @@ -396,18 +397,21 @@ extern { /// The Math.cosh() function returns the hyperbolic cosine of a number, /// that can be expressed using the constant e. + /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/cosh #[wasm_bindgen(static_method_of = Math)] pub fn cosh(x: f32) -> Number; /// The Math.exp() function returns e^x, where x is the argument, and e is Euler's number /// (also known as Napier's constant), the base of the natural logarithms. + /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/exp #[wasm_bindgen(static_method_of = Math)] pub fn exp(x: f32) -> Number; /// The Math.expm1() function returns e^x - 1, where x is the argument, and e the base of the /// natural logarithms. + /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/expm1 #[wasm_bindgen(static_method_of = Math)] pub fn expm1(x: f32) -> Number; @@ -421,23 +425,27 @@ extern { /// The Math.fround() function returns the nearest 32-bit single precision float representation /// of a Number. + /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround #[wasm_bindgen(static_method_of = Math)] pub fn fround(x: f32) -> Number; - + /// The Math.imul() function returns the result of the C-like 32-bit multiplication of the /// two parameters. + /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul #[wasm_bindgen(static_method_of = Math)] pub fn imul(x: i32, y: i32) -> Number; /// The Math.log() function returns the natural logarithm (base e) of a number. /// The JavaScript Math.log() function is equivalent to ln(x) in mathematics. + /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log #[wasm_bindgen(static_method_of = Math)] pub fn log(x: f32) -> Number; /// The Math.log10() function returns the base 10 logarithm of a number. + /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10 #[wasm_bindgen(static_method_of = Math)] pub fn log10(x: f32) -> Number; @@ -448,6 +456,7 @@ extern { pub fn log1p(x: f32) -> Number; /// The Math.log2() function returns the base 2 logarithm of a number. + /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log2 #[wasm_bindgen(static_method_of = Math)] pub fn log2(x: f32) -> Number; diff --git a/tests/all/js_globals/Math.rs b/tests/all/js_globals/Math.rs index e578bf82..dc19c122 100644 --- a/tests/all/js_globals/Math.rs +++ b/tests/all/js_globals/Math.rs @@ -78,7 +78,6 @@ fn acosh() { export function test() { assert.equal(wasm.acosh(1), 0); assert.equal(wasm.acosh(2), Math.acosh(2)); - assert.equal(wasm.acosh(2), Math.acosh(2)); } "#) .test() @@ -315,7 +314,7 @@ fn cos() { export function test() { assert.equal(wasm.cos(0), 1); - // other assertions failing due to rounding errors + assert.equal(wasm.cos(1.5), Math.cos(1.5)); } "#) .test()