Skip to content

Commit fea8e65

Browse files
committed
Fix 'static readonly' not being recognized as constant anymore, see AssemblyScript#44
1 parent 5323e64 commit fea8e65

22 files changed

+2161
-2138
lines changed

dist/asc.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/asc.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ast.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export abstract class Node {
156156

157157
static createParameter(
158158
name: IdentifierExpression,
159-
type: CommonTypeNode | null,
159+
type: CommonTypeNode,
160160
initializer: Expression | null,
161161
kind: ParameterKind,
162162
range: Range
@@ -1040,7 +1040,7 @@ export class ParameterNode extends Node {
10401040
/** Parameter name. */
10411041
name: IdentifierExpression;
10421042
/** Parameter type. */
1043-
type: CommonTypeNode | null;
1043+
type: CommonTypeNode;
10441044
/** Initializer expression, if present. */
10451045
initializer: Expression | null;
10461046
}
@@ -1052,7 +1052,7 @@ export class SignatureNode extends CommonTypeNode {
10521052
/** Accepted parameters. */
10531053
parameterTypes: ParameterNode[];
10541054
/** Return type. */
1055-
returnType: CommonTypeNode | null;
1055+
returnType: CommonTypeNode;
10561056
/** Explicitly provided this type, if any. */
10571057
explicitThisType: TypeNode | null; // can't be a function
10581058
}

src/compiler.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,13 @@ export class Compiler extends DiagnosticEmitter {
510510
}
511511

512512
var nativeType = global.type.toNativeType();
513+
var isConstant = global.isAny(CommonFlags.CONST) || global.is(CommonFlags.STATIC | CommonFlags.READONLY);
513514

514515
// handle imports
515516
if (global.is(CommonFlags.DECLARE)) {
516517

517518
// constant global
518-
if (global.is(CommonFlags.CONST)) {
519+
if (isConstant) {
519520
global.set(CommonFlags.MODULE_IMPORT);
520521
module.addGlobalImport(
521522
global.internalName,
@@ -558,7 +559,7 @@ export class Compiler extends DiagnosticEmitter {
558559
if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) {
559560

560561
// if a constant global, check if the initializer becomes constant after precompute
561-
if (global.is(CommonFlags.CONST)) {
562+
if (isConstant) {
562563
initExpr = this.precomputeExpressionRef(initExpr);
563564
if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) {
564565
this.warning(
@@ -586,7 +587,7 @@ export class Compiler extends DiagnosticEmitter {
586587

587588
} else { // compile as-is
588589

589-
if (global.is(CommonFlags.CONST)) {
590+
if (isConstant) {
590591
let exprType = _BinaryenExpressionGetType(initExpr);
591592
switch (exprType) {
592593
case NativeType.I32: {
@@ -624,15 +625,15 @@ export class Compiler extends DiagnosticEmitter {
624625
}
625626
}
626627
global.set(CommonFlags.INLINED); // inline the value from now on
627-
if (declaration.isTopLevel) { // but keep the element if it might be re-exported
628+
if (declaration.isTopLevel) { // but keep the element as it might be re-exported
628629
module.addGlobal(internalName, nativeType, false, initExpr);
629630
}
630631
if (declaration.range.source.isEntry && declaration.isTopLevelExport) {
631632
module.addGlobalExport(global.internalName, declaration.programLevelInternalName);
632633
}
633634

634635
} else /* mutable */ {
635-
module.addGlobal(internalName, nativeType, !global.is(CommonFlags.CONST), initExpr);
636+
module.addGlobal(internalName, nativeType, !isConstant, initExpr);
636637
}
637638
}
638639
return true;
@@ -844,6 +845,7 @@ export class Compiler extends DiagnosticEmitter {
844845
let flow = instance.flow;
845846
let stmt: ExpressionRef;
846847
if (body.kind == NodeKind.EXPRESSION) { // () => expression
848+
assert(instance.is(CommonFlags.ARROW));
847849
stmt = this.compileExpression((<ExpressionStatement>body).expression, returnType);
848850
flow.set(FlowFlags.RETURNS);
849851
} else {
@@ -854,7 +856,7 @@ export class Compiler extends DiagnosticEmitter {
854856
if (returnType != Type.void && !allBranchesReturn) {
855857
this.error(
856858
DiagnosticCode.A_function_whose_declared_type_is_not_void_must_return_a_value,
857-
assert(declaration.signature.returnType, "return type expected").range
859+
declaration.signature.returnType.range
858860
);
859861
}
860862
}

src/parser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,8 @@ export class Parser extends DiagnosticEmitter {
10321032
if (tn.skip(Token.COLON)) {
10331033
type = this.parseType(tn);
10341034
if (!type) return null;
1035+
} else {
1036+
type = Node.createOmittedType(tn.range(tn.pos));
10351037
}
10361038
let initializer: Expression | null = null;
10371039
if (tn.skip(Token.EQUALS)) {

std/assembly/allocator/arena.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
import { MASK as AL_MASK } from "./common/alignment";
1111

12-
var offset: usize = (HEAP_BASE + AL_MASK) & ~AL_MASK;
12+
var startOffset: usize = (HEAP_BASE + AL_MASK) & ~AL_MASK;
13+
var offset: usize = startOffset;
1314

1415
@global
1516
export function allocate_memory(size: usize): usize {
@@ -37,5 +38,5 @@ export function free_memory(ptr: usize): void {
3738

3839
@global
3940
export function reset_memory(): void {
40-
offset = (HEAP_BASE + AL_MASK) & ~AL_MASK;
41+
offset = startOffset;
4142
}

tests/allocators/arena/optimized.wat

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,96 +2,97 @@
22
(type $ii (func (param i32) (result i32)))
33
(type $iv (func (param i32)))
44
(type $v (func))
5+
(global "$(lib)/allocator/arena/startOffset" (mut i32) (i32.const 0))
56
(global "$(lib)/allocator/arena/offset" (mut i32) (i32.const 0))
67
(global $HEAP_BASE i32 (i32.const 4))
78
(memory $0 1)
89
(export "allocate_memory" (func "$(lib)/allocator/arena/allocate_memory"))
910
(export "free_memory" (func "$(lib)/allocator/arena/free_memory"))
1011
(export "reset_memory" (func "$(lib)/allocator/arena/reset_memory"))
1112
(export "memory" (memory $0))
12-
(start $(lib)/allocator/arena/reset_memory)
13+
(start $start)
1314
(func "$(lib)/allocator/arena/allocate_memory" (; 0 ;) (type $ii) (param $0 i32) (result i32)
1415
(local $1 i32)
1516
(local $2 i32)
1617
(local $3 i32)
1718
(local $4 i32)
18-
;;@ (lib)/allocator/arena.ts:14:2
19+
;;@ (lib)/allocator/arena.ts:17:2
1920
(if
20-
;;@ (lib)/allocator/arena.ts:14:6
21+
;;@ (lib)/allocator/arena.ts:17:6
2122
(i32.eqz
22-
;;@ (lib)/allocator/arena.ts:14:7
23+
;;@ (lib)/allocator/arena.ts:17:7
2324
(get_local $0)
2425
)
25-
;;@ (lib)/allocator/arena.ts:14:20
26+
;;@ (lib)/allocator/arena.ts:17:20
2627
(return
2728
(i32.const 0)
2829
)
2930
)
30-
;;@ (lib)/allocator/arena.ts:18:2
31+
;;@ (lib)/allocator/arena.ts:21:2
3132
(if
32-
;;@ (lib)/allocator/arena.ts:18:6
33+
;;@ (lib)/allocator/arena.ts:21:6
3334
(i32.gt_u
34-
;;@ (lib)/allocator/arena.ts:16:2
35+
;;@ (lib)/allocator/arena.ts:19:2
3536
(tee_local $2
36-
;;@ (lib)/allocator/arena.ts:16:15
37+
;;@ (lib)/allocator/arena.ts:19:15
3738
(i32.and
3839
(i32.add
39-
;;@ (lib)/allocator/arena.ts:16:16
40+
;;@ (lib)/allocator/arena.ts:19:16
4041
(i32.add
41-
;;@ (lib)/allocator/arena.ts:15:2
42+
;;@ (lib)/allocator/arena.ts:18:2
4243
(tee_local $1
43-
;;@ (lib)/allocator/arena.ts:15:12
44+
;;@ (lib)/allocator/arena.ts:18:12
4445
(get_global "$(lib)/allocator/arena/offset")
4546
)
46-
;;@ (lib)/allocator/arena.ts:16:22
47+
;;@ (lib)/allocator/arena.ts:19:22
4748
(get_local $0)
4849
)
49-
;;@ (lib)/allocator/arena.ts:16:29
50+
;;@ (lib)/allocator/arena.ts:19:29
5051
(i32.const 7)
5152
)
5253
(i32.const -8)
5354
)
5455
)
55-
;;@ (lib)/allocator/arena.ts:18:15
56+
;;@ (lib)/allocator/arena.ts:21:15
5657
(i32.shl
57-
;;@ (lib)/allocator/arena.ts:17:2
58+
;;@ (lib)/allocator/arena.ts:20:2
5859
(tee_local $0
59-
;;@ (lib)/allocator/arena.ts:17:20
60+
;;@ (lib)/allocator/arena.ts:20:20
6061
(current_memory)
6162
)
62-
;;@ (lib)/allocator/arena.ts:18:37
63+
;;@ (lib)/allocator/arena.ts:21:37
6364
(i32.const 16)
6465
)
6566
)
66-
;;@ (lib)/allocator/arena.ts:21:4
67+
;;@ (lib)/allocator/arena.ts:24:4
6768
(if
68-
;;@ (lib)/allocator/arena.ts:21:8
69+
;;@ (lib)/allocator/arena.ts:24:8
6970
(i32.lt_s
7071
(grow_memory
71-
;;@ (lib)/allocator/arena.ts:20:22
72+
;;@ (lib)/allocator/arena.ts:23:22
7273
(select
73-
;;@ (lib)/allocator/arena.ts:20:26
74+
;;@ (lib)/allocator/arena.ts:23:26
7475
(get_local $0)
7576
(tee_local $4
76-
;;@ (lib)/allocator/arena.ts:19:4
77+
;;@ (lib)/allocator/arena.ts:22:4
7778
(tee_local $3
78-
;;@ (lib)/allocator/arena.ts:19:22
79+
;;@ (lib)/allocator/arena.ts:22:22
7980
(i32.shr_u
8081
(i32.and
81-
;;@ (lib)/allocator/arena.ts:19:23
82+
;;@ (lib)/allocator/arena.ts:22:23
8283
(i32.add
83-
;;@ (lib)/allocator/arena.ts:19:24
84+
;;@ (lib)/allocator/arena.ts:22:24
8485
(i32.sub
8586
(get_local $2)
86-
;;@ (lib)/allocator/arena.ts:19:33
87+
;;@ (lib)/allocator/arena.ts:22:33
8788
(get_local $1)
8889
)
89-
;;@ (lib)/allocator/arena.ts:19:39
90+
;;@ (lib)/allocator/arena.ts:22:39
9091
(i32.const 65535)
9192
)
9293
(i32.const -65536)
9394
)
94-
;;@ (lib)/allocator/arena.ts:19:62
95+
;;@ (lib)/allocator/arena.ts:22:62
9596
(i32.const 16)
9697
)
9798
)
@@ -102,50 +103,60 @@
102103
)
103104
)
104105
)
105-
;;@ (lib)/allocator/arena.ts:21:35
106+
;;@ (lib)/allocator/arena.ts:24:35
106107
(i32.const 0)
107108
)
108-
;;@ (lib)/allocator/arena.ts:22:6
109+
;;@ (lib)/allocator/arena.ts:25:6
109110
(if
110-
;;@ (lib)/allocator/arena.ts:22:10
111+
;;@ (lib)/allocator/arena.ts:25:10
111112
(i32.lt_s
112113
(grow_memory
113-
;;@ (lib)/allocator/arena.ts:22:22
114+
;;@ (lib)/allocator/arena.ts:25:22
114115
(get_local $3)
115116
)
116-
;;@ (lib)/allocator/arena.ts:22:37
117+
;;@ (lib)/allocator/arena.ts:25:37
117118
(i32.const 0)
118119
)
119-
;;@ (lib)/allocator/arena.ts:23:8
120+
;;@ (lib)/allocator/arena.ts:26:8
120121
(unreachable)
121122
)
122123
)
123124
)
124-
;;@ (lib)/allocator/arena.ts:27:2
125+
;;@ (lib)/allocator/arena.ts:30:2
125126
(set_global "$(lib)/allocator/arena/offset"
126-
;;@ (lib)/allocator/arena.ts:27:11
127+
;;@ (lib)/allocator/arena.ts:30:11
127128
(get_local $2)
128129
)
129-
;;@ (lib)/allocator/arena.ts:28:9
130+
;;@ (lib)/allocator/arena.ts:31:9
130131
(get_local $1)
131132
)
132133
(func "$(lib)/allocator/arena/free_memory" (; 1 ;) (type $iv) (param $0 i32)
133-
;;@ (lib)/allocator/arena.ts:32:46
134+
;;@ (lib)/allocator/arena.ts:35:46
134135
(nop)
135136
)
136137
(func "$(lib)/allocator/arena/reset_memory" (; 2 ;) (type $v)
137-
;;@ (lib)/allocator/arena.ts:38:2
138+
;;@ (lib)/allocator/arena.ts:41:2
138139
(set_global "$(lib)/allocator/arena/offset"
139-
;;@ (lib)/allocator/arena.ts:38:11
140+
;;@ (lib)/allocator/arena.ts:41:11
141+
(get_global "$(lib)/allocator/arena/startOffset")
142+
)
143+
)
144+
(func $start (; 3 ;) (type $v)
145+
(set_global "$(lib)/allocator/arena/startOffset"
146+
;;@ (lib)/allocator/arena.ts:12:25
140147
(i32.and
141148
(i32.add
142-
;;@ (lib)/allocator/arena.ts:38:12
149+
;;@ (lib)/allocator/arena.ts:12:26
143150
(get_global $HEAP_BASE)
144-
;;@ (lib)/allocator/arena.ts:38:24
151+
;;@ (lib)/allocator/arena.ts:12:38
145152
(i32.const 7)
146153
)
147154
(i32.const -8)
148155
)
149156
)
157+
(set_global "$(lib)/allocator/arena/offset"
158+
;;@ (lib)/allocator/arena.ts:13:20
159+
(get_global "$(lib)/allocator/arena/startOffset")
160+
)
150161
)
151162
)

0 commit comments

Comments
 (0)