Initial implementation of 'new'

This doesn't yet call the constructor or use provided parameters and just allocates raw memory
This commit is contained in:
dcodeIO
2018-01-19 16:13:14 +01:00
parent ef7a095494
commit 64c939fdc4
12 changed files with 206 additions and 50 deletions

View File

@ -37,7 +37,8 @@ import {
Global,
FunctionPrototype,
Local,
ElementFlags
ElementFlags,
Class
} from "./program";
/** Initializes the specified program with built-in constants and functions. */
@ -1824,3 +1825,26 @@ export function compileCall(compiler: Compiler, prototype: FunctionPrototype, ty
compiler.error(DiagnosticCode.Operation_not_supported, reportNode.range);
return module.createUnreachable();
}
/** Compiles an allocation of the specified class. */
export function compileAllocate(compiler: Compiler, cls: Class, reportNode: Node): ExpressionRef {
var program = cls.program;
var prototype = program.elements.get(compiler.options.allocateImpl);
if (prototype) {
var instance = (<FunctionPrototype>prototype).resolve(); // reports
if (instance) {
var usizeType = program.target == Target.WASM64 ? Type.usize64 : Type.usize32;
if (!instance.is(ElementFlags.GENERIC) && instance.returnType == usizeType && instance.parameters.length == 1 && instance.parameters[0].type == usizeType) {
if (compiler.compileFunction(instance)) // reports
return compiler.makeCall(instance, [
program.target == Target.WASM64
? compiler.module.createI64(cls.currentMemoryOffset)
: compiler.module.createI32(cls.currentMemoryOffset)
]);
} else
program.error(DiagnosticCode.Implementation_0_must_match_the_signature_1, reportNode.range, compiler.options.allocateImpl, "(size: usize): usize");
}
} else
program.error(DiagnosticCode.Cannot_find_name_0, reportNode.range, compiler.options.allocateImpl);
return compiler.module.createUnreachable();
}