[pypy-svn] r32320 - in pypy/dist/pypy/translator/js: . test
fijal at codespeak.net
fijal at codespeak.net
Thu Sep 14 17:15:28 CEST 2006
Author: fijal
Date: Thu Sep 14 17:15:26 2006
New Revision: 32320
Modified:
pypy/dist/pypy/translator/js/commproxy.py
pypy/dist/pypy/translator/js/database.py
pypy/dist/pypy/translator/js/test/test_basicexternal.py
pypy/dist/pypy/translator/js/test/test_bltn.py
Log:
Added some tests for external objects. Added possibility to run POST method.
Modified: pypy/dist/pypy/translator/js/commproxy.py
==============================================================================
--- pypy/dist/pypy/translator/js/commproxy.py (original)
+++ pypy/dist/pypy/translator/js/commproxy.py Thu Sep 14 17:15:26 2006
@@ -6,7 +6,7 @@
from pypy.objspace.flow.model import Variable, Constant
from pypy.rpython.ootypesystem.bltregistry import ArgDesc
-METHOD_BODY = """
+GET_METHOD_BODY = """
%(class)s.prototype.%(method)s = function ( %(args)s ) {
var data,str;
var x = new XMLHttpRequest();
@@ -32,6 +32,32 @@
}
"""
+POST_METHOD_BODY = """
+%(class)s.prototype.%(method)s = function ( %(args)s ) {
+ var data,str;
+ var x = new XMLHttpRequest();
+ data = %(data)s;
+ str = ""
+ for(i in data) {
+ if (data[i]) {
+ if (str.length == 0) {
+ str += "?";
+ } else {
+ str += "&";
+ }
+ str += i + "=" + data[i].toString();
+ }
+ }
+ //logDebug('%(call)s'+str);
+ x.open("POST", '%(call)s', true);
+ //x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+ x.onreadystatechange = function () { %(real_callback)s(x, callback) };
+ //x.setRequestHeader("Connection", "close");
+ x.send(str);
+ x.send(null);
+}
+"""
+
CALLBACK_BODY = """
function %(real_callback)s (x, cb) {
var d;
@@ -84,11 +110,12 @@
""" Class for rendering xmlhttp request communication
over normal js code
"""
- def __init__(self, ext_obj, name, use_xml=False, base_url=""):
+ def __init__(self, ext_obj, name, use_xml=False, base_url="", method="GET"):
self.ext_obj = ext_obj
self.name = name
self.use_xml = use_xml
self.base_url = base_url
+ self.method = method
def render(self, ilasm):
self.render_body(ilasm)
@@ -112,6 +139,7 @@
else:
url = self.base_url + method_name
+ METHOD_BODY = globals()[self.method + "_METHOD_BODY"]
if USE_MOCHIKIT and self.use_xml:
assert 0, "Cannot use mochikit and xml requests at the same time"
if USE_MOCHIKIT:
Modified: pypy/dist/pypy/translator/js/database.py
==============================================================================
--- pypy/dist/pypy/translator/js/database.py (original)
+++ pypy/dist/pypy/translator/js/database.py Thu Sep 14 17:15:26 2006
@@ -95,11 +95,11 @@
def record_class(self, classdef, name):
self.classes[classdef] = name
- def register_comm_proxy(self, proxy_const, name, use_xml, base_url):
+ def register_comm_proxy(self, proxy_const, *args):
""" Register external object which should be rendered as
method call
"""
- self.proxies.append(XmlHttp(proxy_const, name, use_xml, base_url))
+ self.proxies.append(XmlHttp(proxy_const, *args))
def graph_name(self, graph):
return self.functions.get(graph, None)
@@ -465,8 +465,9 @@
if getattr(_class, '_render_xmlhttp', False):
use_xml = getattr(_class, '_use_xml', False)
base_url = getattr(_class, '_base_url', "") # XXX: should be
+ method = getattr(_class, '_use_method', 'GET')
# on per-method basis
- self.db.register_comm_proxy(self.const, self.name, use_xml, base_url)
+ self.db.register_comm_proxy(self.const, self.name, use_xml, base_url, method)
ilasm.new(self.get_name())
else:
# Otherwise they just exist, or it's not implemented
Modified: pypy/dist/pypy/translator/js/test/test_basicexternal.py
==============================================================================
--- pypy/dist/pypy/translator/js/test/test_basicexternal.py (original)
+++ pypy/dist/pypy/translator/js/test/test_basicexternal.py Thu Sep 14 17:15:26 2006
@@ -70,15 +70,25 @@
'b': ["aa"],
}
+D._fields['c'] = [D(),D()]
+
d = D()
def test_basicexternal_list():
+ def getaa(item):
+ return d.c[item]
+
+ def return_list(i):
+ one = getaa(i)
+ if one:
+ two = getaa(i + 3)
+ return two
+ return one
+
+ fun2 = compile_function(return_list, [int])
+
+def test_basicextenal_dict():
def return_dict():
return d.a
- def return_list():
- return d.b
-
fun1 = compile_function(return_dict, [])
- fun2 = compile_function(return_list, [])
-
Modified: pypy/dist/pypy/translator/js/test/test_bltn.py
==============================================================================
--- pypy/dist/pypy/translator/js/test/test_bltn.py (original)
+++ pypy/dist/pypy/translator/js/test/test_bltn.py Thu Sep 14 17:15:26 2006
@@ -50,3 +50,22 @@
fn = compile_function(callback_stuff, [])
assert check_source_contains(fn, "\.some_callback = callback")
+
+def test_get_elements():
+ from pypy.translator.js.modules import _dom as dom
+
+ def getaa(tname):
+ return dom.get_document().getElementsByTagName(tname)[0].nodeValue
+
+ def some_stuff():
+ one = getaa("some")
+ if one:
+ two = getaa("other")
+ return two
+ # if two:
+ # return one + two
+ # else:
+ # return one
+ return one
+
+ fn = compile_function(some_stuff, [])
More information about the Pypy-commit
mailing list