# Path: pyxc/util.py
# def usingPython3():
#     return sys.version_info[0] == 3
# 
# def check_communicate(cmd, input='', **Popen_kwargs):
#     out, err, returncode = communicateWithReturncode(cmd, input=input, **Popen_kwargs)
#     if returncode != 0:
#         raise SubprocessError(out, err, returncode)
#     return (out, err)
# 
# def exceptionRepr(exc_info=None):
#     
#     if usingPython3():
#         from io import StringIO
#     else:
#         from StringIO import StringIO
#     
#     if not exc_info:
#         exc_info = sys.exc_info()
#     f = StringIO()
#     traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], file=f)
#     return f.getvalue()
# 
# Path: pj/api_internal.py
# def codeToCode(py):
#     
#     t = Transformer(pj.transformations, pj.js_ast.JSStatements)
#     jsAst = t.transformCode(py)
#     js = '%s\n%s' % ('\n'.join(t.snippets), str(jsAst))
#     
#     names = set(topLevelNamesInBody(ast.parse(py).body))
#     if len(names) > 0:
#         js = 'var %s;\n\n%s' % (
#             ', '.join(names),
#             js)
#     
#     return js


import os, json, re, sys
from pyxc.util import usingPython3, check_communicate, exceptionRepr
from pj.api_internal import codeToCode


def valueOfLastLine_js(js):
    newJs = changeLastLine(
                js.strip(),
                lambda line: 'print(json_dumps(%s));' % line.rstrip(';'))
    jsWithJson = '%s\n%s' % (
                getJsonCode(),
                newJs)
    jsOut = runJs(jsWithJson)
    try:
        return json.loads(str(jsOut, 'utf-8'))
    except Exception:
        raise Exception('Error decoding Rhino output %s for js %s' % (
                                repr(jsOut),
                                repr(newJs)))


def changeLastLine(code, f):
    lines = code.split('\n')
    lines[-1] = f(lines[-1])
    return '\n'.join(lines)


def getJsonCode():
    return '''
        var json_dumps = function(x) {
            var json_dumps = arguments.callee;
            if (
                    (x === true) ||

# Path: pyxc/util.py
# def usingPython3():
#     return sys.version_info[0] == 3
# 
# def check_communicate(cmd, input='', **Popen_kwargs):
#     out, err, returncode = communicateWithReturncode(cmd, input=input, **Popen_kwargs)
#     if returncode != 0:
#         raise SubprocessError(out, err, returncode)
#     return (out, err)
# 
# def exceptionRepr(exc_info=None):
#     
#     if usingPython3():
#         from io import StringIO
#     else:
#         from StringIO import StringIO
#     
#     if not exc_info:
#         exc_info = sys.exc_info()
#     f = StringIO()
#     traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], file=f)
#     return f.getvalue()
# 
# Path: pj/api_internal.py
# def codeToCode(py):
#     
#     t = Transformer(pj.transformations, pj.js_ast.JSStatements)
#     jsAst = t.transformCode(py)
#     js = '%s\n%s' % ('\n'.join(t.snippets), str(jsAst))
#     
#     names = set(topLevelNamesInBody(ast.parse(py).body))
#     if len(names) > 0:
#         js = 'var %s;\n\n%s' % (
#             ', '.join(names),
#             js)
#     
#     return js


# Path: pj/tests/dual_eval.py
import os, json, re, sys
from pyxc.util import usingPython3, check_communicate, exceptionRepr
from pj.api_internal import codeToCode



def valueOfLastLine_js(js):
    newJs = changeLastLine(
                js.strip(),
                lambda line: 'print(json_dumps(%s));' % line.rstrip(';'))
    jsWithJson = '%s\n%s' % (
                getJsonCode(),
                newJs)
    jsOut = runJs(jsWithJson)
    try:
        return json.loads(str(jsOut, 'utf-8'))
    except Exception:
        raise Exception('Error decoding Rhino output %s for js %s' % (
                                repr(jsOut),
                                repr(newJs)))


def changeLastLine(code, f):
    lines = code.split('\n')
    lines[-1] = f(lines[-1])
    return '\n'.join(lines)


def getJsonCode():
    return '''
        var json_dumps = function(x) {
            var json_dumps = arguments.callee;
            if (
                    (x === true) ||

                    (x === false) ||