mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-24 18:43:33 +03:00
Implement Array.length binding
This commit is contained in:
parent
9e01e67aa3
commit
a9ca64b689
@ -112,6 +112,13 @@ extern {
|
||||
extern {
|
||||
pub type Array;
|
||||
|
||||
/// The length property of an object which is an instance of type Array sets or returns the number of elements in that array.
|
||||
/// The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length
|
||||
#[wasm_bindgen(method, getter, structural)]
|
||||
pub fn length(this: &Array) -> u32;
|
||||
|
||||
/// The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
|
||||
///
|
||||
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
|
||||
|
@ -422,3 +422,37 @@ fn includes() {
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn length() {
|
||||
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 array_length(this: &js::Array) -> u32 {
|
||||
this.length()
|
||||
}
|
||||
|
||||
"#)
|
||||
.file("test.ts", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let characters = [8, 5, 4, 3, 1, 2]
|
||||
let charactersLength = wasm.array_length(characters);
|
||||
assert.equal(charactersLength, 6);
|
||||
|
||||
var empty : number[] = [];
|
||||
let emptyLength = wasm.array_length(empty);
|
||||
assert.equal(emptyLength, 0);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user