Merge: model: register the implicitly injected Bool class in its hierarchy.
authorJean Privat <jean@pryen.org>
Sat, 13 Dec 2014 07:50:08 +0000 (02:50 -0500)
committerJean Privat <jean@pryen.org>
Sat, 13 Dec 2014 07:50:08 +0000 (02:50 -0500)
The model can inject a primitive a Bool class when required.
It was a hack which was initially introduced by the interpreter that need to manipulate boolean values even if they where not present in the original program.
However, the class was not fully initialized. So fix that.

Maybe a future PR will try to remove this hack (it is not the model's job to inject classes)

Pull-Request: #989
Reviewed-by: Lucas Bajolet <r4pass@hotmail.com>
Reviewed-by: Alexandre Terrasa <alexandre@moz-code.org>

src/interpreter/naive_interpreter.nit
src/model/model.nit
tests/base_empty_module2.nit [new file with mode: 0644]

index 064395d..5d24e6c 100644 (file)
@@ -72,10 +72,12 @@ class NaiveInterpreter
 
        init
        do
-               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)
+               if mainmodule.model.get_mclasses_by_name("Bool") != null then
+                       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)
+               end
                self.null_instance = new PrimitiveInstance[nullable Object](mainmodule.model.null_type, null)
        end
 
index 899f023..9c7a877 100644 (file)
@@ -251,9 +251,13 @@ redef class MModule
        do
                var cla = self.model.get_mclasses_by_name(name)
                if cla == null then
-                       if name == "Bool" then
+                       if name == "Bool" and self.model.get_mclasses_by_name("Object") != null then
+                               # Bool is injected because it is needed by engine to code the result
+                               # of the implicit casts.
                                var c = new MClass(self, name, null, enum_kind, public_visibility)
                                var cladef = new MClassDef(self, c.mclass_type, new Location(null, 0,0,0,0))
+                               cladef.set_supertypes([object_type])
+                               cladef.add_in_hierarchy
                                return c
                        end
                        print("Fatal Error: no primitive class {name}")
diff --git a/tests/base_empty_module2.nit b/tests/base_empty_module2.nit
new file mode 100644 (file)
index 0000000..dc498f0
--- /dev/null
@@ -0,0 +1,17 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2004-2008 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import end