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

fijal at codespeak.net fijal at codespeak.net
Tue Jun 6 17:52:00 CEST 2006


Author: fijal
Date: Tue Jun  6 17:51:59 2006
New Revision: 28397

Modified:
   pypy/dist/pypy/translator/js2/metavm.py
   pypy/dist/pypy/translator/js2/modules/dom.py
   pypy/dist/pypy/translator/js2/opcodes.py
   pypy/dist/pypy/translator/js2/test/test_dom.py
Log:
Added support for oosupport.


Modified: pypy/dist/pypy/translator/js2/metavm.py
==============================================================================
--- pypy/dist/pypy/translator/js2/metavm.py	(original)
+++ pypy/dist/pypy/translator/js2/metavm.py	Tue Jun  6 17:51:59 2006
@@ -3,8 +3,8 @@
 """
 
 #from pypy.translator.js2.jsbuiltin import Builtins
-from pypy.translator.cli.metavm import PushArg, PushAllArgs, StoreResult,\
-    InstructionList, New, SetField, GetField, RuntimeNew, MicroInstruction
+from pypy.translator.oosupport.metavm import PushArg, PushAllArgs, StoreResult,\
+    InstructionList, New, SetField, GetField, MicroInstruction
 
 from pypy.translator.js2.log import log
 from pypy.rpython.ootypesystem import ootype
@@ -59,42 +59,6 @@
     def render(self, generator, op):
         self._render_builtin(generator, self.builtin, op.args)
 
-class _Builtins(object):
-    def __init__(self):
-        list_resize = lambda g,op: SetBuiltinField.run_it(g, op.args[1], 'length', op.args[2])
-        
-        self.builtin_map = {
-            'll_js_jseval' : CallBuiltin('eval'),
-            'll_newlist' : lambda g,op: g.ilasm.load_const("[]"),
-            'll_alloc_and_set' : CallBuiltin('alloc_and_set'),
-            'get_document' : lambda g,op: g.ilasm.load_const('document'),
-            'setTimeout' : CallBuiltin('setTimeout'),
-            'll_int_str' : lambda g,op: Call._render_builtin_method(g, 'toString' , [op.args[2]]),
-            'll_strconcat' : InstructionList([PushAllArgs, '+']),
-            'll_int' : CallBuiltin('parseInt'),
-        }
-        self.builtin_obj_map = {
-            ootype.String.__class__: {
-                'll_strconcat' : InstructionList([PushAllArgs, '+']),
-                'll_strlen' : lambda g,op: GetBuiltinField.run_it(g, op.args[1], 'length'),
-                'll_stritem_nonneg' : ListGetitem,
-                'll_streq' : InstructionList([PushAllArgs, '==']),
-                'll_strcmp' : CallBuiltin('strcmp'),
-                'll_startswith' : CallBuiltin('startswith'),
-                'll_endswith' : CallBuiltin('endswith'),
-            },
-            ootype.List: {
-                'll_setitem_fast' : ListSetitem,
-                'll_getitem_fast' : ListGetitem,
-                '_ll_resize' : list_resize,
-                '_ll_resize_ge' : list_resize,
-                '_ll_resize_le' : list_resize,
-                'll_length' : lambda g,op: GetBuiltinField.run_it(g, op.args[1], 'length'),
-            }
-        }
-        
-Builtins = _Builtins()
-
 class _SameAs(MicroInstruction):
     def render(self, generator, op):
         generator.change_name(op.result, op.args[0])
@@ -184,64 +148,6 @@
         generator.ilasm.load_const(op.args[1].value._name.replace('.', '_'))#[-1])
         generator.cast_function("isinstanceof", 2)
 
-# There are three distinct possibilities where we need to map call differently:
-# 1. Object is marked with rpython_hints as a builtin, so every attribut access
-#    and function call goes as builtin
-# 2. Function called is a builtin, so it might be mapped to attribute access, builtin function call
-#    or even method call
-# 3. Object on which method is called is primitive object and method is mapped to some
-#    method/function/attribute access
-class _GeneralDispatcher(MicroInstruction):
-    def render(self, generator, op):
-        raise NotImplementedError("pure virtual class")
-    
-    def check_builtin(self, this):
-        if not isinstance(this, ootype.Instance):
-            return False
-        return this._hints.get('_suggested_external')
-
-class _MethodDispatcher(_GeneralDispatcher):
-    def render(self, generator, op):
-        method = op.args[0].value
-        this = op.args[1].concretetype
-        if self.check_builtin(this):
-            return CallBuiltinObject.render(generator, op)
-        try:
-            Builtins.builtin_obj_map[this.__class__][method](generator, op)
-            log("%r.%r declared builtin" % (this, method))
-        except KeyError:
-            log("%r.%r declared normal" % (this, method))
-            CallMethod.render(generator, op)
-
-class _CallDispatcher(_GeneralDispatcher):
-    def render(self, generator, op):
-        func = op.args[0]
-        if getattr(func.value._callable, 'suggested_primitive', False):
-            func_name = func.value._name.split("__")[0]
-            log("Function name: %s suggested primitive" % func_name)
-            #if Builtins.builtin_map.has_key(func_name):
-            return Builtins.builtin_map[func_name](generator, op)
-        else:
-            return Call.render(generator, op)
-    
-class _GetFieldDispatcher(_GeneralDispatcher):
-    def render(self, generator, op):
-        if self.check_builtin(op.args[0].concretetype):
-            return GetBuiltinField.render(generator, op)
-        else:
-            return GetField.render(generator, op)
-    
-class _SetFieldDispatcher(_GeneralDispatcher):
-    def render(self, generator, op):
-        if self.check_builtin(op.args[0].concretetype):
-            return SetBuiltinField.render(generator, op)
-        else:
-            return SetField.render(generator, op)
-
-MethodDispatcher = _MethodDispatcher()
-CallDispatcher = _CallDispatcher()
-GetFieldDispatcher = _GetFieldDispatcher()
-SetFieldDispatcher = _SetFieldDispatcher()
 IsInstance = _IsInstance()
 CallMethod = _CallMethod()
 CopyName = [PushAllArgs, _SameAs ()]

Modified: pypy/dist/pypy/translator/js2/modules/dom.py
==============================================================================
--- pypy/dist/pypy/translator/js2/modules/dom.py	(original)
+++ pypy/dist/pypy/translator/js2/modules/dom.py	Tue Jun  6 17:51:59 2006
@@ -39,6 +39,6 @@
 
 def setTimeout(func, delay):
     # scheduler call, but we don't want to mess with threads right now
-    return func
+    func()
 
 setTimeout.suggested_primitive = True

Modified: pypy/dist/pypy/translator/js2/opcodes.py
==============================================================================
--- pypy/dist/pypy/translator/js2/opcodes.py	(original)
+++ pypy/dist/pypy/translator/js2/opcodes.py	Tue Jun  6 17:51:59 2006
@@ -2,17 +2,31 @@
 """ opcode definitions
 """
 
-from pypy.translator.cli.metavm import PushArg, PushAllArgs, StoreResult,\
-    InstructionList, New, SetField, GetField, RuntimeNew, MicroInstruction
+from pypy.translator.oosupport.metavm import PushArg, PushAllArgs, StoreResult,\
+    InstructionList, New, SetField, GetField, MicroInstruction
      
+from pypy.translator.oosupport.metavm import _GetFieldDispatcher, _SetFieldDispatcher, \
+    _CallDispatcher, _MethodDispatcher
+
 from pypy.translator.js2.metavm import SameAs, IsInstance, Call, CallMethod, CopyName, CastString,\
-    _Prefix, _CastFun, _NotImplemented, GetFieldDispatcher, SetFieldDispatcher, CallDispatcher, MethodDispatcher,\
-    CallBuiltin
+    _Prefix, _CastFun, _NotImplemented, CallBuiltin, CallBuiltinObject, GetBuiltinField, SetBuiltinField
+
+from pypy.translator.js2.jsbuiltin import Builtins
 
 DoNothing = [PushAllArgs]
 
 from pypy.translator.js2.log import log
 
+class_map = { 'Call' : Call,
+    'CallMethod' : CallMethod,
+    'CallBuiltinObject' : CallBuiltinObject,
+    'CallBuiltin' : CallBuiltin,
+    'GetBuiltinField' : GetBuiltinField,
+    'GetField' : GetField,
+    'SetField' : SetField,
+    'SetBuiltinField' : SetBuiltinField
+}
+
 opcodes = {'int_mul': '*',
     'int_add': '+',
     'int_sub': '-',
@@ -89,7 +103,7 @@
     'uint_is_true': [PushAllArgs,_Prefix('!!')],
     'float_is_true': [PushAllArgs,_Prefix('!!')],
     
-    'direct_call' : [CallDispatcher],
+    'direct_call' : [_CallDispatcher(Builtins, class_map)],
     'indirect_call' : [_NotImplemented("Indirect call not implemented")],
     'same_as' : SameAs,
     'new' : [New],
@@ -97,9 +111,9 @@
     
     # objects
     
-    'oosetfield' : [SetFieldDispatcher],
-    'oogetfield' : [GetFieldDispatcher],
-    'oosend'     : [MethodDispatcher],
+    'oosetfield' : [_SetFieldDispatcher(Builtins, class_map)],
+    'oogetfield' : [_GetFieldDispatcher(Builtins, class_map)],
+    'oosend'     : [_MethodDispatcher(Builtins, class_map)],
     #'ooupcast'   : [_NotImplemented("Inheritance not implemented (ooupcast)")],
     #'oodowncast' : [_NotImplemented("Inheritance not implemented (oodowncast)")],
     'ooupcast'   : DoNothing,

Modified: pypy/dist/pypy/translator/js2/test/test_dom.py
==============================================================================
--- pypy/dist/pypy/translator/js2/test/test_dom.py	(original)
+++ pypy/dist/pypy/translator/js2/test/test_dom.py	Tue Jun  6 17:51:59 2006
@@ -24,22 +24,32 @@
         assert fn() == '[object HTMLHeadingElement]'
 
     def test_anim(self):
-        def move_it_by(obj, dx, dy, dir):
-            if dir < 0:
-                dx = -dx
-                dy = -dy
-            obj.style.left = str(int(obj.style.left) + dx) + "px"
-            obj.style.top = str(int(obj.style.top) + dy) + "px"
+        class Mover(object):
+            def __init__(self):
+                self.elem = get_document().getElementById("anim_img")
+                self.x = 0
+                self.y = 0
+                self.dir = 1
+            
+            def move_it_by(self, obj, dx, dy):
+                if dir < 0:
+                    dx = -dx
+                    dy = -dy
+                self.x += dx
+                self.y += dy
+                obj.style.left = str(int(obj.style.left) + dx) + "px"
+                obj.style.top = str(int(obj.style.top) + dy) + "px"
         
-        def move_it():
-            move_it_by(get_document().getElementById("anim_img"), 3, 3, 1)
-            setTimeout('move_it()', 100)
+            def move_it(self):
+                self.move_it_by(get_document().getElementById("anim_img"), 3, 3)
+                setTimeout(mov.move_it, 100)
         
         def anim_fun():
             obj = get_document().getElementById("anim_img")
             obj.setAttribute('style', 'position: absolute; top: 0; left: 0;')
-            setTimeout('move_it()', 100)
-            move_it()
+            mov = Mover()
+            setTimeout(mov.move_it, 100)
+            mov.move_it()
         
         fn = compile_function(anim_fun, [], html = 'html/anim.html', is_interactive = True)
         assert fn() == 'ok'



More information about the Pypy-commit mailing list