Skip to content

Commit 9e07c49

Browse files
committed
bindings for Function.length and Function.name
1 parent d79f982 commit 9e07c49

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

src/js.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,25 @@ extern {
208208
pub fn entries(this: &Array) -> ArrayIterator;
209209
}
210210

211+
// Function
212+
#[wasm_bindgen]
213+
extern {
214+
pub type JsFunction;
215+
216+
/// The length property indicates the number of arguments expected by the function.
217+
///
218+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length
219+
#[wasm_bindgen(method, getter, structural)]
220+
pub fn length(this: &JsFunction) -> u32;
221+
222+
/// A Function object's read-only name property indicates the function's name as specified when it was created
223+
/// or "anonymous" for functions created anonymously.
224+
///
225+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name
226+
#[wasm_bindgen(method, getter, structural)]
227+
pub fn name(this: &JsFunction) -> String;
228+
}
229+
211230
// Object.
212231
#[wasm_bindgen]
213232
extern {

tests/all/js_globals/JsFunction.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#![allow(non_snake_case)]
2+
3+
use project;
4+
5+
#[test]
6+
fn length() {
7+
project()
8+
.file("src/lib.rs", r#"
9+
#![feature(proc_macro, wasm_custom_section)]
10+
11+
extern crate wasm_bindgen;
12+
use wasm_bindgen::prelude::*;
13+
use wasm_bindgen::js;
14+
15+
#[wasm_bindgen]
16+
pub fn fn_length(this: &js::JsFunction) -> u32 {
17+
this.length()
18+
}
19+
"#)
20+
.file("test.ts", r#"
21+
import * as assert from "assert";
22+
import * as wasm from "./out";
23+
24+
export function test() {
25+
assert.equal(0, wasm.fn_length(() => {}));
26+
assert.equal(1, wasm.fn_length((a: string) => console.log(a)));
27+
assert.equal(2, wasm.fn_length((a: string, b: string) => console.log({ a, b })));
28+
29+
function fn0() {}
30+
function fn1(a: string) {
31+
console.log(a);
32+
}
33+
function fn2(a: string, b: string) {
34+
console.log({ a, b });
35+
}
36+
37+
assert.equal(0, wasm.fn_length(fn0));
38+
assert.equal(1, wasm.fn_length(fn1));
39+
assert.equal(2, wasm.fn_length(fn2));
40+
}
41+
"#)
42+
.test()
43+
}
44+
45+
#[test]
46+
fn name() {
47+
project()
48+
.file("src/lib.rs", r#"
49+
#![feature(proc_macro, wasm_custom_section)]
50+
51+
extern crate wasm_bindgen;
52+
use wasm_bindgen::prelude::*;
53+
use wasm_bindgen::js;
54+
55+
#[wasm_bindgen]
56+
pub fn fn_name(this: &js::JsFunction) -> String {
57+
this.name()
58+
}
59+
"#)
60+
.file("test.ts", r#"
61+
import * as assert from "assert";
62+
import * as wasm from "./out";
63+
64+
export function test() {
65+
function namedFn() {}
66+
assert.equal('namedFn', wasm.fn_name(namedFn));
67+
68+
assert.equal('bound namedFn', wasm.fn_name(namedFn.bind({})));
69+
70+
const obj = {
71+
method: () => {}
72+
}
73+
assert.equal('method', wasm.fn_name(obj.method));
74+
75+
assert.equal('anonymous', wasm.fn_name(new Function()));
76+
77+
assert.equal('', wasm.fn_name(() => {}));
78+
79+
const closure = () => {};
80+
assert.equal('closure', wasm.fn_name(closure));
81+
}
82+
"#)
83+
.test()
84+
}

tests/all/js_globals/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::project;
55
mod Object;
66
mod Array;
77
mod ArrayIterator;
8+
mod JsFunction;
89
mod JsString;
910

1011
#[test]

0 commit comments

Comments
 (0)