Merge: Method dispatch in nitvm
authorJean Privat <jean@pryen.org>
Fri, 7 Nov 2014 03:37:40 +0000 (22:37 -0500)
committerJean Privat <jean@pryen.org>
Fri, 7 Nov 2014 03:37:40 +0000 (22:37 -0500)
The method dispatch in the nitvm is now functionnal with perfect hashing.

The dispatch is made by using the virtual table of the class.

Signed-off-by: Julien Pagès <julien.projet@gmail.com>

Pull-Request: #851
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>

1  2 
src/interpreter/naive_interpreter.nit
src/vm.nit

@@@ -66,13 -65,17 +66,15 @@@ class NaiveInterprete
        # arguments[1] is the first argument
        var arguments: Array[String]
  
 -      var mainobj: nullable Instance
 +      # The main Sys instance
 +      var mainobj: nullable Instance is noinit
  
 -      init(modelbuilder: ModelBuilder, mainmodule: MModule, arguments: Array[String])
 +      init
        do
 -              self.modelbuilder = modelbuilder
 -              self.mainmodule = mainmodule
 -              self.arguments = arguments
                self.true_instance = new PrimitiveInstance[Bool](mainmodule.bool_type, true)
+               init_instance_primitive(self.true_instance)
                self.false_instance = new PrimitiveInstance[Bool](mainmodule.bool_type, false)
+               init_instance_primitive(self.false_instance)
                self.null_instance = new MutableInstance(mainmodule.model.null_type)
        end
  
        # Return the float instance associated with `val`.
        fun float_instance(val: Float): Instance
        do
-               var ic = self.mainmodule.get_primitive_class("Float")
-               return new PrimitiveInstance[Float](ic.mclass_type, val)
+               var ic = get_primitive_class("Float")
+               var instance = new PrimitiveInstance[Float](ic.mclass_type, val)
+               init_instance_primitive(instance)
+               return instance
        end
  
 -      # The unique intance of the `true` value.
 -      var true_instance: Instance
 +      # The unique instance of the `true` value.
 +      var true_instance: Instance is noinit
  
 -      # The unique intance of the `false` value.
 -      var false_instance: Instance
 +      # The unique instance of the `false` value.
 +      var false_instance: Instance is noinit
  
 -      # The unique intance of the `null` value.
 -      var null_instance: Instance
 +      # The unique instance of the `null` value.
 +      var null_instance: Instance is noinit
  
        # Return a new array made of `values`.
        # The dynamic type of the result is Array[elttype].
        do
                var val = new FlatBuffer.from(txt)
                val.add('\0')
-               var ic = self.mainmodule.get_primitive_class("NativeString")
-               return new PrimitiveInstance[Buffer](ic.mclass_type, val)
+               var ic = get_primitive_class("NativeString")
+               var instance = new PrimitiveInstance[Buffer](ic.mclass_type, val)
+               init_instance_primitive(instance)
+               return instance
        end
  
 +      # Return a new String instance for `txt`
 +      fun string_instance(txt: String): Instance
 +      do
 +              var nat = native_string_instance(txt)
 +              var res = self.send(self.force_get_primitive_method("to_s_with_length", nat.mtype), [nat, self.int_instance(txt.length)])
 +              assert res != null
 +              return res
 +      end
 +
        # The current frame used to store local variables of the current method executed
        fun frame: Frame do return frames.first
  
@@@ -943,9 -959,11 +964,11 @@@ redef class AMethPropde
                else if pname == "calloc_string" then
                        return v.native_string_instance("!" * args[1].to_i)
                else if cname == "NativeArray" then
 -                      if pname == "init" then
 +                      if pname == "new" then
                                var val = new Array[Instance].filled_with(v.null_instance, args[1].to_i)
-                               return new PrimitiveInstance[Array[Instance]](args[0].mtype, val)
+                               var instance = new PrimitiveInstance[Array[Instance]](args[0].mtype, val)
+                               v.init_instance_primitive(instance)
+                               return instance
                        end
                        var recvval = args.first.val.as(Array[Instance])
                        if pname == "[]" then
diff --cc src/vm.nit
Simple merge