# Path: src/funfuzz/js/inspect_shell.py
# def testJsShellOrXpcshell(s):  # pylint: disable=invalid-name,missing-param-doc,missing-return-doc
#     # pylint: disable=missing-return-type-doc,missing-type-doc
#     """Test if a binary is a js shell or xpcshell."""
#     return "xpcshell" if shellSupports(s, ["-e", "Components"]) else "jsShell"
# 
# Path: src/funfuzz/js/js_interesting.py
# JS_LEVELS = 6
# JS_LEVEL_NAMES = [
#     "fine",
#     "jsfunfuzz did not finish",
#     "jsfunfuzz decided to exit",
#     "overall mismatch",
#     "valgrind error",
#     "new assert or crash",
# ]
# VALGRIND_ERROR_EXIT_CODE = 77
# class ShellResult:  # pylint: disable=missing-docstring,too-many-instance-attributes,too-few-public-methods
#     def __init__(self, options, runthis, logPrefix, in_compare_jit, env=None):  # pylint: disable=too-complex
# def understoodJsfunfuzzExit(out, err):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc
# def hitMemoryLimit(err):  # pylint: disable=invalid-name,missing-param-doc,missing-return-doc,missing-return-type-doc
# def oomed(err):  # pylint: disable=missing-docstring,missing-return-doc,missing-return-type-doc
# def summaryString(issues, level, elapsedtime):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc
# def truncateFile(fn, maxSize):  # pylint: disable=invalid-name,missing-docstring
# def set_ulimit():
# def parseOptions(args):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc,missing-return-type-doc
# def init(args):  # pylint: disable=missing-docstring
# def interesting(_args, cwd_prefix):  # pylint: disable=missing-docstring,missing-return-doc
# def main():  # pylint: disable=missing-docstring
# 
# Path: src/funfuzz/js/js_interesting.py
# JS_LEVELS = 6
# JS_LEVEL_NAMES = [
#     "fine",
#     "jsfunfuzz did not finish",
#     "jsfunfuzz decided to exit",
#     "overall mismatch",
#     "valgrind error",
#     "new assert or crash",
# ]
# VALGRIND_ERROR_EXIT_CODE = 77
# class ShellResult:  # pylint: disable=missing-docstring,too-many-instance-attributes,too-few-public-methods
#     def __init__(self, options, runthis, logPrefix, in_compare_jit, env=None):  # pylint: disable=too-complex
# def understoodJsfunfuzzExit(out, err):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc
# def hitMemoryLimit(err):  # pylint: disable=invalid-name,missing-param-doc,missing-return-doc,missing-return-type-doc
# def oomed(err):  # pylint: disable=missing-docstring,missing-return-doc,missing-return-type-doc
# def summaryString(issues, level, elapsedtime):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc
# def truncateFile(fn, maxSize):  # pylint: disable=invalid-name,missing-docstring
# def set_ulimit():
# def parseOptions(args):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc,missing-return-type-doc
# def init(args):  # pylint: disable=missing-docstring
# def interesting(_args, cwd_prefix):  # pylint: disable=missing-docstring,missing-return-doc
# def main():  # pylint: disable=missing-docstring


import gzip
import io
import re
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
from shlex import quote
from lithium.interestingness.utils import file_contains_str
from . import file_manipulation
from ..js.inspect_shell import testJsShellOrXpcshell
from ..js.js_interesting import JS_OVERALL_MISMATCH
from ..js.js_interesting import JS_VG_AMISS
    # 1-line offset.
    if lith_result == LITH_FINISHED and origNumOfLines <= 50 and hasTryItOut and lev >= JS_VG_AMISS:
        intendedLines = []  # pylint: disable=invalid-name
        with io.open(str(infilename), "r", encoding="utf-8", errors="replace") as f:
            for line in f:  # The testcase is likely to already be partially reduced.
                if "dumpln(cookie" not in line:  # jsfunfuzz-specific line ignore
                    # This should be simpler than re.compile.
                    intendedLines.append(line.replace("; count=", ";\ncount=")
                                         .replace('; tryItOut("', ';\ntryItOut("')
                                         # The 1-line offset is added here.
                                         .replace("SPLICE DDBEGIN", "SPLICE DDBEGIN\n"))

        with io.open(str(infilename), "w", encoding="utf-8", errors="replace") as f:
            f.writelines(intendedLines)
        print()
        print("Running 1 instance of 2-line reduction after moving count=X to its own line...")
        print()
        lith_result, lith_details = lith_reduce(["--chunksize=2"])

    # Step 4: Run 1 instance of 2-line reduction again, e.g. to remove pairs of STRICT_MODE lines.
    if lith_result == LITH_FINISHED and origNumOfLines <= 50 and hasTryItOut and lev >= JS_VG_AMISS:
        print()
        print("Running 1 instance of 2-line reduction again...")
        print()
        lith_result, lith_details = lith_reduce(["--chunksize=2"])

    isLevOverallMismatchAsmJsAvailable = (lev == JS_OVERALL_MISMATCH and  # pylint: disable=invalid-name
                                          file_contains_str(str(infilename), b"isAsmJSCompilationAvailable"))
    # Step 5 (not always run): Run character reduction within interesting lines.
    if lith_result == LITH_FINISHED and origNumOfLines <= 50 and targetTime is None and \

# Path: src/funfuzz/js/inspect_shell.py
# def testJsShellOrXpcshell(s):  # pylint: disable=invalid-name,missing-param-doc,missing-return-doc
#     # pylint: disable=missing-return-type-doc,missing-type-doc
#     """Test if a binary is a js shell or xpcshell."""
#     return "xpcshell" if shellSupports(s, ["-e", "Components"]) else "jsShell"
# 
# Path: src/funfuzz/js/js_interesting.py
# JS_LEVELS = 6
# JS_LEVEL_NAMES = [
#     "fine",
#     "jsfunfuzz did not finish",
#     "jsfunfuzz decided to exit",
#     "overall mismatch",
#     "valgrind error",
#     "new assert or crash",
# ]
# VALGRIND_ERROR_EXIT_CODE = 77
# class ShellResult:  # pylint: disable=missing-docstring,too-many-instance-attributes,too-few-public-methods
#     def __init__(self, options, runthis, logPrefix, in_compare_jit, env=None):  # pylint: disable=too-complex
# def understoodJsfunfuzzExit(out, err):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc
# def hitMemoryLimit(err):  # pylint: disable=invalid-name,missing-param-doc,missing-return-doc,missing-return-type-doc
# def oomed(err):  # pylint: disable=missing-docstring,missing-return-doc,missing-return-type-doc
# def summaryString(issues, level, elapsedtime):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc
# def truncateFile(fn, maxSize):  # pylint: disable=invalid-name,missing-docstring
# def set_ulimit():
# def parseOptions(args):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc,missing-return-type-doc
# def init(args):  # pylint: disable=missing-docstring
# def interesting(_args, cwd_prefix):  # pylint: disable=missing-docstring,missing-return-doc
# def main():  # pylint: disable=missing-docstring
# 
# Path: src/funfuzz/js/js_interesting.py
# JS_LEVELS = 6
# JS_LEVEL_NAMES = [
#     "fine",
#     "jsfunfuzz did not finish",
#     "jsfunfuzz decided to exit",
#     "overall mismatch",
#     "valgrind error",
#     "new assert or crash",
# ]
# VALGRIND_ERROR_EXIT_CODE = 77
# class ShellResult:  # pylint: disable=missing-docstring,too-many-instance-attributes,too-few-public-methods
#     def __init__(self, options, runthis, logPrefix, in_compare_jit, env=None):  # pylint: disable=too-complex
# def understoodJsfunfuzzExit(out, err):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc
# def hitMemoryLimit(err):  # pylint: disable=invalid-name,missing-param-doc,missing-return-doc,missing-return-type-doc
# def oomed(err):  # pylint: disable=missing-docstring,missing-return-doc,missing-return-type-doc
# def summaryString(issues, level, elapsedtime):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc
# def truncateFile(fn, maxSize):  # pylint: disable=invalid-name,missing-docstring
# def set_ulimit():
# def parseOptions(args):  # pylint: disable=invalid-name,missing-docstring,missing-return-doc,missing-return-type-doc
# def init(args):  # pylint: disable=missing-docstring
# def interesting(_args, cwd_prefix):  # pylint: disable=missing-docstring,missing-return-doc
# def main():  # pylint: disable=missing-docstring


# Path: src/funfuzz/util/lithium_helpers.py
import gzip
import io
import re
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
from shlex import quote
from lithium.interestingness.utils import file_contains_str
from . import file_manipulation
from ..js.inspect_shell import testJsShellOrXpcshell
from ..js.js_interesting import JS_OVERALL_MISMATCH
from ..js.js_interesting import JS_VG_AMISS

    # 1-line offset.
    if lith_result == LITH_FINISHED and origNumOfLines <= 50 and hasTryItOut and lev >= JS_VG_AMISS:
        intendedLines = []  # pylint: disable=invalid-name
        with io.open(str(infilename), "r", encoding="utf-8", errors="replace") as f:
            for line in f:  # The testcase is likely to already be partially reduced.
                if "dumpln(cookie" not in line:  # jsfunfuzz-specific line ignore
                    # This should be simpler than re.compile.
                    intendedLines.append(line.replace("; count=", ";\ncount=")
                                         .replace('; tryItOut("', ';\ntryItOut("')
                                         # The 1-line offset is added here.
                                         .replace("SPLICE DDBEGIN", "SPLICE DDBEGIN\n"))

        with io.open(str(infilename), "w", encoding="utf-8", errors="replace") as f:
            f.writelines(intendedLines)
        print()
        print("Running 1 instance of 2-line reduction after moving count=X to its own line...")
        print()
        lith_result, lith_details = lith_reduce(["--chunksize=2"])

    # Step 4: Run 1 instance of 2-line reduction again, e.g. to remove pairs of STRICT_MODE lines.
    if lith_result == LITH_FINISHED and origNumOfLines <= 50 and hasTryItOut and lev >= JS_VG_AMISS:
        print()
        print("Running 1 instance of 2-line reduction again...")
        print()
        lith_result, lith_details = lith_reduce(["--chunksize=2"])

    isLevOverallMismatchAsmJsAvailable = (lev == JS_OVERALL_MISMATCH and  # pylint: disable=invalid-name
                                          file_contains_str(str(infilename), b"isAsmJSCompilationAvailable"))
    # Step 5 (not always run): Run character reduction within interesting lines.
    if lith_result == LITH_FINISHED and origNumOfLines <= 50 and targetTime is None and \

            lev >= JS_OVERALL_MISMATCH and not isLevOverallMismatchAsmJsAvailable: