Skip to content

Commit d7d59fd

Browse files
committed
js: Add bindings to Object.keys
1 parent 68072c2 commit d7d59fd

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/js.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ extern {
228228
#[wasm_bindgen(method, js_name = isPrototypeOf)]
229229
pub fn is_prototype_of(this: &Object, value: &JsValue) -> bool;
230230

231+
/// The Object.keys() method returns an array of a given object's property
232+
/// names, in the same order as we get with a normal loop.
233+
///
234+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
235+
#[wasm_bindgen(static_method_of = Object)]
236+
pub fn keys(object: &Object) -> Array;
237+
231238
/// The Object constructor creates an object wrapper.
232239
///
233240
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object

tests/all/js_globals/Object.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,33 @@ fn is_prototype_of() {
123123
.test()
124124
}
125125

126+
#[test]
127+
fn keys() {
128+
project()
129+
.file("src/lib.rs", r#"
130+
#![feature(proc_macro, wasm_custom_section)]
131+
132+
extern crate wasm_bindgen;
133+
use wasm_bindgen::prelude::*;
134+
use wasm_bindgen::js;
135+
136+
#[wasm_bindgen]
137+
pub fn keys(obj: &js::Object) -> js::Array {
138+
js::Object::keys(obj)
139+
}
140+
"#)
141+
.file("test.ts", r#"
142+
import * as assert from "assert";
143+
import * as wasm from "./out";
144+
145+
export function test() {
146+
const obj = { a: 1, b: 2, c: 3 };
147+
assert.deepStrictEqual(wasm.keys(obj), ["a", "b", "c"]);
148+
}
149+
"#)
150+
.test()
151+
}
152+
126153
#[test]
127154
fn property_is_enumerable() {
128155
project()

0 commit comments

Comments
 (0)