Minor cleanup

This commit is contained in:
dcodeIO 2018-03-31 18:18:55 +02:00
parent 3d1f181961
commit 6ff69394f0
10 changed files with 65 additions and 40 deletions

View File

@ -52,16 +52,18 @@ $> asc myModule.ts -o myModule.wasm --optimize --validate --sourceMap
Building Building
-------- --------
To build an UMD bundle to `dist/assemblyscript.js` ([binaryen.js](https://github.com/AssemblyScript/binaryen.js) remains an external dependency): To build an UMD bundle to `dist/assemblyscript.js` (depends on [binaryen.js](https://github.com/AssemblyScript/binaryen.js)):
``` ```
$> npm run build $> npm run build
``` ```
This also builds a browser version of [asc](./bin) to `dist/asc.js` (depends on assemblyscript.js).
Running the [tests](./tests): Running the [tests](./tests):
``` ```
$> npm test $> npm test
``` ```
**Note** that freshly cloned copies of the compiler will use ts-node to run [the sources](./src) directly, which is useful in development. Once built, `asc` will use the distribution files instead. This can also be checked by running `asc -v` (it is running the sources if it states `-dev`). **Note** that freshly cloned copies of the compiler will use the distribution files, but it can also run [the sources](./src) directly through ts-node after an `npm run clean`, which is useful in development. This condition can also be checked by running `asc -v` (it is running the sources if it states `-dev`).

View File

@ -815,13 +815,13 @@ var Buf = typeof global !== "undefined" && global.Buffer || Uint8Array;
function createMemoryStream(fn) { function createMemoryStream(fn) {
var stream = []; var stream = [];
stream.write = function(chunk) { stream.write = function(chunk) {
if (fn) fn(chunk);
if (typeof chunk === "string") { if (typeof chunk === "string") {
let buffer = new Buf(utf8.length(chunk)); let buffer = new Buf(utf8.length(chunk));
utf8.write(chunk, buffer, 0); utf8.write(chunk, buffer, 0);
chunk = buffer; chunk = buffer;
} }
this.push(chunk); this.push(chunk);
if (fn) fn(chunk);
}; };
stream.toBuffer = function() { stream.toBuffer = function() {
var offset = 0, i = 0, k = this.length; var offset = 0, i = 0, k = this.length;

2
dist/asc.js vendored

File diff suppressed because one or more lines are too long

2
dist/asc.js.map vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -192,16 +192,12 @@ export function compileCall(
compiler.currentType = Type.bool; compiler.currentType = Type.bool;
let classType = type.classReference; let classType = type.classReference;
if (classType) { if (classType) {
let stringPrototype = compiler.program.elementsLookup.get("String"); let stringInstance = compiler.program.stringInstance;
if (stringPrototype) {
assert(stringPrototype.kind == ElementKind.CLASS_PROTOTYPE);
let stringInstance = (<ClassPrototype>stringPrototype).resolve(null);
if (!stringInstance) return module.createUnreachable(); if (!stringInstance) return module.createUnreachable();
if (classType.isAssignableTo(stringInstance)) { if (classType.isAssignableTo(stringInstance)) {
return module.createI32(1); return module.createI32(1);
} }
} }
}
return module.createI32(0); return module.createI32(0);
} }
case "isArray": { case "isArray": {

View File

@ -4976,7 +4976,7 @@ export class Compiler extends DiagnosticEmitter {
let classType = contextualType.classReference; let classType = contextualType.classReference;
if ( if (
classType && classType &&
classType.prototype == this.program.elementsLookup.get("Array") classType.prototype == this.program.arrayPrototype
) { ) {
return this.compileStaticArray( return this.compileStaticArray(
assert(classType.typeArguments)[0], assert(classType.typeArguments)[0],
@ -5137,7 +5137,7 @@ export class Compiler extends DiagnosticEmitter {
var module = this.module; var module = this.module;
// obtain the array type // obtain the array type
var arrayPrototype = assert(this.program.elementsLookup.get("Array")); var arrayPrototype = assert(this.program.arrayPrototype);
if (!arrayPrototype || arrayPrototype.kind != ElementKind.CLASS_PROTOTYPE) return module.createUnreachable(); if (!arrayPrototype || arrayPrototype.kind != ElementKind.CLASS_PROTOTYPE) return module.createUnreachable();
var arrayType = (<ClassPrototype>arrayPrototype).resolve([ elementType ]); var arrayType = (<ClassPrototype>arrayPrototype).resolve([ elementType ]);
if (!arrayType) return module.createUnreachable(); if (!arrayType) return module.createUnreachable();

View File

@ -131,6 +131,10 @@ export class Program extends DiagnosticEmitter {
fileLevelExports: Map<string,Element> = new Map(); fileLevelExports: Map<string,Element> = new Map();
/** Module-level exports by exported name. */ /** Module-level exports by exported name. */
moduleLevelExports: Map<string,Element> = new Map(); moduleLevelExports: Map<string,Element> = new Map();
/** Array prototype reference. */
arrayPrototype: ClassPrototype | null = null;
/** String instance reference. */
stringInstance: Class | null = null;
/** Constructs a new program, optionally inheriting parser diagnostics. */ /** Constructs a new program, optionally inheriting parser diagnostics. */
constructor(diagnostics: DiagnosticMessage[] | null = null) { constructor(diagnostics: DiagnosticMessage[] | null = null) {
@ -307,6 +311,32 @@ export class Program extends DiagnosticEmitter {
if (element) this.elementsLookup.set(alias, element); if (element) this.elementsLookup.set(alias, element);
} }
} }
// register array
var arrayPrototype = this.elementsLookup.get("Array");
if (arrayPrototype) {
assert(arrayPrototype.kind == ElementKind.CLASS_PROTOTYPE);
this.arrayPrototype = <ClassPrototype>arrayPrototype;
}
// register string
var stringPrototype = this.elementsLookup.get("String");
if (stringPrototype) {
assert(stringPrototype.kind == ElementKind.CLASS_PROTOTYPE);
let stringInstance = (<ClassPrototype>stringPrototype).resolve(null); // reports
if (stringInstance) {
if (this.typesLookup.has("string")) {
let declaration = (<ClassPrototype>stringPrototype).declaration;
this.error(
DiagnosticCode.Duplicate_identifier_0,
declaration.name.range, declaration.programLevelInternalName
);
} else {
this.stringInstance = stringInstance;
this.typesLookup.set("string", stringInstance.type);
}
}
}
} }
/** Tries to resolve an import by traversing exports and queued exports. */ /** Tries to resolve an import by traversing exports and queued exports. */
@ -479,22 +509,6 @@ export class Program extends DiagnosticEmitter {
} }
this.checkGlobalOptions(prototype, declaration); this.checkGlobalOptions(prototype, declaration);
// check and possibly register string type
if (
prototype.is(CommonFlags.GLOBAL) &&
declaration.name.text == "String"
) {
if (!this.typesLookup.has("string")) {
let instance = prototype.resolve(null);
if (instance) this.typesLookup.set("string", instance.type);
} else {
this.error(
DiagnosticCode.Duplicate_identifier_0,
declaration.name.range, declaration.programLevelInternalName
);
}
}
} }
private initializeField( private initializeField(
@ -1823,7 +1837,7 @@ export class Program extends DiagnosticEmitter {
elementAccess: ElementAccessExpression, elementAccess: ElementAccessExpression,
contextualFunction: Function contextualFunction: Function
): ResolvedElement | null { ): ResolvedElement | null {
// start by resolving the lhs target (expression before the last dot) // start by resolving the lhs target
var targetExpression = elementAccess.expression; var targetExpression = elementAccess.expression;
resolvedElement = this.resolveExpression( resolvedElement = this.resolveExpression(
targetExpression, targetExpression,
@ -1850,6 +1864,7 @@ export class Program extends DiagnosticEmitter {
} }
break; break;
} }
// FIXME: indexed access on indexed access
} }
this.error( this.error(
DiagnosticCode.Index_signature_is_missing_in_type_0, DiagnosticCode.Index_signature_is_missing_in_type_0,
@ -2957,11 +2972,13 @@ export class ClassPrototype extends Element {
throw new Error("type argument count mismatch"); throw new Error("type argument count mismatch");
} }
var simpleName = this.simpleName;
var internalName = this.internalName; var internalName = this.internalName;
if (instanceKey.length) { if (instanceKey.length) {
simpleName += "<" + instanceKey + ">";
internalName += "<" + instanceKey + ">"; internalName += "<" + instanceKey + ">";
} }
instance = new Class(this, internalName, typeArguments, baseClass); instance = new Class(this, simpleName, internalName, typeArguments, baseClass);
instance.contextualTypeArguments = contextualTypeArguments; instance.contextualTypeArguments = contextualTypeArguments;
this.instances.set(instanceKey, instance); this.instances.set(instanceKey, instance);
@ -3119,11 +3136,12 @@ export class Class extends Element {
/** Constructs a new class. */ /** Constructs a new class. */
constructor( constructor(
prototype: ClassPrototype, prototype: ClassPrototype,
simpleName: string,
internalName: string, internalName: string,
typeArguments: Type[] | null = null, typeArguments: Type[] | null = null,
base: Class | null = null base: Class | null = null
) { ) {
super(prototype.program, prototype.simpleName, internalName); super(prototype.program, simpleName, internalName);
this.prototype = prototype; this.prototype = prototype;
this.flags = prototype.flags; this.flags = prototype.flags;
this.typeArguments = typeArguments; this.typeArguments = typeArguments;
@ -3173,7 +3191,7 @@ export class Class extends Element {
} }
toString(): string { toString(): string {
return this.prototype.simpleName; return this.simpleName;
} }
} }
@ -3209,11 +3227,12 @@ export class Interface extends Class {
/** Constructs a new interface. */ /** Constructs a new interface. */
constructor( constructor(
prototype: InterfacePrototype, prototype: InterfacePrototype,
simpleName: string,
internalName: string, internalName: string,
typeArguments: Type[] = [], typeArguments: Type[] = [],
base: Interface | null = null base: Interface | null = null
) { ) {
super(prototype, internalName, typeArguments, base); super(prototype, simpleName, internalName, typeArguments, base);
} }
} }

View File

@ -1,7 +1,15 @@
const asc = require("../dist/asc.js"); const asc = require("../dist/asc.js");
const stdout = asc.createMemoryStream(); function log(arg) {
const stderr = asc.createMemoryStream(); console.log(arg);
}
const stdout = asc.createMemoryStream(arg => {
console.log("out:", arg);
});
const stderr = asc.createMemoryStream(arg => {
console.log("err:", arg);
});
const files = { const files = {
"/module.ts": `import "allocator/arena";` "/module.ts": `import "allocator/arena";`