Exemplo n.º 1
0
def rpython2javascript(mod, function_names, jsconfig=None, use_pdb=True):
    if isinstance(function_names, str):
        function_names = [function_names]
        # avoid confusion
    if mod is None:
        # this means actual module, which is quite hairy to get in python,
        # so we cheat
        import sys
        mod = sys.modules[sys._getframe(1).f_globals['__name__']]
    
    if jsconfig is None:
        jsconfig = Config(js_optiondescr)
    if use_pdb:
        jsconfig.use_pdb = True
    module_name = mod.__name__
    if not function_names and 'main' in mod.__dict__:
        function_names.append('main')
    for func_name in function_names:
        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_defaults:
            lgt = len(func_code.func_defaults)
        else:
            lgt = 0
        if func_code.func_code.co_argcount > 0 and func_code.func_code. \
                co_argcount != lgt:
            raise BadSignature("Function %s does not have default arguments" % func_name)
    source_ssf = get_source_ssf(mod, module_name, function_names)
    exec(source_ssf) in globals()
    # now we gonna just cut off not needed function
    # XXX: Really do that
    #options = optparse.Values(defaults=DEFAULT_OPTIONS)
    from pypy.config.pypyoption import get_pypy_config
    config = get_pypy_config(translating=True)
    driver = TranslationDriver(config=config)
    try:
        driver.setup(some_strange_function_which_will_never_be_called, [], policy = JsPolicy())
        driver.proceed(["compile_js"])
        if jsconfig.view:
            driver.translator.view()
        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, use_pdb)