bindings for Function.prototype.bind()

This commit is contained in:
Alexander Kryvomaz 2018-07-01 15:44:36 +03:00
parent cf08aee341
commit f850a6fafc
2 changed files with 47 additions and 0 deletions

View File

@ -325,6 +325,13 @@ extern "C" {
#[wasm_bindgen(method)]
pub fn apply(this: &Function, context: &JsValue, args: &Array) -> Function;
/// The bind() method creates a new function that, when called, has its this keyword set to the provided value,
/// with a given sequence of arguments preceding any provided when the new function is called.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
#[wasm_bindgen(method)]
pub fn bind(this: &Function, context: &JsValue) -> Function;
/// The length property indicates the number of arguments expected by the function.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length

View File

@ -38,6 +38,46 @@ fn apply() {
.test()
}
#[test]
fn bind() {
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 bind(this: &js::Function, context: &JsValue) -> js::Function {
this.bind(context)
}
"#,
)
.file(
"test.ts",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const obj = {
a: 0,
fn: function () {
return this.a + 1;
}
}
const boundFn = wasm.bind(obj.fn, { a: 41 });
assert.equal(boundFn(), 42);
}
"#,
)
.test()
}
#[test]
fn length() {
project()