<commit_before>import json
from . import _dukpy

try:
    from collections.abc import Iterable
except ImportError:
    from collections import Iterable

try:  # pragma: no cover
    unicode
    string_types = (str, unicode)
except NameError:  # pragma: no cover
    string_types = (bytes, str)


class JSInterpreter(object):
    """JavaScript Interpreter"""
    def __init__(self):
        self._ctx = _dukpy.create_context()

    def evaljs(self, code, **kwargs):
        """Runs JavaScript code in the context of the interpreter.

        All arguments will be converted to plain javascript objects
        through the JSON encoder and will be available in `dukpy`
        global object.

        Returns the last object on javascript stack.
        """
        jsvars = json.dumps(kwargs)
        jscode = code

        if not isinstance(code, string_types):
            jscode = ';\n'.join(code)

        if not isinstance(jscode, bytes):
            jscode = jscode.encode('utf-8')

        res = _dukpy.eval_string(self._ctx, jscode, jsvars)
        if res is None:
            return None

        return json.loads(res.decode('utf-8'))<commit_msg>Fix unicode source code on py3<commit_after>import json
from . import _dukpy

try:
    from collections.abc import Iterable
except ImportError:
    from collections import Iterable

try:  # pragma: no cover
    unicode
    string_types = (str, unicode)
    jscode_type = str
except NameError:  # pragma: no cover
    string_types = (bytes, str)
    jscode_type = str


class JSInterpreter(object):
    """JavaScript Interpreter"""
    def __init__(self):
        self._ctx = _dukpy.create_context()

    def evaljs(self, code, **kwargs):
        """Runs JavaScript code in the context of the interpreter.

        All arguments will be converted to plain javascript objects
        through the JSON encoder and will be available in `dukpy`
        global object.

        Returns the last object on javascript stack.
        """
        jsvars = json.dumps(kwargs)
        jscode = code

        if not isinstance(code, string_types):
            jscode = ';\n'.join(code)

        if not isinstance(jscode, str):
            # Source code must be str on both Py2 and Py3
            # so it must be encoded on Py2 and decoded on Py3
            if isinstance(jscode, bytes):
                jscode = jscode.decode('utf-8')
            else:
                jscode = jscode.encode('utf-8')

        res = _dukpy.eval_string(self._ctx, jscode, jsvars)
        if res is None:
            return None

        return json.loads(res.decode('utf-8'))
{'fraction_non_alphanumeric': 0.08381391772395233, 'fraction_numerical': 0.0038446751249519417, 'mean_word_length': 2.335897435897436, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5766274', 'n_tokens_mistral': 775, 'n_tokens_neox': 744, 'n_words': 253}