From 58c907e98576b2bf70423c7b083f0575d7dcbf5b Mon Sep 17 00:00:00 2001 From: Chris Midgley Date: Wed, 18 Aug 2021 17:02:41 +0100 Subject: [PATCH] avm2: implement string.split for undefined (#5064) * avm2: implement string.split for undefined * chore: cargo fmt * dev: avoid explicit ArrayStorage --- CONTRIBUTING.md | 3 ++- core/src/avm2/globals/string.rs | 20 ++++++++++-------- tests/tests/swfs/avm2/string_split/output.txt | 3 ++- tests/tests/swfs/avm2/string_split/test.as | 3 ++- tests/tests/swfs/avm2/string_split/test.swf | Bin 724 -> 726 bytes 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 173356568..252491905 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -143,7 +143,8 @@ To add a test here, create a .swf file that runs `trace()` statements. You can d * [`mtasc`](http://web.archive.org/web/20210324063628/http://tech.motion-twin.com/mtasc.html) (ActionScript 2 only) * if you create a file `test.as` with a `class Test` with a `static function main` with the code you want to run, you can compile it using `mtasc -main -header 200:150:30 test.as -swf test.swf` * [`mxmlc`](https://helpx.adobe.com/air/kb/archived-air-sdk-version.html) (ActionScript 3 only) - * if you create a file `test.as`, you can compile it using `mxmlc test.as`. `mxmlc` is located in the `bin` folder of the downloadable AIR SDK. + * if you create a file `test.as` with a `class Test`, you can compile it using `mxmlc Test.as`. `mxmlc` is located in the `bin` folder of the downloadable AIR SDK. + * you may want to use docker instead -- something like `docker run -it --rm -v ${PWD}:/src jeko/airbuild mxmlc ./Test.as` works well Run the .swf in Flash Player and create a file `output.txt` with the contents of the trace statements. Add the `output.txt`, `test.swf` and either the `test.as` or `test.fla` file to a directory under `core/tests/swfs/avm1` (or `avm2`) named after what your test tests, and add a line in `regression_tests.rs` to have Ruffle run it. diff --git a/core/src/avm2/globals/string.rs b/core/src/avm2/globals/string.rs index c320bcb66..077c8498b 100644 --- a/core/src/avm2/globals/string.rs +++ b/core/src/avm2/globals/string.rs @@ -11,6 +11,7 @@ use crate::avm2::ArrayObject; use crate::avm2::Error; use crate::string_utils; use gc_arena::{GcCell, MutationContext}; +use std::iter; /// Implements `String`'s instance initializer. pub fn instance_init<'gc>( @@ -126,12 +127,16 @@ fn split<'gc>( args: &[Value<'gc>], ) -> Result, Error> { if let Some(this) = this { - if matches!(args.get(0).unwrap_or(&Value::Undefined), Value::Undefined) { - log::warn!("string.split(undefined) - not implemented"); + let delimiter = args.get(0).unwrap_or(&Value::Undefined); + if matches!(delimiter, Value::Undefined) { + let this = Value::from(this); + return Ok( + ArrayObject::from_storage(activation, iter::once(this).collect()) + .unwrap() + .into(), + ); } - if args - .get(0) - .unwrap_or(&Value::Undefined) + if delimiter .coerce_to_object(activation)? .as_regexp() .is_some() @@ -139,10 +144,7 @@ fn split<'gc>( log::warn!("string.split(regex) - not implemented"); } let this = Value::from(this).coerce_to_string(activation)?; - let delimiter = args - .get(0) - .unwrap_or(&Value::Undefined) - .coerce_to_string(activation)?; + let delimiter = delimiter.coerce_to_string(activation)?; let limit = match args.get(1).unwrap_or(&Value::Undefined) { Value::Undefined => usize::MAX, limit => limit.coerce_to_i32(activation)?.max(0) as usize, diff --git a/tests/tests/swfs/avm2/string_split/output.txt b/tests/tests/swfs/avm2/string_split/output.txt index f020ed42e..c6d56e57a 100644 --- a/tests/tests/swfs/avm2/string_split/output.txt +++ b/tests/tests/swfs/avm2/string_split/output.txt @@ -5,5 +5,6 @@ a,b,c // text.split("") a,.,b,.,c -// text.split() - unimplemented +// text.split() +a.b.c // text.split(regex) - unimplemented diff --git a/tests/tests/swfs/avm2/string_split/test.as b/tests/tests/swfs/avm2/string_split/test.as index 76a962489..a91f31b88 100644 --- a/tests/tests/swfs/avm2/string_split/test.as +++ b/tests/tests/swfs/avm2/string_split/test.as @@ -16,7 +16,8 @@ trace(text.split(".")); trace('// text.split("")'); trace(text.split("")); -trace('// text.split() - unimplemented'); +trace('// text.split()'); +trace(text.split()); trace('// text.split(regex) - unimplemented'); diff --git a/tests/tests/swfs/avm2/string_split/test.swf b/tests/tests/swfs/avm2/string_split/test.swf index 9618de290b6cb4c44cabbaed660efacd6d546590..e015dba6c09ad39060f071e7efc1f433500a85d5 100644 GIT binary patch delta 475 zcmV<10VMv^1=a-#T31sZ+5`Xq#jyzp0e=YMLL#^{dFfKJZ_6eJg%@|={bwTFWBVbm z_fkW?=^R;)xG?DX|yM$KH%M8%g%J`q#>MGMm?ic!cu&L#`-)e|=zrRXDaMAWyRxMQ*4 zJQ!d%2fl^mTc*4`lNES#PF(BtQGS;2eV2HxML=s~8Y_TN@TFekyS08mGqKMfqqI!A zR$AcWEx&caRc1g|V#QJqSQ<68Xnziul*mSAeD>}o@}?dc`WAT6@Qu!cM@^CS8hC6d zIS?NF-k2_Bv)Wtsg0Hl$t3kwhE8U9a+|T(~ZrfDqqhgp|uo?b6yC$AioJ)tW$&I6R zp->eyJ^BF>m;6|XeLGY%@`aHkE8eV delta 473 zcmV;~0Ve*|1=IxzT31sm-~<2w!?6hn0e`|TRKDA59fk5DC=cTEuXsZaUlAtW*mZ!g zkIfct-Vsd<*g*ik`H52}>)g15gFnFBctWTwlHh^gV}6R8A%eqKZ|9w24J%%ie9Um4 zDi-m|bQ$CA-{A-I^{%LK>y^d4c;{0q#+uU(z?1lfN8MM_#(8iu>0~lVsx!~7Eq}%9 zgO^9w#qOs{9NX-_3$JeKF}SzJFnT|RGDZzF{tn{}#@J_y%?<`NyEGxJXmAuj?rAXH zSc)Ep^a9N}#AHzqh{*&ap$Xi%FQ8&55shCcuyG7YJMF~_Q+j-X+vuo_;f2^%O4CUQ z@st%U`LP?H&2gI4Ml@Oi@J1UZCcJ+{ zNexn3{-(P6xWZiZgCmSb5)}C$JrE|6bH-J_no7lGw(yx~z;jBlqPJFB-J-9sjk)y# PCxom2?Ipke>lrInp?~iy