mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-19 01:41:30 +00:00
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:
@ -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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user