[pypy-svn] r39725 - in pypy/dist/pypy/translator/js: examples jssrc lib modules

guido at codespeak.net guido at codespeak.net
Fri Mar 2 17:49:39 CET 2007


Author: guido
Date: Fri Mar  2 17:49:37 2007
New Revision: 39725

Modified:
   pypy/dist/pypy/translator/js/examples/pythonconsole.py
   pypy/dist/pypy/translator/js/jssrc/misc.js
   pypy/dist/pypy/translator/js/lib/server.py
   pypy/dist/pypy/translator/js/modules/mochikit.py
Log:
(fijal, guido) Fixed python console on IE, in the process we switched to using
MochiKit's Signal module for event handling, added HTTP headers to make sure
IE doesn't cache JSON responses, and fixed some getitem accesses on strings 
(which are not supported by IE).


Modified: pypy/dist/pypy/translator/js/examples/pythonconsole.py
==============================================================================
--- pypy/dist/pypy/translator/js/examples/pythonconsole.py	(original)
+++ pypy/dist/pypy/translator/js/examples/pythonconsole.py	Fri Mar  2 17:49:37 2007
@@ -12,7 +12,8 @@
 
 import sys, os, cStringIO
 from cgi import parse_qs
-from pypy.translator.js.modules.dom import setTimeout, document
+from pypy.translator.js.modules.dom import setTimeout, document, window
+from pypy.translator.js.modules.mochikit import connect, disconnect
 from pypy.rpython.ootypesystem.bltregistry import MethodDesc, BasicExternal
 from pypy.translator.js import commproxy
 from pypy.rpython.extfunc import _callable
@@ -38,7 +39,7 @@
 <p>Note that a default timeout for the console is 5 minutes, after that time
 console just dies and stops responding</p>
 
-    <pre id="data"></pre>
+    <div id="data"></div>
 
     <input id="inp" size="100" type="text" autocomplete="off"/>
 
@@ -57,7 +58,13 @@
 
 def add_text(text):
     data_elem = document.getElementById("data")
-    data_elem.appendChild(document.createTextNode(text))
+    lines = text.split('\n')
+    lines.pop()
+    for line in lines:
+        pre = document.createElement('pre')
+        pre.style.margin = '0px'
+        pre.appendChild(document.createTextNode(line))
+        data_elem.appendChild(pre)
 
 class Storage(object):
     def __init__(self):
@@ -67,7 +74,7 @@
 storage = Storage()
 
 def keypressed(key):
-    kc = key.keyCode
+    kc = key._event.keyCode
     if kc == ord("\r"):
         inp_elem = document.getElementById("inp")
         cmd = inp_elem.value
@@ -88,7 +95,7 @@
                 storage.level = 0
 
 def setup_page():
-    document.onkeypress = keypressed
+    connect(document, 'onkeypress', keypressed)
     document.getElementById("inp").focus()
 
 class Server(HTTPServer, BasicExternal):
@@ -177,6 +184,12 @@
         self.send_response(200)
         self.send_header("Content-type", content_type)
         self.send_header("Content-length", len(data))
+        self.send_header('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT')
+        self.send_header('Last-Modified',
+                         time.strftime("%a, %d %b %Y %H:%M:%S GMT"))
+        self.send_header('Cache-Control', 'no-cache, must-revalidate')
+        self.send_header('Cache-Control', 'post-check=0, pre-check=0')
+        self.send_header('Pragma', 'no-cache')
         self.end_headers()
         self.wfile.write(data)
 

Modified: pypy/dist/pypy/translator/js/jssrc/misc.js
==============================================================================
--- pypy/dist/pypy/translator/js/jssrc/misc.js	(original)
+++ pypy/dist/pypy/translator/js/jssrc/misc.js	Fri Mar  2 17:49:37 2007
@@ -49,11 +49,11 @@
 }
 
 function startswith(s1, s2) {
-    if (s1.length<s2.length) {
+    if (s1.length < s2.length) {
         return(false);
     }
     for (i = 0; i < s2.length; ++i){
-        if (s1[i]!=s2[i]) {
+        if (s1.charAt(i) != s2.charAt(i)) {
             return(false);
         }
     }
@@ -61,11 +61,11 @@
 }
 
 function endswith(s1, s2) {
-    if (s2.length>s1.length) {
+    if (s2.length > s1.length) {
         return(false);
     }
-    for (i = s1.length-s2.length; i<s1.length; ++i) {
-        if (s1[i]!=s2[i-s1.length+s2.length]) {
+    for (i = s1.length-s2.length; i < s1.length; ++i) {
+        if (s1.charAt(i) != s2.charAt(i - s1.length + s2.length)) {
             return(false);
         }
     }
@@ -73,16 +73,16 @@
 }
 
 function splitchr(s, ch) {
-    var i, lst;
+    var i, lst, next;
     lst = [];
     next = "";
     for (i = 0; i<s.length; ++i) {
-        if (s[i] == ch) {
+        if (s.charAt(i) == ch) {
             lst.length += 1;
             lst[lst.length-1] = next;
             next = "";
         } else {
-            next += s[i];
+            next += s.charAt(i);
         }
     }
     lst.length += 1;
@@ -134,7 +134,7 @@
 }
 
 StringBuilder.prototype.ll_append = function(s) {
-    this.l += s;
+    this.l.push(s);
 }
 
 StringBuilder.prototype.ll_allocate = function(t) {

Modified: pypy/dist/pypy/translator/js/lib/server.py
==============================================================================
--- pypy/dist/pypy/translator/js/lib/server.py	(original)
+++ pypy/dist/pypy/translator/js/lib/server.py	Fri Mar  2 17:49:37 2007
@@ -68,7 +68,8 @@
             if exec_meth is None:
                 self.send_error(404, "File %s not found" % path)
             else:
-                self.serve_data('text/json', json.write(exec_meth(**args)))
+                self.serve_data('text/json', json.write(exec_meth(**args)),
+                                True)
         else:
             if rest:
                 outp = method_to_call(rest, **args)
@@ -87,13 +88,23 @@
     
     do_POST = do_GET
     
-    def serve_data(self, content_type, data):
+    def serve_data(self, content_type, data, nocache=False):
         self.send_response(200)
         self.send_header("Content-type", content_type)
         self.send_header("Content-length", len(data))
+        if nocache:
+            self.send_nocache_headers()
         self.end_headers()
         self.wfile.write(data)
 
+    def send_nocache_headers(self):
+        self.send_header('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT')
+        self.send_header('Last-Modified',
+                         time.strftime("%a, %d %b %Y %H:%M:%S GMT"))
+        self.send_header('Cache-Control', 'no-cache, must-revalidate')
+        self.send_header('Cache-Control', 'post-check=0, pre-check=0')
+        self.send_header('Pragma', 'no-cache')
+
 class Static(object):
     exposed = True
     

Modified: pypy/dist/pypy/translator/js/modules/mochikit.py
==============================================================================
--- pypy/dist/pypy/translator/js/modules/mochikit.py	(original)
+++ pypy/dist/pypy/translator/js/modules/mochikit.py	Fri Mar  2 17:49:37 2007
@@ -3,6 +3,7 @@
 """
 
 from pypy.rpython.extfunc import _callable, register_external
+from pypy.rpython.ootypesystem.bltregistry import BasicExternal, MethodDesc
 from pypy.translator.js.modules import dom
 
 # MochiKit.LoggingPane
@@ -47,9 +48,17 @@
 
 # MochiKit.Signal
 
+class Event(BasicExternal):
+    pass
+
+Event._fields = {
+    '_event': dom.KeyEvent,
+}
+
+
 def connect(src, signal, dest):
     print 'connecting event %s' % (event,)
-register_external(connect, args=[dom.EventTarget, str, _callable([dom.Event])],
+register_external(connect, args=[dom.EventTarget, str, _callable([Event])],
                   result=int)
 
 def disconnect(id):



More information about the Pypy-commit mailing list