Skip to content

Commit 70a0123

Browse files
MaxGraeydcodeIO
authored andcommitted
Fix some cases in string comparision (AssemblyScript#66)
1 parent 26601dc commit 70a0123

5 files changed

Lines changed: 109 additions & 112 deletions

File tree

std/assembly/string.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ export class String {
6363
changetype<usize>(this) + ((<usize>pos + 1) << 1),
6464
HEADER_SIZE
6565
);
66-
if (second < 0xDC00 || second > 0xDFFF) {
67-
return first;
68-
}
66+
if (second < 0xDC00 || second > 0xDFFF) return first;
6967
return ((first - 0xD800) << 10) + (second - 0xDC00) + 0x10000;
7068
}
7169

@@ -83,16 +81,19 @@ export class String {
8381
var outLen: usize = thisLen + otherLen;
8482
if (outLen == 0) return EMPTY;
8583
var out = allocate(outLen);
84+
8685
move_memory(
8786
changetype<usize>(out) + HEADER_SIZE,
8887
changetype<usize>(this) + HEADER_SIZE,
8988
thisLen << 1
9089
);
90+
9191
move_memory(
9292
changetype<usize>(out) + HEADER_SIZE + (thisLen << 1),
9393
changetype<usize>(other) + HEADER_SIZE,
9494
otherLen << 1
9595
);
96+
9697
return out;
9798
}
9899

@@ -102,9 +103,7 @@ export class String {
102103
var end: isize = <isize>min(max(endPosition, 0), this.length);
103104
var searchLength: isize = searchString.length;
104105
var start: isize = end - searchLength;
105-
if (start < 0) {
106-
return false;
107-
}
106+
if (start < 0) return false;
108107
return !compare_memory(
109108
changetype<usize>(this) + HEADER_SIZE + (start << 1),
110109
changetype<usize>(searchString) + HEADER_SIZE,
@@ -116,8 +115,10 @@ export class String {
116115
private static __eq(left: String, right: String): bool {
117116
if (left === null) return right === null;
118117
else if (right === null) return false;
118+
119119
var leftLength = left.length;
120120
if (leftLength != right.length) return false;
121+
121122
return !compare_memory(
122123
changetype<usize>(left) + HEADER_SIZE,
123124
changetype<usize>(right) + HEADER_SIZE,
@@ -132,9 +133,7 @@ export class String {
132133

133134
@operator(">")
134135
private static __gt(left: String, right: String): bool {
135-
if (!changetype<usize>(left) || !changetype<usize>(right)) {
136-
return false;
137-
}
136+
if (left === null || right === null) return false;
138137

139138
var leftLength = left.length;
140139
var rightLength = right.length;
@@ -152,9 +151,8 @@ export class String {
152151

153152
@operator(">=")
154153
private static __gte(left: String, right: String): bool {
155-
if (!changetype<usize>(left) || !changetype<usize>(right)) {
156-
return false;
157-
}
154+
if (left === null) return right === null;
155+
else if (right === null) return false;
158156

159157
var leftLength = left.length;
160158
var rightLength = right.length;
@@ -172,9 +170,7 @@ export class String {
172170

173171
@operator("<")
174172
private static __lt(left: String, right: String): bool {
175-
if (!changetype<usize>(left) || !changetype<usize>(right)) {
176-
return false;
177-
}
173+
if (left === null || right === null) return false;
178174

179175
var leftLength = left.length;
180176
var rightLength = right.length;
@@ -192,9 +188,8 @@ export class String {
192188

193189
@operator("<=")
194190
private static __lte(left: String, right: String): bool {
195-
if (!changetype<usize>(left) || !changetype<usize>(right)) {
196-
return false;
197-
}
191+
if (left === null) return right === null;
192+
else if (right === null) return false;
198193

199194
var leftLength = left.length;
200195
var rightLength = right.length;

tests/compiler/std/array-access.optimized.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
(call $abort
126126
(i32.const 0)
127127
(i32.const 8)
128-
(i32.const 239)
128+
(i32.const 234)
129129
(i32.const 4)
130130
)
131131
(unreachable)

tests/compiler/std/array-access.untouched.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
(call $abort
196196
(i32.const 0)
197197
(i32.const 8)
198-
(i32.const 239)
198+
(i32.const 234)
199199
(i32.const 4)
200200
)
201201
(unreachable)

tests/compiler/std/string.optimized.wat

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
(call $abort
155155
(i32.const 0)
156156
(i32.const 72)
157-
(i32.const 239)
157+
(i32.const 234)
158158
(i32.const 4)
159159
)
160160
(unreachable)
@@ -264,7 +264,7 @@
264264
(call $abort
265265
(i32.const 0)
266266
(i32.const 72)
267-
(i32.const 100)
267+
(i32.const 101)
268268
(i32.const 4)
269269
)
270270
(unreachable)
@@ -374,7 +374,7 @@
374374
(call $abort
375375
(i32.const 0)
376376
(i32.const 72)
377-
(i32.const 218)
377+
(i32.const 213)
378378
(i32.const 4)
379379
)
380380
(unreachable)
@@ -1085,7 +1085,7 @@
10851085
(call $abort
10861086
(i32.const 0)
10871087
(i32.const 72)
1088-
(i32.const 563)
1088+
(i32.const 558)
10891089
(i32.const 10)
10901090
)
10911091
(unreachable)
@@ -3171,7 +3171,7 @@
31713171
(call $abort
31723172
(i32.const 0)
31733173
(i32.const 72)
3174-
(i32.const 79)
3174+
(i32.const 77)
31753175
(i32.const 4)
31763176
)
31773177
(unreachable)
@@ -3394,46 +3394,43 @@
33943394
(local $2 i32)
33953395
(local $3 i32)
33963396
(if
3397-
(i32.and
3398-
(if (result i32)
3399-
(tee_local $2
3400-
(i32.eqz
3401-
(get_local $0)
3402-
)
3403-
)
3404-
(get_local $2)
3405-
(i32.eqz
3406-
(get_local $1)
3407-
)
3397+
(get_local $0)
3398+
(if
3399+
(i32.eqz
3400+
(get_local $1)
3401+
)
3402+
(return
3403+
(i32.const 0)
34083404
)
3409-
(i32.const 1)
34103405
)
34113406
(return
3412-
(i32.const 0)
3407+
(i32.eqz
3408+
(get_local $1)
3409+
)
34133410
)
34143411
)
3415-
(set_local $3
3412+
(set_local $2
34163413
(i32.load
34173414
(get_local $1)
34183415
)
34193416
)
34203417
(if
34213418
(i32.eqz
3422-
(tee_local $2
3419+
(tee_local $3
34233420
(i32.load
34243421
(get_local $0)
34253422
)
34263423
)
34273424
)
34283425
(return
34293426
(i32.eqz
3430-
(get_local $3)
3427+
(get_local $2)
34313428
)
34323429
)
34333430
)
34343431
(if
34353432
(i32.eqz
3436-
(get_local $3)
3433+
(get_local $2)
34373434
)
34383435
(return
34393436
(i32.const 1)
@@ -3451,11 +3448,11 @@
34513448
)
34523449
(i32.shl
34533450
(select
3454-
(get_local $2)
34553451
(get_local $3)
3452+
(get_local $2)
34563453
(i32.lt_s
3457-
(get_local $2)
34583454
(get_local $3)
3455+
(get_local $2)
34593456
)
34603457
)
34613458
(i32.const 1)
@@ -3540,22 +3537,19 @@
35403537
(local $2 i32)
35413538
(local $3 i32)
35423539
(if
3543-
(i32.and
3544-
(if (result i32)
3545-
(tee_local $2
3546-
(i32.eqz
3547-
(get_local $0)
3548-
)
3549-
)
3550-
(get_local $2)
3551-
(i32.eqz
3552-
(get_local $1)
3553-
)
3540+
(get_local $0)
3541+
(if
3542+
(i32.eqz
3543+
(get_local $1)
3544+
)
3545+
(return
3546+
(i32.const 0)
35543547
)
3555-
(i32.const 1)
35563548
)
35573549
(return
3558-
(i32.const 0)
3550+
(i32.eqz
3551+
(get_local $1)
3552+
)
35593553
)
35603554
)
35613555
(set_local $2

0 commit comments

Comments
 (0)