[pypy-svn] r27899 - in pypy/dist/pypy/translator/js2: . test

fijal at codespeak.net fijal at codespeak.net
Tue May 30 11:23:43 CEST 2006


Author: fijal
Date: Tue May 30 11:23:03 2006
New Revision: 27899

Modified:
   pypy/dist/pypy/translator/js2/_class.py
   pypy/dist/pypy/translator/js2/asmgen.py
   pypy/dist/pypy/translator/js2/database.py
   pypy/dist/pypy/translator/js2/function.py
   pypy/dist/pypy/translator/js2/js.py
   pypy/dist/pypy/translator/js2/opcodes.py
   pypy/dist/pypy/translator/js2/test/test_class.py
Log:
Added inheritance.


Modified: pypy/dist/pypy/translator/js2/_class.py
==============================================================================
--- pypy/dist/pypy/translator/js2/_class.py	(original)
+++ pypy/dist/pypy/translator/js2/_class.py	Tue May 30 11:23:03 2006
@@ -13,13 +13,19 @@
         self.name = classdef._name.split('.')[-1]
 
         if not self.is_root(classdef):
-            self.db.pending_class(classdef._superclass)
+            self.parent = self.db.pending_class(classdef._superclass)
+            self.order = self.parent.order + 1
+        else:
+            self.order = 0
 
     def __hash__(self):
         return hash(self.classdef)
 
     def __eq__(self, other):
         return self.classdef == other.classdef
+    
+    def __cmp__(self, other):
+        return cmp(self.order, other.order)
 
     def is_root(classdef):
         return classdef._superclass is None
@@ -28,13 +34,6 @@
     def get_name(self):
         return self.name
 
-    def get_base_class(self):
-        base_class = self.classdef._superclass
-        if self.is_root(base_class):
-            return '[mscorlib]System.Object'
-        else:
-            return base_class._name
-
     def render(self, ilasm):
         if self.is_root(self.classdef):
             return
@@ -52,16 +51,20 @@
             #if cts_type != 'void':
         #    ilasm.field(f_name, cts_type)
 
-        # TODO: should the .ctor set the default values?
-        #self._ctor()
-
-        # lazy import to avoid circular dependencies
-        #import pypy.translator.cli.function as function
+        if not self.is_root(self.classdef):
+            basename = self.basename(self.classdef._superclass._name)
+            if basename != 'Root':
+                ilasm.inherits(self.name, basename)
+        
         for m_name, m_meth in self.classdef._methods.iteritems():
             f = self.db.function_class(self.db, m_meth.graph, m_name, is_method = True, _class = self.name)
             f.render(ilasm)
-
+        
+        
         self.db.record_class(self.classdef, self.name)
+    
+    def basename(self, name):
+        return name.split('.')[-1]
 
     #def _ctor(self):
     #    self.ilasm.begin_function('.ctor', [], 'void', False, 'specialname', 'rtspecialname', 'instance')

Modified: pypy/dist/pypy/translator/js2/asmgen.py
==============================================================================
--- pypy/dist/pypy/translator/js2/asmgen.py	(original)
+++ pypy/dist/pypy/translator/js2/asmgen.py	Tue May 30 11:23:03 2006
@@ -224,5 +224,8 @@
     def end_try(self):
         self.codegenerator.closeblock()
     
+    def inherits(self, subclass_name, parent_name):
+        self.codegenerator.writeline("%s.inherits(%s);"%(subclass_name, parent_name))
+    
     #def finish ( self ):
     #    self . outfile . write ( "%r" % self . right_hand )

Modified: pypy/dist/pypy/translator/js2/database.py
==============================================================================
--- pypy/dist/pypy/translator/js2/database.py	(original)
+++ pypy/dist/pypy/translator/js2/database.py	Tue May 30 11:23:03 2006
@@ -50,7 +50,9 @@
         self.pending_node(self.function_class(self, graph))
 
     def pending_class(self, classdef):
-        self.pending_node(Class(self, classdef))
+        c = Class(self, classdef)
+        self.pending_node(c)
+        return c
 
     def pending_record(self, record):
         r = Record(self, record)

Modified: pypy/dist/pypy/translator/js2/function.py
==============================================================================
--- pypy/dist/pypy/translator/js2/function.py	(original)
+++ pypy/dist/pypy/translator/js2/function.py	Tue May 30 11:23:03 2006
@@ -16,7 +16,7 @@
 
 from pypy.translator.js2.log import log
 
-class LoopFinder:
+class LoopFinder(object):
 
     def __init__(self, startblock):
         self.loops = {}
@@ -60,6 +60,7 @@
         self._class = _class
         self._set_args()
         self._set_locals()
+        self.order = 0
 
     def get_name(self):
         return self.name
@@ -69,6 +70,9 @@
 
     def __eq__(self, other):
         return self.graph == other.graph
+    
+    def __cmp__(self, other):
+        return cmp(self.order, other.order)
 
     def _is_return_block(self, block):
         return (not block.exits) and len(block.inputargs) == 1

Modified: pypy/dist/pypy/translator/js2/js.py
==============================================================================
--- pypy/dist/pypy/translator/js2/js.py	(original)
+++ pypy/dist/pypy/translator/js2/js.py	Tue May 30 11:23:03 2006
@@ -25,18 +25,34 @@
 
 from pypy.translator.cli.gencli import GenCli
 
+from heapq import heappush, heappop
+
 def _path_join(root_path, *paths):
     path = root_path
     for p in paths:
         path = os.path.join(path, p)
     return path
 
-class JS(object):
+class JS(GenCli):
     def __init__(self, translator, functions=[], stackless=False, compress=False, logging=False):
-        self.cli = GenCli(udir, translator, type_system_class = JTS, opcode_dict = opcodes,\
+        GenCli.__init__(self, udir, translator, type_system_class = JTS, opcode_dict = opcodes,\
             name_suffix = '.js', function_class = Function, database_class = LowLevelDatabase)
         self.translator = translator
     
+    def gen_pendings(self):
+        while self.db._pending_nodes:
+            node = self.db._pending_nodes.pop()
+            to_render = []
+            nparent = node
+            while nparent.order != 0:
+                nparent = nparent.parent
+                to_render.append(nparent)
+            to_render.reverse()
+            for i in to_render:
+                i.render(self.ilasm)
+            
+            node.render(self.ilasm)
+        
     def write_source(self):
         
         # write down additional functions
@@ -44,15 +60,16 @@
         # not be used as inlined, rather another script to load
         # this is just workaround
         
-        self.cli.generate_source(AsmGen)
-        self.filename = self.cli.tmpfile
+        self.generate_source(AsmGen)
 
-        data = self.filename.open().read()
+        data = self.tmpfile.open().read()
         src_filename = _path_join(os.path.dirname(__file__), 'jssrc', 'misc.js')
-        f = self.cli.tmpfile.open("w")
+        f = self.tmpfile.open("w")
         s = open(src_filename).read()
         f.write(s)
         f.write(data)
         f.close()
         
-        return self.cli.tmpfile
+        self.filename = self.tmpfile
+        
+        return self.tmpfile

Modified: pypy/dist/pypy/translator/js2/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/js2/opcodes.py	(original)
+++ pypy/dist/pypy/translator/js2/opcodes.py	Tue May 30 11:23:03 2006
@@ -213,8 +213,10 @@
     'oosetfield' : [SetField],
     'oogetfield' : [GetField],
     'oosend'     : [CallMethod],
-    'ooupcast'   : [_NotImplemented("Inheritance not implemented (ooupcast)")],
-    'oodowncast' : [_NotImplemented("Inheritance not implemented (oodowncast)")],
+    #'ooupcast'   : [_NotImplemented("Inheritance not implemented (ooupcast)")],
+    #'oodowncast' : [_NotImplemented("Inheritance not implemented (oodowncast)")],
+    'ooupcast'   : DoNothing,
+    'oodowncast' : DoNothing,        
     'oononnull'  : [PushAllArgs,_Prefix('!!')],
     'oostring'   : [CastString],
     'oois'       : '==', # FIXME: JS does not have real equal

Modified: pypy/dist/pypy/translator/js2/test/test_class.py
==============================================================================
--- pypy/dist/pypy/translator/js2/test/test_class.py	(original)
+++ pypy/dist/pypy/translator/js2/test/test_class.py	Tue May 30 11:23:03 2006
@@ -24,47 +24,45 @@
         assert f(2) == 10
 
     def test_inherit1(self):
-        py.test.skip("Inheritance not implemented")
-        #py.test.skip("issue 'null' for Ptr's? or recurse into Ptr.TO?) see: opwriter.py")
         f = compile_function(llvmsnippet.class_inherit1, [])
         assert f() == 11
 
     def test_inherit2(self):
-        py.test.skip("Inheritance not implemented")
+        #py.test.skip("Inheritance not implemented")
         #py.test.skip("issue 'null' for Ptr's? or recurse into Ptr.TO?) see: opwriter.py")
         f = compile_function(llvmsnippet.class_inherit2, [])
         assert f() == 1
 
     def test_method_of_base_class(self):
-        py.test.skip("Inheritance not implemented")
+        #py.test.skip("Inheritance not implemented")
         #py.test.skip("issue 'null' for Ptr's? or recurse into Ptr.TO?) see: opwriter.py")
         f = compile_function(llvmsnippet.method_of_base_class, [])
         assert f() == 14
 
     def test_attribute_from_base_class(self):
-        py.test.skip("Inheritance not implemented")
+        #py.test.skip("Inheritance not implemented")
         f = compile_function(llvmsnippet.attribute_from_base_class, [])
         assert f() == 4
 
     def test_direct_call_of_virtual_method(self):
-        py.test.skip("Inheritance not implemented")
+        #py.test.skip("Inheritance not implemented")
         #py.test.skip("issue 'null' for Ptr's? or recurse into Ptr.TO?) see: opwriter.py")
         f = compile_function(llvmsnippet.direct_call_of_virtual_method, [])
         assert f() == 14
 
     def test_flow_type(self):
-        py.test.skip("Inheritance not implemented")
+        py.test.skip("isinstanceof not implemented")
         f = compile_function(llvmsnippet.flow_type, [])
         assert f() == 16
 
     def test_merge_class(self):
-        py.test.skip("Inheritance not implemented")
+        #py.test.skip("Inheritance not implemented")
         f = compile_function(llvmsnippet.merge_classes, [bool])
         assert f(True) == 1
         assert f(False) == 2
 
     def test_attribute_instance(self):
-        py.test.skip("Inheritance not implemented")
+        #py.test.skip("Inheritance not implemented")
         f = compile_function(llvmsnippet.attribute_instance, [bool])
         assert f(True) == 1
         assert f(False) == 2
@@ -83,12 +81,13 @@
         assert f(15) == 25
 
     def test_call_degrading_func(self):
-        py.test.skip("Inheritance not implemented")
+        py.test.skip("isinstanceof not implemented")
         f = compile_function(llvmsnippet.call_degrading_func, [bool])
         assert f(True) == llvmsnippet.call_degrading_func(True)
         assert f(False) == llvmsnippet.call_degrading_func(False)
     
     def test_circular_classdef(self):
-        py.test.skip("Inheritance not implemented")
+        py.test.skip("Problems with constant names")
+        #py.test.skip("Inheritance not implemented")
         f = compile_function(llvmsnippet.circular_classdef, [])
         assert f() == 10



More information about the Pypy-commit mailing list