[pypy-svn] r36100 - in pypy/dist/pypy/translator/js: . demo/jsdemo test

fijal at codespeak.net fijal at codespeak.net
Tue Jan 2 00:20:45 CET 2007


Author: fijal
Date: Tue Jan  2 00:20:43 2007
New Revision: 36100

Modified:
   pypy/dist/pypy/translator/js/_class.py
   pypy/dist/pypy/translator/js/asmgen.py
   pypy/dist/pypy/translator/js/database.py
   pypy/dist/pypy/translator/js/demo/jsdemo/pythonconsole.py
   pypy/dist/pypy/translator/js/function.py
   pypy/dist/pypy/translator/js/opcodes.py
   pypy/dist/pypy/translator/js/test/runtest.py
   pypy/dist/pypy/translator/js/test/test_rpython.py
Log:
A bit of refactoring regarding class attributes and such. Mostly whack whack whack here and there. Killed some skips, some stay still.


Modified: pypy/dist/pypy/translator/js/_class.py
==============================================================================
--- pypy/dist/pypy/translator/js/_class.py	(original)
+++ pypy/dist/pypy/translator/js/_class.py	Tue Jan  2 00:20:43 2007
@@ -44,6 +44,8 @@
         self.ilasm = ilasm
         
         ilasm.begin_function(self.name, [])
+        # we need to copy here all the arguments
+        self.copy_class_attributes(ilasm)
         ilasm.end_function()
         
         # begin to_String method
@@ -66,8 +68,13 @@
             f = self.db.genoo.Function(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 copy_class_attributes(self, ilasm):
+        for field_name, (field_type, field_value) in self.classdef._fields.items():
+            ilasm.load_str("this")
+            self.db.load_const(field_type, field_value, ilasm)
+            ilasm.set_field(None, field_name)
+    
     def basename(self, name):
         return name.replace('.', '_')#[-1]

Modified: pypy/dist/pypy/translator/js/asmgen.py
==============================================================================
--- pypy/dist/pypy/translator/js/asmgen.py	(original)
+++ pypy/dist/pypy/translator/js/asmgen.py	Tue Jan  2 00:20:43 2007
@@ -213,9 +213,12 @@
         self.right_hand.append("%s.%s"%(self.right_hand.pop(), name))
     
     def new(self, obj):
-        log("New: %r"%obj)
+        #log("New: %r"%obj)
         self.right_hand.append("new %s()"%obj)
     
+    def runtimenew(self):
+        self.right_hand.append("new %s()" % self.right_hand.pop())
+    
     def load_self(self):
         self.right_hand.append("this")
     

Modified: pypy/dist/pypy/translator/js/database.py
==============================================================================
--- pypy/dist/pypy/translator/js/database.py	(original)
+++ pypy/dist/pypy/translator/js/database.py	Tue Jan  2 00:20:43 2007
@@ -110,6 +110,8 @@
         if self.is_primitive(type_):
             return None
         const = AbstractConst.make(self, value)
+        if not const:
+            return None
         try:
             if retval == 'name':
                 return self.consts[const]
@@ -229,6 +231,11 @@
             return DictConst(db, const)
         elif isinstance(const, bltregistry._external_type):
             return ExtObject(db, const)
+        elif isinstance(const, ootype._class):
+            if const._INSTANCE:
+                return ClassConst(db, const)
+            else:
+                return None
         else:
             assert False, 'Unknown constant: %s %r' % (const, typeOf(const))
     make = staticmethod(make)
@@ -285,32 +292,28 @@
     def record_fields(self):
         if not self.obj:
             return
-        # we support only primitives, tuples, strings and lists
-        
+            import pdb;pdb.set_trace()
         INSTANCE = self.obj._TYPE
-        while INSTANCE:
-            for i, (_type, val) in INSTANCE._fields.iteritems():
-                if _type is not ootype.Void and i.startswith('o'):
-                    name = self.db.record_const(getattr(self.obj, i), _type, 'const')
-                    if name is not None:
-                        self.depends.add(name)
-                        name.depends_on.add(self)
-            INSTANCE = INSTANCE._superclass
+        #while INSTANCE:
+        for i, (_type, val) in INSTANCE._allfields().items():
+            if _type is not ootype.Void:
+                name = self.db.record_const(getattr(self.obj, i), _type, 'const')
+                if name is not None:
+                    self.depends.add(name)
+                    name.depends_on.add(self)
         
     def init_fields(self, ilasm, const_var, name):
         if not self.obj:
             return
         
         INSTANCE = self.obj._TYPE
-        while INSTANCE:
-            for i, (_type, el) in INSTANCE._fields.iteritems():
-                if _type is not ootype.Void and i.startswith('o'):
-                    ilasm.load_local(const_var)
-                    self.db.load_const(_type, getattr(self.obj, i), ilasm)
-                    ilasm.set_field(None, "%s.%s"%(name, i))
-                    ilasm.store_void()
-            INSTANCE = INSTANCE._superclass
-            #raise NotImplementedError("Default fields of instances")
+        #while INSTANCE:
+        for i, (_type, el) in INSTANCE._allfields().items():
+            if _type is not ootype.Void:
+                ilasm.load_local(const_var)
+                self.db.load_const(_type, getattr(self.obj, i), ilasm)
+                ilasm.set_field(None, "%s.%s"%(name, i))
+                ilasm.store_void()
 
 class RecordConst(AbstractConst):
     def get_name(self):
@@ -400,6 +403,26 @@
     def init_fields(self, ilasm, const_var, name):
         pass
 
+class ClassConst(AbstractConst):
+    def __init__(self, db, const):
+        super(ClassConst, self).__init__(db, const)
+        self.cts.lltype_to_cts(const._INSTANCE) # force scheduling of class
+    
+    def get_name(self):
+        return "const_class"
+    
+    def get_key(self):
+        return self.get_name()
+    
+    def get_name(self):
+        return self.const._INSTANCE._name.replace(".", "_")
+    
+    def init(self, ilasm):
+        ilasm.load_const("%s" % self.get_name())
+    
+    #def init_fields(self, ilasm, const_var, name):
+    #    pass
+
 class BuiltinConst(AbstractConst):
     def __init__(self, name):
         self.name = name

Modified: pypy/dist/pypy/translator/js/demo/jsdemo/pythonconsole.py
==============================================================================
--- pypy/dist/pypy/translator/js/demo/jsdemo/pythonconsole.py	(original)
+++ pypy/dist/pypy/translator/js/demo/jsdemo/pythonconsole.py	Tue Jan  2 00:20:43 2007
@@ -177,7 +177,7 @@
         self.wfile.write(data)
 
 
-def build_http_server(server_address=('', 8000)):
+def build_http_server(server_address=('', 8001)):
     global httpd
     httpd = Server(server_address, RequestHandler)
     print 'http://127.0.0.1:%d' % (server_address[1],)

Modified: pypy/dist/pypy/translator/js/function.py
==============================================================================
--- pypy/dist/pypy/translator/js/function.py	(original)
+++ pypy/dist/pypy/translator/js/function.py	Tue Jan  2 00:20:43 2007
@@ -10,7 +10,7 @@
 from pypy.translator.cli.option import getoption
 from pypy.translator.cli.cts import CTS
 from pypy.translator.cli.opcodes import opcodes
-from pypy.translator.cli.metavm import Generator,InstructionList
+from pypy.translator.oosupport.metavm import Generator,InstructionList
 from pypy.translator.cli.node import Node
 from pypy.translator.cli.class_ import Class
 
@@ -411,6 +411,12 @@
     
     def call_external_method(self, name, arg_len):
         self.ilasm.call_method(None, name, [0]*arg_len)
+        
+    def instantiate(self):
+        self.ilasm.runtimenew()
+    
+    def downcast(self, TYPE):
+        pass
 
     def load(self, v):
         if isinstance(v, flowmodel.Variable):

Modified: pypy/dist/pypy/translator/js/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/js/opcodes.py	(original)
+++ pypy/dist/pypy/translator/js/opcodes.py	Tue Jan  2 00:20:43 2007
@@ -2,7 +2,7 @@
 """
 
 from pypy.translator.oosupport.metavm import PushArg, PushAllArgs, StoreResult,\
-    InstructionList, New, SetField, GetField, MicroInstruction
+    InstructionList, New, SetField, GetField, MicroInstruction, RuntimeNew
      
 from pypy.translator.oosupport.metavm import _GetFieldDispatcher, _SetFieldDispatcher, \
     _CallDispatcher, _MethodDispatcher
@@ -110,6 +110,7 @@
     'indirect_call' : [IndirectCall],
     'same_as' : SameAs,
     'new' : [New],
+    'runtimenew' : [RuntimeNew],
     'instanceof' : [IsInstance],
     
     # objects

Modified: pypy/dist/pypy/translator/js/test/runtest.py
==============================================================================
--- pypy/dist/pypy/translator/js/test/runtest.py	(original)
+++ pypy/dist/pypy/translator/js/test/runtest.py	Tue Jan  2 00:20:43 2007
@@ -15,6 +15,8 @@
 from pypy.translator.transformer.debug import DebugTransformer
 from pypy.rlib.nonconst import NonConstant
 
+from pypy.rpython.llinterp import LLException
+
 log = log.runtest
 use_browsertest = conftest.option.browser
 use_tg = conftest.option.tg
@@ -119,6 +121,8 @@
             res = 1e300 * 1e300
         elif s == 'NaN':
             res = (1e300 * 1e300) / (1e300 * 1e300)
+        elif s.startswith("uncaught exception:"):
+            raise LLException(str(s))
         else:
             log('javascript result:', s)
             try:
@@ -169,9 +173,14 @@
         #import exceptions # needed by eval
         #try:
         #import pdb; pdb.set_trace()
-        res = self.interpret(fn, args)
-        assert res.startswith('uncaught exception:')
-        assert re.search(str(exception), res)
+        try:
+            res = self.interpret(fn, args)
+        except LLException, e:
+            s = e.args[0]
+            assert s.startswith('uncaught exception:')
+            assert re.search(str(exception), s)
+        else:
+            raise AssertionError("Did not raise, returned %s" % res)
         #except ExceptionWrapper, ex:
         #    assert issubclass(eval(ex.class_name), exception)
         #else:

Modified: pypy/dist/pypy/translator/js/test/test_rpython.py
==============================================================================
--- pypy/dist/pypy/translator/js/test/test_rpython.py	(original)
+++ pypy/dist/pypy/translator/js/test/test_rpython.py	Tue Jan  2 00:20:43 2007
@@ -7,15 +7,10 @@
 from pypy.rpython.test.test_rtuple import BaseTestRtuple
 from pypy.rpython.test.test_rstr import BaseTestRstr
 
-py.test.skip("Those tests are totally broken, need deeper look")
-
 class TestJsException(JsTest, BaseTestException):
     pass
 
 class TestJsClass(JsTest, BaseTestRclass):
-    def test_classattr_as_defaults(self):
-        py.test.skip("WIP")
-    
     def test_recursive_prebuilt_instance(self):
         py.test.skip("WIP")
     
@@ -27,19 +22,13 @@
     
     def test_mixin(self):
         py.test.skip("WIP")
-    
-    def test_type(self):
+        
+    def test_getattr_on_classes(self):
         py.test.skip("WIP")
-    
+        
     def test_hash_preservation(self):
         py.test.skip("WIP")
-    
-    def test_ne(self):
-        py.test.skip("WIP")
-    
-    def test_eq(self):
-        py.test.skip("WIP")
-    
+
     def test_issubclass_type(self):
         py.test.skip("WIP")
     
@@ -49,14 +38,14 @@
     def test_recursive_prebuilt_instance_classattr(self):
         py.test.skip("WIP")
 
-##class TestJsList(JsTest, BaseTestRlist):
-##    pass
+#class TestJsList(JsTest, BaseTestRlist):
+#    pass
 ##    
-##class TestJsPBC(JsTest, BaseTestRPBC):
-##    pass
+#class TestJsPBC(JsTest, BaseTestRPBC):
+#    pass
 ##
-##class TestJsRtuple(JsTest, BaseTestRtuple):
-##    pass
+#class TestJsRtuple(JsTest, BaseTestRtuple):
+#    pass
 ##
-##class TestJsStr(JsTest, BaseTestRstr):
-##    pass
+#class TestJsStr(JsTest, BaseTestRstr):
+#    pass



More information about the Pypy-commit mailing list