Skip to content

Commit 792202a

Browse files
committed
Add an option to alias global objects
This for example allows to switch the default 'Math' implementation a program will use. Uses 'NativeMath' by default.
1 parent e75d006 commit 792202a

File tree

14 files changed

+186
-167
lines changed

14 files changed

+186
-167
lines changed

bin/asc.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,22 @@ exports.main = function main(argv, options, callback) {
363363
assemblyscript.setImportTable(compilerOptions, !!args.importTable);
364364
assemblyscript.setMemoryBase(compilerOptions, args.memoryBase >>> 0);
365365
assemblyscript.setSourceMap(compilerOptions, args.sourceMap != null);
366+
assemblyscript.setGlobalAlias(compilerOptions, "Math", "NativeMath");
367+
368+
// Add or override global aliases if specified
369+
var aliases = args.use;
370+
if (aliases != null) {
371+
if (typeof aliases === "string") aliases = aliases.split(",");
372+
for (let i = 0, k = aliases.length; i < k; ++i) {
373+
let part = aliases[i];
374+
let p = part.indexOf("=");
375+
if (p < 0) return callback(Error("Global alias '" + part + "' is invalid."));
376+
let name = part.substring(0, p).trim();
377+
let alias = part.substring(p + 1).trim();
378+
if (!name.length || !alias.length) return callback(Error("Global alias '" + part + "' is invalid."));
379+
assemblyscript.setGlobalAlias(compilerOptions, name, alias);
380+
}
381+
}
366382

367383
var module;
368384
stats.compileCount++;

bin/asc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@
122122
],
123123
"type": "string"
124124
},
125+
"use": {
126+
"desc": [
127+
"Aliases a global object under another name, e.g., to switch",
128+
"the default 'Math' implementation used: --use Math=JSMath"
129+
],
130+
"type": "string",
131+
"aliases": [ "u" ]
132+
},
125133
"trapMode": {
126134
"desc": [
127135
"Sets the trap mode to use.",

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/compiler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ export class Options {
155155
memoryBase: u32 = 0;
156156
/** If true, generates information necessary for source maps. */
157157
sourceMap: bool = false;
158+
/** Global aliases. */
159+
globalAliases: Map<string,string> | null = null;
158160

159161
/** Tests if the target is WASM64 or, otherwise, WASM32. */
160162
get isWasm64(): bool {

src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ export function setMemoryBase(options: Options, memoryBase: u32): void {
125125
options.memoryBase = memoryBase;
126126
}
127127

128+
/** Sets a 'globalAliases' value. */
129+
export function setGlobalAlias(options: Options, name: string, alias: string): void {
130+
var globalAliases = options.globalAliases;
131+
if (!globalAliases) options.globalAliases = globalAliases = new Map();
132+
globalAliases.set(name, alias);
133+
}
134+
128135
/** Finishes parsing. */
129136
export function finishParsing(parser: Parser): Program {
130137
return parser.finish();

src/program.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,15 @@ export class Program extends DiagnosticEmitter {
298298
);
299299
}
300300
}
301+
302+
// set up global aliases
303+
var globalAliases = options.globalAliases;
304+
if (globalAliases) {
305+
for (let [alias, name] of globalAliases) {
306+
let element = this.elementsLookup.get(name); // TODO: error? has no source range
307+
if (element) this.elementsLookup.set(alias, element);
308+
}
309+
}
301310
}
302311

303312
/** Tries to resolve an import by traversing exports and queued exports. */
@@ -319,8 +328,8 @@ export class Program extends DiagnosticEmitter {
319328
} while (true);
320329
}
321330

322-
/** Processes internal decorators, if present. */
323-
private checkInternalDecorators(
331+
/** Processes global options, if present. */
332+
private checkGlobalOptions(
324333
element: Element,
325334
declaration: DeclarationStatement
326335
): void {
@@ -469,7 +478,7 @@ export class Program extends DiagnosticEmitter {
469478
}
470479
}
471480

472-
this.checkInternalDecorators(prototype, declaration);
481+
this.checkGlobalOptions(prototype, declaration);
473482

474483
// check and possibly register string type
475484
if (
@@ -909,7 +918,7 @@ export class Program extends DiagnosticEmitter {
909918
this.initializeEnumValue(values[i], element);
910919
}
911920

912-
this.checkInternalDecorators(element, declaration);
921+
this.checkGlobalOptions(element, declaration);
913922
}
914923

915924
private initializeEnumValue(
@@ -1133,7 +1142,7 @@ export class Program extends DiagnosticEmitter {
11331142
}
11341143
}
11351144

1136-
this.checkInternalDecorators(prototype, declaration);
1145+
this.checkGlobalOptions(prototype, declaration);
11371146
}
11381147

11391148
private initializeImports(
@@ -1292,7 +1301,7 @@ export class Program extends DiagnosticEmitter {
12921301
}
12931302
}
12941303

1295-
this.checkInternalDecorators(prototype, declaration);
1304+
this.checkGlobalOptions(prototype, declaration);
12961305
}
12971306

12981307
private initializeNamespace(
@@ -1307,7 +1316,7 @@ export class Program extends DiagnosticEmitter {
13071316
namespace = new Namespace(this, simpleName, internalName, declaration);
13081317
namespace.namespace = parentNamespace;
13091318
this.elementsLookup.set(internalName, namespace);
1310-
this.checkInternalDecorators(namespace, declaration);
1319+
this.checkGlobalOptions(namespace, declaration);
13111320
}
13121321

13131322
if (parentNamespace) {
@@ -1472,7 +1481,7 @@ export class Program extends DiagnosticEmitter {
14721481
this.moduleLevelExports.set(internalName, global);
14731482
}
14741483
}
1475-
this.checkInternalDecorators(global, declaration);
1484+
this.checkGlobalOptions(global, declaration);
14761485
}
14771486
}
14781487

std/assembly.d.ts

Lines changed: 48 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -353,102 +353,56 @@ declare class Set<T> {
353353
clear(): void;
354354
}
355355

356-
declare namespace JSMath {
357-
export const E: f64;
358-
export const LN2: f64;
359-
export const LN10: f64;
360-
export const LOG2E: f64;
361-
export const LOG10E: f64;
362-
export const PI: f64;
363-
export const SQRT1_2: f64;
364-
export const SQRT2: f64;
365-
export function abs(x: f64): f64;
366-
export function acos(x: f64): f64;
367-
export function acosh(x: f64): f64;
368-
export function asin(x: f64): f64;
369-
export function asinh(x: f64): f64;
370-
export function atan(x: f64): f64;
371-
export function atan2(y: f64, x: f64): f64;
372-
export function atanh(x: f64): f64;
373-
export function cbrt(x: f64): f64;
374-
export function ceil(x: f64): f64;
375-
export function clz32(x: f64): i32;
376-
export function cos(x: f64): f64;
377-
export function cosh(x: f64): f64;
378-
export function exp(x: f64): f64;
379-
export function expm1(x: f64): f64;
380-
export function floor(x: f64): f64;
381-
export function fround(x: f64): f32;
382-
export function hypot(value1: f64, value2: f64): f64; // TODO: see std/math
383-
export function imul(a: f64, b: f64): i32;
384-
export function log(x: f64): f64;
385-
export function log10(x: f64): f64;
386-
export function log1p(x: f64): f64;
387-
export function log2(x: f64): f64;
388-
export function max(value1: f64, value2: f64): f64; // TODO: see std/math
389-
export function min(value1: f64, value2: f64): f64; // TODO: see std/math
390-
export function pow(base: f64, exponent: f64): f64;
391-
export function random(): f64;
392-
export function round(x: f64): f64;
393-
export function sign(x: f64): f64;
394-
export function sin(x: f64): f64;
395-
export function sinh(x: f64): f64;
396-
export function sqrt(x: f64): f64;
397-
export function tan(x: f64): f64;
398-
export function tanh(x: f64): f64;
399-
export function trunc(x: f64): f64;
356+
interface IMath<T> {
357+
readonly E: T;
358+
readonly LN2: T;
359+
readonly LN10: T;
360+
readonly LOG2E: T;
361+
readonly LOG10E: T;
362+
readonly PI: T;
363+
readonly SQRT1_2: T;
364+
readonly SQRT2: T;
365+
abs(x: T): T;
366+
acos(x: T): T;
367+
acosh(x: T): T;
368+
asin(x: T): T;
369+
asinh(x: T): T;
370+
atan(x: T): T;
371+
atan2(y: T, x: T): T;
372+
atanh(x: T): T;
373+
cbrt(x: T): T;
374+
ceil(x: T): T;
375+
clz32(x: T): i32;
376+
cos(x: T): T;
377+
cosh(x: T): T;
378+
exp(x: T): T;
379+
expm1(x: T): T;
380+
floor(x: T): T;
381+
fround(x: T): f32;
382+
hypot(value1: T, value2: T): T; // TODO: rest
383+
imul(a: T, b: T): i32;
384+
log(x: T): T;
385+
log10(x: T): T;
386+
log1p(x: T): T;
387+
log2(x: T): T;
388+
max(value1: T, value2: T): T; // TODO: rest
389+
min(value1: T, value2: T): T; // TODO: rest
390+
pow(base: T, exponent: T): T;
391+
random(): T;
392+
round(x: T): T;
393+
sign(x: T): T;
394+
sin(x: T): T;
395+
sinh(x: T): T;
396+
sqrt(x: T): T;
397+
tan(x: T): T;
398+
tanh(x: T): T;
399+
trunc(x: T): T;
400400
}
401401

402-
declare namespace Math {
403-
export const E: f64;
404-
export const LN2: f64;
405-
export const LN10: f64;
406-
export const LOG2E: f64;
407-
export const LOG10E: f64;
408-
export const PI: f64;
409-
export const SQRT1_2: f64;
410-
export const SQRT2: f64;
411-
export function abs(x: f64): f64;
412-
export function ceil(x: f64): f64;
413-
export function clz32(x: f64): i32;
414-
export function exp(x: f64): f64;
415-
export function floor(x: f64): f64;
416-
export function fround(x: f64): f32;
417-
export function imul(a: f64, b: f64): i32;
418-
export function log(x: f64): f64;
419-
export function max(value1: f64, value2: f64): f64; // TODO: see std/math
420-
export function min(value1: f64, value2: f64): f64; // TODO: see std/math
421-
export function pow(x: f64, y: f64): f64;
422-
export function round(x: f64): f64;
423-
export function sign(x: f64): f64;
424-
export function sqrt(x: f64): f64;
425-
export function trunc(x: f64): f64;
426-
}
427-
428-
declare namespace Mathf {
429-
export const E: f32;
430-
export const LN2: f32;
431-
export const LN10: f32;
432-
export const LOG2E: f32;
433-
export const LOG10E: f32;
434-
export const PI: f32;
435-
export const SQRT1_2: f32;
436-
export const SQRT2: f32;
437-
export function abs(x: f32): f32;
438-
export function exp(x: f32): f32;
439-
export function ceil(x: f32): f32;
440-
export function clz32(x: f32): i32;
441-
export function floor(x: f32): f32;
442-
export function imul(a: f32, b: f32): i32;
443-
export function log(x: f32): f32;
444-
export function max(value1: f32, value2: f32): f32; // TODO: see std/math
445-
export function min(value1: f32, value2: f32): f32; // TODO: see std/math
446-
export function pow(x: f32, y: f32): f32;
447-
export function round(x: f32): f32;
448-
export function sign(x: f32): f32;
449-
export function sqrt(x: f32): f32;
450-
export function trunc(x: f32): f32;
451-
}
402+
declare const JSMath: IMath<f64>;
403+
declare const NativeMath: IMath<f64>;
404+
declare const NativeMathf: IMath<f32>;
405+
declare const Math: IMath<f64>;
452406

453407
// Internal decorators
454408

std/assembly/math.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ import {
5858
trunc as builtin_trunc
5959
} from "./builtins";
6060

61-
// Math/Mathf.log/exp/pow
61+
// NativeMath/NativeMathf.log/exp/pow
6262
// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6363
// Developed at SunPro, a Sun Microsystems, Inc. business.
6464
// Permission to use, copy, modify, and distribute this
6565
// software is freely granted, provided that this notice
6666
// is preserved.
6767

68-
export namespace Math {
68+
export namespace NativeMath {
6969

7070
export const E = 2.7182818284590452354;
7171
export const LN2 = 0.69314718055994530942;
@@ -468,16 +468,16 @@ export namespace Math {
468468
}
469469
}
470470

471-
export namespace Mathf {
471+
export namespace NativeMathf {
472472

473-
export const E = <f32>Math.E;
474-
export const LN2 = <f32>Math.LN2;
475-
export const LN10 = <f32>Math.LN10;
476-
export const LOG2E = <f32>Math.LOG2E;
477-
export const LOG10E = <f32>Math.LOG10E;
478-
export const PI = <f32>Math.PI;
479-
export const SQRT1_2 = <f32>Math.SQRT1_2;
480-
export const SQRT2 = <f32>Math.SQRT2;
473+
export const E = <f32>NativeMath.E;
474+
export const LN2 = <f32>NativeMath.LN2;
475+
export const LN10 = <f32>NativeMath.LN10;
476+
export const LOG2E = <f32>NativeMath.LOG2E;
477+
export const LOG10E = <f32>NativeMath.LOG10E;
478+
export const PI = <f32>NativeMath.PI;
479+
export const SQRT1_2 = <f32>NativeMath.SQRT1_2;
480+
export const SQRT2 = <f32>NativeMath.SQRT2;
481481

482482
export function abs(x: f32): f32 {
483483
return builtin_abs(x);

0 commit comments

Comments
 (0)