Skip to content

Commit

Permalink
avm2+tests: Fix String.substr with negative second value, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord-McSweeney authored and Aaron1011 committed Jun 10, 2023
1 parent cfb23d1 commit 4d8feea
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions core/src/avm2/globals/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,20 @@ fn substr<'gc>(
.unwrap_or(&Value::Number(0x7fffffff as f64))
.coerce_to_number(activation)?;

let len = if len < 0. {
if len.is_infinite() {
0.
} else {
let wrapped_around = this.len() as f64 + len;
if wrapped_around as usize + start_index >= this.len() {
return Ok("".into());
};
wrapped_around
}
} else {
len
};

let end_index = if len == f64::INFINITY {
this.len()
} else {
Expand Down
22 changes: 22 additions & 0 deletions tests/tests/swfs/avm2/string_substr_negative/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"abcdefg".substr(3, 5)
defg
"abcdefg".substr(0, -2)
abcde
"abcdefg".substr(1, -2)
bcdef
"abcdefg".substr(2, -2)

"abcdefg".substr(1, -4)
bcd
"abcdefg".substr(1, -Infinity)

"abcdefg".substr(2, -1)

"abcdefg".substr(2, 9)
cdefg
"abcdefg".substr(0, -3)
abcd
"abcdefg".substr(2, -7)

"abcdefg".substr(5, -10)

Binary file not shown.
1 change: 1 addition & 0 deletions tests/tests/swfs/avm2/string_substr_negative/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
num_frames = 1

0 comments on commit 4d8feea

Please sign in to comment.