diff --git a/core/src/avm1/globals/array.rs b/core/src/avm1/globals/array.rs index 381437b60..cff8a7ecb 100644 --- a/core/src/avm1/globals/array.rs +++ b/core/src/avm1/globals/array.rs @@ -182,10 +182,11 @@ pub fn unshift<'gc>( ) -> Result, Error<'gc>> { let old_length = this.length(); let new_length = old_length + args.len(); - let offset = new_length - old_length; + let offset = args.len(); if old_length > 0 { - for i in (old_length - 1..new_length).rev() { + // Move all elements up by [offset], in reverse order. + for i in (offset..new_length).rev() { this.set_array_element( i, this.array_element(i - offset), @@ -195,6 +196,7 @@ pub fn unshift<'gc>( } for i in 0..args.len() { + // Put the new elements at the start of the array. this.set_array_element( i, args.get(i).unwrap().to_owned(), diff --git a/core/tests/swfs/avm1/array_trivial/output.txt b/core/tests/swfs/avm1/array_trivial/output.txt index fe84b15dc..035733f31 100644 --- a/core/tests/swfs/avm1/array_trivial/output.txt +++ b/core/tests/swfs/avm1/array_trivial/output.txt @@ -191,3 +191,15 @@ undefined // array 1,2 +// array.unshift(1,2) +4 + +// array +1,2,1,2 + +// array.unshift(3,4,5) +7 + +// array +3,4,5,1,2,1,2 + diff --git a/core/tests/swfs/avm1/array_trivial/test.fla b/core/tests/swfs/avm1/array_trivial/test.fla index 39fc5e6d4..690c7f699 100644 Binary files a/core/tests/swfs/avm1/array_trivial/test.fla and b/core/tests/swfs/avm1/array_trivial/test.fla differ diff --git a/core/tests/swfs/avm1/array_trivial/test.swf b/core/tests/swfs/avm1/array_trivial/test.swf index 7e8cf5a6f..15e495182 100644 Binary files a/core/tests/swfs/avm1/array_trivial/test.swf and b/core/tests/swfs/avm1/array_trivial/test.swf differ