Changeset 5295:c0864e7a5d15 in livinglogic.python.xist

Show
Ignore:
Timestamp:
01/25/13 16:10:11 (7 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Add call_js() for calling UL4 functions in Javascript.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • test/test_ul4.py

    r5292 r5295  
    1414import pytest 
    1515 
    16 from ll import ul4c, color, misc 
     16from ll import ul4c, color, misc, ul4on 
    1717from ll.xist.ns import html, ul4 
    1818 
     
    101101    js = "template = {};\ndata = {};\nprint(template.renders(data));\n".format(js, ul4c._asjson(variables)) 
    102102    f = sys._getframe(1) 
    103     print("Testing Javascript code compiled by Python ({}, line {}):".format(f.f_code.co_filename, f.f_lineno)) 
     103    print("Testing Javascript template compiled by Python ({}, line {}):".format(f.f_code.co_filename, f.f_lineno)) 
    104104    print(js) 
    105105    with tempfile.NamedTemporaryFile(mode="wb", suffix=".js") as f: 
     
    128128    ?>""".format(phpexpr(template.dumps()), phpexpr(variables)) 
    129129    f = sys._getframe(1) 
    130     print("Testing PHP code compiled by Python ({}, line {}):".format(f.f_code.co_filename, f.f_lineno)) 
     130    print("Testing PHP template compiled by Python ({}, line {}):".format(f.f_code.co_filename, f.f_lineno)) 
    131131    print(php) 
    132132    with tempfile.NamedTemporaryFile(mode="wb", suffix=".php") as f: 
     
    358358    print(repr(variables)) 
    359359    return function(**variables) 
     360 
     361 
     362def call_js(__, *, keepws=True, **variables): 
     363    """ 
     364    Compile the function from the source ``__``, and generate Javascript source 
     365    from it and call it with the variables ``variables``. 
     366 
     367    (this requires an installed ``d8`` shell from V8 (http://code.google.com/p/v8/)) 
     368    """ 
     369    function = ul4c.Function(__, keepws=keepws) 
     370    js = function.jssource() 
     371    js = "func = {};\ndata = {};\nprint(ul4on.dumps(func.call(data)));\n".format(js, ul4c._asjson(variables)) 
     372    f = sys._getframe(1) 
     373    print("Testing Javascript function compiled by Python ({}, line {}):".format(f.f_code.co_filename, f.f_lineno)) 
     374    print(js) 
     375    with tempfile.NamedTemporaryFile(mode="wb", suffix=".js") as f: 
     376        f.write(js.encode("utf-8")) 
     377        f.flush() 
     378        dir = os.path.expanduser("~/checkouts/LivingLogic.Javascript.ul4") 
     379        proc = subprocess.Popen("d8 {dir}/ul4on.js {dir}/ul4.js {fn}".format(dir=dir, fn=f.name), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) 
     380        (stdout, stderr) = proc.communicate() 
     381    stdout = stdout.decode("utf-8") 
     382    stderr = stderr.decode("utf-8") 
     383    # Check if we have an exception 
     384    if proc.returncode: 
     385        print(stdout, file=sys.stdout) 
     386        print(stderr, file=sys.stderr) 
     387        raise RuntimeError((stderr or stdout).splitlines()[0]) 
     388    return ul4on.loads(stdout) 
    360389 
    361390 
     
    29452974def test_pass_function(r): 
    29462975    assert "&lt;" == r("<?template x?><?print xe('<')?><?end template?><?render x.render(xe=xmlescape)?>") 
    2947     assert "&lt;" == r("<?function xe?><?return xmlescape(s)?><?end function?><?template x?><?print xe(s='<')?><?end template?><?render x.render(xe=xe)?>") 
     2976    assert "&lt;&lt;" == r("<?function xe?><?return xmlescape(s)?><?end function?><?template x?><?print xe(s='<')?><?end template?><?render x.render(xe=xe)?><?render x.render()?>") 
    29482977 
    29492978 
     
    31763205 
    31773206@pytest.mark.ul4 
     3207def test_function_name(c): 
     3208    assert "f" == c("<?function f?><?return self.name?><?end function?><?return f()?>") 
     3209 
     3210 
     3211@pytest.mark.ul4 
    31783212def test_function_closure(c): 
    31793213    assert 24 == c("<?code y=3?><?function inner?><?return 2*x*y?><?end function?><?return inner(x=4)?>")