Skip to content

Commit 1cc33bf

Browse files
MaxGraeydcodeIO
authored andcommitted
Various improvements & refactorings (#976)
1 parent 8a55531 commit 1cc33bf

File tree

4 files changed

+113
-111
lines changed

4 files changed

+113
-111
lines changed

src/binary.ts

Lines changed: 91 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ export enum NameKind {
7272

7373
/** WebAssembly types. */
7474
export enum Type {
75-
i32 = 0x7f,
76-
i64 = 0x7e,
77-
f32 = 0x7d,
78-
f64 = 0x7c,
75+
i32 = 0x7F,
76+
i64 = 0x7E,
77+
f32 = 0x7D,
78+
f64 = 0x7C,
7979
anyfunc = 0x70,
8080
func = 0x60,
8181
none = 0x40
@@ -89,28 +89,28 @@ export enum Op {
8989
loop = 0x03,
9090
if_ = 0x04,
9191
else_ = 0x05,
92-
end = 0x0b,
93-
br = 0x0c,
94-
br_if = 0x0d,
95-
br_table = 0x0e,
96-
return_ = 0x0f,
92+
end = 0x0B,
93+
br = 0x0C,
94+
br_if = 0x0D,
95+
br_table = 0x0E,
96+
return_ = 0x0F,
9797
call = 0x10,
9898
call_indirect = 0x11,
99-
drop = 0x1a,
100-
select = 0x1b,
99+
drop = 0x1A,
100+
select = 0x1B,
101101
get_local = 0x20,
102102
set_local = 0x21,
103103
tee_local = 0x22,
104104
get_global = 0x23,
105105
set_global = 0x24,
106106
i32_load = 0x28,
107107
i64_load = 0x29,
108-
f32_load = 0x2a,
109-
f64_load = 0x2b,
110-
i32_load8_s = 0x2c,
111-
i32_load8_u = 0x2d,
112-
i32_load16_s = 0x2e,
113-
i32_load16_u = 0x2f,
108+
f32_load = 0x2A,
109+
f64_load = 0x2B,
110+
i32_load8_s = 0x2C,
111+
i32_load8_u = 0x2D,
112+
i32_load16_s = 0x2E,
113+
i32_load16_u = 0x2F,
114114
i64_load8_s = 0x30,
115115
i64_load8_u = 0x31,
116116
i64_load16_s = 0x32,
@@ -121,12 +121,12 @@ export enum Op {
121121
i64_store = 0x37,
122122
f32_store = 0x38,
123123
f64_store = 0x39,
124-
i32_store8 = 0x3a,
125-
i32_store16 = 0x3b,
126-
i64_store8 = 0x3c,
127-
i64_store16 = 0x3d,
128-
i64_store32 = 0x3e,
129-
memory_size = 0x3f,
124+
i32_store8 = 0x3A,
125+
i32_store16 = 0x3B,
126+
i64_store8 = 0x3C,
127+
i64_store16 = 0x3D,
128+
i64_store32 = 0x3E,
129+
memory_size = 0x3F,
130130
memory_grow = 0x40,
131131
i32_const = 0x41,
132132
i64_const = 0x42,
@@ -137,12 +137,12 @@ export enum Op {
137137
i32_ne = 0x47,
138138
i32_lt_s = 0x48,
139139
i32_lt_u = 0x49,
140-
i32_gt_s = 0x4a,
141-
i32_gt_u = 0x4b,
142-
i32_le_s = 0x4c,
143-
i32_le_u = 0x4d,
144-
i32_ge_s = 0x4e,
145-
i32_ge_u = 0x4f,
140+
i32_gt_s = 0x4A,
141+
i32_gt_u = 0x4B,
142+
i32_le_s = 0x4C,
143+
i32_le_u = 0x4D,
144+
i32_ge_s = 0x4E,
145+
i32_ge_u = 0x4F,
146146
i64_eqz = 0x50,
147147
i64_eq = 0x51,
148148
i64_ne = 0x52,
@@ -153,12 +153,12 @@ export enum Op {
153153
i64_le_s = 0x57,
154154
i64_le_u = 0x58,
155155
i64_ge_s = 0x59,
156-
i64_ge_u = 0x5a,
157-
f32_eq = 0x5b,
158-
f32_ne = 0x5c,
159-
f32_lt = 0x5d,
160-
f32_gt = 0x5e,
161-
f32_le = 0x5f,
156+
i64_ge_u = 0x5A,
157+
f32_eq = 0x5B,
158+
f32_ne = 0x5C,
159+
f32_lt = 0x5D,
160+
f32_gt = 0x5E,
161+
f32_le = 0x5F,
162162
f32_ge = 0x60,
163163
f64_eq = 0x61,
164164
f64_ne = 0x62,
@@ -169,12 +169,12 @@ export enum Op {
169169
i32_clz = 0x67,
170170
i32_ctz = 0x68,
171171
i32_popcnt = 0x69,
172-
i32_add = 0x6a,
173-
i32_sub = 0x6b,
174-
i32_mul = 0x6c,
175-
i32_div_s = 0x6d,
176-
i32_div_u = 0x6e,
177-
i32_rem_s = 0x6f,
172+
i32_add = 0x6A,
173+
i32_sub = 0x6B,
174+
i32_mul = 0x6C,
175+
i32_div_s = 0x6D,
176+
i32_div_u = 0x6E,
177+
i32_rem_s = 0x6F,
178178
i32_rem_u = 0x70,
179179
i32_and = 0x71,
180180
i32_or = 0x72,
@@ -185,12 +185,12 @@ export enum Op {
185185
i32_rotl = 0x77,
186186
i32_rotr = 0x78,
187187
i64_clz = 0x79,
188-
i64_ctz = 0x7a,
189-
i64_popcnt = 0x7b,
190-
i64_add = 0x7c,
191-
i64_sub = 0x7d,
192-
i64_mul = 0x7e,
193-
i64_div_s = 0x7f,
188+
i64_ctz = 0x7A,
189+
i64_popcnt = 0x7B,
190+
i64_add = 0x7C,
191+
i64_sub = 0x7D,
192+
i64_mul = 0x7E,
193+
i64_div_s = 0x7F,
194194
i64_div_u = 0x80,
195195
i64_rem_s = 0x81,
196196
i64_rem_u = 0x82,
@@ -201,12 +201,12 @@ export enum Op {
201201
i64_shr_s = 0x87,
202202
i64_shr_u = 0x88,
203203
i64_rotl = 0x89,
204-
i64_rotr = 0x8a,
205-
f32_abs = 0x8b,
206-
f32_neg = 0x8c,
207-
f32_ceil = 0x8d,
208-
f32_floor = 0x8e,
209-
f32_trunc = 0x8f,
204+
i64_rotr = 0x8A,
205+
f32_abs = 0x8B,
206+
f32_neg = 0x8C,
207+
f32_ceil = 0x8D,
208+
f32_floor = 0x8E,
209+
f32_trunc = 0x8F,
210210
f32_nearest = 0x90,
211211
f32_sqrt = 0x91,
212212
f32_add = 0x92,
@@ -217,44 +217,44 @@ export enum Op {
217217
f32_max = 0x97,
218218
f32_copysign = 0x98,
219219
f64_abs = 0x99,
220-
f64_neg = 0x9a,
221-
f64_ceil = 0x9b,
222-
f64_floor = 0x9c,
223-
f64_trunc = 0x9d,
224-
f64_nearest = 0x9e,
225-
f64_sqrt = 0x9f,
226-
f64_add = 0xa0,
227-
f64_sub = 0xa1,
228-
f64_mul = 0xa2,
229-
f64_div = 0xa3,
230-
f64_min = 0xa4,
231-
f64_max = 0xa5,
232-
f64_copysign = 0xa6,
233-
i32_wrap_i64 = 0xa7,
234-
i32_trunc_s_f32 = 0xa8,
235-
i32_trunc_u_f32 = 0xa9,
236-
i32_trunc_s_f64 = 0xaa,
237-
i32_trunc_u_f64 = 0xab,
238-
i64_extend_s_i32 = 0xac,
239-
i64_extend_u_i32 = 0xad,
240-
i64_trunc_s_f32 = 0xae,
241-
i64_trunc_u_f32 = 0xaf,
242-
i64_trunc_s_f64 = 0xb0,
243-
i64_trunc_u_f64 = 0xb1,
244-
f32_convert_s_i32 = 0xb2,
245-
f32_convert_u_i32 = 0xb3,
246-
f32_convert_s_i64 = 0xb4,
247-
f32_convert_u_i64 = 0xb5,
248-
f32_demote_f64 = 0xb6,
249-
f64_convert_s_i32 = 0xb7,
250-
f64_convert_u_i32 = 0xb8,
251-
f64_convert_s_i64 = 0xb9,
252-
f64_convert_u_i64 = 0xba,
253-
f64_promote_f32 = 0xbb,
254-
i32_reinterpret_f32 = 0xbc,
255-
i64_reinterpret_f64 = 0xbd,
256-
f32_reinterpret_i32 = 0xbe,
257-
f64_reinterpret_i64 = 0xbf
220+
f64_neg = 0x9A,
221+
f64_ceil = 0x9B,
222+
f64_floor = 0x9C,
223+
f64_trunc = 0x9D,
224+
f64_nearest = 0x9E,
225+
f64_sqrt = 0x9F,
226+
f64_add = 0xA0,
227+
f64_sub = 0xA1,
228+
f64_mul = 0xA2,
229+
f64_div = 0xA3,
230+
f64_min = 0xA4,
231+
f64_max = 0xA5,
232+
f64_copysign = 0xA6,
233+
i32_wrap_i64 = 0xA7,
234+
i32_trunc_s_f32 = 0xA8,
235+
i32_trunc_u_f32 = 0xA9,
236+
i32_trunc_s_f64 = 0xAA,
237+
i32_trunc_u_f64 = 0xAB,
238+
i64_extend_s_i32 = 0xAC,
239+
i64_extend_u_i32 = 0xAD,
240+
i64_trunc_s_f32 = 0xAE,
241+
i64_trunc_u_f32 = 0xAF,
242+
i64_trunc_s_f64 = 0xB0,
243+
i64_trunc_u_f64 = 0xB1,
244+
f32_convert_s_i32 = 0xB2,
245+
f32_convert_u_i32 = 0xB3,
246+
f32_convert_s_i64 = 0xB4,
247+
f32_convert_u_i64 = 0xB5,
248+
f32_demote_f64 = 0xB6,
249+
f64_convert_s_i32 = 0xB7,
250+
f64_convert_u_i32 = 0xB8,
251+
f64_convert_s_i64 = 0xB9,
252+
f64_convert_u_i64 = 0xBA,
253+
f64_promote_f32 = 0xBB,
254+
i32_reinterpret_f32 = 0xBC,
255+
i64_reinterpret_f64 = 0xBD,
256+
f32_reinterpret_i32 = 0xBE,
257+
f64_reinterpret_i64 = 0xBF
258258
}
259259

260260
enum ReaderState {

src/flow.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ export class Flow {
488488
lookupLocal(name: string): Local | null {
489489
var current: Flow | null = this;
490490
var scope: Map<String,Local> | null;
491-
do if ((scope = current.scopedLocals) && (scope.has(name))) return scope.get(name)!;
491+
do if ((scope = current.scopedLocals) && scope.has(name)) return scope.get(name)!;
492492
while (current = current.parent);
493493
return this.parentFunction.localsByName.get(name)!;
494494
}
@@ -504,30 +504,30 @@ export class Flow {
504504
isLocalFlag(index: i32, flag: LocalFlags, defaultIfInlined: bool = true): bool {
505505
if (index < 0) return defaultIfInlined;
506506
var localFlags = this.localFlags;
507-
return index < localFlags.length && (unchecked(this.localFlags[index]) & flag) == flag;
507+
return index < localFlags.length && (unchecked(localFlags[index]) & flag) == flag;
508508
}
509509

510510
/** Tests if the local at the specified index has any of the specified flags. */
511511
isAnyLocalFlag(index: i32, flag: LocalFlags, defaultIfInlined: bool = true): bool {
512512
if (index < 0) return defaultIfInlined;
513513
var localFlags = this.localFlags;
514-
return index < localFlags.length && (unchecked(this.localFlags[index]) & flag) != 0;
514+
return index < localFlags.length && (unchecked(localFlags[index]) & flag) != 0;
515515
}
516516

517517
/** Sets the specified flag or flags on the local at the specified index. */
518518
setLocalFlag(index: i32, flag: LocalFlags): void {
519519
if (index < 0) return;
520520
var localFlags = this.localFlags;
521521
var flags = index < localFlags.length ? unchecked(localFlags[index]) : 0;
522-
this.localFlags[index] = flags | flag;
522+
localFlags[index] = flags | flag;
523523
}
524524

525525
/** Unsets the specified flag or flags on the local at the specified index. */
526526
unsetLocalFlag(index: i32, flag: LocalFlags): void {
527527
if (index < 0) return;
528528
var localFlags = this.localFlags;
529529
var flags = index < localFlags.length ? unchecked(localFlags[index]) : 0;
530-
this.localFlags[index] = flags & ~flag;
530+
localFlags[index] = flags & ~flag;
531531
}
532532

533533
/** Pushes a new break label to the stack, for example when entering a loop that one can `break` from. */
@@ -582,10 +582,10 @@ export class Flow {
582582
for (let i = 0, k = localFlags.length; i < k; ++i) {
583583
let flags = localFlags[i];
584584
this.setLocalFlag(i, flags & LocalFlags.ANY_CONDITIONAL);
585-
if (flags & LocalFlags.RETAINED) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_RETAINED);
586-
if (flags & LocalFlags.READFROM) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_READFROM);
585+
if (flags & LocalFlags.RETAINED) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_RETAINED);
586+
if (flags & LocalFlags.READFROM) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_READFROM);
587587
if (flags & LocalFlags.WRITTENTO) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_WRITTENTO);
588-
if (flags & LocalFlags.RETURNED) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_RETURNED);
588+
if (flags & LocalFlags.RETURNED) this.setLocalFlag(i, LocalFlags.CONDITIONALLY_RETURNED);
589589
}
590590
}
591591

@@ -786,9 +786,10 @@ export class Flow {
786786
if (!ifFalse) break;
787787
// Logical OR: (if (condition 1 ifFalse))
788788
// the only way this had become false is if condition and ifFalse are false
789+
let exprType = getExpressionType(ifTrue);
789790
if (
790-
(getExpressionType(ifTrue) == NativeType.I32 && getConstValueI32(ifTrue) != 0) ||
791-
(getExpressionType(ifTrue) == NativeType.I64 && (getConstValueI64Low(ifTrue) != 0 || getConstValueI64High(ifTrue) != 0))
791+
(exprType == NativeType.I32 && getConstValueI32(ifTrue) != 0) ||
792+
(exprType == NativeType.I64 && (getConstValueI64Low(ifTrue) != 0 || getConstValueI64High(ifTrue) != 0))
792793
) {
793794
this.inheritNonnullIfFalse(getIfCondition(expr));
794795
this.inheritNonnullIfFalse(getIfFalse(expr));

src/util/charcode.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ export function isOctalDigit(c: i32): bool {
184184

185185
/** Tests if the specified character code is a valid start of an identifier. */
186186
export function isIdentifierStart(c: i32): bool {
187-
return c >= CharCode.a && c <= CharCode.z
188-
|| c >= CharCode.A && c <= CharCode.Z
187+
const c0 = c | 32; // unify uppercases and lowercases a|A - z|Z
188+
return c0 >= CharCode.a && c0 <= CharCode.z
189189
|| c == CharCode._
190190
|| c == CharCode.DOLLAR
191-
|| c > 0x7f && isUnicodeIdentifierStart(c);
191+
|| c > 0x7F && isUnicodeIdentifierStart(c);
192192
}
193193

194194
/** Tests if the specified character code is a valid keyword character. */
@@ -198,12 +198,12 @@ export function isKeywordCharacter(c: i32): bool {
198198

199199
/** Tests if the specified character code is a valid part of an identifier. */
200200
export function isIdentifierPart(c: i32): bool {
201-
return c >= CharCode.a && c <= CharCode.z
202-
|| c >= CharCode.A && c <= CharCode.Z
201+
const c0 = c | 32; // unify uppercases and lowercases a|A - z|Z
202+
return c0 >= CharCode.a && c0 <= CharCode.z
203203
|| c >= CharCode._0 && c <= CharCode._9
204204
|| c == CharCode._
205205
|| c == CharCode.DOLLAR
206-
|| c > 0x7f && isUnicodeIdentifierPart(c);
206+
|| c > 0x7F && isUnicodeIdentifierPart(c);
207207
}
208208

209209
// storing as u16 to save memory
@@ -368,11 +368,11 @@ function lookupInUnicodeMap(code: u16, map: u16[]): bool {
368368
}
369369

370370
function isUnicodeIdentifierStart(code: i32): bool {
371-
return code < 0 || code > 0xffff ? false
371+
return code < 170 || code > 65500 ? false
372372
: lookupInUnicodeMap(code as u16, unicodeIdentifierStart);
373373
}
374374

375375
function isUnicodeIdentifierPart(code: i32): bool {
376-
return code < 0 || code > 0xffff ? false
376+
return code < 170 || code > 65500 ? false
377377
: lookupInUnicodeMap(code as u16, unicodeIdentifierPart);
378378
}

src/util/collections.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export function makeArray<V>(original: Array<V> | null = null): Array<V> {
22
if (original) {
3-
let cloned = new Array<V>(original.length);
4-
for (let i = 0, k = original.length; i < k; ++i) unchecked(cloned[i] = original[i]);
3+
let len = original.length;
4+
let cloned = new Array<V>(len);
5+
for (let i = 0; i < len; ++i) unchecked(cloned[i] = original[i]);
56
return cloned;
67
}
78
return new Array<V>();

0 commit comments

Comments
 (0)