Browse Source

Fix some panics in String.prototype properties (#888)

* Fix some panics in String.prototype properties

* Address review comments
pull/890/head
George Roman 4 years ago committed by GitHub
parent
commit
f22a1c673b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      boa/src/builtins/string/mod.rs
  2. 28
      boa/src/builtins/string/tests.rs

21
boa/src/builtins/string/mod.rs

@ -189,7 +189,8 @@ impl String {
let primitive_val = this.to_string(ctx)?; let primitive_val = this.to_string(ctx)?;
let pos = args let pos = args
.get(0) .get(0)
.expect("failed to get argument for String method") .cloned()
.unwrap_or_else(Value::undefined)
.to_integer(ctx)? as i32; .to_integer(ctx)? as i32;
// Calling .len() on a string would give the wrong result, as they are bytes not the number of // Calling .len() on a string would give the wrong result, as they are bytes not the number of
@ -235,7 +236,8 @@ impl String {
let length = primitive_val.chars().count(); let length = primitive_val.chars().count();
let pos = args let pos = args
.get(0) .get(0)
.expect("failed to get argument for String method") .cloned()
.unwrap_or_else(Value::undefined)
.to_integer(ctx)? as i32; .to_integer(ctx)? as i32;
if pos >= length as i32 || pos < 0 { if pos >= length as i32 || pos < 0 {
@ -325,20 +327,21 @@ impl String {
// Then we convert it into a Rust String by wrapping it in from_value // Then we convert it into a Rust String by wrapping it in from_value
let primitive_val = this.to_string(ctx)?; let primitive_val = this.to_string(ctx)?;
// 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() as i32;
let start = args let start = args
.get(0) .get(0)
.expect("failed to get argument for String method") .cloned()
.unwrap_or_else(Value::undefined)
.to_integer(ctx)? as i32; .to_integer(ctx)? as i32;
let end = args let end = args
.get(1) .get(1)
.expect("failed to get argument in slice") .cloned()
.unwrap_or_else(|| Value::integer(length))
.to_integer(ctx)? as i32; .to_integer(ctx)? as i32;
// 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() as i32;
let from = if start < 0 { let from = if start < 0 {
max(length.wrapping_add(start), 0) max(length.wrapping_add(start), 0)
} else { } else {

28
boa/src/builtins/string/tests.rs

@ -766,6 +766,34 @@ fn last_index_non_integer_position_argument() {
assert_eq!(forward(&mut engine, "'abcx'.lastIndexOf('x', null)"), "3"); assert_eq!(forward(&mut engine, "'abcx'.lastIndexOf('x', null)"), "3");
} }
#[test]
fn char_at() {
let mut engine = Context::new();
assert_eq!(forward(&mut engine, "'abc'.charAt(1)"), "\"b\"");
assert_eq!(forward(&mut engine, "'abc'.charAt(9)"), "\"\"");
assert_eq!(forward(&mut engine, "'abc'.charAt()"), "\"a\"");
assert_eq!(forward(&mut engine, "'abc'.charAt(null)"), "\"a\"");
}
#[test]
fn char_code_at() {
let mut engine = Context::new();
assert_eq!(forward(&mut engine, "'abc'.charCodeAt(1)"), "98");
assert_eq!(forward(&mut engine, "'abc'.charCodeAt(9)"), "NaN");
assert_eq!(forward(&mut engine, "'abc'.charCodeAt()"), "97");
assert_eq!(forward(&mut engine, "'abc'.charCodeAt(null)"), "97");
}
#[test]
fn slice() {
let mut engine = Context::new();
assert_eq!(forward(&mut engine, "'abc'.slice()"), "\"abc\"");
assert_eq!(forward(&mut engine, "'abc'.slice(1)"), "\"bc\"");
assert_eq!(forward(&mut engine, "'abc'.slice(-1)"), "\"c\"");
assert_eq!(forward(&mut engine, "'abc'.slice(0, 9)"), "\"abc\"");
assert_eq!(forward(&mut engine, "'abc'.slice(9, 10)"), "\"\"");
}
#[test] #[test]
fn empty_iter() { fn empty_iter() {
let mut engine = Context::new(); let mut engine = Context::new();

Loading…
Cancel
Save