1
0
mirror of https://github.com/fluencelabs/wasm-bindgen synced 2025-06-22 17:21:35 +00:00
Files
.cargo
ci
crates
anyref-xform
backend
cli
cli-support
futures
js-sys
src
tests
wasm
Array.rs
ArrayBuffer.rs
ArrayIterator.rs
Boolean.rs
DataView.rs
Date.rs
Error.rs
EvalError.rs
Function.js
Function.rs
Generator.js
Generator.rs
Intl.rs
Iterator.js
Iterator.rs
JSON.rs
JsString.js
JsString.rs
Map.rs
MapIterator.rs
Math.rs
Number.rs
Object.js
Object.rs
Promise.rs
Proxy.js
Proxy.rs
RangeError.rs
ReferenceError.rs
Reflect.js
Reflect.rs
RegExp.rs
Set.rs
SetIterator.rs
SharedArrayBuffer.js
SharedArrayBuffer.rs
Symbol.js
Symbol.rs
SyntaxError.rs
TypeError.rs
TypedArray.rs
UriError.rs
WeakMap.rs
WeakSet.rs
WebAssembly.js
WebAssembly.rs
global_fns.rs
main.rs
headless.js
headless.rs
CHANGELOG.md
Cargo.toml
README.md
macro
macro-support
shared
test
test-macro
threads-xform
typescript
typescript-tests
wasm-interpreter
web-sys
webidl
webidl-tests
examples
guide
releases
src
tests
.gitattributes
.gitignore
CHANGELOG.md
CONTRIBUTING.md
Cargo.toml
LICENSE-APACHE
LICENSE-MIT
README.md
_package.json
azure-pipelines.yml
build.rs
publish.rs
wasm-bindgen/crates/js-sys/tests/wasm/Function.rs

71 lines
1.8 KiB
Rust
Raw Normal View History

use js_sys::*;
2018-07-20 12:43:07 -07:00
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_test::*;
2018-07-20 12:43:07 -07:00
#[wasm_bindgen]
extern "C" {
2018-07-20 12:43:07 -07:00
#[wasm_bindgen(js_name = max, js_namespace = Math)]
static MAX: Function;
type ArrayPrototype;
#[wasm_bindgen(method, getter, structural)]
pub fn push(this: &ArrayPrototype) -> Function;
#[wasm_bindgen(js_name = prototype, js_namespace = Array)]
static ARRAY_PROTOTYPE2: ArrayPrototype;
2018-07-20 12:43:07 -07:00
}
#[wasm_bindgen_test]
fn apply() {
let args = Array::new();
args.push(&1.into());
args.push(&2.into());
args.push(&3.into());
assert_eq!(MAX.apply(&JsValue::undefined(), &args).unwrap(), 3);
2018-07-20 12:43:07 -07:00
let arr = JsValue::from(Array::new());
let args = Array::new();
args.push(&1.into());
ARRAY_PROTOTYPE2.push().apply(&arr, &args).unwrap();
assert_eq!(Array::from(&arr).length(), 1);
2018-07-20 12:43:07 -07:00
}
#[wasm_bindgen(module = "tests/wasm/Function.js")]
extern "C" {
2018-07-20 12:43:07 -07:00
fn get_function_to_bind() -> Function;
fn get_value_to_bind_to() -> JsValue;
fn call_function(f: Function) -> JsValue;
}
#[wasm_bindgen_test]
fn bind() {
let f = get_function_to_bind();
let new_f = f.bind(&get_value_to_bind_to());
assert_eq!(call_function(f), 1);
assert_eq!(call_function(new_f), 2);
}
#[wasm_bindgen_test]
fn length() {
assert_eq!(MAX.length(), 2);
assert_eq!(ARRAY_PROTOTYPE2.push().length(), 1);
2018-07-20 12:43:07 -07:00
}
#[wasm_bindgen_test]
fn name() {
assert_eq!(JsValue::from(MAX.name()), "max");
assert_eq!(JsValue::from(ARRAY_PROTOTYPE2.push().name()), "push");
2018-07-20 12:43:07 -07:00
}
#[wasm_bindgen_test]
fn to_string() {
assert!(MAX.to_string().length() > 0);
}
#[wasm_bindgen_test]
fn function_inheritance() {
assert!(MAX.is_instance_of::<Function>());
assert!(MAX.is_instance_of::<Object>());
let _: &Object = MAX.as_ref();
}