avm1: Correctly handle undefined delimiter in String.split()

This commit is contained in:
nosamu 2023-12-19 07:04:31 -06:00 committed by Lord-McSweeney
parent ba6ba842d7
commit d443bd9def
4 changed files with 39 additions and 27 deletions

View File

@ -258,16 +258,17 @@ fn split<'gc>(
args: &[Value<'gc>], args: &[Value<'gc>],
) -> Result<Value<'gc>, Error<'gc>> { ) -> Result<Value<'gc>, Error<'gc>> {
let this = Value::from(this).coerce_to_string(activation)?; let this = Value::from(this).coerce_to_string(activation)?;
let delimiter = args
.get(0)
.unwrap_or(&Value::Undefined)
.coerce_to_string(activation)?;
let limit = match args.get(1).unwrap_or(&Value::Undefined) { let limit = match args.get(1).unwrap_or(&Value::Undefined) {
Value::Undefined => usize::MAX, Value::Undefined => usize::MAX,
limit => limit.coerce_to_i32(activation)?.max(0) as usize, limit => limit.coerce_to_i32(activation)?.max(0) as usize,
}; };
if let Some(delimiter) = match args.get(0).unwrap_or(&Value::Undefined) {
&Value::Undefined => None,
v => Some(v.coerce_to_string(activation)?),
} {
if delimiter.is_empty() { if delimiter.is_empty() {
// When using an empty delimiter, Str::split adds an extra beginning and trailing item, but Flash does not. // When using an empty delimiter, Str::split adds an extra beginning and trailing item,
// but Flash does not.
// e.g., split("foo", "") returns ["", "f", "o", "o", ""] in Rust but ["f, "o", "o"] in Flash. // e.g., split("foo", "") returns ["", "f", "o", "o", ""] in Rust but ["f, "o", "o"] in Flash.
// Special case this to match Flash's behavior. // Special case this to match Flash's behavior.
Ok(ArrayObject::new( Ok(ArrayObject::new(
@ -288,6 +289,14 @@ fn split<'gc>(
) )
.into()) .into())
} }
} else {
Ok(ArrayObject::new(
activation.context.gc_context,
activation.context.avm1.prototypes().array,
[this.into()]
)
.into())
}
} }
fn substr<'gc>( fn substr<'gc>(

View File

@ -242,8 +242,8 @@ HELLO𝔄hello
// split // split
// s.split(",") // s.split(",")
5 6
A,,b,c, A,,b,undefined0,c,
// s.split(",", 2) // s.split(",", 2)
2 2
A, A,
@ -254,20 +254,23 @@ A,
0 0
// s.split(",", undefined) // s.split(",", undefined)
5 6
A,,b,c, A,,b,undefined0,c,
// s.split(",", null) // s.split(",", null)
0 0
// s.split("") // s.split("")
7 18
A,,,,,b,,,c,, A,,,,,b,,,u,n,d,e,f,i,n,e,d,0,,,c,,
// s.split(undefined) // s.split(undefined)
1 1
A,,b,c, A,,b,undefined0,c,
// s.split(undefined, 0)
1
A,,b,undefined0,c,
// s.split() // s.split()
1 1
A,,b,c, A,,b,undefined0,c,
// toLowerCase // toLowerCase
// "teST😋".toLowerCase() // "teST😋".toLowerCase()