[pypy-svn] r29154 - in pypy/dist/pypy/translator/js: . modules/test test tools

fijal at codespeak.net fijal at codespeak.net
Thu Jun 22 19:11:30 CEST 2006


Author: fijal
Date: Thu Jun 22 19:11:28 2006
New Revision: 29154

Added:
   pypy/dist/pypy/translator/js/modules/test/test_xmlhttp.py
   pypy/dist/pypy/translator/js/tools/
Modified:
   pypy/dist/pypy/translator/js/commproxy.py
   pypy/dist/pypy/translator/js/metavm.py
   pypy/dist/pypy/translator/js/test/runtest.py
   pypy/dist/pypy/translator/js/test/tgtest.py
Log:
Added mochikit mapping and simple transparent proxy demo (One way). New way of declaring exported methods (decorators in future?)


Modified: pypy/dist/pypy/translator/js/commproxy.py
==============================================================================
--- pypy/dist/pypy/translator/js/commproxy.py	(original)
+++ pypy/dist/pypy/translator/js/commproxy.py	Thu Jun 22 19:11:28 2006
@@ -7,15 +7,19 @@
 
 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);
+    var data,str;
+    x = new XMLHttpRequest();
+    data = %(data)s;
+    str = "?"
+    for(i in data) {
+        str += i + "=" + data[i].toString() + ";";
+    }
+    x.open("GET", '%(call)s' + str, true);
+    //x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+    x.onreadystatechange = function () { %(real_callback)s(callback) };
+    //x.setRequestHeader("Connection", "close");
+    //x.send(data);
+    x.send(null);
 }
 """
 
@@ -23,8 +27,12 @@
 function %(real_callback)s (cb) {
    var d;
    if (x.readyState == 4) {
-      eval ( "d = " + x.responseText );
-      cb(d);
+      if (x.responseText) {
+         eval ( "d = " + x.responseText );
+         cb(d);
+      } else {
+         cb({});
+      }
    }
 }
 """
@@ -47,11 +55,8 @@
         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))]
+        args, retval = method.args, method.retval.name
+        real_args = list(arg.name for arg in 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
@@ -59,4 +64,3 @@
         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/metavm.py
==============================================================================
--- pypy/dist/pypy/translator/js/metavm.py	(original)
+++ pypy/dist/pypy/translator/js/metavm.py	Thu Jun 22 19:11:28 2006
@@ -7,6 +7,8 @@
 
 from pypy.translator.js.log import log
 from pypy.rpython.ootypesystem import ootype
+from types import FunctionType
+from pypy.objspace.flow.model import Constant
 
 class _ListSetitem(MicroInstruction):
     def render(self, generator, op):
@@ -44,7 +46,14 @@
     
     def _render_builtin_method(self, generator, builtin, args):
         for func_arg in args:
-            generator.load(func_arg)
+            # FIXME: we cheat here
+            if isinstance(func_arg, Constant) and func_arg.concretetype is ootype.Void and isinstance(func_arg.value, FunctionType):
+                graph = generator.db.translator.annotator.bookkeeper.getdesc(func_arg.value).cachedgraph(None)
+                generator.db.pending_function(graph)
+                name = graph.name
+                generator.ilasm.load_str(name)
+            else:
+                generator.load(func_arg)
         generator.call_external_method(builtin, len(args)-1)
 
     def _render_function(self, generator, graph, args):

Added: pypy/dist/pypy/translator/js/modules/test/test_xmlhttp.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/js/modules/test/test_xmlhttp.py	Thu Jun 22 19:11:28 2006
@@ -0,0 +1,19 @@
+
+""" xmlhttp proxy test
+"""
+
+from pypy.translator.js.demo.jsdemo.proxy import ProxyRootInstance, ProxyRoot
+from pypy.translator.js.test.runtest import compile_function
+
+def empty_fun(d):
+    pass
+
+def test_basic_proxy():
+    def run_proxy():
+        ProxyRootInstance.send_result("3", "", empty_fun)
+    
+    fn = compile_function(run_proxy, [], root = ProxyRoot)
+    result = fn()
+    assert result == 3
+
+#def test_proxy_double_side

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	Thu Jun 22 19:11:28 2006
@@ -15,6 +15,8 @@
 use_browsertest = conftest.option.browser
 use_tg = conftest.option.tg
 
+port = 8080
+
 def _CLI_is_on_path():
     try:
         py.path.local.sysfind('js') #we recommend Spidermonkey
@@ -42,7 +44,11 @@
         #self.js = JS(t, [function, callback_function], stackless)
         self.js = JS(t, [function], stackless)
         self.js.write_source()
-        self.root = root
+        if root is None:
+            from pypy.translator.js.demo.jsdemo.controllers import Root
+            self.root = Root
+        else:
+            self.root = root
 
     def _conv(self, v):
         #if isinstance(v, str):
@@ -62,10 +68,12 @@
                 log("Used html: %r" % self.html)
                 output = jstest(self.js.filename, function_call, use_browsertest, self.html, self.is_interactive)
             else:
+                global port
                 from pypy.translator.js.test.tgtest import run_tgtest
-                out = run_tgtest(self, tg_root = self.root).results
-                assert out[1] == 'undefined'
+                out = run_tgtest(self, tg_root = self.root, port=port).results
+                assert out[1] == 'undefined' or out[1] == ""
                 output = out[0]
+                port += 1
         else:
             cmd = 'echo "load(\'%s\'); print(%s)" | js 2>&1' % (self.js.filename, function_call)
             log(cmd)

Modified: pypy/dist/pypy/translator/js/test/tgtest.py
==============================================================================
--- pypy/dist/pypy/translator/js/test/tgtest.py	(original)
+++ pypy/dist/pypy/translator/js/test/tgtest.py	Thu Jun 22 19:11:28 2006
@@ -12,21 +12,22 @@
 import sys
 import webbrowser
 
-from pypy.jsdemo.jsdemo import controllers
+from pypy.translator.js.demo.jsdemo import controllers
 
 conf_file = os.path.join(os.path.dirname(controllers.__file__), "..", "dev.cfg")
 
 class run_tgtest(object):
-    def __init__(self, compiled_fun, tg_root = None):
+    def __init__(self, compiled_fun, tg_root = None, port = 8080):
         def cont():
             cherrypy.server.wait()
-            webbrowser.open("http://localhost:8080/")
+            webbrowser.open("http://localhost:%d/" % port)
             cherrypy.root.wait_for_results()
             self.results = cherrypy.root.results
             cherrypy.server.stop()
             cherrypy.server.interrupt = SystemExit()
-        
+            
         cherrypy.config.update(file=conf_file)
+        cherrypy.config.update({'global':{'server.socketPort':port}})
 
         if tg_root is None:
             cherrypy.root = controllers.Root()



More information about the Pypy-commit mailing list