[pypy-svn] r28980 - pypy/dist/pypy/translator/js

fijal at codespeak.net fijal at codespeak.net
Tue Jun 20 11:47:19 CEST 2006


Author: fijal
Date: Tue Jun 20 11:47:17 2006
New Revision: 28980

Added:
   pypy/dist/pypy/translator/js/commproxy.py
Modified:
   pypy/dist/pypy/translator/js/function.py
Log:
Added semi transparent communication proxy.


Added: pypy/dist/pypy/translator/js/commproxy.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/js/commproxy.py	Tue Jun 20 11:47:17 2006
@@ -0,0 +1,62 @@
+
+""" Communication proxy rendering
+"""
+
+
+from pypy.objspace.flow.model import Variable, Constant
+
+METHOD_BODY = """
+%(class)s.prototype.%(method)s = function ( %(args)s ) {
+   var data;
+   x = new XMLHttpRequest();
+   x.open("GET", '%(call)s', true);
+   x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+   x.onreadystatechange = function () { %(real_callback)s(callback) };
+   //x.setRequestHeader("Connection", "close");
+   data = %(data)s;
+   //x.send(data);
+   x.send(null);
+}
+"""
+
+CALLBACK_BODY = """
+function %(real_callback)s (cb) {
+   var d;
+   if (x.readyState == 4) {
+      eval ( "d = " + x.responseText );
+      cb(d);
+   }
+}
+"""
+
+class XmlHttp(object):
+    """ Class for rendering xmlhttp request communication
+    over normal js code
+    """
+    def __init__(self, ext_obj, name):
+        self.ext_obj = ext_obj
+        self.name = name
+    
+    def render(self, ilasm):
+        self.render_body(ilasm)
+        for method_name, method in self.ext_obj._TYPE._class_._methods.iteritems():
+            self.render_method(method_name, method, ilasm)
+    
+    def render_body(self, ilasm):
+        ilasm.begin_function(self.name, [])
+        ilasm.end_function()
+    
+    def render_method(self, method_name, method, ilasm):
+        args, retval = method
+        if isinstance(args, dict):
+            real_args = args.keys()
+        else:
+            real_args = ['v%d' % i for i in xrange(len(args))]
+        # FIXME: dirty JS here
+        data = "{%s}" % ",".join(["'%s':%s" % (i,i) for i in real_args if i != 'callback'])
+        real_callback = Variable("callback").name
+        ilasm.codegenerator.write(CALLBACK_BODY % {'real_callback':real_callback})
+        ilasm.codegenerator.write(METHOD_BODY % {'class':self.name, 'method':method_name,\
+            'args':",".join(real_args), 'data':data, 'call':'http://localhost:8080/'+method_name,\
+            'real_callback':real_callback})
+        

Modified: pypy/dist/pypy/translator/js/function.py
==============================================================================
--- pypy/dist/pypy/translator/js/function.py	(original)
+++ pypy/dist/pypy/translator/js/function.py	Tue Jun 20 11:47:17 2006
@@ -242,7 +242,7 @@
         
         #self.loops = LoopFinder(self.graph.startblock).loops
         if self.is_method:
-            self.ilasm.begin_method(self.name, self._class, args)
+            self.ilasm.begin_method(self.name, self._class, [i[1] for i in args])
         else:
             self.ilasm.begin_function(self.name, args)
         #log("loops: %r"%self.loops)



More information about the Pypy-commit mailing list