Skip to content

Commit 9876c3f

Browse files
authored
Add WASI abort, trace and seed implementations (#1159)
1 parent b7df27c commit 9876c3f

Some content is hidden

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

41 files changed

+12910
-4749
lines changed

cli/asc.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,6 @@ exports.main = function main(argv, options, callback) {
248248
assemblyscript.setNoUnsafe(compilerOptions, args.noUnsafe);
249249
assemblyscript.setPedantic(compilerOptions, args.pedantic);
250250

251-
// Initialize default aliases
252-
assemblyscript.setGlobalAlias(compilerOptions, "Math", "NativeMath");
253-
assemblyscript.setGlobalAlias(compilerOptions, "Mathf", "NativeMathf");
254-
assemblyscript.setGlobalAlias(compilerOptions, "abort", "~lib/builtins/abort");
255-
assemblyscript.setGlobalAlias(compilerOptions, "trace", "~lib/builtins/trace");
256-
257251
// Add or override aliases if specified
258252
if (args.use) {
259253
let aliases = args.use;

lib/loader/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,15 @@ function preInstantiate(imports) {
6969
const env = (imports.env = imports.env || {});
7070
env.abort = env.abort || function abort(mesg, file, line, colm) {
7171
const memory = baseModule.memory || env.memory; // prefer exported, otherwise try imported
72-
throw Error("abort: " + getString(memory, mesg) + " at " + getString(memory, file) + ":" + line + ":" + colm);
73-
}
72+
throw Error("abort: " + getString(memory, mesg) + " in " + getString(memory, file) + "(" + line + ":" + colm + ")");
73+
};
7474
env.trace = env.trace || function trace(mesg, n) {
7575
const memory = baseModule.memory || env.memory;
7676
console.log("trace: " + getString(memory, mesg) + (n ? " " : "") + Array.prototype.slice.call(arguments, 2, 2 + n).join(", "));
77-
}
77+
};
78+
env.seed = env.seed || function seed() {
79+
return Date.now();
80+
};
7881
imports.Math = imports.Math || Math;
7982
imports.Date = imports.Date || Date;
8083

src/builtins.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ export namespace BuiltinNames {
119119
export const setArgumentsLength = "~setArgumentsLength";
120120

121121
// std/builtins.ts
122+
export const abort = "~lib/builtins/abort";
123+
export const trace = "~lib/builtins/trace";
124+
export const seed = "~lib/builtins/seed";
125+
122126
export const isInteger = "~lib/builtins/isInteger";
123127
export const isFloat = "~lib/builtins/isFloat";
124128
export const isBoolean = "~lib/builtins/isBoolean";
@@ -583,6 +587,11 @@ export namespace BuiltinNames {
583587
export const Uint64Array = "~lib/typedarray/Uint64Array";
584588
export const Float32Array = "~lib/typedarray/Float32Array";
585589
export const Float64Array = "~lib/typedarray/Float64Array";
590+
591+
// std/bindings/wasi.ts
592+
export const wasiAbort = "~lib/wasi/index/abort";
593+
export const wasiTrace = "~lib/wasi/index/trace";
594+
export const wasiSeed = "~lib/wasi/index/seed";
586595
}
587596

588597
/** Builtin compilation context. */

src/common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export namespace CommonNames {
154154
export const ASC_TABLE_BASE = "ASC_TABLE_BASE";
155155
export const ASC_OPTIMIZE_LEVEL = "ASC_OPTIMIZE_LEVEL";
156156
export const ASC_SHRINK_LEVEL = "ASC_SHRINK_LEVEL";
157+
export const ASC_WASI = "ASC_WASI";
157158
export const ASC_FEATURE_SIGN_EXTENSION = "ASC_FEATURE_SIGN_EXTENSION";
158159
export const ASC_FEATURE_MUTABLE_GLOBALS = "ASC_FEATURE_MUTABLE_GLOBALS";
159160
export const ASC_FEATURE_NONTRAPPING_F2I = "ASC_FEATURE_NONTRAPPING_F2I";
@@ -189,6 +190,8 @@ export namespace CommonNames {
189190
export const ArrayBuffer = "ArrayBuffer";
190191
export const Math = "Math";
191192
export const Mathf = "Mathf";
193+
export const NativeMath = "NativeMath";
194+
export const NativeMathf = "NativeMathf";
192195
export const Int8Array = "Int8Array";
193196
export const Int16Array = "Int16Array";
194197
export const Int32Array = "Int32Array";
@@ -203,6 +206,8 @@ export namespace CommonNames {
203206
export const Error = "Error";
204207
// runtime
205208
export const abort = "abort";
209+
export const trace = "trace";
210+
export const seed = "seed";
206211
export const pow = "pow";
207212
export const mod = "mod";
208213
export const alloc = "__alloc";

src/compiler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ export class Compiler extends DiagnosticEmitter {
426426

427427
// compile the start function if not empty or if explicitly requested
428428
var startIsEmpty = !startFunctionBody.length;
429-
var explicitStart = options.explicitStart;
429+
var explicitStart = program.isWasi || options.explicitStart;
430430
if (!startIsEmpty || explicitStart) {
431431
let signature = startFunctionInstance.signature;
432432
if (!startIsEmpty && explicitStart) {
@@ -8832,7 +8832,7 @@ export class Compiler extends DiagnosticEmitter {
88328832
reportNode: Node
88338833
): ExpressionRef {
88348834
var ctor = this.ensureConstructor(classInstance, reportNode);
8835-
if (ctor.hasDecorator(DecoratorFlags.UNSAFE)) this.checkUnsafe(reportNode);
8835+
if (classInstance.type.isUnmanaged || ctor.hasDecorator(DecoratorFlags.UNSAFE)) this.checkUnsafe(reportNode);
88368836
var expr = this.compileCallDirect( // no need for another autoreleased local
88378837
ctor,
88388838
argumentExpressions,

src/program.ts

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ import {
139139
Parser
140140
} from "./parser";
141141

142+
import {
143+
BuiltinNames
144+
} from "./builtins";
145+
142146
/** Represents a yet unresolved `import`. */
143147
class QueuedImport {
144148
constructor(
@@ -513,6 +517,12 @@ export class Program extends DiagnosticEmitter {
513517
nextSignatureId: i32 = 0;
514518
/** An indicator if the program has been initialized. */
515519
initialized: bool = false;
520+
521+
/** Tests whether this is a WASI program. */
522+
get isWasi(): bool {
523+
return this.elementsByName.has(CommonNames.ASC_WASI);
524+
}
525+
516526
/** Constructs a new program, optionally inheriting parser diagnostics. */
517527
constructor(
518528
/** Compiler options. */
@@ -1000,23 +1010,49 @@ export class Program extends DiagnosticEmitter {
10001010
// set up global aliases
10011011
{
10021012
let globalAliases = options.globalAliases;
1003-
if (globalAliases) {
1004-
// TODO: for (let [alias, name] of globalAliases) {
1005-
for (let _keys = Map_keys(globalAliases), i = 0, k = _keys.length; i < k; ++i) {
1006-
let alias = unchecked(_keys[i]);
1007-
let name = assert(globalAliases.get(alias));
1008-
if (!name.length) continue; // explicitly disabled
1009-
let firstChar = name.charCodeAt(0);
1010-
if (firstChar >= CharCode._0 && firstChar <= CharCode._9) {
1011-
this.registerConstantInteger(alias, Type.i32, i64_new(<i32>parseInt(name, 10)));
1013+
if (!globalAliases) globalAliases = new Map();
1014+
let isWasi = this.isWasi;
1015+
if (!globalAliases.has(CommonNames.abort)) {
1016+
globalAliases.set(CommonNames.abort,
1017+
isWasi
1018+
? BuiltinNames.wasiAbort
1019+
: BuiltinNames.abort
1020+
);
1021+
}
1022+
if (!globalAliases.has(CommonNames.trace)) {
1023+
globalAliases.set(CommonNames.trace,
1024+
isWasi
1025+
? BuiltinNames.wasiTrace
1026+
: BuiltinNames.trace
1027+
);
1028+
}
1029+
if (!globalAliases.has(CommonNames.seed)) {
1030+
globalAliases.set(CommonNames.seed,
1031+
isWasi
1032+
? BuiltinNames.wasiSeed
1033+
: BuiltinNames.seed
1034+
);
1035+
}
1036+
if (!globalAliases.has(CommonNames.Math)) {
1037+
globalAliases.set(CommonNames.Math, CommonNames.NativeMath);
1038+
}
1039+
if (!globalAliases.has(CommonNames.Mathf)) {
1040+
globalAliases.set(CommonNames.Mathf, CommonNames.NativeMathf);
1041+
}
1042+
// TODO: for (let [alias, name] of globalAliases) {
1043+
for (let _keys = Map_keys(globalAliases), i = 0, k = _keys.length; i < k; ++i) {
1044+
let alias = unchecked(_keys[i]);
1045+
let name = assert(globalAliases.get(alias));
1046+
if (!name.length) continue; // explicitly disabled
1047+
let firstChar = name.charCodeAt(0);
1048+
if (firstChar >= CharCode._0 && firstChar <= CharCode._9) {
1049+
this.registerConstantInteger(alias, Type.i32, i64_new(<i32>parseInt(name, 10)));
1050+
} else {
1051+
let elementsByName = this.elementsByName;
1052+
if (elementsByName.has(name)) {
1053+
elementsByName.set(alias, assert(elementsByName.get(name)));
10121054
} else {
1013-
let elementsByName = this.elementsByName;
1014-
let element = elementsByName.get(name);
1015-
if (element) {
1016-
if (elementsByName.has(alias)) throw new Error("duplicate global element: " + name);
1017-
elementsByName.set(alias, element);
1018-
}
1019-
else throw new Error("no such global element: " + name);
1055+
throw new Error("no such global element: " + name);
10201056
}
10211057
}
10221058
}

0 commit comments

Comments
 (0)