Skip to content

bindings for Function.length and Function.name #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,25 @@ extern {
pub fn entries(this: &Array) -> ArrayIterator;
}

// Function
#[wasm_bindgen]
extern {
pub type JsFunction;

/// 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
#[wasm_bindgen(method, getter, structural)]
pub fn length(this: &JsFunction) -> u32;

/// A Function object's read-only name property indicates the function's name as specified when it was created
/// or "anonymous" for functions created anonymously.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
#[wasm_bindgen(method, getter, structural)]
pub fn name(this: &JsFunction) -> String;
}

// Number.
#[wasm_bindgen]
extern {
Expand Down Expand Up @@ -240,7 +259,6 @@ extern {
/// 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.
Expand Down
84 changes: 84 additions & 0 deletions tests/all/js_globals/JsFunction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#![allow(non_snake_case)]

use project;

#[test]
fn length() {
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 fn_length(this: &js::JsFunction) -> u32 {
this.length()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";

export function test() {
assert.equal(0, wasm.fn_length(() => {}));
assert.equal(1, wasm.fn_length((a: string) => console.log(a)));
assert.equal(2, wasm.fn_length((a: string, b: string) => console.log({ a, b })));

function fn0() {}
function fn1(a: string) {
console.log(a);
}
function fn2(a: string, b: string) {
console.log({ a, b });
}

assert.equal(0, wasm.fn_length(fn0));
assert.equal(1, wasm.fn_length(fn1));
assert.equal(2, wasm.fn_length(fn2));
}
"#)
.test()
}

#[test]
fn name() {
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 fn_name(this: &js::JsFunction) -> String {
this.name()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";

export function test() {
function namedFn() {}
assert.equal('namedFn', wasm.fn_name(namedFn));

assert.equal('bound namedFn', wasm.fn_name(namedFn.bind({})));

const obj = {
method: () => {}
}
assert.equal('method', wasm.fn_name(obj.method));

assert.equal('anonymous', wasm.fn_name(new Function()));

assert.equal('', wasm.fn_name(() => {}));

const closure = () => {};
assert.equal('closure', wasm.fn_name(closure));
}
"#)
.test()
}
1 change: 1 addition & 0 deletions tests/all/js_globals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::project;
mod Object;
mod Array;
mod ArrayIterator;
mod JsFunction;
mod JsString;
mod Number;

Expand Down