1
0
mirror of https://github.com/fluencelabs/wasm-bindgen synced 2025-03-16 18:20:51 +00:00

Merge branch 'master' into master

This commit is contained in:
Nick Fitzgerald 2018-06-23 16:19:08 -07:00 committed by GitHub
commit b1cbb56224
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 198 additions and 1 deletions
src
tests/all/js_globals

@ -227,6 +227,40 @@ extern {
pub fn name(this: &JsFunction) -> String;
}
// Number.
#[wasm_bindgen]
extern {
pub type Number;
/// The toLocaleString() method returns a string with a language sensitive
/// representation of this number.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
#[wasm_bindgen(method, js_name = toLocaleString)]
pub fn to_locale_string(this: &Number, locale: String) -> String;
/// The toPrecision() method returns a string representing the Number
/// object to the specified precision.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision
#[wasm_bindgen(catch, method, js_name = toPrecision)]
pub fn to_precision(this: &Number, precision: u8) -> Result<String, JsValue>;
/// The toString() method returns a string representing the
/// specified Number object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString
#[wasm_bindgen(catch, method, js_name = toString)]
pub fn to_string(this: &Number, radix: u8) -> Result<String, JsValue>;
/// The valueOf() method returns the wrapped primitive value of
/// a Number object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/valueOf
#[wasm_bindgen(method, js_name = valueOf)]
pub fn value_of(this: &Number) -> Number;
}
// Object.
#[wasm_bindgen]
extern {
@ -273,6 +307,13 @@ extern {
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable
#[wasm_bindgen(method, js_name = propertyIsEnumerable)]
pub fn property_is_enumerable(this: &Object, property: &JsValue) -> bool;
/// The valueOf() method returns the primitive value of the
/// specified object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf
#[wasm_bindgen(method, js_name = valueOf)]
pub fn value_of(this: &Object) -> Object;
}
// String
@ -287,4 +328,4 @@ extern {
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
#[wasm_bindgen(method, js_class = "String")]
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
}
}

@ -0,0 +1,127 @@
#![allow(non_snake_case)]
use super::project;
#[test]
fn to_locale_string() {
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 to_locale_string(this: &js::Number, locale: String) -> String {
this.to_locale_string(locale)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let number = 1234.45;
assert.equal(wasm.to_locale_string(number, "de-DE"), "1,234.45");
assert.equal(wasm.to_locale_string(number, "en-US"), "1,234.45");
assert.equal(wasm.to_locale_string(number, "zh-Hans-CN-u-nu-hanidec"), "1,234.45");
}
"#)
.test()
}
#[test]
fn to_precision() {
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 to_precision(this: &js::Number, precision: u8) -> String {
let result = this.to_precision(precision);
let result = match result {
Ok(num) => num,
Err(_err) => "RangeError".to_string()
};
result
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
assert.equal(wasm.to_precision(0.1, 3), "0.100");
assert.equal(wasm.to_precision(10, 101), "RangeError");
}
"#)
.test()
}
#[test]
fn to_string() {
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 to_string(this: &js::Number, radix: u8) -> String {
let result = this.to_string(radix);
let result = match result {
Ok(num) => num,
Err(_err) => "RangeError".to_string()
};
result
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let number = 42;
assert.equal(wasm.to_string(number, 10), "42");
assert.equal(wasm.to_string(233, 16), "e9");
assert.equal(wasm.to_string(number, 100), "RangeError");
}
"#)
.test()
}
#[test]
fn value_of() {
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 js_value_of(this: &js::Number) -> js::Number {
this.value_of()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let number = 42;
assert.equal(wasm.js_value_of(number), 42);
assert.equal(typeof wasm.js_value_of(number), "number");
}
"#)
.test()
}

@ -184,3 +184,31 @@ fn to_locale_string() {
"#)
.test()
}
#[test]
fn value_of() {
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 value_of(obj: &js::Object) -> js::Object {
obj.value_of()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const obj = { foo: 42 };
assert.strictEqual(wasm.value_of(obj), obj);
assert.notStrictEqual(wasm.value_of(obj), { foo: 42 });
}
"#)
.test()
}

@ -7,6 +7,7 @@ mod Array;
mod ArrayIterator;
mod JsFunction;
mod JsString;
mod Number;
#[test]
#[cfg(feature = "std")]