From: Alexis Laferrière Date: Thu, 5 Feb 2015 14:59:55 +0000 (-0500) Subject: nitc: fix calling extern constructors from extern code in separate compiler X-Git-Tag: v0.7.2~25^2~1 X-Git-Url: http://nitlanguage.org nitc: fix calling extern constructors from extern code in separate compiler Signed-off-by: Alexis Laferrière --- diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index b1a5879..07a8ede 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -1381,6 +1381,24 @@ abstract class AbstractCompilerVisitor # Generate a alloc-instance + init-attributes fun init_instance(mtype: MClassType): RuntimeVariable is abstract + # Allocate and init attributes of an instance of a standard or extern class + # + # Does not support universals and the pseudo-internal `NativeArray` class. + fun init_instance_or_extern(mtype: MClassType): RuntimeVariable + do + var recv + var ctype = mtype.ctype + assert mtype.mclass.name != "NativeArray" + if ctype == "val*" then + recv = init_instance(mtype) + else if ctype == "char*" then + recv = new_expr("NULL/*special!*/", mtype) + else + recv = new_expr("({ctype})0/*special!*/", mtype) + end + return recv + end + # Set a GC finalizer on `recv`, only if `recv` isa Finalizable fun set_finalizer(recv: RuntimeVariable) do @@ -2874,22 +2892,17 @@ redef class ANewExpr do var mtype = self.recvtype assert mtype != null - var recv - var ctype = mtype.ctype + if mtype.mclass.name == "NativeArray" then assert self.n_args.n_exprs.length == 1 var l = v.expr(self.n_args.n_exprs.first, null) assert mtype isa MGenericType var elttype = mtype.arguments.first return v.native_array_instance(elttype, l) - else if ctype == "val*" then - recv = v.init_instance(mtype) - else if ctype == "char*" then - recv = v.new_expr("NULL/*special!*/", mtype) - else - recv = v.new_expr("({ctype})0/*special!*/", mtype) end + var recv = v.init_instance_or_extern(mtype) + var callsite = self.callsite.as(not null) var args = v.varargize(callsite.mpropdef, recv, self.n_args.n_exprs) var res2 = v.compile_callsite(callsite, args) diff --git a/src/compiler/compiler_ffi.nit b/src/compiler/compiler_ffi.nit index dea917b..42423f9 100644 --- a/src/compiler/compiler_ffi.nit +++ b/src/compiler/compiler_ffi.nit @@ -388,7 +388,7 @@ redef class MExplicitCall var recv_var = null if mproperty.is_init then var recv_mtype = recv_mtype - recv_var = nitni_visitor.init_instance(recv_mtype) + recv_var = nitni_visitor.init_instance_or_extern(recv_mtype) nitni_visitor.add("{mtype.ctype} recv /* var self: {mtype} */;") nitni_visitor.add("recv = {recv_var};") else