Port Reflect tests to wasm

This commit is contained in:
Alex Crichton 2018-07-21 20:07:55 -07:00
parent 315b5d848e
commit 59c04e26d4
7 changed files with 220 additions and 613 deletions

View File

@ -112,8 +112,8 @@ extern "C" {
#[wasm_bindgen]
pub fn escape(string: &str) -> JsString;
/// The unescape() function computes a new string in which hexadecimal escape
/// sequences are replaced with the character that it represents. The escape sequences might
/// The unescape() function computes a new string in which hexadecimal escape
/// sequences are replaced with the character that it represents. The escape sequences might
/// be introduced by a function like escape. Usually, decodeURI or decodeURIComponent
/// are preferred over unescape.
///
@ -1859,8 +1859,9 @@ extern "C" {
/// arguments as specified.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/apply
#[wasm_bindgen(static_method_of = Reflect)]
pub fn apply(target: &Function, this_argument: &JsValue, arguments_list: &Array) -> JsValue;
#[wasm_bindgen(static_method_of = Reflect, catch)]
pub fn apply(target: &Function, this_argument: &JsValue, arguments_list: &Array)
-> Result<JsValue, JsValue>;
/// The static `Reflect.construct()` method acts like the new operator, but
/// as a function. It is equivalent to calling `new target(...args)`. It
@ -1891,7 +1892,7 @@ extern "C" {
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/get
#[wasm_bindgen(static_method_of = Reflect)]
pub fn get(target: &Object, key: &JsValue) -> JsValue;
pub fn get(target: &JsValue, key: &JsValue) -> JsValue;
/// The static `Reflect.getOwnPropertyDescriptor()` method is similar to
/// `Object.getOwnPropertyDescriptor()`. It returns a property descriptor
@ -1908,14 +1909,14 @@ extern "C" {
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/getPrototypeOf
#[wasm_bindgen(static_method_of = Reflect, js_name = getPrototypeOf)]
pub fn get_prototype_of(target: &Object) -> Object;
pub fn get_prototype_of(target: &JsValue) -> Object;
/// The static `Reflect.has()` method works like the in operator as a
/// function.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/has
#[wasm_bindgen(static_method_of = Reflect)]
pub fn has(target: &Object, property_key: &JsValue) -> bool;
pub fn has(target: &JsValue, property_key: &JsValue) -> bool;
/// The static `Reflect.isExtensible()` method determines if an object is
/// extensible (whether it can have new properties added to it). It is
@ -1930,7 +1931,7 @@ extern "C" {
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/ownKeys
#[wasm_bindgen(static_method_of = Reflect, js_name = ownKeys)]
pub fn own_keys(target: &Object) -> Array;
pub fn own_keys(target: &JsValue) -> Array;
/// The static `Reflect.preventExtensions()` method prevents new
/// properties from ever being added to an object (i.e. prevents
@ -1946,9 +1947,9 @@ extern "C" {
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/set
#[wasm_bindgen(static_method_of = Reflect)]
pub fn set(target: &Object, property_key: &JsValue, value: &JsValue) -> bool;
pub fn set(target: &JsValue, property_key: &JsValue, value: &JsValue) -> bool;
#[wasm_bindgen(static_method_of = Reflect, js_name = set)]
pub fn set_with_receiver(target: &Object, property_key: &JsValue, value: &JsValue, receiver: &Object) -> bool;
pub fn set_with_receiver(target: &JsValue, property_key: &JsValue, value: &JsValue, receiver: &JsValue) -> bool;
/// The static `Reflect.setPrototypeOf()` method is the same
/// method as `Object.setPrototypeOf()`. It sets the prototype

View File

@ -1,601 +0,0 @@
#![allow(non_snake_case)]
use project;
#[test]
fn apply() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn apply(target: &js_sys::Function, this_argument: &JsValue, arguments_list: &js_sys::Array) -> JsValue {
js_sys::Reflect::apply(target, this_argument, arguments_list)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
assert.equal(wasm.apply("".charAt, "ponies", [3]), "i");
}
"#,
)
.test()
}
#[test]
fn construct() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn construct(target: &js_sys::Function, arguments_list: &js_sys::Array) -> JsValue {
js_sys::Reflect::construct(target, arguments_list)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
class Rectangle {
constructor(x, y){
this.x = x,
this.y = y
}
static eq(x, y) {
return x === y;
}
}
const args = [10, 10];
assert.equal(wasm.construct(Rectangle, args).x, 10);
}
"#,
)
.test()
}
#[test]
fn construct_with_new_target() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn construct_with_new_target(target: &js_sys::Function, arguments_list: &js_sys::Array, new_target: &js_sys::Function) -> JsValue {
js_sys::Reflect::construct_with_new_target(target, arguments_list, new_target)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
class Rectangle {
constructor(x, y){
this.x = x,
this.y = y
}
static eq(x, y) {
return x === y;
}
}
class Rectangle2 {
constructor(x, y){
this.x = x,
this.y = y
}
static eq(x, y) {
return x === y;
}
}
const args = [10, 10];
assert.equal(wasm.construct_with_new_target(Rectangle, args, Rectangle2).x, 10);
}
"#,
)
.test()
}
#[test]
fn define_property() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn define_property(target: &js_sys::Object, property_key: &JsValue, attributes: &js_sys::Object) -> bool {
js_sys::Reflect::define_property(target, property_key, attributes)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const object = {};
assert.equal(wasm.define_property(object, "key", { value: 42}), true)
}
"#,
)
.test()
}
#[test]
fn delete_property() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn delete_property(target: &js_sys::Object, property_key: &JsValue) -> bool {
js_sys::Reflect::delete_property(target, property_key)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const object = {
property: 42
};
wasm.delete_property(object, "property");
assert.equal(object.property, undefined);
const array = [1, 2, 3, 4, 5];
wasm.delete_property(array, 3);
assert.equal(array[3], undefined);
}
"#,
)
.test()
}
#[test]
fn get() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn get(target: &js_sys::Object, property_key: &JsValue) -> JsValue {
js_sys::Reflect::get(target, property_key)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const object = {
property: 42
};
assert.equal(wasm.get(object, "property"), 42);
const array = [1, 2, 3, 4, 5];
assert.equal(wasm.get(array, 3), 4);
}
"#,
)
.test()
}
#[test]
fn get_own_property_descriptor() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn get_own_property_descriptor(target: &js_sys::Object, property_key: &JsValue) -> JsValue {
js_sys::Reflect::get_own_property_descriptor(target, property_key)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const object = {
property: 42
};
assert.equal(wasm.get_own_property_descriptor(object, "property").value, 42);
assert.equal(wasm.get_own_property_descriptor(object, "property1"), undefined);
}
"#,
)
.test()
}
#[test]
fn get_prototype_of() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn get_prototype_of(target: &js_sys::Object) -> js_sys::Object {
js_sys::Reflect::get_prototype_of(target)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const object = {
property: 42
};
const array = [1, 2, 3];
assert.equal(wasm.get_prototype_of(object), Object.prototype);
assert.equal(wasm.get_prototype_of(array), Array.prototype);
}
"#,
)
.test()
}
#[test]
fn has() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn has(target: &js_sys::Object, property_key: &JsValue) -> bool {
js_sys::Reflect::has(target, property_key)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const object = {
property: 42
};
const array = [1, 2, 3, 4]
assert.equal(wasm.has(object, "property"), true);
assert.equal(wasm.has(object, "foo"), false);
assert.equal(wasm.has(array, 3), true);
assert.equal(wasm.has(array, 10), false);
}
"#,
)
.test()
}
#[test]
fn is_extensible() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn is_extensible(target: &js_sys::Object) -> bool {
js_sys::Reflect::is_extensible(target)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const object = {
property: 42
};
assert.equal(wasm.is_extensible(object), true);
Reflect.preventExtensions(object);
assert.equal(wasm.is_extensible(object), false);
const object2 = Object.seal({});
assert.equal(wasm.is_extensible(object2), false);
}
"#,
)
.test()
}
#[test]
fn own_keys() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn own_keys(target: &js_sys::Object) -> js_sys::Array {
js_sys::Reflect::own_keys(target)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const object = {
property: 42
};
const array = [];
assert.equal(wasm.own_keys(object)[0], "property");
assert.equal(wasm.own_keys(array)[0], "length");
}
"#,
)
.test()
}
#[test]
fn prevent_extensions() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn prevent_extensions(target: &js_sys::Object) -> bool {
js_sys::Reflect::prevent_extensions(target)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const object1 = {};
wasm.prevent_extensions(object1);
assert.equal(Reflect.isExtensible(object1), false);
}
"#,
)
.test()
}
#[test]
fn set() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn set(target: &js_sys::Object, property_key: &JsValue, value: &JsValue) -> bool {
js_sys::Reflect::set(target, property_key, value)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const object = {};
const array = [1, 2, 3, 4];
assert.equal(wasm.set(object, "key", "value"), true);
assert.equal(wasm.set(array, 0, 100), true);
assert.equal(Reflect.get(object, "key"), "value");
assert.equal(array[0], 100);
}
"#,
)
.test()
}
#[test]
fn set_with_receiver() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn set_with_receiver(target: &js_sys::Object, property_key: &JsValue, value: &JsValue, receiver: &js_sys::Object) -> bool {
js_sys::Reflect::set_with_receiver(target, property_key, value, receiver)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const object = {};
const array = [1, 2, 3, 4];
assert.equal(wasm.set_with_receiver({}, "key", "value", object), true);
assert.equal(wasm.set_with_receiver([], 0, 100, array), true);
assert.equal(Reflect.get(object, "key"), "value");
assert.equal(array[0], 100);
}
"#,
)
.test()
}
#[test]
fn set_prototype_of() {
project()
.file(
"src/lib.rs",
r#"
#![feature(use_extern_macros)]
extern crate wasm_bindgen;
extern crate js_sys;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn set_prototype_of(target: &js_sys::Object, prototype: &JsValue) -> bool {
js_sys::Reflect::set_prototype_of(target, prototype)
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
const object = {};
assert.equal(wasm.set_prototype_of(object, Object.prototype), true);
assert.equal(Object.getPrototypeOf(object), Object.prototype);
assert.equal(wasm.set_prototype_of(object, null), true);
assert.equal(Object.getPrototypeOf(object), null);
}
"#,
)
.test()
}

View File

@ -11,4 +11,3 @@ fn project() -> project_builder::Project {
// Keep these tests in alphabetical order, just like the imports in `src/js.rs`.
mod ArrayIterator;
mod Reflect;

View File

@ -0,0 +1,25 @@
exports.get_char_at = function() {
return "foo".charAt;
};
exports.Rectangle = class {
constructor(x, y){
this.x = x,
this.y = y
}
static eq(x, y) {
return x === y;
}
};
exports.Rectangle2 = class {
constructor(x, y){
this.x = x,
this.y = y
}
static eq(x, y) {
return x === y;
}
};

View File

@ -0,0 +1,182 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
use js_sys::*;
#[wasm_bindgen(module = "tests/wasm/Reflect.js", version = "*")]
extern {
fn get_char_at() -> Function;
#[wasm_bindgen(js_name = Rectangle)]
static RECTANGLE_CLASS: Function;
#[wasm_bindgen(js_name = Rectangle2)]
static RECTANGLE2_CLASS: Function;
#[derive(Clone)]
type Rectangle;
#[wasm_bindgen(constructor)]
fn new() -> Rectangle;
#[wasm_bindgen(method, getter, structural)]
fn x(this: &Rectangle) -> u32;
#[wasm_bindgen(method, getter, structural, js_name = x)]
fn x_jsval(this: &Rectangle) -> JsValue;
#[wasm_bindgen(method, setter, structural)]
fn set_x(this: &Rectangle, x: u32);
}
#[wasm_bindgen]
extern {
#[wasm_bindgen(js_name = prototype, js_namespace = Object)]
static OBJECT_PROTOTYPE: JsValue;
#[wasm_bindgen(js_name = prototype, js_namespace = Array)]
static ARRAY_PROTOTYPE: JsValue;
type DefinePropertyAttrs;
#[wasm_bindgen(method, setter, structural)]
fn set_value(this: &DefinePropertyAttrs, val: &JsValue);
type PropertyDescriptor;
#[wasm_bindgen(method, getter, structural)]
fn value(this: &PropertyDescriptor) -> JsValue;
}
#[wasm_bindgen_test]
fn apply() {
let args = Array::new();
args.push(3.into());
assert_eq!(Reflect::apply(&get_char_at(), &"ponies".into(), &args).unwrap(), "i");
}
#[wasm_bindgen_test]
fn construct() {
let args = Array::new();
args.push(10.into());
args.push(10.into());
let instance = Reflect::construct(&RECTANGLE_CLASS, &args);
assert_eq!(Rectangle::from(instance).x(), 10);
}
#[wasm_bindgen_test]
fn construct_with_new_target() {
let args = Array::new();
args.push(10.into());
args.push(10.into());
let instance = Reflect::construct_with_new_target(
&RECTANGLE_CLASS,
&args,
&RECTANGLE2_CLASS,
);
assert_eq!(Rectangle::from(instance).x(), 10);
}
#[wasm_bindgen_test]
fn define_property() {
let value = DefinePropertyAttrs::from(JsValue::from(Object::new()));
value.set_value(&42.into());
let obj = Object::from(JsValue::from(value));
assert!(Reflect::define_property(&obj, &"key".into(), &obj));
}
#[wasm_bindgen_test]
fn delete_property() {
let r = Rectangle::new();
r.set_x(10);
let obj = Object::from(JsValue::from(r.clone()));
Reflect::delete_property(&obj, &"x".into());
assert!(r.x_jsval().is_undefined());
let array = Array::new();
array.push(1.into());
let obj = Object::from(JsValue::from(array));
Reflect::delete_property(&obj, &0.into());
let array = Array::from(JsValue::from(obj));
assert!(array.length() == 1);
array.for_each(&mut |x, _, _| assert!(x.is_undefined()));
}
#[wasm_bindgen_test]
fn get() {
let r = Rectangle::new();
r.set_x(10);
let obj = JsValue::from(r.clone());
assert_eq!(Reflect::get(&obj, &"x".into()), 10);
}
#[wasm_bindgen_test]
fn get_own_property_descriptor() {
let r = Rectangle::new();
r.set_x(10);
let obj = Object::from(JsValue::from(r.clone()));
let desc = Reflect::get_own_property_descriptor(&obj, &"x".into());
assert_eq!(PropertyDescriptor::from(desc).value(), 10);
let desc = Reflect::get_own_property_descriptor(&obj, &"foo".into());
assert!(PropertyDescriptor::from(desc).value().is_undefined());
}
#[wasm_bindgen_test]
fn get_prototype_of() {
let proto = JsValue::from(Reflect::get_prototype_of(&Object::new().into()));
assert_eq!(proto, *OBJECT_PROTOTYPE);
let proto = JsValue::from(Reflect::get_prototype_of(&Array::new().into()));
assert_eq!(proto, *ARRAY_PROTOTYPE);
}
#[wasm_bindgen_test]
fn has() {
let obj = JsValue::from(Rectangle::new());
assert!(Reflect::has(&obj, &"x".into()));
assert!(!Reflect::has(&obj, &"foo".into()));
}
#[wasm_bindgen_test]
fn is_extensible() {
let obj = Object::from(JsValue::from(Rectangle::new()));
assert!(Reflect::is_extensible(&obj));
Reflect::prevent_extensions(&obj);
assert!(!Reflect::is_extensible(&obj));
let obj = Object::seal(&Object::new());
assert!(!Reflect::is_extensible(&obj));
}
#[wasm_bindgen_test]
fn own_keys() {
let obj = JsValue::from(Rectangle::new());
let keys = Reflect::own_keys(&obj);
assert!(keys.length() == 2);
keys.for_each(&mut |k, _, _| {
assert!(k == "x" || k == "y");
});
}
#[wasm_bindgen_test]
fn prevent_extensions() {
let obj = Object::new();
Reflect::prevent_extensions(&obj);
assert!(!Reflect::is_extensible(&obj));
}
#[wasm_bindgen_test]
fn set() {
let obj = JsValue::from(Object::new());
assert!(Reflect::set(&obj, &"key".into(), &"value".into()));
assert_eq!(Reflect::get(&obj, &"key".into()), "value");
}
#[wasm_bindgen_test]
fn set_with_receiver() {
let obj1 = JsValue::from(Object::new());
let obj2 = JsValue::from(Object::new());
assert!(Reflect::set_with_receiver(&obj2, &"key".into(), &"value".into(), &obj2));
assert!(Reflect::get(&obj1, &"key".into()).is_undefined());
assert_eq!(Reflect::get(&obj2, &"key".into()), "value");
}
#[wasm_bindgen_test]
fn set_prototype_of() {
let obj = Object::new();
assert!(Reflect::set_prototype_of(&obj, &JsValue::null()));
let obj = JsValue::from(obj);
assert_eq!(JsValue::from(Reflect::get_prototype_of(&obj)), JsValue::null());
}

View File

@ -24,6 +24,7 @@ pub mod Math;
pub mod Number;
pub mod Object;
pub mod Proxy;
pub mod Reflect;
pub mod Set;
pub mod SetIterator;
pub mod Symbol;

View File

@ -20,6 +20,6 @@ xxx_debug_only_print_generated_code = []
[dependencies]
syn = { version = '0.14', features = ['full'] }
quote = '0.6'
proc-macro2 = "0.4.8"
proc-macro2 = "0.4.9"
wasm-bindgen-backend = { path = "../backend", version = "=0.2.12" }
wasm-bindgen-shared = { path = "../shared", version = "=0.2.12" }