[pypy-svn] r28400 - in pypy/dist/pypy/translator/js2: . test
fijal at codespeak.net
fijal at codespeak.net
Tue Jun 6 19:10:08 CEST 2006
Author: fijal
Date: Tue Jun 6 19:10:07 2006
New Revision: 28400
Modified:
pypy/dist/pypy/translator/js2/metavm.py
pypy/dist/pypy/translator/js2/test/test_dom.py
Log:
First javascript animation working.
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 19:10:07 2006
@@ -154,6 +154,21 @@
generator.load(func_arg)
generator.call_external(op.args[0].name, op.args[1:])
+class _SetTimeout(MicroInstruction):
+ # FIXME: Dirty hack for javascript callback stuff
+ def render(self, generator, op):
+ val = op.args[1].value
+ if isinstance(val, ootype.StaticMethod):
+ real_name = val._name
+ generator.db.pending_function(val.graph)
+ else:
+ real_name = val.concretize().value._name
+ generator.db.pending_function(val.concretize().value.graph)
+ generator.load_str("'%s()'" % real_name)
+ generator.load(op.args[2])
+ generator.call_external('setTimeout',[0]*2)
+
+SetTimeout = _SetTimeout()
IndirectCall = _IndirectCall()
IsInstance = _IsInstance()
CallMethod = _CallMethod()
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 19:10:07 2006
@@ -5,7 +5,7 @@
import py
from pypy.translator.js2.test.runtest import compile_function
-from pypy.translator.js2.modules.dom import document, setTimeout, Node, get_document
+from pypy.translator.js2.modules.dom import document, Node, get_document, setTimeout
from pypy.translator.js2 import conftest
import time
@@ -13,43 +13,48 @@
if not conftest.option.browser:
py.test.skip("Works only in browser (right now?)")
-class TestDOM(object):
- def test_document_base(self):
- def f():
- return get_document().getElementById("dupa")
- #document.getElementById("dupa").setInnerHTML("<h1>Fire!</h1>")
- #return document.getElementById("dupa")
-
- fn = compile_function(f, [], html = 'html/test.html')
- assert fn() == '[object HTMLHeadingElement]'
-
- def test_anim(self):
- 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(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;')
- 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'
+def test_document_base():
+ def f():
+ return get_document().getElementById("dupa")
+ #document.getElementById("dupa").setInnerHTML("<h1>Fire!</h1>")
+ #return document.getElementById("dupa")
+
+ fn = compile_function(f, [], html = 'html/test.html')
+ assert fn() == '[object HTMLHeadingElement]'
+
+class Mover(object):
+ def __init__(self):
+ self.x = 0
+ self.y = 0
+ self.dir = 1
+
+ def move_it_by(self, obj, dx, dy):
+ if self.dir < 0:
+ dx = -dx
+ dy = -dy
+ self.x += dx
+ self.y += dy
+ if self.x > 100:
+ self.dir = -1
+ if self.x < 0:
+ self.dir = 1
+ obj.style.left = str(int(obj.style.left) + dx) + "px"
+ obj.style.top = str(int(obj.style.top) + dy) + "px"
+
+ def move_it(self):
+ self.move_it_by(get_document().getElementById("anim_img"), 3, 3)
+
+movers = [Mover(), Mover()]
+
+def move_it():
+ movers[0].move_it()
+ setTimeout(move_it, 10)
+
+def test_anim_f():
+ def anim_fun():
+ obj = get_document().getElementById("anim_img")
+ obj.setAttribute('style', 'position: absolute; top: 0; left: 0;')
+ setTimeout(move_it, 10)
+
+ fn = compile_function(anim_fun, [], html = 'html/anim.html', is_interactive = True)
+ assert fn() == 'ok'
More information about the Pypy-commit
mailing list