[pypy-svn] r32077 - in pypy/dist/pypy: bin translator/js translator/js/modules/test translator/js/test

fijal at codespeak.net fijal at codespeak.net
Fri Sep 8 12:51:27 CEST 2006


Author: fijal
Date: Fri Sep  8 12:50:55 2006
New Revision: 32077

Modified:
   pypy/dist/pypy/bin/jscompile.py
   pypy/dist/pypy/translator/js/helper.py
   pypy/dist/pypy/translator/js/main.py
   pypy/dist/pypy/translator/js/modules/test/test_dom.py
   pypy/dist/pypy/translator/js/test/runtest.py
   pypy/dist/pypy/translator/js/test/test_class.py
Log:
Added command line options to jscompile, fixed some typos, improved exception handlers.


Modified: pypy/dist/pypy/bin/jscompile.py
==============================================================================
--- pypy/dist/pypy/bin/jscompile.py	(original)
+++ pypy/dist/pypy/bin/jscompile.py	Fri Sep  8 12:50:55 2006
@@ -1,6 +1,9 @@
 #!/usr/bin/env python
 """ RPython to javascript compiler
 Usage: jscompiler module_to_compile [list of functions to export]
+
+- or -
+jscompiler --help to show list of options
 """
 
 import autopath
@@ -8,5 +11,30 @@
 
 from pypy.translator.js.main import rpython2javascript_main
 
+from pypy.tool import option
+import optparse
+make_option = optparse.make_option
+
+class Options(option.Options):
+    pass
+
+def get_options():
+    options = []
+    
+    options.append(make_option(
+        '--view', action='store_true', dest='view',
+        help='View flow graphs'))
+    
+    options.append(make_option(
+        '-o', '--output', action='store', type='string', dest='output',
+        default='output.js', help='File to save results (default output.js)'))
+    
+    return options
+    
+def process_options(argv):
+    return option.process_options(get_options(), Options, argv)
+    
+
 if __name__ == '__main__':
-    rpython2javascript_main(sys.argv[1:])
+    argv = process_options(sys.argv[1:])
+    rpython2javascript_main(argv, Options)

Modified: pypy/dist/pypy/translator/js/helper.py
==============================================================================
--- pypy/dist/pypy/translator/js/helper.py	(original)
+++ pypy/dist/pypy/translator/js/helper.py	Fri Sep  8 12:50:55 2006
@@ -16,7 +16,7 @@
     get_document().childNodes[0].childNodes[1].appendChild(debug_div)
     return debug_div
 
-def show_traceback(tb):
+def show_traceback(tb, exc):
     debug_div = get_document().getElementById("debug_div")
     if not debug_div:
         # create div here
@@ -25,10 +25,14 @@
     pre_div = get_document().createElement("pre")
     pre_div.style.color = "#FF0000"
     debug_div.appendChild(pre_div)
+    txt = get_document().createTextNode("")
+    pre_div.appendChild(txt)
     for tb_entry in tb[1:]:
         # 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
+        txt.nodeValue += line1 + '\n' + line2
+
+    txt.nodeValue += str(exc)

Modified: pypy/dist/pypy/translator/js/main.py
==============================================================================
--- pypy/dist/pypy/translator/js/main.py	(original)
+++ pypy/dist/pypy/translator/js/main.py	Fri Sep  8 12:50:55 2006
@@ -12,6 +12,7 @@
 from pypy.annotation.policy import AnnotatorPolicy
 import optparse
 import py
+from pypy.tool.option import Options
 
 class FunctionNotFound(Exception):
     pass
@@ -32,7 +33,7 @@
     return ",".join(func_data.func_code.co_varnames\
         [:func_data.func_code.co_argcount])
 
-def rpython2javascript_main(argv):
+def rpython2javascript_main(argv, opts):
     if len(argv) < 1:
         print "usage: module <function_names>"
         import sys
@@ -42,7 +43,10 @@
         module_name = module_name[:-3]
     function_names = argv[1:]
     mod = __import__(module_name, None, None, ["Module"])
-    return rpython2javascript(mod, function_names)
+    source = rpython2javascript(mod, function_names, opts=opts)
+    if opts.output != '':
+        open(opts.output, "w").write(source)
+        print "Written file %s" % opts.output
 
 # some strange function source
 source_ssf_base = """
@@ -65,9 +69,9 @@
         traceback_handler.enter(NonConst("entrypoint"), NonConst("()"), NonConst(""), NonConst(0))
         %(module_name)s.%(fun_name)s(%(arg_names)s)
         traceback_handler.leave(NonConst("entrypoint"))
-    except:
+    except Exception, e:
         new_tb = traceback_handler.tb[:]
-        show_traceback(new_tb)
+        show_traceback(new_tb, str(e))
 """
 
 function_base = "%(module)s.%(fun_name)s(%(args)s)"
@@ -94,7 +98,7 @@
     print retval
     return retval
 
-def rpython2javascript(mod, function_names, use_debug=True):
+def rpython2javascript(mod, function_names, use_debug=True, opts=Options):
     module_name = mod.__name__
     if not function_names and 'main' in mod.__dict__:
         function_names.append('main')
@@ -115,6 +119,8 @@
     try:
         driver.setup(some_strange_function_which_will_never_be_called, [], policy = JsPolicy())
         driver.proceed(["compile_js"])
+        if getattr(opts, 'view', False):
+            driver.translator.view()
         return driver.gen.tmpfile.open().read()
         # XXX: Add some possibility to write down selected file
     except Exception, e:

Modified: pypy/dist/pypy/translator/js/modules/test/test_dom.py
==============================================================================
--- pypy/dist/pypy/translator/js/modules/test/test_dom.py	(original)
+++ pypy/dist/pypy/translator/js/modules/test/test_dom.py	Fri Sep  8 12:50:55 2006
@@ -37,6 +37,8 @@
 movers = [Mover("anim_img"), Mover("anim_img2")]
 movers[1].x = 20
 
+xml = XMLHttpRequest()
+
 def move_it():
     movers[0].move_it()
     #movers[1].move_it()
@@ -52,7 +54,8 @@
             #document.getElementById("dupa").setInnerHTML("<h1>Fire!</h1>")
             #return document.getElementById("dupa")
         
-        fn = compile_function(f, [], html = 'html/test.html')
+        fn = compile_function(f, [], html = str(py.path.local(__file__).\
+            dirpath('html').join('test.html')))
         assert fn() == '[object HTMLHeadingElement]'
     
     def test_anim_f(self):  
@@ -71,13 +74,11 @@
         fn = compile_function(anim_fun, [], html = 'html/anim.html')
         assert fn() == '3px'
     
-    xml = XMLHttpRequest()
-    
     def t_xml_fun(self):
         if xml.readyState == 4:
             alert('Wow!')
             
-    def test_xmlhttp(self):
+    def DONTtest_xmlhttp(self):
         """ Low level XMLHttpRequest test
         """
         def xml_fun():

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	Fri Sep  8 12:50:55 2006
@@ -41,8 +41,6 @@
             DebugTransformer(t).transform_all()
         t.buildrtyper(type_system="ootype").specialize()
 
-        #backend_optimizations(t, raisingop2direct_call_all=True, inline_threshold=0, mallocs=False)
-        #backend_optimizations(t)
         if view or option.view:
             t.view()
         #self.js = JS(t, [function, callback_function], stackless)
@@ -59,8 +57,6 @@
         return self.js.tmpfile.open().read()
 
     def _conv(self, v):
-        #if isinstance(v, str):
-        #    return "{hash:0, chars:'%s'}" % v
         if isinstance(v, str):
             return repr(v)
         return str(v).lower()

Modified: pypy/dist/pypy/translator/js/test/test_class.py
==============================================================================
--- pypy/dist/pypy/translator/js/test/test_class.py	(original)
+++ pypy/dist/pypy/translator/js/test/test_class.py	Fri Sep  8 12:50:55 2006
@@ -140,7 +140,7 @@
 
 def test_instance_ret():
     def instance_ret():
-        return C()
+        return str(C())
     
     fn = compile_function(instance_ret, [])
     assert fn() == '<pypy_translator_js_test_test_class_C instance>'



More information about the Pypy-commit mailing list