mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-31 17:31:06 +00:00
feat(js) Implement Boolean
bindings.
Cf https://github.com/rustwasm/wasm-bindgen/issues/275.
This commit is contained in:
parent
c16b9a903c
commit
91bc7a199c
18
src/js.rs
18
src/js.rs
@ -243,6 +243,24 @@ extern {
|
|||||||
pub fn entries(this: &Array) -> ArrayIterator;
|
pub fn entries(this: &Array) -> ArrayIterator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Boolean
|
||||||
|
#[wasm_bindgen]
|
||||||
|
extern {
|
||||||
|
pub type Boolean;
|
||||||
|
|
||||||
|
/// The `Boolean()` constructor creates an object wrapper for a boolean value.
|
||||||
|
///
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
|
||||||
|
#[wasm_bindgen(constructor)]
|
||||||
|
pub fn new(value: JsValue) -> Boolean;
|
||||||
|
|
||||||
|
/// The `valueOf()` method returns the primitive value of a `Boolean` object.
|
||||||
|
///
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/valueOf
|
||||||
|
#[wasm_bindgen(method, js_name = valueOf)]
|
||||||
|
pub fn value_of(this: &Boolean) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
// Function
|
// Function
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
extern {
|
extern {
|
||||||
|
55
tests/all/js_globals/Boolean.rs
Normal file
55
tests/all/js_globals/Boolean.rs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
|
use project;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn new_undefined() {
|
||||||
|
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 new_boolean() -> js::Boolean {
|
||||||
|
js::Boolean::new(JsValue::undefined())
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.new_boolean().valueOf(), false);
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn new_truely() {
|
||||||
|
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 new_boolean() -> js::Boolean {
|
||||||
|
js::Boolean::new(JsValue::from("foo"))
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.new_boolean().valueOf(), true);
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
@ -4,6 +4,7 @@ use super::project;
|
|||||||
|
|
||||||
mod Array;
|
mod Array;
|
||||||
mod ArrayIterator;
|
mod ArrayIterator;
|
||||||
|
mod Boolean;
|
||||||
mod Date;
|
mod Date;
|
||||||
mod Function;
|
mod Function;
|
||||||
mod JsString;
|
mod JsString;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user