|
|
|
@ -30,6 +30,28 @@ pub fn to_string(this: Value, _: Value, _: Vec<Value>) -> ResultValue {
|
|
|
|
|
Ok(to_value(format!("{}", primitive_val).to_string())) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Get the character at the supplied index
|
|
|
|
|
// https://tc39.github.io/ecma262/#sec-string.prototype.charat
|
|
|
|
|
pub fn char_at(this: Value, _: Value, args: Vec<Value>) -> ResultValue { |
|
|
|
|
// ^^ represents instance ^^ represents arguments (we only care about the first one in this case)
|
|
|
|
|
// First we get it the actual string a private field stored on the object only the engine has access to.
|
|
|
|
|
// Then we convert it into a Rust String by wrapping it in from_value
|
|
|
|
|
let primitive_val: String = |
|
|
|
|
from_value(this.get_private_field(String::from("PrimitiveValue"))).unwrap(); |
|
|
|
|
let pos = from_value(args[0].clone()).unwrap(); |
|
|
|
|
|
|
|
|
|
// Calling .len() on a string would give the wrong result, as they are bytes not the number of unicode code points
|
|
|
|
|
// Note that this is an O(N) operation (because UTF-8 is complex) while getting the number of bytes is an O(1) operation.
|
|
|
|
|
let length = primitive_val.chars().count(); |
|
|
|
|
|
|
|
|
|
// We should return an empty string is pos is out of range
|
|
|
|
|
if pos > length || pos < 0 as usize { |
|
|
|
|
return Ok(to_value::<String>(String::new())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Ok(to_value::<char>(primitive_val.chars().nth(pos).unwrap())) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Create a new `String` object
|
|
|
|
|
pub fn _create(global: Value) -> Value { |
|
|
|
|
let string = to_value(make_string as NativeFunctionData); |
|
|
|
@ -43,6 +65,7 @@ pub fn _create(global: Value) -> Value {
|
|
|
|
|
set: Gc::new(ValueData::Undefined), |
|
|
|
|
}; |
|
|
|
|
proto.set_prop_slice("length", prop); |
|
|
|
|
proto.set_field_slice("charAt", to_value(char_at as NativeFunctionData)); |
|
|
|
|
proto.set_field_slice("toString", to_value(to_string as NativeFunctionData)); |
|
|
|
|
string.set_field_slice(PROTOTYPE, proto); |
|
|
|
|
string |
|
|
|
|