Skip to content

Remove compat javascript behaviour for string concatenation #1682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 8, 2021
15 changes: 7 additions & 8 deletions std/assembly/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ import { Array } from "./array";
return (first - 0xD800 << 10) + (second - 0xDC00) + 0x10000;
}

@operator("+") private static __concat(left: String, right: String): String {
return select<String>(left, changetype<String>("null"), left !== null).concat(right);
@operator("+") private static __concat(left: String | null, right: String | null): String {
if (right === null) return select<String>(left!, changetype<String>(""), left !== null);
if (left === null) return right;
return left.concat(right);
}

concat(other: String): String {
if (other === null) other = changetype<String>("null");
var thisSize: isize = this.length << 1;
var otherSize: isize = other.length << 1;
var outSize: usize = thisSize + otherSize;
Expand All @@ -96,7 +97,6 @@ import { Array } from "./array";
}

endsWith(search: String, end: i32 = String.MAX_LENGTH): bool {
if (search === null) return false;
end = min(max(end, 0), this.length);
var searchLength = <isize>search.length;
var searchStart = <isize>end - searchLength;
Expand Down Expand Up @@ -135,11 +135,11 @@ import { Array } from "./array";
return res ? res > 0 : leftLength > rightLength;
}

@operator(">=") private static __gte(left: String, right: String): bool {
@operator(">=") private static __gte(left: String | null, right: String | null): bool {
return !this.__lt(left, right);
}

@operator("<") private static __lt(left: String, right: String): bool {
@operator("<") private static __lt(left: String | null, right: String | null): bool {
if (left === right || left === null || right === null) return false;
var rightLength = right.length;
if (!rightLength) return false;
Expand All @@ -150,7 +150,7 @@ import { Array } from "./array";
return res ? res < 0 : leftLength < rightLength;
}

@operator("<=") private static __lte(left: String, right: String): bool {
@operator("<=") private static __lte(left: String | null, right: String | null): bool {
return !this.__gt(left, right);
}

Expand Down Expand Up @@ -196,7 +196,6 @@ import { Array } from "./array";
}

startsWith(search: String, start: i32 = 0): bool {
if (search === null) search = changetype<String>("null");
var len = <isize>this.length;
var searchStart = min(max(<isize>start, 0), len);
var searchLength = <isize>search.length;
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/builtins.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -822,9 +822,9 @@
i32.const 5
f64.const 0
f64.const 0
f64.const 22
f64.const 23
f64.const 23
f64.const 24
f64.const 24
call $~lib/builtins/trace
global.get $~lib/memory/__stack_pointer
i32.const 1376
Expand Down
8 changes: 4 additions & 4 deletions tests/compiler/builtins.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2587,11 +2587,11 @@
local.set $1
i32.const 0
local.set $0
i32.const 23
i32.const 22
local.set $6
i32.const 24
i32.const 23
local.set $7
i32.const 24
i32.const 23
local.set $8
i32.const 256
local.set $9
Expand Down Expand Up @@ -2636,7 +2636,7 @@
unreachable
end
local.get $6
i32.const 23
i32.const 22
i32.eq
i32.eqz
if
Expand Down
27 changes: 11 additions & 16 deletions tests/compiler/std/array-access.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
(data (i32.const 1160) "\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y")
(data (i32.const 1276) "\1c")
(data (i32.const 1288) "\01")
(data (i32.const 1308) "\1c")
(data (i32.const 1320) "\01\00\00\00\08\00\00\00n\00u\00l\00l")
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 17724))
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 17692))
(export "memory" (memory $0))
(export "i32ArrayArrayElementAccess" (func $export:std/array-access/i32ArrayArrayElementAccess))
(export "stringArrayPropertyAccess" (func $export:std/array-access/stringArrayPropertyAccess))
Expand All @@ -24,11 +22,11 @@
(export "stringArrayArrayMethodCall" (func $export:std/array-access/stringArrayArrayMethodCall))
(func $~stack_check
global.get $~lib/memory/__stack_pointer
i32.const 1340
i32.const 1308
i32.lt_s
if
i32.const 17744
i32.const 17792
i32.const 17712
i32.const 17760
i32.const 1
i32.const 1
call $~lib/builtins/abort
Expand All @@ -41,19 +39,16 @@
(local $3 i32)
(local $4 i32)
global.get $~lib/memory/__stack_pointer
i32.const 12
i32.const 8
i32.sub
global.set $~lib/memory/__stack_pointer
call $~stack_check
global.get $~lib/memory/__stack_pointer
i64.const 0
i64.store
global.get $~lib/memory/__stack_pointer
i32.const 0
i32.store offset=8
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store offset=4
i32.store
local.get $0
i32.const 20
i32.sub
Expand All @@ -63,7 +58,7 @@
local.set $1
global.get $~lib/memory/__stack_pointer
i32.const 1296
i32.store offset=4
i32.store
local.get $1
i32.const 1292
i32.load
Expand All @@ -81,18 +76,18 @@
i32.lt_s
if
global.get $~lib/memory/__stack_pointer
i32.const 12
i32.const 8
i32.add
global.set $~lib/memory/__stack_pointer
i32.const 0
return
end
global.get $~lib/memory/__stack_pointer
local.get $0
i32.store offset=4
i32.store
global.get $~lib/memory/__stack_pointer
i32.const 1296
i32.store offset=8
i32.store offset=4
block $__inlined_func$~lib/util/string/compareImpl (result i32)
i32.const 1296
local.set $3
Expand Down Expand Up @@ -172,7 +167,7 @@
i32.const 0
end
global.get $~lib/memory/__stack_pointer
i32.const 12
i32.const 8
i32.add
global.set $~lib/memory/__stack_pointer
i32.eqz
Expand Down
37 changes: 12 additions & 25 deletions tests/compiler/std/array-access.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
(data (i32.const 76) ",\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\1a\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s\00\00\00")
(data (i32.const 124) "|\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00^\00\00\00E\00l\00e\00m\00e\00n\00t\00 \00t\00y\00p\00e\00 \00m\00u\00s\00t\00 \00b\00e\00 \00n\00u\00l\00l\00a\00b\00l\00e\00 \00i\00f\00 \00a\00r\00r\00a\00y\00 \00i\00s\00 \00h\00o\00l\00e\00y\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data (i32.const 252) "\1c\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00")
(data (i32.const 284) "\1c\00\00\00\00\00\00\00\00\00\00\00\01\00\00\00\08\00\00\00n\00u\00l\00l\00\00\00\00\00")
(table $0 1 funcref)
(global $~lib/ASC_SHRINK_LEVEL i32 (i32.const 0))
(global $~lib/memory/__data_end i32 (i32.const 316))
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 16700))
(global $~lib/memory/__heap_base i32 (i32.const 16700))
(global $~lib/memory/__data_end i32 (i32.const 284))
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 16668))
(global $~lib/memory/__heap_base i32 (i32.const 16668))
(export "memory" (memory $0))
(export "i32ArrayArrayElementAccess" (func $export:std/array-access/i32ArrayArrayElementAccess))
(export "stringArrayPropertyAccess" (func $export:std/array-access/stringArrayPropertyAccess))
Expand Down Expand Up @@ -169,8 +168,8 @@
global.get $~lib/memory/__data_end
i32.lt_s
if
i32.const 16720
i32.const 16768
i32.const 16688
i32.const 16736
i32.const 1
i32.const 1
call $~lib/builtins/abort
Expand Down Expand Up @@ -248,30 +247,18 @@
(local $7 i32)
(local $8 i32)
global.get $~lib/memory/__stack_pointer
i32.const 12
i32.const 8
i32.sub
global.set $~lib/memory/__stack_pointer
call $~stack_check
global.get $~lib/memory/__stack_pointer
i64.const 0
i64.store
global.get $~lib/memory/__stack_pointer
i32.const 0
i32.store offset=8
local.get $1
i32.const 0
i32.eq
if
global.get $~lib/memory/__stack_pointer
i32.const 304
local.tee $1
i32.store
end
local.get $0
local.set $8
global.get $~lib/memory/__stack_pointer
local.get $8
i32.store offset=4
i32.store
local.get $8
call $~lib/string/String#get:length
local.set $3
Expand All @@ -295,7 +282,7 @@
local.set $8
global.get $~lib/memory/__stack_pointer
local.get $8
i32.store offset=4
i32.store
local.get $8
call $~lib/string/String#get:length
local.set $7
Expand All @@ -308,7 +295,7 @@
i32.const 0
local.set $8
global.get $~lib/memory/__stack_pointer
i32.const 12
i32.const 8
i32.add
global.set $~lib/memory/__stack_pointer
local.get $8
Expand All @@ -318,22 +305,22 @@
local.set $8
global.get $~lib/memory/__stack_pointer
local.get $8
i32.store offset=4
i32.store
local.get $8
local.get $6
local.get $1
local.set $8
global.get $~lib/memory/__stack_pointer
local.get $8
i32.store offset=8
i32.store offset=4
local.get $8
i32.const 0
local.get $7
call $~lib/util/string/compareImpl
i32.eqz
local.set $8
global.get $~lib/memory/__stack_pointer
i32.const 12
i32.const 8
i32.add
global.set $~lib/memory/__stack_pointer
local.get $8
Expand Down
Loading