-
-
Notifications
You must be signed in to change notification settings - Fork 687
Expand file tree
/
Copy pathreference.ts
More file actions
186 lines (154 loc) · 5.58 KB
/
reference.ts
File metadata and controls
186 lines (154 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Canonical aliases
export type funcref = ref_func | null;
export type externref = ref_extern | null;
export type anyref = ref_any | null;
export type eqref = ref_eq | null;
export type i31ref = ref_i31 | null;
export type structref = ref_struct | null;
export type arrayref = ref_array | null;
export type stringref = ref_string | null;
// TODO: Conflict with the instruction namespaces
// export type stringview_wtf8 = ref_stringview_wtf8 | null;
// export type stringview_wtf16 = ref_stringview_wtf16 | null;
// export type stringview_iter = ref_stringview_iter | null;
@unmanaged
abstract class Ref {
}
@final @unmanaged
export abstract class RefFunc extends Ref {
}
@final @unmanaged
export abstract class RefExtern extends Ref {
}
@final @unmanaged
export abstract class RefAny extends Ref {
}
@final @unmanaged
export abstract class RefEq extends Ref {
}
@final @unmanaged
export abstract class RefI31 extends Ref {
}
@final @unmanaged
export abstract class RefStruct extends Ref {
}
@final @unmanaged
export abstract class RefArray extends Ref {
}
import { E_INDEXOUTOFRANGE } from "util/error";
@final @unmanaged
export abstract class RefString extends Ref {
@lazy static readonly MAX_LENGTH: i32 = (1 << 30) - 1;
static fromCharCode(unit: i32, surr: i32 = -1): ref_string {
if (~surr) unit = 0x10000 + ((unit & 0x3FF) << 10) | (surr & 0x3FF);
return string.from_code_point(unit);
}
static fromCodePoint(cp: i32): ref_string {
if (<u32>cp > 0x10ffff) throw new Error("Invalid code point");
return string.from_code_point(cp);
}
// @ts-ignore: this on getter
get length(this: ref_string): i32 {
return string.measure_wtf16(this);
}
at(this: ref_string, pos: i32): stringref {
let len = string.measure_wtf16(this);
pos += select(0, len, pos >= 0);
if (<u32>pos >= <u32>len) throw new RangeError(E_INDEXOUTOFRANGE);
return string.from_code_point(stringview_wtf16.get_codeunit(string.as_wtf16(this), pos));
}
@operator("[]") charAt(this: ref_string, pos: i32): stringref {
if (<u32>pos >= <u32>string.measure_wtf16(this)) return "";
return string.from_code_point(stringview_wtf16.get_codeunit(string.as_wtf16(this), pos));
}
charCodeAt(this: ref_string, pos: i32): i32 {
if (<u32>pos >= <u32>string.measure_wtf16(this)) return -1; // (NaN)
return stringview_wtf16.get_codeunit(string.as_wtf16(this), pos);
}
codePointAt(this: ref_string, pos: i32): i32 {
let len = string.measure_wtf16(this);
if (<u32>pos >= <u32>len) return -1; // (undefined)
let view = string.as_wtf16(this);
let first = <i32>stringview_wtf16.get_codeunit(view, pos);
if ((first & 0xFC00) != 0xD800 || pos + 1 == len) return first;
let second = <i32>stringview_wtf16.get_codeunit(view, pos + 1);
if ((second & 0xFC00) != 0xDC00) return first;
return (first - 0xD800 << 10) + (second - 0xDC00) + 0x10000;
}
@operator("+")
concat(this: ref_string, other: ref_string): ref_string {
return string.concat(this, other);
}
endsWith(this: ref_string, search: ref_string, end: i32 = RefString.MAX_LENGTH): bool {
end = min(max(end, 0), string.measure_wtf16(this));
let searchLength = string.measure_wtf16(search);
let searchStart = end - searchLength;
if (searchStart < 0) return false;
return string.eq(
stringview_wtf16.slice(string.as_wtf16(this), searchStart, searchStart + searchLength),
search
);
}
@operator("==") private static __eq(left: ref_string | null, right: ref_string | null): bool {
return string.eq(left, right);
}
@operator.prefix("!")
private static __not(str: ref_string | null): bool {
return str == null;
}
@operator("!=")
private static __ne(left: ref_string | null, right: ref_string | null): bool {
return !string.eq(left, right);
}
@operator(">") private static __gt(left: ref_string, right: ref_string): bool {
return string.compare(left, right) > 0;
}
@operator(">=") private static __gte(left: ref_string, right: ref_string): bool {
return string.compare(left, right) >= 0;
}
@operator("<") private static __lt(left: ref_string, right: ref_string): bool {
return string.compare(left, right) < 0;
}
@operator("<=") private static __lte(left: ref_string, right: ref_string): bool {
return string.compare(left, right) <= 0;
}
includes(this: ref_string, search: ref_string, start: i32 = 0): bool {
return this.indexOf(search, start) != -1;
}
indexOf(this: ref_string, search: ref_string, start: i32 = 0): i32 {
let searchLen = string.measure_wtf16(search);
if (!searchLen) return 0;
let len = string.measure_wtf16(this);
if (!len) return -1;
let searchStart = min(max(start, 0), len);
let view = string.as_wtf16(this);
for (len -= searchLen; searchStart <= len; ++searchStart) {
// FIXME: slice is suboptimal
if (string.eq(
stringview_wtf16.slice(view, searchStart, searchStart + searchLen),
search
)) {
return searchStart;
}
}
return -1;
}
lastIndexOf(this: ref_string, search: ref_string, start: i32 = i32.MAX_VALUE): i32 {
let searchLen = string.measure_wtf16(search);
if (!searchLen) return string.measure_wtf16(this);
let len = string.measure_wtf16(this);
if (!len) return -1;
let searchStart = min(max(start, 0), len - searchLen);
for (; searchStart >= 0; --searchStart) {
// FIXME: slice is suboptimal
if (string.eq(
stringview_wtf16.slice(string.as_wtf16(this), searchStart, searchStart + searchLen),
search
)) {
return searchStart;
}
}
return -1;
}
// TODO: port more
}