Skip to content

Commit 4207f64

Browse files
committed
Warn on constant locals not being actual constants; Simplify changetype
1 parent 2f12c7f commit 4207f64

File tree

14 files changed

+90
-74
lines changed

14 files changed

+90
-74
lines changed

bin/asc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var assemblyscript;
77
var isDev = true;
88
try {
99
assemblyscript = require("../dist/assemblyscript.js");
10-
require("source-map-support").install();
10+
try { require("source-map-support").install(); } catch (e) {} // optional
1111
isDev = false;
1212
} catch (e) {
1313
require("ts-node").register({ project: require("path").join(__dirname, "..", "src") });

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"dependencies": {
1414
"binaryen": "40.0.0-nightly.20171209",
1515
"glob": "^7.1.2",
16-
"minimist": "^1.2.0",
17-
"source-map-support": "^0.5.0"
16+
"minimist": "^1.2.0"
1817
},
1918
"devDependencies": {
2019
"@types/chalk": "^2.2.0",
@@ -26,6 +25,7 @@
2625
"chalk": "^2.3.0",
2726
"diff": "^3.4.0",
2827
"long": "^3.2.0",
28+
"source-map-support": "^0.5.0",
2929
"ts-loader": "^3.2.0",
3030
"ts-node": "^4.0.2",
3131
"tslint": "^5.8.0",
@@ -42,15 +42,15 @@
4242
},
4343
"scripts": {
4444
"build": "webpack",
45-
"clean": "rm dist/assemblyscript.*",
45+
"clean": "node scripts/clean",
46+
"lint": "tslint --project src",
4647
"test:config": "npm run test:config:assembly --scripts-prepend-node-path && npm run test:config:portable --scripts-prepend-node-path && npm run test:config:src --scripts-prepend-node-path",
4748
"test:config:assembly": "tsc --noEmit -p std/assembly --diagnostics --listFiles",
4849
"test:config:portable": "tsc --noEmit -p std/portable --diagnostics --listFiles",
4950
"test:config:src": "tsc --noEmit -p src --diagnostics --listFiles",
5051
"test:parser": "node tests/parser",
5152
"test:compiler": "node tests/compiler",
52-
"test": "npm run test:config --scripts-prepend-node-path && npm run test:parser --scripts-prepend-node-path && npm run test:compiler --scripts-prepend-node-path",
53-
"lint": "tslint --project src"
53+
"test": "npm run test:config --scripts-prepend-node-path && npm run test:parser --scripts-prepend-node-path && npm run test:compiler --scripts-prepend-node-path"
5454
},
5555
"files": [
5656
"bin/",

scripts/clean.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var fs = require("fs");
2+
var glob = require("glob");
3+
4+
glob("*", { cwd: __dirname + "/../dist" }, (err, matches) => {
5+
if (err)
6+
console.log("Failed to list files in 'dist/': " + err.message);
7+
else
8+
matches.forEach(match => {
9+
fs.unlink(__dirname + "/../dist/" + match, err => {
10+
if (err)
11+
console.log("Failed to delete 'dist/" + match + "': " + err.message);
12+
else
13+
console.log("Deleted 'dist/" + match + "'");
14+
});
15+
});
16+
});

src/ast.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,15 +2185,14 @@ function builderEndsWith(sb: string[], code: CharCode): bool {
21852185
export function escapeString(str: string): string {
21862186
var k = str.length;
21872187
var ret = new Array<string>(k);
2188-
ret.length = 0;
21892188
for (var i = 0, c: string; i < k; ++i) {
21902189
switch (c = str.charAt(i)) {
2191-
case "\\": ret.push("\\\\"); break;
2192-
case "\"": ret.push("\\\""); break;
2193-
case "\r": ret.push("\\r"); break;
2194-
case "\n": ret.push("\\n"); break;
2195-
case "\0": ret.push("\\0"); break;
2196-
default: ret.push(c);
2190+
case "\\": ret[i] = "\\\\"; break;
2191+
case "\"": ret[i] = "\\\""; break;
2192+
case "\r": ret[i] = "\\r"; break;
2193+
case "\n": ret[i] = "\\n"; break;
2194+
case "\0": ret[i] = "\\0"; break;
2195+
default: ret[i] = c;
21972196
}
21982197
}
21992198
return "\"" + ret.join("") + "\"";

src/builtins.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -689,11 +689,11 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
689689

690690
// other
691691

692-
case "changetype": // changetype<T1,T2>(value: T1) -> T2
693-
if (!validateCall(compiler, typeArguments, 2, operands, 1, reportNode))
692+
case "changetype": // changetype<T>(value: *) -> T
693+
if (!validateCall(compiler, typeArguments, 1, operands, 1, reportNode))
694694
return module.createUnreachable();
695-
if ((typeArguments[0] == usizeType && typeArguments[1].classType) || (typeArguments[0].classType && typeArguments[1] == usizeType)) {
696-
arg0 = compiler.compileExpression(operands[0], typeArguments[0]);
695+
arg0 = compiler.compileExpression(operands[0], Type.void, ConversionKind.NONE);
696+
if ((compiler.currentType == usizeType && typeArguments[1].classType) || (compiler.currentType.classType && typeArguments[1] == usizeType)) {
697697
compiler.currentType = typeArguments[1];
698698
return arg0;
699699
}

src/compiler.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ export class Compiler extends DiagnosticEmitter {
386386
if (!global.isMutable) {
387387
initExpr = this.precomputeExpressionRef(initExpr);
388388
if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) {
389-
this.warning(DiagnosticCode.Compiling_constant_global_with_non_constant_initializer_as_mutable, declaration.range);
389+
this.warning(DiagnosticCode.Compiling_constant_with_non_constant_initializer_as_mutable, declaration.range);
390390
initializeInStart = true;
391391
}
392392
} else
@@ -461,7 +461,7 @@ export class Compiler extends DiagnosticEmitter {
461461
initExpr = this.precomputeExpressionRef(initExpr);
462462
if (_BinaryenExpressionGetId(initExpr) != ExpressionId.Const) {
463463
if (element.isConstant)
464-
this.warning(DiagnosticCode.Compiling_constant_global_with_non_constant_initializer_as_mutable, declaration.range);
464+
this.warning(DiagnosticCode.Compiling_constant_with_non_constant_initializer_as_mutable, declaration.range);
465465
initInStart = true;
466466
}
467467
}
@@ -476,7 +476,7 @@ export class Compiler extends DiagnosticEmitter {
476476
this.module.createI32(1)
477477
);
478478
if (element.isConstant)
479-
this.warning(DiagnosticCode.Compiling_constant_global_with_non_constant_initializer_as_mutable, declaration.range);
479+
this.warning(DiagnosticCode.Compiling_constant_with_non_constant_initializer_as_mutable, declaration.range);
480480
initInStart = true;
481481
}
482482
if (initInStart) {
@@ -1029,7 +1029,8 @@ export class Compiler extends DiagnosticEmitter {
10291029
}
10301030
this.currentFunction.locals.set(name, local);
10311031
continue;
1032-
}
1032+
} else
1033+
this.warning(DiagnosticCode.Compiling_constant_with_non_constant_initializer_as_mutable, declaration.range);
10331034
} else {
10341035
this.error(DiagnosticCode._const_declarations_must_be_initialized, declaration.range);
10351036
}

src/diagnosticMessages.generated.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export enum DiagnosticCode {
66
Operation_not_supported = 102,
77
Operation_is_unsafe = 103,
88
Cannot_export_a_mutable_global = 104,
9-
Compiling_constant_global_with_non_constant_initializer_as_mutable = 105,
9+
Compiling_constant_with_non_constant_initializer_as_mutable = 105,
1010
Type_0_cannot_be_changed_to_type_1 = 106,
1111
Unterminated_string_literal = 1002,
1212
Identifier_expected = 1003,
@@ -87,7 +87,7 @@ export function diagnosticCodeToString(code: DiagnosticCode): string {
8787
case 102: return "Operation not supported.";
8888
case 103: return "Operation is unsafe.";
8989
case 104: return "Cannot export a mutable global.";
90-
case 105: return "Compiling constant global with non-constant initializer as mutable.";
90+
case 105: return "Compiling constant with non-constant initializer as mutable.";
9191
case 106: return "Type '{0}' cannot be changed to type '{1}'.";
9292
case 1002: return "Unterminated string literal.";
9393
case 1003: return "Identifier expected.";

src/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Operation not supported.": 102,
55
"Operation is unsafe.": 103,
66
"Cannot export a mutable global.": 104,
7-
"Compiling constant global with non-constant initializer as mutable.": 105,
7+
"Compiling constant with non-constant initializer as mutable.": 105,
88
"Type '{0}' cannot be changed to type '{1}'.": 106,
99

1010
"Unterminated string literal.": 1002,

src/module.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ export class Module {
264264
static create(): Module {
265265
var module = new Module();
266266
module.ref = _BinaryenModuleCreate();
267-
module.lit = changetype<usize,BinaryenLiteral>(Heap.allocate(16));
267+
module.lit = changetype<BinaryenLiteral>(Heap.allocate(16));
268268
module.noEmit = false;
269269
return module;
270270
}
@@ -274,18 +274,18 @@ export class Module {
274274
try {
275275
var module = new Module();
276276
module.ref = _BinaryenModuleRead(cArr, buffer.length);
277-
module.lit = changetype<usize,BinaryenLiteral>(Heap.allocate(16));
277+
module.lit = changetype<BinaryenLiteral>(Heap.allocate(16));
278278
module.noEmit = false;
279279
return module;
280280
} finally {
281-
Heap.dispose(changetype<usize,usize>(cArr));
281+
Heap.dispose(changetype<usize>(cArr));
282282
}
283283
}
284284

285285
static createStub(): Module {
286286
var module = new Module();
287287
module.ref = 0;
288-
module.lit = changetype<usize,BinaryenLiteral>(0);
288+
module.lit = changetype<BinaryenLiteral>(0);
289289
module.noEmit = true;
290290
return module;
291291
}
@@ -806,7 +806,7 @@ export class Module {
806806
dispose(): void {
807807
if (!this.ref) return; // sic
808808
_BinaryenModuleDispose(this.ref);
809-
Heap.dispose(changetype<BinaryenLiteral, usize>(this.lit));
809+
Heap.dispose(changetype<usize>(this.lit));
810810
}
811811

812812
createRelooper(): Relooper {

std/assembly.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ declare const HEAP_BASE: usize;
162162
/** Determines the byte size of the specified core or class type. Compiles to a constant. */
163163
declare function sizeof<T>(): usize;
164164
/** Changes the type of a value to another one. Useful for casting class instances to their pointer values and vice-versa. */
165-
declare function changetype<T1,T2>(value: T1): T2;
165+
declare function changetype<T>(value: any): T;
166166
/** Tests if a 32-bit or 64-bit float is `NaN`. */
167167
declare function isNaN<T = f32 | f64>(value: T): bool;
168168
/** Tests if a 32-bit or 64-bit float is finite, that is not `NaN` or +/-`Infinity`. */

std/assembly/array.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ export class Array<T> {
1818
}
1919

2020
dispose(): void {
21-
store<i64>(changetype<this,usize>(this), 0);
21+
store<i64>(changetype<usize>(this), 0);
2222
Heap.dispose(this.ptr);
2323
this.ptr = 0;
24-
Heap.dispose(changetype<this,usize>(this));
24+
Heap.dispose(changetype<usize>(this));
2525
}
2626

2727
// TODO

std/assembly/heap.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const ALIGN_LOG2: usize = 3;
22
const ALIGN_SIZE: usize = 1 << ALIGN_LOG2;
33
const ALIGN_MASK: usize = ALIGN_SIZE - 1;
44

5-
let HEAP_OFFSET: usize = HEAP_BASE; // HEAP_BASE is a constant generated by the compiler
5+
var HEAP_OFFSET: usize = HEAP_BASE; // HEAP_BASE is a constant generated by the compiler
66

77
// TODO: maybe tlsf
88

@@ -15,11 +15,11 @@ export class Heap {
1515

1616
static allocate(size: usize): usize {
1717
if (!size) return 0;
18-
const len: i32 = current_memory();
18+
var len: i32 = current_memory();
1919
if (HEAP_OFFSET + size > <usize>len << 16)
2020
if(grow_memory(max<i32>(<i32>ceil<f64>(<f64>size / 65536), len * 2 - len)) < 0)
2121
unreachable();
22-
const ptr: usize = HEAP_OFFSET;
22+
var ptr: usize = HEAP_OFFSET;
2323
if ((HEAP_OFFSET += size) & ALIGN_MASK) // align next offset
2424
HEAP_OFFSET = (HEAP_OFFSET | ALIGN_MASK) + 1;
2525
return ptr;
@@ -33,8 +33,8 @@ export class Heap {
3333
assert(dest >= HEAP_BASE);
3434

3535
// the following is based on musl's implementation of memcpy
36-
let dst: usize = dest;
37-
let w: u32, x: u32;
36+
var dst: usize = dest;
37+
var w: u32, x: u32;
3838

3939
// copy 1 byte each until src is aligned to 4 bytes
4040
while (n && src % 4) {
@@ -180,7 +180,7 @@ export class Heap {
180180
// the following is based on musl's implementation of memset
181181
if (!n) return dest;
182182

183-
let s: usize = dest;
183+
var s: usize = dest;
184184

185185
// Fill head and tail with minimal branching
186186
store<u8>(s, c); store<u8>(s + n - 1, c);
@@ -192,12 +192,12 @@ export class Heap {
192192
if (n <= 8) return dest;
193193

194194
// Align to 4 bytes
195-
let k: usize = -s & 3;
195+
var k: usize = -s & 3;
196196
s += k;
197197
n -= k;
198198
n &= -4;
199199

200-
let c32: u32 = -1 / 255 * c;
200+
var c32: u32 = -1 / 255 * c;
201201

202202
// Fill head and tail in preparation of setting 32 bytes at a time
203203
store<u32>(s, c32);
@@ -223,7 +223,7 @@ export class Heap {
223223
n -= k;
224224

225225
// Set 32 bytes at a time
226-
let c64: u64 = <u64>c32 | (<u64>c32 << 32);
226+
var c64: u64 = <u64>c32 | (<u64>c32 << 32);
227227
while (n >= 32) {
228228
store<u64>(s, c64);
229229
store<u64>(s + 8, c64);

0 commit comments

Comments
 (0)