Add basic support for String.prototype.charAt() (#306)

* String - charAt() implementation

* String - charAt() - add js_class
This commit is contained in:
Lachezar Lechev 2018-06-25 20:24:44 +02:00 committed by Nick Fitzgerald
parent 245f0f0eea
commit d28d81f38d
2 changed files with 36 additions and 0 deletions

View File

@ -347,6 +347,13 @@ extern {
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
#[wasm_bindgen(method, js_class = "String")]
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
/// The String object's charAt() method returns a new string consisting of the single
/// UTF-16 code unit located at the specified offset into the string.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt
#[wasm_bindgen(method, js_class = "String", js_name = charAt)]
pub fn char_at(this: &JsString, index: u32) -> JsString;
}
impl<'a> From<&'a str> for JsString {

View File

@ -2,6 +2,35 @@
use project;
#[test]
fn char_at() {
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 string_char_at(this: &js::JsString, index: u32) -> js::JsString {
this.char_at(index)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
var anyString = 'Brave new world';
export function test() {
assert.equal(wasm.string_char_at(anyString, 0), "B");
assert.equal(wasm.string_char_at(anyString, 999), "");
}
"#)
.test()
}
#[test]
fn slice() {
project()