js: Add bindings to Object.keys

This commit is contained in:
Nick Fitzgerald 2018-06-22 14:45:33 -07:00
parent 21fa3beabd
commit eb04d15a65
2 changed files with 34 additions and 0 deletions

View File

@ -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

View File

@ -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()