Skip to content

Commit eb04d15

Browse files
committed
js: Add bindings to Object.keys
1 parent 21fa3be commit eb04d15

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
@@ -293,6 +293,13 @@ extern {
293293
#[wasm_bindgen(method, js_name = isPrototypeOf)]
294294
pub fn is_prototype_of(this: &Object, value: &JsValue) -> bool;
295295

296+
/// The Object.keys() method returns an array of a given object's property
297+
/// names, in the same order as we get with a normal loop.
298+
///
299+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
300+
#[wasm_bindgen(static_method_of = Object)]
301+
pub fn keys(object: &Object) -> Array;
302+
296303
/// The Object constructor creates an object wrapper.
297304
///
298305
/// 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)