@@ -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`. */
143147class 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