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

fijal at codespeak.net fijal at codespeak.net
Wed Sep 6 11:12:38 CEST 2006


Author: fijal
Date: Wed Sep  6 11:12:11 2006
New Revision: 32024

Added:
   pypy/dist/pypy/translator/js/helper.py   (contents, props changed)
Modified:
   pypy/dist/pypy/translator/js/main.py
   pypy/dist/pypy/translator/js/modules/_dom.py
   pypy/dist/pypy/translator/js/test/test_transformer.py
Log:
Added basic transformation which allows you to display traceback.


Added: pypy/dist/pypy/translator/js/helper.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/js/helper.py	Wed Sep  6 11:12:11 2006
@@ -0,0 +1,34 @@
+
+""" Some helpers
+"""
+
+from pypy.translator.js.modules._dom import get_document
+
+def escape(s):
+    #return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;"). \
+    #    replace("'", "\\'").replace(" ", "&nbsp;").replace("\n", "<br/>")
+    return s
+
+def create_debug_div():
+    debug_div = get_document().createElement("div")
+    # XXX attach it somewhere...
+    #body = get_document().getElementsByTagName('body')[0]
+    get_document().childNodes[0].childNodes[1].appendChild(debug_div)
+    return debug_div
+
+def show_traceback(tb):
+    debug_div = get_document().getElementById("debug_div")
+    if not debug_div:
+        # create div here
+        debug_div = create_debug_div()
+
+    pre_div = get_document().createElement("pre")
+    pre_div.style.color = "#FF0000"
+    debug_div.appendChild(pre_div)
+    for tb_entry in tb:
+        # list of tuples...
+        fun_name, args, filename, lineno = tb_entry
+        # some source maybe? or so?
+        line1 = escape("%s %s" % (fun_name, args))
+        line2 = escape("  %s: %s\n" % (filename, lineno))
+        pre_div.innerHTML += line1 + '\n' + line2

Modified: pypy/dist/pypy/translator/js/main.py
==============================================================================
--- pypy/dist/pypy/translator/js/main.py	(original)
+++ pypy/dist/pypy/translator/js/main.py	Wed Sep  6 11:12:11 2006
@@ -11,6 +11,7 @@
 from pypy.rpython.nonconst import NonConstant
 from pypy.annotation.policy import AnnotatorPolicy
 import optparse
+import py
 
 class FunctionNotFound(Exception):
     pass
@@ -25,7 +26,11 @@
     l = []
     for i in xrange(func_data.func_code.co_argcount):
         l.append("NonConstant(%s)" % repr(func_data.func_defaults[i]))
-    return "(%s)" % ",".join(l)
+    return ",".join(l)
+
+def get_arg_names(func_data):
+    return ",".join(func_data.func_code.co_varnames\
+        [:func_data.func_code.co_argcount])
 
 def rpython2javascript_main(argv):
     if len(argv) < 1:
@@ -39,6 +44,56 @@
     mod = __import__(module_name, None, None, ["Module"])
     return rpython2javascript(mod, function_names)
 
+# some strange function source
+source_ssf_base = """
+
+import %(module_name)s
+from pypy.translator.js.helper import show_traceback
+from pypy.translator.transformer.debug import traceback_handler
+from pypy.rpython.nonconst import NonConstant as NonConst
+
+%(function_defs)s
+
+def some_strange_function_which_will_never_be_called():
+    
+%(functions)s
+"""
+
+wrapped_function_def_base = """
+def %(fun_name)s(%(arg_names)s):
+    try:
+        traceback_handler.enter(NonConst("entrypoint"), NonConst("()"), NonConst(""), NonConst(0))
+        %(module_name)s.%(fun_name)s(%(arg_names)s)
+        traceback_handler.leave(NonConst("entrypoint"))
+    except:
+        new_tb = traceback_handler.tb[:]
+        show_traceback(new_tb)
+"""
+
+function_base = "%(module)s.%(fun_name)s(%(args)s)"
+wrapped_function_base = "%(fun_name)s(%(args)s)"
+
+def get_source_ssf(mod, module_name, function_names, use_debug=True):
+    #source_ssf = "\n".join(["import %s" % module_name, "def some_strange_function_which_will_never_be_called():"] + ["  "+\
+    #    module_name+"."+fun_name+get_args(mod.__dict__[fun_name]) for fun_name in function_names])
+    function_list = []
+    function_def_list = []
+    for fun_name in function_names:
+        args = get_args(mod.__dict__[fun_name])
+        arg_names = get_arg_names(mod.__dict__[fun_name])
+        if not use_debug:
+            base = function_base
+        else:
+            base = wrapped_function_base
+            function_def_list.append(py.code.Source(wrapped_function_def_base %
+                locals()))
+        function_list.append(py.code.Source(base % locals()))
+    function_defs = "\n\n".join([str(i) for i in function_def_list])
+    functions = "\n".join([str(i.indent()) for i in function_list])
+    retval = source_ssf_base % locals()
+    print retval
+    return retval
+
 def rpython2javascript(mod, function_names, use_debug=True):
     module_name = mod.__name__
     if not function_names and 'main' in mod.__dict__:
@@ -47,13 +102,13 @@
         if func_name not in mod.__dict__:
             raise FunctionNotFound("function %r was not found in module %r" % (func_name, module_name))
         func_code = mod.__dict__[func_name]
-        if func_code.func_code.co_argcount > 0 and func_code.func_code.co_argcount != len(func_code.func_defaults):
+        if func_code.func_code.co_argcount > 0 and func_code.func_code. \
+                co_argcount != len(func_code.func_defaults):
             raise BadSignature("Function %s does not have default arguments" % func_name)
-    source_ssf = "\n".join(["import %s" % module_name, "def some_strange_function_which_will_never_be_called():"] + ["  "+\
-        module_name+"."+fun_name+get_args(mod.__dict__[fun_name]) for fun_name in function_names])
+    source_ssf = get_source_ssf(mod, module_name, function_names, use_debug)
     exec(source_ssf) in globals()
-    #fn = compile_function([mod.__dict__[f_name] for f_name in function_names], [[] for i in function_names])
     # now we gonna just cut off not needed function
+    # XXX: Really do that
     options = optparse.Values(defaults=DEFAULT_OPTIONS)
     options.debug_transform = use_debug
     driver = TranslationDriver(options=options)
@@ -61,6 +116,7 @@
         driver.setup(some_strange_function_which_will_never_be_called, [], policy = JsPolicy())
         driver.proceed(["compile_js"])
         return driver.gen.tmpfile.open().read()
+        # XXX: Add some possibility to write down selected file
     except Exception, e:
         # do something nice with it
         debug(driver)

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	Wed Sep  6 11:12:11 2006
@@ -237,7 +237,7 @@
         'getAttributeNS' : MethodDesc(["aa", "aa"], "aa"),
         'getAttributeNode' : MethodDesc(["aa"], Element()),
         'getAttributeNodeNS' : MethodDesc(["aa", "aa"], Element()),
-        'getElementsByTagName' : MethodDesc(["aa"], [Element()]),
+        'getElementsByTagName' : MethodDesc(["aa"], [Element(), Element()]),
         'hasAttribute' : MethodDesc(["aa"], True),
         'hasAttributeNS' : MethodDesc(["aa", "aa"], True),
         'hasAttributes' : MethodDesc([], True),

Modified: pypy/dist/pypy/translator/js/test/test_transformer.py
==============================================================================
--- pypy/dist/pypy/translator/js/test/test_transformer.py	(original)
+++ pypy/dist/pypy/translator/js/test/test_transformer.py	Wed Sep  6 11:12:11 2006
@@ -86,3 +86,5 @@
     lst = retval.split("|")
     check_tb(lst[1], "g", "()", wrapper, 3)
     check_tb(lst[2], "f", "(3, 'dupa'", g, 1)
+
+# XXX: indirect call test as well



More information about the Pypy-commit mailing list