+
+ fun can_be_primitive(value: RuntimeVariable): Bool
+ do
+ var t = value.mcasttype
+ if t isa MNullableType then t = t.mtype
+ if not t isa MClassType then return false
+ var k = t.mclass.kind
+ return k == interface_kind or t.ctype != "val*"
+ end
+
+ fun maybe_null(value: RuntimeVariable): Bool
+ do
+ var t = value.mcasttype
+ return t isa MNullableType or t isa MNullType
+ end
+
+ redef fun array_instance(array, elttype)
+ do
+ var nclass = self.get_class("NativeArray")
+ var arrayclass = self.get_class("Array")
+ var arraytype = arrayclass.get_mtype([elttype])
+ var res = self.init_instance(arraytype)
+ self.add("\{ /* {res} = array_instance Array[{elttype}] */")
+ var length = self.int_instance(array.length)
+ var nat = native_array_instance(elttype, length)
+ for i in [0..array.length[ do
+ var r = self.autobox(array[i], self.object_type)
+ self.add("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
+ end
+ self.send(self.get_property("with_native", arrayclass.intro.bound_mtype), [res, nat, length])
+ self.check_init_instance(res, arraytype)
+ self.add("\}")
+ return res
+ end
+
+ fun native_array_instance(elttype: MType, length: RuntimeVariable): RuntimeVariable
+ do
+ var mtype = self.get_class("NativeArray").get_mtype([elttype])
+ assert mtype isa MGenericType
+ var compiler = self.compiler.as(SeparateCompiler)
+ if mtype.need_anchor then
+ var buff = new Buffer
+ retrieve_anchored_livetype(mtype, buff)
+ mtype = self.anchor(mtype).as(MClassType)
+ return self.new_expr("NEW_{mtype.mclass.c_name}({length}, (struct type *) livetypes_{mtype.mclass.c_name}{buff.to_s})", mtype)
+ end
+ compiler.undead_types.add(mtype)
+ return self.new_expr("NEW_{mtype.mclass.c_name}({length}, (struct type *) &type_{mtype.c_name})", mtype)
+ end
+
+ redef fun native_array_def(pname, ret_type, arguments)
+ do
+ var elttype = arguments.first.mtype
+ var nclass = self.get_class("NativeArray")
+ var recv = "((struct instance_{nclass.c_name}*){arguments[0]})->values"
+ if pname == "[]" then
+ self.ret(self.new_expr("{recv}[{arguments[1]}]", ret_type.as(not null)))
+ return
+ else if pname == "[]=" then
+ self.add("{recv}[{arguments[1]}]={arguments[2]};")
+ return
+ else if pname == "copy_to" then
+ var recv1 = "((struct instance_{nclass.c_name}*){arguments[1]})->values"
+ self.add("memcpy({recv1}, {recv}, {arguments[2]}*sizeof({elttype.ctype}));")
+ return
+ end
+ end
+
+ redef fun calloc_array(ret_type, arguments)
+ do
+ var ret = ret_type.as(MGenericType)
+ var compiler = self.compiler.as(SeparateCompiler)
+ compiler.undead_types.add(ret)
+ var mclass = self.get_class("ArrayCapable")
+ var ft = mclass.mclass_type.arguments.first.as(MParameterType)
+ self.ret(self.new_expr("NEW_{ret.mclass.c_name}({arguments[1]}, (struct type*) livetypes_array__NativeArray[self->type->fts_table->fts[{ft.const_color}]->livecolor])", ret_type))
+ end