- module kernel : Add output_class_name method to Object that display the object class name in console (debug only)
- module String : Add native_class_name method to Object (refinement) that return the class name in NativeString format
- module String : Add class_name method to Object (refinement) that return the class name in String format using native_class_name
Signed-off-by: Alexandre Terrasa <alexandre@moz-concept.com>
typedef signed long int bigint; /* standard int value, must be larger that any poiner */
typedef bigint (*fun_t) (bigint); /* generic function pointer */
typedef bigint cid_t; /* class identifier */
+typedef char* cname_t; /* class name */
typedef bigint val_t; /* value (everything is a val_t) */
typedef union obj_tu {union classtable_elt_tu * vft; bigint object_id; val_t objectSize;} *obj_t; /* standard object */
-typedef union classtable_elt_tu { bigint i; fun_t f; cid_t cid;} classtable_elt_t; /* classtable element */
+typedef union classtable_elt_tu { bigint i; fun_t f; cid_t cid; cname_t cname;} classtable_elt_t; /* classtable element */
typedef struct Nit_NativeArray {const classtable_elt_t * vft; bigint object_id; bigint size; val_t val[1];} * Nit_NativeArray;
typedef classtable_elt_t * classtable_t; /* classtable */
'>'.output
end
+ # Display class name on stdout (debug only).
+ fun output_class_name is intern
+
protected fun exit(exit_value: Int) is intern # Quit the program.
protected fun sys: Sys is intern # The global sys object
end
# User redeable representation of `self'.
fun to_s: String do return inspect
+ # The class name of the object in NativeString format.
+ private fun native_class_name: NativeString is intern
+
+ # The class name of the object.
+ # FIXME: real type information is not available at runtime. Therefore, for instance, an instance of List[Bool] has just "List" for classname
+ fun class_name: String do return new String.from_cstring(native_class_name)
+
# Developper readable representation of `self'.
# Usualy, it uses the form "<CLASSNAME:#OBJECTID bla bla bla>"
fun inspect: String
do
- var r = inspect_head
- # r.add('>')
- return r
+ return "<{inspect_head}>"
end
- # Return "<CLASSNAME:#OBJECTID".
- # This fuction is mainly used with the redefinition of the inspect(0) method
+ # Return "CLASSNAME:#OBJECTID".
+ # This fuction is mainly used with the redefinition of the inspect method
protected fun inspect_head: String
do
- return "<{object_id.to_hex}"
+ return "{class_name}:#{object_id.to_hex}"
end
protected fun args: Sequence[String]
end
end
+redef class TableEltClassSelfName
+ redef fun compile_to_c(v, c)
+ do
+ var prog = v.program
+ return "\"{c.global.name}\" /* {prog.table_information.color(self)}: Class Name */"
+ end
+end
+
redef class TableEltClassObjectSize
redef fun compile_to_c(v, c)
do
s = "NEW_NativeArray(UNTAG_Int({regs[1]}), sizeof(val_t))"
else if n == once "calloc_string".to_symbol then
s = "BOX_NativeString((char*)raw_alloc((UNTAG_Int({regs[1]}) * sizeof(char))))"
+ # Add output_class_name native implementation
+ else if n == once "output_class_name".to_symbol then
+ s = "printf(\"%s\\n\", VAL2VFT({regs[0]})[2].cname);"
+ # Add class_name implementation
+ else if n == once "native_class_name".to_symbol then
+ s = "BOX_NativeString(VAL2VFT({regs[0]})[2].cname);"
end
+
if s == null then
var ll = location
if ll != null then v.add_instr("fprintf(stderr, \"{ll.to_s}: \");")
ctab.add(new TableEltClassSelfId)
ctab.add(new TableEltClassObjectSize)
+ ctab.add(new TableEltClassSelfName)
itab.add(new TableEltVftPointer)
itab.add(new TableEltObjectId)
redef fun is_related_to(c) do return true
end
+# The element that represent the class name
+class TableEltClassSelfName
+ super TableElt
+ redef fun is_related_to(c) do return true
+end
+
# The element that represent the Object Size
class TableEltClassObjectSize
super TableElt
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 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.
+
+class Test end
+class MyArray[E] end
+
+class Toto
+ redef fun class_name do return "TotoToto"
+end
+
+var test1 = new Test
+var test2: Object = new Test
+var test3 = new MyArray[Int]
+var test4 = new Toto
+
+print "".class_name
+print 1.class_name
+
+print test1.class_name
+print test2.class_name
+print test3.class_name
+print test4.class_name
--- /dev/null
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2009 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.
+
+class Test end
+class MyArray[E] end
+
+class Toto
+ redef fun output_class_name do print "TotoToto"
+end
+
+var test1 = new Test
+var test2: Object = new Test
+var test3 = new MyArray[Int]
+var test4 = new Toto
+
+"".output_class_name
+1.output_class_name
+test1.output_class_name
+test2.output_class_name
+test3.output_class_name
+test4.output_class_name
--- /dev/null
+String
+Int
+Test
+Test
+MyArray
+TotoToto
--- /dev/null
+String
+Int
+Test
+Test
+MyArray
+TotoToto