Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some panics in String.prototype properties #888

Merged
merged 2 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions boa/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ impl String {
let primitive_val = this.to_string(ctx)?;
let pos = args
.get(0)
.expect("failed to get argument for String method")
.cloned()
.unwrap_or_else(Value::undefined)
.to_integer(ctx)? as i32;

// Calling .len() on a string would give the wrong result, as they are bytes not the number of
Expand Down Expand Up @@ -235,7 +236,8 @@ impl String {
let length = primitive_val.chars().count();
let pos = args
.get(0)
.expect("failed to get argument for String method")
.cloned()
.unwrap_or_else(Value::undefined)
.to_integer(ctx)? as i32;

if pos >= length as i32 || pos < 0 {
Expand Down Expand Up @@ -325,20 +327,21 @@ impl String {
// Then we convert it into a Rust String by wrapping it in from_value
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
.get(0)
.expect("failed to get argument for String method")
.cloned()
.unwrap_or_else(Value::undefined)
.to_integer(ctx)? as i32;

let end = args
.get(1)
.expect("failed to get argument in slice")
.cloned()
.unwrap_or_else(|| Value::integer(length))
.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 {
max(length.wrapping_add(start), 0)
} else {
Expand Down
28 changes: 28 additions & 0 deletions boa/src/builtins/string/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,34 @@ fn last_index_non_integer_position_argument() {
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]
fn empty_iter() {
let mut engine = Context::new();
Expand Down