From 85aa0b97fcaa6c16adc647d458ca9588dac44a68 Mon Sep 17 00:00:00 2001 From: Aleksander Sotov Date: Fri, 25 Oct 2024 15:53:07 +0300 Subject: [PATCH] Improve Array.$_set for null value arguments Issue: https://gitee.com/openharmony/arkcompiler_runtime_core/issues/IAZV7N Testing: add new test to ets_func_tests Signed-off-by: Aleksander Sotov --- .../plugins/ets/stdlib/escompat/Array.sts | 10 ++++++++-- .../ets/templates/stdlib/Array_common.erb | 8 +++++++- .../ets/templates/stdlib/Array_escompat.erb | 2 +- .../ets_func_tests/escompat/ArrayTest1.sts | 19 +++++++++++++++++++ .../std/containers/ConsoleLogTest.sts | 2 ++ .../containers/ConsoleLogTest.sts.expected | 1 + 6 files changed, 38 insertions(+), 4 deletions(-) diff --git a/static_core/plugins/ets/stdlib/escompat/Array.sts b/static_core/plugins/ets/stdlib/escompat/Array.sts index 66e513cf43..2364f64a4f 100644 --- a/static_core/plugins/ets/stdlib/escompat/Array.sts +++ b/static_core/plugins/ets/stdlib/escompat/Array.sts @@ -102,7 +102,7 @@ export class Array implements ReadonlyArray, Iterable { if (idx >= this.actualLength) { throw new RangeError("Out of bounds") } - this.buffer[idx] = val as Object + this.buffer[idx] = val } private $_set_unsafe(idx: int, val: T): void { @@ -1944,7 +1944,13 @@ export class Array implements ReadonlyArray, Iterable { return "" } const sepReal = __runtimeIsSameReference(sep, undefined) ? "," : sep! - let sb = new StringBuilder(new String(this.$_get_unsafe(0))) + // NOTE(aleksander-sotov) We can't call String constructor here due to internal issue 18583 + const first_el = this.$_get_unsafe(0) + let first_str = "undefined" + if (!__runtimeIsSameReference(first_el, undefined)) { + first_str = new String(first_el) + } + let sb = new StringBuilder(first_str) for (let i: int = 1; i < this.actualLength; i++) { const tmp = this.$_get_unsafe(i) sb.append(sepReal); diff --git a/static_core/plugins/ets/templates/stdlib/Array_common.erb b/static_core/plugins/ets/templates/stdlib/Array_common.erb index 904c1fd4d7..0b09e36f36 100644 --- a/static_core/plugins/ets/templates/stdlib/Array_common.erb +++ b/static_core/plugins/ets/templates/stdlib/Array_common.erb @@ -620,7 +620,13 @@ return "" } const sepReal = __runtimeIsSameReference(sep, undefined) ? "," : sep! - let sb = new StringBuilder(new String(<%= get_unsafe.(this, '0') %>)) + <% if el_type == "T" %>// NOTE(aleksander-sotov) We can't call String constructor here due to internal issue 18583 + const first_el = <%= get_unsafe.(this, '0') %> + let first_str = "undefined" + if (!__runtimeIsSameReference(first_el, undefined)) { + first_str = new String(first_el) + } + let sb = new StringBuilder(first_str)<% else %>let sb = new StringBuilder(new String(<%= get_unsafe.(this, '0') %>))<% end %> for (let i: int = 1; i < <%= this_len_int %>; i++) { const tmp = <%= get_unsafe.(this, 'i') %> sb.append(sepReal); diff --git a/static_core/plugins/ets/templates/stdlib/Array_escompat.erb b/static_core/plugins/ets/templates/stdlib/Array_escompat.erb index 157ad390c1..80b2fae73d 100644 --- a/static_core/plugins/ets/templates/stdlib/Array_escompat.erb +++ b/static_core/plugins/ets/templates/stdlib/Array_escompat.erb @@ -85,7 +85,7 @@ export class Array implements ReadonlyArray, Iterable { if (idx >= this.actualLength) { throw new RangeError("Out of bounds") } - this.buffer[idx] = val as Object + this.buffer[idx] = val } private $_set_unsafe(idx: int, val: T): void { diff --git a/static_core/plugins/ets/tests/ets_func_tests/escompat/ArrayTest1.sts b/static_core/plugins/ets/tests/ets_func_tests/escompat/ArrayTest1.sts index 62936fd064..235e2e0fb7 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/escompat/ArrayTest1.sts +++ b/static_core/plugins/ets/tests/ets_func_tests/escompat/ArrayTest1.sts @@ -27,6 +27,8 @@ function main(): int { failures += check((): int => { return isArrayNullUndefined()}, "isArrayNullUndefined"); failures += check((): int => { return push()}, "push"); failures += check((): int => { return pushECMA()}, "pushECMA"); + failures += check((): int => { return setNull()}, "setNull"); + failures += check((): int => { return setCustomClass()}, "setCustomClass"); failures += check((): int => { return toSplicedArg2()}, "toSplicedArg2"); failures += check((): int => { return toSplicedArg1()}, "toSplicedArg1"); return check(failures, "All tests run"); @@ -113,6 +115,23 @@ function pushECMA(): int { return success; } +function setNull(): int { + let arr = new Array(3); + arr[1] = "abc" + arr[2] = null; + if (!(arr[2] instanceof null)) { + return fail; + } + return success; +} + +function setCustomClass() : int { + class A {} + let arr = new Array(1) + arr[0] = new A() + return success +} + function toSplicedArg1(): int { let arr = new Array; for (let i: int = 0; i < dd.length; ++i) { diff --git a/static_core/plugins/ets/tests/ets_func_tests/std/containers/ConsoleLogTest.sts b/static_core/plugins/ets/tests/ets_func_tests/std/containers/ConsoleLogTest.sts index 36f8f42531..f751f117e8 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/std/containers/ConsoleLogTest.sts +++ b/static_core/plugins/ets/tests/ets_func_tests/std/containers/ConsoleLogTest.sts @@ -15,4 +15,6 @@ function main(): void { console.log(null, undefined, 11, "123", new Object()) + let arr = new Array(3) + console.log(arr) } diff --git a/static_core/plugins/ets/tests/ets_func_tests/std/containers/ConsoleLogTest.sts.expected b/static_core/plugins/ets/tests/ets_func_tests/std/containers/ConsoleLogTest.sts.expected index 69a799bcfa..b8e6d0d1fd 100644 --- a/static_core/plugins/ets/tests/ets_func_tests/std/containers/ConsoleLogTest.sts.expected +++ b/static_core/plugins/ets/tests/ets_func_tests/std/containers/ConsoleLogTest.sts.expected @@ -12,3 +12,4 @@ # limitations under the License. # null undefined 11 123 std.core.Object {} +undefined,undefined,undefined