Skip to content

Commit 3b5b96f

Browse files
authored
Fix declaration mismatch in generated constructors (AssemblyScript#505)
1 parent 4be7814 commit 3b5b96f

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

src/compiler.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ import {
4848
SETTER_PREFIX,
4949
LibrarySymbols,
5050
CommonSymbols,
51-
INDEX_SUFFIX,
52-
LIBRARY_PREFIX
51+
INDEX_SUFFIX
5352
} from "./common";
5453

5554
import {
@@ -147,7 +146,8 @@ import {
147146

148147
nodeIsConstantValue,
149148
findDecorator,
150-
FieldDeclaration
149+
FieldDeclaration,
150+
FunctionDeclaration
151151
} from "./ast";
152152

153153
import {
@@ -5716,10 +5716,11 @@ export class Compiler extends DiagnosticEmitter {
57165716
}
57175717
let parameterTypes = instance.signature.parameterTypes;
57185718
let parameterNodes = instance.prototype.signatureNode.parameters;
5719+
assert(parameterNodes.length == parameterTypes.length);
57195720
let allOptionalsAreConstant = true;
57205721
for (let i = numArguments; i < maxArguments; ++i) {
57215722
let initializer = parameterNodes[i].initializer;
5722-
if (!(initializer !== null && nodeIsConstantValue(initializer.kind))) {
5723+
if (!(initializer && nodeIsConstantValue(initializer.kind))) {
57235724
allOptionalsAreConstant = false;
57245725
break;
57255726
}
@@ -6653,22 +6654,38 @@ export class Compiler extends DiagnosticEmitter {
66536654
return instance;
66546655
}
66556656

6656-
// use the signature of the parent constructor if a derived class
6657+
// clone base constructor if a derived class
66576658
var baseClass = classInstance.base;
6658-
var signature = baseClass
6659-
? this.ensureConstructor(baseClass, reportNode).signature
6660-
: new Signature(null, classInstance.type, classInstance.type);
6661-
6662-
instance = new Function(
6663-
CommonSymbols.constructor,
6664-
new FunctionPrototype(CommonSymbols.constructor, classInstance,
6665-
this.program.makeNativeFunctionDeclaration(CommonSymbols.constructor,
6666-
CommonFlags.INSTANCE | CommonFlags.CONSTRUCTOR
6667-
)
6668-
),
6669-
signature,
6670-
null
6671-
);
6659+
if (baseClass) {
6660+
let baseCtor = this.ensureConstructor(baseClass, reportNode);
6661+
instance = new Function(
6662+
CommonSymbols.constructor,
6663+
new FunctionPrototype(
6664+
CommonSymbols.constructor,
6665+
classInstance,
6666+
// declaration is important, i.e. to access optional parameter initializers
6667+
(<FunctionDeclaration>baseCtor.declaration).clone()
6668+
),
6669+
baseCtor.signature,
6670+
null
6671+
);
6672+
6673+
// otherwise make a default constructor
6674+
} else {
6675+
instance = new Function(
6676+
CommonSymbols.constructor,
6677+
new FunctionPrototype(
6678+
CommonSymbols.constructor,
6679+
classInstance,
6680+
this.program.makeNativeFunctionDeclaration(CommonSymbols.constructor,
6681+
CommonFlags.INSTANCE | CommonFlags.CONSTRUCTOR
6682+
)
6683+
),
6684+
new Signature(null, classInstance.type, classInstance.type),
6685+
null
6686+
);
6687+
}
6688+
66726689
instance.internalName = classInstance.internalName + INSTANCE_DELIMITER + "constructor";
66736690
instance.set(CommonFlags.COMPILED);
66746691
instance.prototype.setResolvedInstance("", instance);
@@ -6677,6 +6694,7 @@ export class Compiler extends DiagnosticEmitter {
66776694
this.currentFlow = instance.flow;
66786695

66796696
// generate body
6697+
var signature = instance.signature;
66806698
var module = this.module;
66816699
var nativeSizeType = this.options.nativeSizeType;
66826700
var stmts = new Array<ExpressionRef>();

0 commit comments

Comments
 (0)