Example #1
0
 def walk(self, script):
     self.block_no = 1
     
     try:
         PyV8.JSEngine().compile(script).visit(self)
     except UnicodeDecodeError:
         enc = chardet.detect(script)
         PyV8.JSEngine().compile(script.decode(enc['encoding'])).visit(self)
Example #2
0
    def walk(self, script):
        self.block_no = 1

        try:
            PyV8.JSEngine().compile(script).visit(self)
        except UnicodeDecodeError:
            enc = log.Encoding.detect(script, safe=True)
            if not enc:
                return

            PyV8.JSEngine().compile(script.decode(enc)).visit(self)
        except:
            pass
Example #3
0
    def walk(self, script):
        self.block_no = 1

        try:
            PyV8.JSEngine().compile(script).visit(self)
        except UnicodeDecodeError:
            enc = log.Encoding.detect(script, safe=True)
            if enc is None:
                return

            PyV8.JSEngine().compile(script.decode(enc['encoding'])).visit(self)
        except:  # pylint:disable=bare-except
            pass
Example #4
0
        def exec_(self, source):
            source = '''\
            (function() {{
                {0};
                {1};
            }})()'''.format(encode_unicode_codepoints(self._source),
                            encode_unicode_codepoints(source))
            source = str(source)

            import PyV8
            import contextlib
            #backward compatibility
            with contextlib.nested(PyV8.JSContext(),
                                   PyV8.JSEngine()) as (ctxt, engine):
                js_errors = (PyV8.JSError, IndexError, ReferenceError,
                             SyntaxError, TypeError)
                try:
                    script = engine.compile(source)
                except js_errors as e:
                    raise RuntimeError(e)
                try:
                    value = script.run()
                except js_errors as e:
                    raise ProgramError(e)
                return self.convert(value)
        def _exec_(self, source):
            source = '''\
            (function() {{
                {0};
                {1};
            }})()'''.format(encode_unicode_codepoints(self._source),
                            encode_unicode_codepoints(source))
            source = str(source)

            # backward compatibility
            with PyV8.JSContext() as ctxt, PyV8.JSEngine() as engine:
                js_errors = (PyV8.JSError, IndexError, ReferenceError,
                             SyntaxError, TypeError)
                try:
                    script = engine.compile(source)
                except js_errors as e:
                    raise exceptions.ProgramError(e)
                try:
                    value = script.run()
                except js_errors as e:
                    raise exceptions.ProgramError(e)
                return self.convert(value)
Example #6
0
    def run(self,
            func=None,
            fargs=[],
            precompil_only=False,
            compil_only=False):
        """
        Run JS code with libraries and return last operand result or
            `func` result if it present

        :param str func: (optional) JS function name
        :param list fargs: (optional) list of JS function args
        :param bool precompil_only: (optional) if true, then it will
            return precommiled data all JS code
        :param bool compil_only: (optional) if true, then it
            will compile the JS code and throw errors if they were

        :raise SyntaxError: JS syntax error
        :raise PyV8.JSError: JS runtime error (contain fields:
            name, message, scriptName, lineNum, startPos, endPos,
            startCol, endCol, sourceLine, stackTrace)
        """
        precompiled = OrderedDict()
        compiled = OrderedDict()

        with PyV8.JSLocker():
            with PyV8.JSContext() as js_context:
                self.logger.debug('Set JS global context class attributes')
                for k, v in self.js_global_vars.items():
                    self.logger.debug('Set attribute name=%s, value=%s' %
                                      (k, v))
                    # Convert to JS objects
                    setattr(js_context.locals, k,
                            self._get_js_obj(js_context, v))

                with PyV8.JSEngine() as engine:
                    precompil_error = False
                    try:
                        for js_lib, js_code in self.js_libs_code.items():
                            self.logger.debug('Precompile JS lib: %s' % js_lib)
                            precompiled[js_lib] = engine.precompile(js_code)
                    except SyntaxError:
                        precompil_error = True

                    if not precompil_error and precompil_only:
                        return precompiled

                    for js_lib, js_code in self.js_libs_code.items():
                        self.logger.debug('Compile JS lib: %s' % js_lib)
                        cparams = dict(source=self.js_libs_code[js_lib],
                                       name=js_lib)
                        if js_lib in precompiled:
                            cparams['precompiled'] = precompiled[js_lib]
                        compiled[js_lib] = engine.compile(**cparams)
                    if compil_only:
                        return True

                    result = None
                    for js_lib, js_script in compiled.items():
                        self.logger.debug('Run JS lib: %s' % js_lib)
                        result = js_script.run()

                    if not func or type(func) != str:
                        return result

                    if fargs and not isinstance(fargs, (list, tuple)):
                        raise ArgumentError(
                            'The "fargs" must be list or tuple')

                    if func not in js_context.locals:
                        raise JSFunctionNotExists(
                            'Function "%s" not exists in JS context' % func)

                    # Convert to JS objects
                    for i in range(len(fargs)):
                        fargs[i] = self._get_js_obj(js_context, fargs[i])

                    # Convert to Python objects
                    return self._get_py_obj(js_context,
                                            js_context.locals[func](*fargs))