From eb04d15a653becf0ed16ca0bbf61b444756a6a80 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Fri, 22 Jun 2018 14:45:33 -0700 Subject: [PATCH] js: Add bindings to `Object.keys` --- src/js.rs | 7 +++++++ tests/all/js_globals/Object.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/js.rs b/src/js.rs index 58d0cdd3..bebe3cb9 100644 --- a/src/js.rs +++ b/src/js.rs @@ -293,6 +293,13 @@ extern { #[wasm_bindgen(method, js_name = isPrototypeOf)] pub fn is_prototype_of(this: &Object, value: &JsValue) -> bool; + /// The Object.keys() method returns an array of a given object's property + /// names, in the same order as we get with a normal loop. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys + #[wasm_bindgen(static_method_of = Object)] + pub fn keys(object: &Object) -> Array; + /// The Object constructor creates an object wrapper. /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object diff --git a/tests/all/js_globals/Object.rs b/tests/all/js_globals/Object.rs index e4fc92e7..2ca6a093 100755 --- a/tests/all/js_globals/Object.rs +++ b/tests/all/js_globals/Object.rs @@ -123,6 +123,33 @@ fn is_prototype_of() { .test() } +#[test] +fn keys() { + 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 keys(obj: &js::Object) -> js::Array { + js::Object::keys(obj) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const obj = { a: 1, b: 2, c: 3 }; + assert.deepStrictEqual(wasm.keys(obj), ["a", "b", "c"]); + } + "#) + .test() +} + #[test] fn property_is_enumerable() { project()