mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-18 07:11:56 +03:00
695 lines
19 KiB
Rust
695 lines
19 KiB
Rust
#![allow(non_snake_case)]
|
|
|
|
use project;
|
|
|
|
#[test]
|
|
fn apply() {
|
|
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 apply(target: &js::Function, this_argument: &JsValue, arguments_list: &js::Array) -> JsValue {
|
|
let result = js::Reflect::apply(target, this_argument, arguments_list);
|
|
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
r#"
|
|
import * as assert from "assert";
|
|
import * as wasm from "./out";
|
|
|
|
export function test() {
|
|
assert.equal(wasm.apply("".charAt, "ponies", [3]), "i");
|
|
assert.equal(wasm.apply("", "ponies", [3]), "TypeError: Function.prototype.apply was called on , which is a string and not a function");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
}
|
|
|
|
#[test]
|
|
fn construct() {
|
|
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 construct(target: &js::Function, arguments_list: &js::Array) -> JsValue {
|
|
let result = js::Reflect::construct(target, arguments_list);
|
|
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
r#"
|
|
import * as assert from "assert";
|
|
import * as wasm from "./out";
|
|
|
|
export function test() {
|
|
class Rectangle {
|
|
public x: number;
|
|
public y: number;
|
|
|
|
constructor(x: number, y: number){
|
|
this.x = x,
|
|
this.y = y
|
|
}
|
|
|
|
static eq(x: number, y: number) {
|
|
return x === y;
|
|
}
|
|
|
|
}
|
|
|
|
const args = [10, 10];
|
|
|
|
assert.equal(wasm.construct(Rectangle, args).x, 10);
|
|
assert.equal(wasm.construct("", args), "TypeError: is not a constructor");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
}
|
|
|
|
#[test]
|
|
fn construct_with_new_target() {
|
|
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 construct_with_new_target(target: &js::Function, arguments_list: &js::Array, new_target: &js::Function) -> JsValue {
|
|
let result = js::Reflect::construct_with_new_target(target, arguments_list, new_target);
|
|
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
r#"
|
|
import * as assert from "assert";
|
|
import * as wasm from "./out";
|
|
|
|
export function test() {
|
|
class Rectangle {
|
|
public x: number;
|
|
public y: number;
|
|
|
|
constructor(x: number, y: number){
|
|
this.x = x,
|
|
this.y = y
|
|
}
|
|
|
|
static eq(x: number, y: number) {
|
|
return x === y;
|
|
}
|
|
|
|
}
|
|
|
|
class Rectangle2 {
|
|
public x: number;
|
|
public y: number;
|
|
|
|
constructor(x: number, y: number){
|
|
this.x = x,
|
|
this.y = y
|
|
}
|
|
|
|
static eq(x: number, y: number) {
|
|
return x === y;
|
|
}
|
|
|
|
}
|
|
|
|
const args = [10, 10];
|
|
|
|
assert.equal(wasm.construct_with_new_target(Rectangle, args, Rectangle2).x, 10);
|
|
assert.equal(wasm.construct_with_new_target(Rectangle, args, ""), "TypeError: is not a constructor");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
}
|
|
|
|
#[test]
|
|
fn define_property() {
|
|
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 define_property(target: &js::Object, property_key: &JsValue, attributes: &js::Object) -> JsValue {
|
|
let result = js::Reflect::define_property(target, property_key, attributes);
|
|
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
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)
|
|
assert.equal(wasm.define_property("", "key", { value: 42 }), "TypeError: Reflect.defineProperty called on non-object");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
}
|
|
|
|
#[test]
|
|
fn delete_property() {
|
|
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 delete_property(target: &JsValue, property_key: &JsValue) -> JsValue {
|
|
let result = js::Reflect::delete_property(target, property_key);
|
|
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
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);
|
|
|
|
assert.equal(wasm.delete_property("", 3), "TypeError: Reflect.deleteProperty called on non-object");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
}
|
|
|
|
#[test]
|
|
fn get() {
|
|
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 get(target: &JsValue, property_key: &JsValue) -> JsValue {
|
|
let result = js::Reflect::get(target, property_key);
|
|
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
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);
|
|
|
|
assert.equal(wasm.get("", 3), "TypeError: Reflect.get called on non-object");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
}
|
|
|
|
#[test]
|
|
fn get_own_property_descriptor() {
|
|
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 get_own_property_descriptor(target: &JsValue, property_key: &JsValue) -> JsValue {
|
|
let result = js::Reflect::get_own_property_descriptor(target, property_key);
|
|
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
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);
|
|
assert.equal(wasm.get_own_property_descriptor("", "property1"), "TypeError: Reflect.getOwnPropertyDescriptor called on non-object");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
}
|
|
|
|
#[test]
|
|
fn get_prototype_of() {
|
|
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 get_prototype_of(target: &JsValue) -> JsValue {
|
|
let result = js::Reflect::get_prototype_of(target);
|
|
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
r#"
|
|
import * as assert from "assert";
|
|
import * as wasm from "./out";
|
|
|
|
export function test() {
|
|
const object = {
|
|
property: 42
|
|
};
|
|
const array: number[] = [1, 2, 3];
|
|
|
|
assert.equal(wasm.get_prototype_of(object), Object.prototype);
|
|
assert.equal(wasm.get_prototype_of(array), Array.prototype);
|
|
assert.equal(wasm.get_prototype_of(""), "TypeError: Reflect.getPrototypeOf called on non-object");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
}
|
|
|
|
#[test]
|
|
fn has() {
|
|
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 has(target: &js::Object, property_key: &JsValue) -> bool {
|
|
js::Reflect::has(target, property_key)
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
r#"
|
|
import * as assert from "assert";
|
|
import * as wasm from "./out";
|
|
|
|
export function test() {
|
|
const object = {
|
|
property: 42
|
|
};
|
|
const array: number[] = [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(proc_macro, wasm_custom_section)]
|
|
|
|
extern crate wasm_bindgen;
|
|
use wasm_bindgen::prelude::*;
|
|
use wasm_bindgen::js;
|
|
|
|
#[wasm_bindgen]
|
|
pub fn is_extensible(target: &js::Object) -> JsValue {
|
|
let result = js::Reflect::is_extensible(target);
|
|
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
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);
|
|
assert.equal(wasm.is_extensible(""), "TypeError: Reflect.isExtensible called on non-object");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
}
|
|
|
|
#[test]
|
|
fn own_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 own_keys(target: &js::Object) -> JsValue {
|
|
let result = js::Reflect::own_keys(target);
|
|
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
r#"
|
|
import * as assert from "assert";
|
|
import * as wasm from "./out";
|
|
|
|
export function test() {
|
|
const object = {
|
|
property: 42
|
|
};
|
|
const array: number[] = [];
|
|
|
|
assert.equal(wasm.own_keys(object)[0], "property");
|
|
assert.equal(wasm.own_keys(array)[0], "length");
|
|
|
|
assert.equal(wasm.own_keys(""), "TypeError: Reflect.ownKeys called on non-object");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
}
|
|
|
|
#[test]
|
|
fn prevent_extensions() {
|
|
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 prevent_extensions(target: &js::Object) -> JsValue {
|
|
let result = js::Reflect::prevent_extensions(target);
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
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);
|
|
assert.equal(wasm.prevent_extensions(""), "TypeError: Reflect.preventExtensions called on non-object");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
}
|
|
|
|
#[test]
|
|
fn set() {
|
|
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 set(target: &JsValue, property_key: &JsValue, value: &JsValue) -> JsValue {
|
|
let result = js::Reflect::set(target, property_key, value);
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
r#"
|
|
import * as assert from "assert";
|
|
import * as wasm from "./out";
|
|
|
|
export function test() {
|
|
const object = {};
|
|
const array: number[] = [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);
|
|
assert.equal(wasm.set("", "key", "value"), "TypeError: Reflect.set called on non-object");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
}
|
|
|
|
#[test]
|
|
fn set_with_receiver() {
|
|
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 set_with_receiver(target: &JsValue, property_key: &JsValue, value: &JsValue, receiver: &JsValue) -> JsValue {
|
|
let result = js::Reflect::set_with_receiver(target, property_key, value, receiver);
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
r#"
|
|
import * as assert from "assert";
|
|
import * as wasm from "./out";
|
|
|
|
export function test() {
|
|
const object = {};
|
|
const array: number[] = [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);
|
|
assert.equal(wasm.set_with_receiver("", "key", "value", ""), "TypeError: Reflect.set called on non-object");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
}
|
|
|
|
#[test]
|
|
fn set_prototype_of() {
|
|
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 set_prototype_of(target: &JsValue, prototype: &JsValue) -> JsValue {
|
|
let result = js::Reflect::set_prototype_of(target, prototype);
|
|
|
|
match result {
|
|
Ok(val) => val,
|
|
Err(_err) => _err
|
|
}
|
|
}
|
|
"#,
|
|
)
|
|
.file(
|
|
"test.ts",
|
|
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);
|
|
|
|
assert.equal(wasm.set_prototype_of("", Object.prototype), "TypeError: Reflect.setPrototypeOf called on non-object");
|
|
}
|
|
"#,
|
|
)
|
|
.test()
|
|
} |