[pypy-svn] r32297 - in pypy/dist/pypy/translator/js: . modules
fijal at codespeak.net
fijal at codespeak.net
Thu Sep 14 11:16:54 CEST 2006
Author: fijal
Date: Thu Sep 14 11:16:51 2006
New Revision: 32297
Modified:
pypy/dist/pypy/translator/js/commproxy.py
pypy/dist/pypy/translator/js/database.py
pypy/dist/pypy/translator/js/modules/_dom.py
Log:
Add a possibility for URL mapping (will evolve in future to dict kind of stuff), added possibility of having XML requests instead of JSON (but who'll use them anyway?)
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 11:16:51 2006
@@ -46,6 +46,18 @@
}
"""
+CALLBACK_XML_BODY = """
+function %(real_callback)s (x, cb) {
+ if (x.readyState == 4) {
+ if (x.responseXML) {
+ cb(x.responseXML.documentElement);
+ } else {
+ cb(null);
+ }
+ }
+}
+"""
+
MOCHIKIT_BODY = """
%(class)s.prototype.%(method)s = function ( %(args)s ) {
var data,str;
@@ -72,9 +84,11 @@
""" Class for rendering xmlhttp request communication
over normal js code
"""
- def __init__(self, ext_obj, name):
+ def __init__(self, ext_obj, name, use_xml=False, base_url=""):
self.ext_obj = ext_obj
self.name = name
+ self.use_xml = use_xml
+ self.base_url = base_url
def render(self, ilasm):
self.render_body(ilasm)
@@ -93,11 +107,22 @@
# FIXME: dirty JS here
data = "{%s}" % ",".join(["'%s':%s" % (i,i) for i in real_args if i != 'callback'])
real_callback = Variable("callback").name
+ if len(self.base_url) > 0 and not self.base_url.endswith("/"):
+ url = self.base_url + "/" +method_name
+ else:
+ url = self.base_url + method_name
+
+ if USE_MOCHIKIT and self.use_xml:
+ assert 0, "Cannot use mochikit and xml requests at the same time"
if USE_MOCHIKIT:
- ilasm.codegenerator.write(MOCHIKIT_BODY % {'class':self.name, 'method':method_name,\
+ ilasm.codegenerator.write(MOCHIKIT_BODY % {'class':self.name, 'method':url,\
'args':','.join(real_args), 'data':data, 'call':method_name})
else:
- ilasm.codegenerator.write(CALLBACK_BODY % {'real_callback':real_callback})
+ if not self.use_xml:
+ callback_body = CALLBACK_BODY
+ else:
+ callback_body = CALLBACK_XML_BODY
+ 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':method_name,\
+ 'args':",".join(real_args), 'data':data, 'call':url,\
'real_callback':real_callback})
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 11:16:51 2006
@@ -95,11 +95,11 @@
def record_class(self, classdef, name):
self.classes[classdef] = name
- def register_comm_proxy(self, proxy_const, name):
+ def register_comm_proxy(self, proxy_const, name, use_xml, base_url):
""" Register external object which should be rendered as
method call
"""
- self.proxies.append(XmlHttp(proxy_const, name))
+ self.proxies.append(XmlHttp(proxy_const, name, use_xml, base_url))
def graph_name(self, graph):
return self.functions.get(graph, None)
@@ -461,8 +461,12 @@
return self.const._TYPE._name.split('.')[-1][:-2]
def init(self, ilasm):
- if getattr(self.const._TYPE._class_, '_render_xmlhttp', False):
- self.db.register_comm_proxy(self.const, self.name)
+ _class = self.const._TYPE._class_
+ if getattr(_class, '_render_xmlhttp', False):
+ use_xml = getattr(_class, '_use_xml', False)
+ base_url = getattr(_class, '_base_url', "") # XXX: should be
+ # on per-method basis
+ self.db.register_comm_proxy(self.const, self.name, use_xml, base_url)
ilasm.new(self.get_name())
else:
# Otherwise they just exist, or it's not implemented
Modified: pypy/dist/pypy/translator/js/modules/_dom.py
==============================================================================
--- pypy/dist/pypy/translator/js/modules/_dom.py (original)
+++ pypy/dist/pypy/translator/js/modules/_dom.py Thu Sep 14 11:16:51 2006
@@ -7,7 +7,7 @@
import time
from pypy.rpython.ootypesystem.bltregistry import BasicExternal, MethodDesc
-#from pypy.translator.stackless.test.test_transform import one
+from pypy.translator.stackless.test.test_transform import one
class Attribute(BasicExternal):
pass
More information about the Pypy-commit
mailing list