Skip to content

Commit 06198a3

Browse files
committed
Rename lib prefix to '~lib' (parens aren't valid); Add built-in alignof<T>; Prepare for ArrayBufferView
1 parent 3b50720 commit 06198a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2281
-2204
lines changed

bin/asc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ exports.compileString = (source, extraArgs={}) => new Promise((resolve, reject)
9393
const libDir = path.join(__dirname, "../std", "assembly");
9494
const libFiles = require("glob").sync("**/*.ts", { cwd: libDir });
9595
libFiles.forEach(file =>
96-
exports.libraryFiles["(lib)/" + file.replace(/\.ts$/, "")] = readFileNode(path.join(libDir, file), { encoding: "utf8" })
96+
exports.libraryFiles["~lib/" + file.replace(/\.ts$/, "")] = readFileNode(path.join(libDir, file), { encoding: "utf8" })
9797
);
9898
}
9999

@@ -265,7 +265,7 @@ exports.main = function main(argv, options, callback) {
265265
}
266266
}
267267

268-
// Otherwise try nextFile.ts, nextFile/index.ts, (lib)/nextFile.ts
268+
// Otherwise try nextFile.ts, nextFile/index.ts, ~lib/nextFile.ts
269269
} else {
270270
sourceText = readFile(path.join(baseDir, sourcePath + ".ts"));
271271
if (sourceText !== null) {

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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ export abstract class Node {
534534
identifier: IdentifierExpression,
535535
typeParameters: TypeParameterNode[],
536536
extendsType: TypeNode | null, // can't be a function
537-
implementsTypes: TypeNode[], // can't be a function
537+
implementsTypes: TypeNode[] | null, // can't be functions
538538
members: DeclarationStatement[],
539539
decorators: DecoratorNode[] | null,
540540
flags: CommonFlags,
@@ -546,7 +546,7 @@ export abstract class Node {
546546
stmt.name = identifier; identifier.parent = stmt;
547547
stmt.typeParameters = typeParameters; setParent(typeParameters, stmt);
548548
stmt.extendsType = extendsType; if (extendsType) extendsType.parent = stmt;
549-
stmt.implementsTypes = implementsTypes; setParent(implementsTypes, stmt);
549+
stmt.implementsTypes = implementsTypes; if (implementsTypes) setParent(implementsTypes, stmt);
550550
stmt.members = members; setParent(members, stmt);
551551
stmt.decorators = decorators; if (decorators) setParent(decorators, stmt);
552552
return stmt;
@@ -756,17 +756,21 @@ export abstract class Node {
756756

757757
static createInterfaceDeclaration(
758758
name: IdentifierExpression,
759+
typeParameters: TypeParameterNode[],
759760
extendsType: TypeNode | null, // can't be a function
760761
members: DeclarationStatement[],
762+
decorators: DecoratorNode[] | null,
761763
flags: CommonFlags,
762764
range: Range
763765
): InterfaceDeclaration {
764766
var stmt = new InterfaceDeclaration();
765767
stmt.range = range;
766768
stmt.flags = flags;
767769
stmt.name = name; name.parent = stmt;
770+
stmt.typeParameters = typeParameters; if (typeParameters) setParent(typeParameters, stmt);
768771
stmt.extendsType = extendsType; if (extendsType) extendsType.parent = stmt;
769772
stmt.members = members; setParent(members, stmt);
773+
stmt.decorators = decorators; if (decorators) setParent(decorators, stmt);
770774
return stmt;
771775
}
772776

@@ -1487,10 +1491,10 @@ export class ClassDeclaration extends DeclarationStatement {
14871491

14881492
/** Accepted type parameters. */
14891493
typeParameters: TypeParameterNode[];
1490-
/** Base class type being extended. */
1494+
/** Base class type being extended, if any. */
14911495
extendsType: TypeNode | null; // can't be a function
1492-
/** Interface types being implemented. */
1493-
implementsTypes: TypeNode[]; // can't be a function
1496+
/** Interface types being implemented, if any. */
1497+
implementsTypes: TypeNode[] | null; // can't be functions
14941498
/** Class member declarations. */
14951499
members: DeclarationStatement[];
14961500

src/builtins.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,55 @@ export function compileCall(
17901790
}
17911791
return ret;
17921792
}
1793+
case "alignof": { // alignof<T!>() -> usize
1794+
compiler.currentType = compiler.options.usizeType;
1795+
if (operands.length != 0) {
1796+
if (!(typeArguments && typeArguments.length == 1)) {
1797+
compiler.error(
1798+
DiagnosticCode.Expected_0_type_arguments_but_got_1,
1799+
reportNode.range, "1", typeArguments ? typeArguments.length.toString(10) : "0"
1800+
);
1801+
}
1802+
compiler.error(
1803+
DiagnosticCode.Expected_0_arguments_but_got_1,
1804+
reportNode.range, "0", operands.length.toString(10)
1805+
);
1806+
return module.createUnreachable();
1807+
}
1808+
if (!(typeArguments && typeArguments.length == 1)) {
1809+
compiler.error(
1810+
DiagnosticCode.Expected_0_type_arguments_but_got_1,
1811+
reportNode.range, "1", typeArguments ? typeArguments.length.toString(10) : "0"
1812+
);
1813+
}
1814+
let byteSize = (<Type[]>typeArguments)[0].byteSize;
1815+
let alignLog2: i32;
1816+
switch (byteSize) {
1817+
case 1: { alignLog2 = 0; break; }
1818+
case 2: { alignLog2 = 1; break; }
1819+
case 4: { alignLog2 = 2; break; }
1820+
case 8: { alignLog2 = 3; break; }
1821+
default: { assert(false); return module.createUnreachable(); }
1822+
}
1823+
if (compiler.options.isWasm64) {
1824+
// implicitly wrap if contextual type is a 32-bit integer
1825+
if (contextualType.is(TypeFlags.INTEGER) && contextualType.size <= 32) {
1826+
compiler.currentType = Type.u32;
1827+
ret = module.createI32(alignLog2);
1828+
} else {
1829+
ret = module.createI64(alignLog2, 0);
1830+
}
1831+
} else {
1832+
// implicitly extend if contextual type is a 64-bit integer
1833+
if (contextualType.is(TypeFlags.INTEGER) && contextualType.size == 64) {
1834+
compiler.currentType = Type.u64;
1835+
ret = module.createI64(alignLog2, 0);
1836+
} else {
1837+
ret = module.createI32(alignLog2);
1838+
}
1839+
}
1840+
return ret;
1841+
}
17931842
case "offsetof": { // offsetof<T!>(fieldName?: string) -> usize
17941843
compiler.currentType = compiler.options.usizeType;
17951844
if (operands.length > 1) {

src/compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ export class Compiler extends DiagnosticEmitter {
367367
}
368368
}
369369

370-
// try (lib)/file.ts
370+
// try ~lib/file.ts
371371
expected = LIBRARY_PREFIX + normalizedPathWithoutExtension + ".ts";
372372
for (let i = 0, k = sources.length; i < k; ++i) {
373373
let source = sources[i];

src/diagnosticMessages.generated.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export enum DiagnosticCode {
6464
Declaration_expected = 1146,
6565
_const_declarations_must_be_initialized = 1155,
6666
Unterminated_regular_expression_literal = 1161,
67+
Interface_declaration_cannot_have_implements_clause = 1176,
6768
Binary_digit_expected = 1177,
6869
Octal_digit_expected = 1178,
6970
An_implementation_cannot_be_declared_in_ambient_contexts = 1183,
@@ -169,6 +170,7 @@ export function diagnosticCodeToString(code: DiagnosticCode): string {
169170
case 1146: return "Declaration expected.";
170171
case 1155: return "'const' declarations must be initialized.";
171172
case 1161: return "Unterminated regular expression literal.";
173+
case 1176: return "Interface declaration cannot have 'implements' clause.";
172174
case 1177: return "Binary digit expected.";
173175
case 1178: return "Octal digit expected.";
174176
case 1183: return "An implementation cannot be declared in ambient contexts.";

src/diagnosticMessages.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"Declaration expected.": 1146,
5858
"'const' declarations must be initialized.": 1155,
5959
"Unterminated regular expression literal.": 1161,
60+
"Interface declaration cannot have 'implements' clause.": 1176,
6061
"Binary digit expected.": 1177,
6162
"Octal digit expected.": 1178,
6263
"An implementation cannot be declared in ambient contexts.": 1183,

0 commit comments

Comments
 (0)