nitg/ffi: adds the annotations c_[copiler|linker]_option
[nit.git] / src / naive_interpreter.nit
index d2ccbce..ef2a5f3 100644 (file)
@@ -62,7 +62,6 @@ redef class ModelBuilder
                if initprop != null then
                        interpreter.send(initprop, [mainobj])
                end
-               interpreter.check_init_instance(mainobj)
                var mainprop = mainmodule.try_get_primitive_method("main", sys_type.mclass)
                if mainprop != null then
                        interpreter.send(mainprop, [mainobj])
@@ -243,7 +242,6 @@ private class NaiveInterpreter
                var res = new MutableInstance(mtype)
                self.init_instance(res)
                self.send(self.force_get_primitive_method("with_native", mtype), [res, nat, self.int_instance(values.length)])
-               self.check_init_instance(res)
                return res
        end
 
@@ -298,10 +296,8 @@ private class NaiveInterpreter
        # Store known method, used to trace methods as thez are reached
        var discover_call_trace: Set[MMethodDef] = new HashSet[MMethodDef]
 
-       # Execute `mpropdef` for a `args` (where `args[0]` is the receiver).
-       # Return a falue if `mpropdef` is a function, or null if it is a procedure.
-       # The call is direct/static. There is no message-seding/late-binding.
-       fun call(mpropdef: MMethodDef, args: Array[Instance]): nullable Instance
+       # Common code for calls to injected methods and normal methods
+       fun call_commons(mpropdef: MMethodDef, args: Array[Instance]): Array[Instance]
        do
                var vararg_rank = mpropdef.msignature.vararg_rank
                if vararg_rank >= 0 then
@@ -328,6 +324,15 @@ private class NaiveInterpreter
                                args.add(rawargs[i+1])
                        end
                end
+               return args
+       end
+
+       # Execute `mpropdef` for a `args` (where `args[0]` is the receiver).
+       # Return a falue if `mpropdef` is a function, or null if it is a procedure.
+       # The call is direct/static. There is no message-seding/late-binding.
+       fun call(mpropdef: MMethodDef, args: Array[Instance]): nullable Instance
+       do
+               args = call_commons(mpropdef, args)
                return call_without_varargs(mpropdef, args)
        end
 
@@ -383,13 +388,9 @@ private class NaiveInterpreter
                end
        end
 
-       # Execute `mproperty` for a `args` (where `args[0]` is the receiver).
-       # Return a falue if `mproperty` is a function, or null if it is a procedure.
-       # The call is polimotphic. There is a message-seding/late-bindng according to te receiver (args[0]).
-       fun send(mproperty: MMethod, args: Array[Instance]): nullable Instance
+       # Common code for runtime injected calls and normal calls
+       fun send_commons(mproperty: MMethod, args: Array[Instance], mtype: MType): nullable Instance
        do
-               var recv = args.first
-               var mtype = recv.mtype
                if mtype isa MNullType then
                        if mproperty.name == "==" then
                                return self.bool_instance(args[0] == args[1])
@@ -398,8 +399,19 @@ private class NaiveInterpreter
                        end
                        #fatal("Reciever is null. {mproperty}. {args.join(" ")} {self.frame.current_node.class_name}")
                        fatal("Reciever is null")
-                       abort
                end
+               return null
+       end
+
+       # Execute `mproperty` for a `args` (where `args[0]` is the receiver).
+       # Return a falue if `mproperty` is a function, or null if it is a procedure.
+       # The call is polimotphic. There is a message-seding/late-bindng according to te receiver (args[0]).
+       fun send(mproperty: MMethod, args: Array[Instance]): nullable Instance
+       do
+               var recv = args.first
+               var mtype = recv.mtype
+               var ret = send_commons(mproperty, args, mtype)
+               if ret != null then return ret
                var propdef = mproperty.lookup_first_definition(self.mainmodule, mtype)
                return self.call(propdef, args)
        end
@@ -449,19 +461,6 @@ private class NaiveInterpreter
                end
        end
 
-       # Check that non nullable attributes of `recv` are correctly initialized.
-       # This function is used as the last instruction of a new
-       fun check_init_instance(recv: Instance)
-       do
-               if not recv isa MutableInstance then return
-               for npropdef in collect_attr_propdef(recv.mtype) do
-                       if npropdef.n_expr == null then
-                               # Force read to check the initialization
-                               self.read_attribute(npropdef.mpropdef.mproperty, recv)
-                       end
-               end
-       end
-
        # This function determine the correct type according the reciever of the current definition (self).
        fun unanchor_type(mtype: MType): MType
        do
@@ -481,7 +480,7 @@ abstract class Instance
        fun is_true: Bool do abort
 
        # Return true if `self` IS `o` (using the Nit semantic of is)
-       fun eq_is(o: Instance): Bool do return self is o
+       fun eq_is(o: Instance): Bool do return self.is_same_instance(o)
 
        # Human readable object identity "Type#number"
        redef fun to_s do return "{mtype}"
@@ -537,7 +536,7 @@ class PrimitiveInstance[E: Object]
        redef fun eq_is(o)
        do
                if not o isa PrimitiveInstance[Object] then return false
-               return self.val is o.val
+               return self.val.is_same_instance(o.val)
        end
 
        redef fun to_s do return "{mtype}#{val.object_id}({val})"
@@ -587,9 +586,23 @@ redef class APropdef
 end
 
 redef class AConcreteMethPropdef
+
        redef fun call(v, mpropdef, args)
        do
                var f = new Frame(self, self.mpropdef.as(not null), args)
+               call_commons(v, mpropdef, args, f)
+               v.frames.shift
+               if v.returnmark == f then
+                       v.returnmark = null
+                       var res = v.escapevalue
+                       v.escapevalue = null
+                       return res
+               end
+               return null
+       end
+
+       private fun call_commons(v: NaiveInterpreter, mpropdef: MMethodDef, args: Array[Instance], f: Frame)
+       do
                for i in [0..mpropdef.msignature.arity[ do
                        var variable = self.n_signature.n_params[i].variable
                        assert variable != null
@@ -612,14 +625,6 @@ redef class AConcreteMethPropdef
                end
 
                v.stmt(self.n_block)
-               v.frames.shift
-               if v.returnmark == f then
-                       v.returnmark = null
-                       var res = v.escapevalue
-                       v.escapevalue = null
-                       return res
-               end
-               return null
        end
 end
 
@@ -654,6 +659,8 @@ redef class AInternMethPropdef
                        return v.bool_instance(args[0] != args[1])
                else if pname == "is_same_type" then
                        return v.bool_instance(args[0].mtype == args[1].mtype)
+               else if pname == "is_same_instance" then
+                       return v.bool_instance(args[1] != null and args[0].eq_is(args[1]))
                else if pname == "exit" then
                        exit(args[1].to_i)
                        abort
@@ -878,6 +885,8 @@ redef class AExternMethPropdef
                        else if pname == "file_chdir" then
                                recvval.to_s.chdir
                                return null
+                       else if pname == "file_realpath" then
+                               return v.native_string_instance(recvval.to_s.realpath)
                        else if pname == "get_environ" then
                                var txt = recvval.to_s.environ
                                return v.native_string_instance(txt)
@@ -1326,17 +1335,6 @@ redef class AOrElseExpr
        end
 end
 
-redef class AEeExpr
-       redef fun expr(v)
-       do
-               var i = v.expr(self.n_expr)
-               if i == null then return null
-               var i2 = v.expr(self.n_expr2)
-               if i2 == null then return null
-               return v.bool_instance(i.eq_is(i2))
-       end
-end
-
 redef class AIntExpr
        redef fun expr(v)
        do
@@ -1410,7 +1408,6 @@ redef class ACrangeExpr
                var res = new MutableInstance(mtype)
                v.init_instance(res)
                v.send(v.force_get_primitive_method("init", mtype), [res, e1, e2])
-               v.check_init_instance(res)
                return res
        end
 end
@@ -1426,7 +1423,6 @@ redef class AOrangeExpr
                var res = new MutableInstance(mtype)
                v.init_instance(res)
                v.send(v.force_get_primitive_method("without_last", mtype), [res, e1, e2])
-               v.check_init_instance(res)
                return res
        end
 end
@@ -1606,7 +1602,6 @@ redef class ANewExpr
                        #self.debug("got {res2} from {mproperty}. drop {recv}")
                        return res2
                end
-               v.check_init_instance(recv)
                return recv
        end
 end