Skip to content

Fix casting to bool for strings #567

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 4 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions std/assembly/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class String {
);
} else {
code -= 0x10000;
let hi: u32 = (code >>> 10) + 0xD800;
let hi: u32 = (code >>> 10) + 0xD800;
let lo: u32 = (code & 0x3FF) + 0xDC00;
store<u32>(
changetype<usize>(out),
Expand Down Expand Up @@ -110,9 +110,9 @@ export class String {
assert(this !== null);
if (other === null) other = changetype<String>("null");

var thisLen: isize = this.length;
var thisLen: isize = this.length;
var otherLen: isize = other.length;
var outLen: usize = thisLen + otherLen;
var outLen: usize = thisLen + otherLen;
if (outLen == 0) return changetype<String>("");
var out = allocateUnsafe(outLen);
copyUnsafe(out, 0, this, 0, thisLen);
Expand Down Expand Up @@ -141,6 +141,11 @@ export class String {
return !compareUnsafe(left, 0, right, 0, leftLength);
}

@operator.prefix("!")
private static __not(str: String): bool {
return str === null || !str.length;
}

@operator("!=")
private static __ne(left: String, right: String): bool {
return !this.__eq(left, right);
Expand Down Expand Up @@ -345,8 +350,8 @@ export class String {
var out = allocateUnsafe(targetLength);
if (len > padLen) {
let count = (len - 1) / padLen;
let base = count * padLen;
let rest = len - base;
let base = count * padLen;
let rest = len - base;
repeatUnsafe(out, 0, padString, count);
if (rest) copyUnsafe(out, base, padString, 0, rest);
} else {
Expand Down Expand Up @@ -394,9 +399,9 @@ export class String {
}

slice(beginIndex: i32, endIndex: i32 = i32.MAX_VALUE): String {
var len = this.length;
var len = this.length;
var begin = beginIndex < 0 ? max(beginIndex + len, 0) : min(beginIndex, len);
var end = endIndex < 0 ? max(endIndex + len, 0) : min(endIndex, len);
var end = endIndex < 0 ? max(endIndex + len, 0) : min(endIndex, len);
len = end - begin;
if (len <= 0) return changetype<String>("");
var out = allocateUnsafe(len);
Expand Down Expand Up @@ -512,7 +517,7 @@ export class String {
cp = (
(cp & 7) << 18 |
(load<u8>(ptr + ptrPos++) & 63) << 12 |
(load<u8>(ptr + ptrPos++) & 63) << 6 |
(load<u8>(ptr + ptrPos++) & 63) << 6 |
load<u8>(ptr + ptrPos++) & 63
) - 0x10000;
store<u16>(buf + bufPos, 0xD800 + (cp >> 10));
Expand All @@ -523,7 +528,7 @@ export class String {
assert(ptrPos + 2 <= len);
store<u16>(buf + bufPos,
(cp & 15) << 12 |
(load<u8>(ptr + ptrPos++) & 63) << 6 |
(load<u8>(ptr + ptrPos++) & 63) << 6 |
load<u8>(ptr + ptrPos++) & 63
);
bufPos += 2;
Expand All @@ -548,8 +553,8 @@ export class String {
++off; ++pos;
} else if (c1 < 2048) {
let ptr = buf + off;
store<u8>(ptr, c1 >> 6 | 192);
store<u8>(ptr, c1 & 63 | 128, 1);
store<u8>(ptr, c1 >> 6 | 192);
store<u8>(ptr, c1 & 63 | 128, 1);
off += 2; ++pos;
} else {
let ptr = buf + off;
Expand All @@ -559,15 +564,15 @@ export class String {
c1 = 0x10000 + ((c1 & 0x03FF) << 10) + (c2 & 0x03FF);
store<u8>(ptr, c1 >> 18 | 240);
store<u8>(ptr, c1 >> 12 & 63 | 128, 1);
store<u8>(ptr, c1 >> 6 & 63 | 128, 2);
store<u8>(ptr, c1 >> 6 & 63 | 128, 2);
store<u8>(ptr, c1 & 63 | 128, 3);
off += 4; pos += 2;
continue;
}
}
store<u8>(ptr, c1 >> 12 | 224);
store<u8>(ptr, c1 >> 6 & 63 | 128, 1);
store<u8>(ptr, c1 & 63 | 128, 2);
store<u8>(ptr, c1 >> 12 | 224);
store<u8>(ptr, c1 >> 6 & 63 | 128, 1);
store<u8>(ptr, c1 & 63 | 128, 2);
off += 3; ++pos;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/number.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2348,7 +2348,7 @@
if
i32.const 0
i32.const 2072
i32.const 249
i32.const 254
i32.const 4
call $~lib/env/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/number.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3525,7 +3525,7 @@
if
i32.const 0
i32.const 2072
i32.const 249
i32.const 254
i32.const 4
call $~lib/env/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/std/array-access.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
if
i32.const 0
i32.const 16
i32.const 224
i32.const 229
i32.const 4
call $~lib/env/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/std/array-access.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
if
i32.const 0
i32.const 16
i32.const 224
i32.const 229
i32.const 4
call $~lib/env/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/std/array.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6238,7 +6238,7 @@
if
i32.const 0
i32.const 4056
i32.const 249
i32.const 254
i32.const 4
call $~lib/env/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/std/array.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -11617,7 +11617,7 @@
if
i32.const 0
i32.const 4056
i32.const 249
i32.const 254
i32.const 4
call $~lib/env/abort
unreachable
Expand Down
8 changes: 4 additions & 4 deletions tests/compiler/std/string-utf8.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1525,7 +1525,7 @@
if
i32.const 0
i32.const 72
i32.const 507
i32.const 512
i32.const 8
call $~lib/env/abort
unreachable
Expand Down Expand Up @@ -1572,7 +1572,7 @@
if
i32.const 0
i32.const 72
i32.const 511
i32.const 516
i32.const 8
call $~lib/env/abort
unreachable
Expand Down Expand Up @@ -1651,7 +1651,7 @@
if
i32.const 0
i32.const 72
i32.const 523
i32.const 528
i32.const 8
call $~lib/env/abort
unreachable
Expand Down Expand Up @@ -1706,7 +1706,7 @@
if
i32.const 0
i32.const 72
i32.const 532
i32.const 537
i32.const 4
call $~lib/env/abort
unreachable
Expand Down
8 changes: 4 additions & 4 deletions tests/compiler/std/string-utf8.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2008,7 +2008,7 @@
if
i32.const 0
i32.const 72
i32.const 507
i32.const 512
i32.const 8
call $~lib/env/abort
unreachable
Expand Down Expand Up @@ -2062,7 +2062,7 @@
if
i32.const 0
i32.const 72
i32.const 511
i32.const 516
i32.const 8
call $~lib/env/abort
unreachable
Expand Down Expand Up @@ -2157,7 +2157,7 @@
if
i32.const 0
i32.const 72
i32.const 523
i32.const 528
i32.const 8
call $~lib/env/abort
unreachable
Expand Down Expand Up @@ -2220,7 +2220,7 @@
if
i32.const 0
i32.const 72
i32.const 532
i32.const 537
i32.const 4
call $~lib/env/abort
unreachable
Expand Down
Loading