Ejemplo n.º 1
0
 def init_ctx(self):
     cc = '''
     exports.AST_Node = AST_Node;
     exports.ALL_KEYWORDS = ALL_KEYWORDS;
     exports.tokenizer = tokenizer;
     exports.parse = parse;
     exports.OutputStream = OutputStream;
     exports.IDENTIFIER_PAT = IDENTIFIER_PAT;
     '''
     self.prompt = self.ps1
     readline = '''
     exports.createInterface = function(options) { rl.completer = options.completer; return rl; }
     '''
     self.ctx = Context(builtin_modules={'readline':readline, 'compiler':cc})
     self.ctx.g.Duktape.write = self.output.write
     self.ctx.eval(r'''console = { log: function() { Duktape.write(Array.prototype.slice.call(arguments).join(' ') + '\n');}};
                   console['error'] = console['log'];''')
     cc = P(COMPILER_PATH, data=True, allow_user_override=False)
     self.ctx.eval(cc)
     baselib = dict(dict(self.ctx.g.rs_baselib_pyj)['beautifed'])
     baselib = '\n\n'.join(baselib.itervalues())
     self.ctx.eval('module = {}')
     self.ctx.eval(self.ctx.g.rs_repl_js, fname='repl.js')
     self.ctx.g.repl_options = {
         'baselib': baselib, 'show_js': self.show_js,
         'histfile':False,
         'input':True, 'output':True, 'ps1':self.ps1, 'ps2':self.ps2,
         'terminal':self.output.isatty,
         'enum_global': 'Object.keys(this)',
         'lib_path': self.libdir or os.path.dirname(P(COMPILER_PATH))  # TODO: Change this to load pyj files from the src code
     }
Ejemplo n.º 2
0
def compiler():
    ans = getattr(tls, 'compiler', None)
    if ans is None:
        from duktape import Context
        c = tls.compiler = Context()
        c.eval(P('coffee-script.js', data=True).decode('utf-8'))
    return tls.compiler
Ejemplo n.º 3
0
def compiler():
    c = getattr(tls, 'compiler', None)
    if c is None:
        c = tls.compiler = Context()
        c.eval('exports = {}; sha1sum = Duktape.sha1sum;', noreturn=True)
        buf = BytesIO()
        decompress(P(COMPILER_PATH, data=True, allow_user_override=False), buf)
        c.eval(buf.getvalue(), fname=COMPILER_PATH, noreturn=True)
    return c
Ejemplo n.º 4
0
def compiler():
    c = getattr(tls, 'compiler', None)
    if c is None:
        c = tls.compiler = Context(
            base_dirs=(P('rapydscript', allow_user_override=False), ))
        c.eval(P(COMPILER_PATH, data=True,
                 allow_user_override=False).decode('utf-8'),
               fname='rapydscript-compiler.js')
        c.g.current_output_options = {}
    return c
Ejemplo n.º 5
0
def update_rapydscript():
    vm_js = '''
    exports.createContext = function(x) { x.AST_Node = {}; return x; }
    exports.runInContext = function() { return null; }
    '''
    fs_js = '''
    exports.realpathSync = function(x) { return x; }
    exports.readFileSync = function() { return ""; }
    '''
    path_js = '''
    exports.join = function(x, y) { return x + '/' + y; }
    exports.dirname = function(x) { return x; }
    exports.resolve = function(x) { return x; }
    '''

    d = os.path.dirname
    base = d(d(d(d(d(abspath(__file__))))))
    base = os.path.join(base, 'rapydscript')
    ctx = Context(base_dirs=(base, ),
                  builtin_modules={
                      'path': path_js,
                      'fs': fs_js,
                      'vm': vm_js
                  })
    ctx.g.require.id = 'rapydscript/bin'
    try:
        ctx.eval('RapydScript = require("../tools/compiler")',
                 fname='bin/rapydscript')
    except JSError as e:
        raise SystemExit('%s:%s:%s' % (e.fileName, e.lineNumber, e.message))
    data = b'\n\n'.join(
        open(os.path.join(base, 'lib', x + '.js'), 'rb').read()
        for x in ctx.g.RapydScript.FILENAMES)

    package = json.load(open(os.path.join(base, 'package.json')))
    baselib = parse_baselib(
        open(os.path.join(base, 'src', 'baselib.pyj'),
             'rb').read().decode('utf-8'))
    ctx = Context()
    ctx.eval(data.decode('utf-8'))
    baselib = {
        'beautifed': compile_baselib(ctx, baselib),
        'minified': compile_baselib(ctx, baselib, False)
    }
    repl = open(os.path.join(base, 'tools', 'repl.js'), 'rb').read()

    with open(P(COMPILER_PATH, allow_user_override=False), 'wb') as f:
        f.write(data)
        f.write(b'\n\nrs_baselib_pyj = ' + json.dumps(baselib) + b';')
        f.write(b'\n\nrs_repl_js = ' + json.dumps(repl) + b';')
        f.write(b'\n\nrs_package_version = ' + json.dumps(package['version']) +
                b';\n')
Ejemplo n.º 6
0
 def init_ctx(self):
     cc = '''
     exports.AST_Node = AST_Node;
     exports.ALL_KEYWORDS = ALL_KEYWORDS;
     exports.tokenizer = tokenizer;
     exports.parse = parse;
     exports.OutputStream = OutputStream;
     exports.IDENTIFIER_PAT = IDENTIFIER_PAT;
     '''
     self.prompt = self.ps1
     readline = '''
     exports.createInterface = function(options) { rl.completer = options.completer; return rl; }
     '''
     self.ctx = Context(builtin_modules={
         'readline': readline,
         'compiler': cc
     })
     self.ctx.g.Duktape.write = self.output.write
     self.ctx.eval(
         r'''console = { log: function() { Duktape.write(Array.prototype.slice.call(arguments).join(' ') + '\n');}};
                   console['error'] = console['log'];''')
     cc = P(COMPILER_PATH, data=True, allow_user_override=False)
     self.ctx.eval(cc)
     baselib = dict(dict(self.ctx.g.rs_baselib_pyj)['beautifed'])
     baselib = '\n\n'.join(baselib.itervalues())
     self.ctx.eval('module = {}')
     self.ctx.eval(self.ctx.g.rs_repl_js, fname='repl.js')
     self.ctx.g.repl_options = {
         'baselib': baselib,
         'show_js': self.show_js,
         'histfile': False,
         'input': True,
         'output': True,
         'ps1': self.ps1,
         'ps2': self.ps2,
         'terminal': self.output.isatty,
         'enum_global': 'Object.keys(this)',
         'lib_path': self.libdir or os.path.dirname(
             P(COMPILER_PATH
               ))  # TODO: Change this to load pyj files from the src code
     }
Ejemplo n.º 7
0
def update_rapydscript():
    vm_js = '''
    exports.createContext = function(x) { x.AST_Node = {}; return x; }
    exports.runInContext = function() { return null; }
    '''
    fs_js = '''
    exports.realpathSync = function(x) { return x; }
    exports.readFileSync = function() { return ""; }
    '''
    path_js = '''
    exports.join = function(x, y) { return x + '/' + y; }
    exports.dirname = function(x) { return x; }
    exports.resolve = function(x) { return x; }
    '''

    d = os.path.dirname
    base = d(d(d(d(d(abspath(__file__))))))
    base = os.path.join(base, 'rapydscript')
    ctx = Context(base_dirs=(base,), builtin_modules={'path':path_js, 'fs':fs_js, 'vm':vm_js})
    ctx.g.require.id = 'rapydscript/bin'
    try:
        ctx.eval('RapydScript = require("../tools/compiler")', fname='bin/rapydscript')
    except JSError as e:
        raise SystemExit('%s:%s:%s' % (e.fileName, e.lineNumber, e.message))
    data = b'\n\n'.join(open(os.path.join(base, 'lib', x + '.js'), 'rb').read() for x in ctx.g.RapydScript.FILENAMES)

    package = json.load(open(os.path.join(base, 'package.json')))
    baselib = parse_baselib(open(os.path.join(base, 'src', 'baselib.pyj'), 'rb').read().decode('utf-8'))
    ctx = Context()
    ctx.eval(data.decode('utf-8'))
    baselib = {'beautifed': compile_baselib(ctx, baselib), 'minified': compile_baselib(ctx, baselib, False)}
    repl = open(os.path.join(base, 'tools', 'repl.js'), 'rb').read()

    with open(P(COMPILER_PATH, allow_user_override=False), 'wb') as f:
        f.write(data)
        f.write(b'\n\nrs_baselib_pyj = ' + json.dumps(baselib) + b';')
        f.write(b'\n\nrs_repl_js = ' + json.dumps(repl) + b';')
        f.write(b'\n\nrs_package_version = ' + json.dumps(package['version']) + b';\n')
Ejemplo n.º 8
0
class Repl(Thread):

    LINE_CONTINUATION_CHARS = r'\:'
    daemon = True

    def __init__(self, ps1='>>> ', ps2='... ', show_js=False, libdir=None):
        Thread.__init__(self, name='RapydScriptREPL')
        self.to_python = to_python
        self.JSError = JSError
        self.enc = getattr(sys.stdin, 'encoding', None) or 'utf-8'
        try:
            import readline
            self.readline = readline
        except ImportError:
            pass
        self.output = ANSIStream(sys.stdout)
        self.to_repl = Queue()
        self.from_repl = Queue()
        self.ps1, self.ps2 = ps1, ps2
        self.show_js, self.libdir = show_js, libdir
        self.prompt = ''
        self.completions = None
        self.start()

    def init_ctx(self):
        cc = '''
        exports.AST_Node = AST_Node;
        exports.ALL_KEYWORDS = ALL_KEYWORDS;
        exports.tokenizer = tokenizer;
        exports.parse = parse;
        exports.OutputStream = OutputStream;
        exports.IDENTIFIER_PAT = IDENTIFIER_PAT;
        '''
        self.prompt = self.ps1
        readline = '''
        exports.createInterface = function(options) { rl.completer = options.completer; return rl; }
        '''
        self.ctx = Context(builtin_modules={'readline':readline, 'compiler':cc})
        self.ctx.g.Duktape.write = self.output.write
        self.ctx.eval(r'''console = { log: function() { Duktape.write(Array.prototype.slice.call(arguments).join(' ') + '\n');}};
                      console['error'] = console['log'];''')
        cc = P(COMPILER_PATH, data=True, allow_user_override=False)
        self.ctx.eval(cc)
        baselib = dict(dict(self.ctx.g.rs_baselib_pyj)['beautifed'])
        baselib = '\n\n'.join(baselib.itervalues())
        self.ctx.eval('module = {}')
        self.ctx.eval(self.ctx.g.rs_repl_js, fname='repl.js')
        self.ctx.g.repl_options = {
            'baselib': baselib, 'show_js': self.show_js,
            'histfile':False,
            'input':True, 'output':True, 'ps1':self.ps1, 'ps2':self.ps2,
            'terminal':self.output.isatty,
            'enum_global': 'Object.keys(this)',
            'lib_path': self.libdir or os.path.dirname(P(COMPILER_PATH))  # TODO: Change this to load pyj files from the src code
        }

    def get_from_repl(self):
        while True:
            try:
                return self.from_repl.get(True, 1)
            except Empty:
                if not self.is_alive():
                    raise SystemExit(1)

    def run(self):
        self.init_ctx()
        rl = None

        def set_prompt(p):
            self.prompt = p

        def prompt(lw):
            self.from_repl.put(to_python(lw))

        self.ctx.g.set_prompt = set_prompt
        self.ctx.g.prompt = prompt

        self.ctx.eval('''
        listeners = {};
        rl = {
            setPrompt:set_prompt,
            write:Duktape.write,
            clearLine: function() {},
            on: function(ev, cb) { listeners[ev] = cb; return rl; },
            prompt: prompt,
            sync_prompt: true,
            send_line: function(line) { listeners['line'](line); },
            send_interrupt: function() { listeners['SIGINT'](); },
            close: function() {listeners['close'](); },
        };
        ''')
        rl = self.ctx.g.rl
        self.ctx.eval('module.exports(repl_options)')
        completer = to_python(rl.completer)
        send_interrupt = to_python(rl.send_interrupt)
        send_line = to_python(rl.send_line)

        while True:
            ev, line = self.to_repl.get()
            try:
                if ev == 'SIGINT':
                    self.output.write('\n')
                    send_interrupt()
                elif ev == 'line':
                    send_line(line)
                else:
                    val = completer(line)
                    val = to_python(val)
                    self.from_repl.put(val[0])
            except Exception as e:
                if isinstance(e, JSError):
                    print (e.stack or e.message, file=sys.stderr)
                else:
                    import traceback
                    traceback.print_exc()

                for i in xrange(100):
                    # Do this many times to ensure we dont deadlock
                    self.from_repl.put(None)

    def __call__(self):
        if hasattr(self, 'readline'):
            history = os.path.join(cache_dir(), 'pyj-repl-history.txt')
            self.readline.parse_and_bind("tab: complete")
            try:
                self.readline.read_history_file(history)
            except EnvironmentError as e:
                if e.errno != errno.ENOENT:
                    raise
            atexit.register(partial(self.readline.write_history_file, history))

        def completer(text, num):
            if self.completions is None:
                self.to_repl.put(('complete', text))
                self.completions = filter(None, self.get_from_repl())
                if self.completions is None:
                    return None
            try:
                return self.completions[num]
            except (IndexError, TypeError, AttributeError, KeyError):
                self.completions = None

        if hasattr(self, 'readline'):
            self.readline.set_completer(completer)

        while True:
            lw = self.get_from_repl()
            if lw is None:
                raise SystemExit(1)
            q = self.prompt
            if hasattr(self, 'readline'):
                self.readline.set_pre_input_hook(lambda:(self.readline.insert_text(lw), self.readline.redisplay()))
            else:
                q += lw
            try:
                line = raw_input(q)
                self.to_repl.put(('line', line))
            except EOFError:
                return
            except KeyboardInterrupt:
                self.to_repl.put(('SIGINT', None))
Ejemplo n.º 9
0
class Repl(Thread):

    LINE_CONTINUATION_CHARS = r'\:'
    daemon = True

    def __init__(self, ps1='>>> ', ps2='... ', show_js=False, libdir=None):
        Thread.__init__(self, name='RapydScriptREPL')
        self.to_python = to_python
        self.JSError = JSError
        self.enc = getattr(sys.stdin, 'encoding', None) or 'utf-8'
        try:
            import readline
            self.readline = readline
        except ImportError:
            pass
        self.output = ANSIStream(sys.stdout)
        self.to_repl = Queue()
        self.from_repl = Queue()
        self.ps1, self.ps2 = ps1, ps2
        self.show_js, self.libdir = show_js, libdir
        self.prompt = ''
        self.completions = None
        self.start()

    def init_ctx(self):
        cc = '''
        exports.AST_Node = AST_Node;
        exports.ALL_KEYWORDS = ALL_KEYWORDS;
        exports.tokenizer = tokenizer;
        exports.parse = parse;
        exports.OutputStream = OutputStream;
        exports.IDENTIFIER_PAT = IDENTIFIER_PAT;
        '''
        self.prompt = self.ps1
        readline = '''
        exports.createInterface = function(options) { rl.completer = options.completer; return rl; }
        '''
        self.ctx = Context(builtin_modules={
            'readline': readline,
            'compiler': cc
        })
        self.ctx.g.Duktape.write = self.output.write
        self.ctx.eval(
            r'''console = { log: function() { Duktape.write(Array.prototype.slice.call(arguments).join(' ') + '\n');}};
                      console['error'] = console['log'];''')
        cc = P(COMPILER_PATH, data=True, allow_user_override=False)
        self.ctx.eval(cc)
        baselib = dict(dict(self.ctx.g.rs_baselib_pyj)['beautifed'])
        baselib = '\n\n'.join(baselib.itervalues())
        self.ctx.eval('module = {}')
        self.ctx.eval(self.ctx.g.rs_repl_js, fname='repl.js')
        self.ctx.g.repl_options = {
            'baselib': baselib,
            'show_js': self.show_js,
            'histfile': False,
            'input': True,
            'output': True,
            'ps1': self.ps1,
            'ps2': self.ps2,
            'terminal': self.output.isatty,
            'enum_global': 'Object.keys(this)',
            'lib_path': self.libdir or os.path.dirname(
                P(COMPILER_PATH
                  ))  # TODO: Change this to load pyj files from the src code
        }

    def get_from_repl(self):
        while True:
            try:
                return self.from_repl.get(True, 1)
            except Empty:
                if not self.is_alive():
                    raise SystemExit(1)

    def run(self):
        self.init_ctx()
        rl = None

        def set_prompt(p):
            self.prompt = p

        def prompt(lw):
            self.from_repl.put(to_python(lw))

        self.ctx.g.set_prompt = set_prompt
        self.ctx.g.prompt = prompt

        self.ctx.eval('''
        listeners = {};
        rl = {
            setPrompt:set_prompt,
            write:Duktape.write,
            clearLine: function() {},
            on: function(ev, cb) { listeners[ev] = cb; return rl; },
            prompt: prompt,
            sync_prompt: true,
            send_line: function(line) { listeners['line'](line); },
            send_interrupt: function() { listeners['SIGINT'](); },
            close: function() {listeners['close'](); },
        };
        ''')
        rl = self.ctx.g.rl
        self.ctx.eval('module.exports(repl_options)')
        completer = to_python(rl.completer)
        send_interrupt = to_python(rl.send_interrupt)
        send_line = to_python(rl.send_line)

        while True:
            ev, line = self.to_repl.get()
            try:
                if ev == 'SIGINT':
                    self.output.write('\n')
                    send_interrupt()
                elif ev == 'line':
                    send_line(line)
                else:
                    val = completer(line)
                    val = to_python(val)
                    self.from_repl.put(val[0])
            except Exception as e:
                if isinstance(e, JSError):
                    print(e.stack or e.message, file=sys.stderr)
                else:
                    import traceback
                    traceback.print_exc()

                for i in xrange(100):
                    # Do this many times to ensure we dont deadlock
                    self.from_repl.put(None)

    def __call__(self):
        if hasattr(self, 'readline'):
            history = os.path.join(cache_dir(), 'pyj-repl-history.txt')
            self.readline.parse_and_bind("tab: complete")
            try:
                self.readline.read_history_file(history)
            except EnvironmentError as e:
                if e.errno != errno.ENOENT:
                    raise
            atexit.register(partial(self.readline.write_history_file, history))

        def completer(text, num):
            if self.completions is None:
                self.to_repl.put(('complete', text))
                self.completions = filter(None, self.get_from_repl())
                if self.completions is None:
                    return None
            try:
                return self.completions[num]
            except (IndexError, TypeError, AttributeError, KeyError):
                self.completions = None

        if hasattr(self, 'readline'):
            self.readline.set_completer(completer)

        while True:
            lw = self.get_from_repl()
            if lw is None:
                raise SystemExit(1)
            q = self.prompt
            if hasattr(self, 'readline'):
                self.readline.set_pre_input_hook(lambda: (
                    self.readline.insert_text(lw), self.readline.redisplay()))
            else:
                q += lw
            try:
                line = raw_input(q)
                self.to_repl.put(('line', line))
            except EOFError:
                return
            except KeyboardInterrupt:
                self.to_repl.put(('SIGINT', None))