js/src/jsapi-tests/binast/invalid/lib/filter_runner.py
author Tooru Fujisawa <arai_a@mac.com>
Tue, 05 Feb 2019 00:34:08 +0900
changeset 516095 1143fee910c70ae6ea25e325b7908f38c8cfeba6
permissions -rw-r--r--
Bug 1524965 - Part 3: Add a script to generate testcases for BinAST with invalid content. r=Yoric Integrated binjs_convert_from_json into encode.py. encode.py now generates invalid BinAST file if --binjs_convert_from_json option is provided. Source of test files are located inside js/src/jsapi-tests/binast/invalid/tests, with .js file as a source of JSON, and .py file for filtering the JSON. The generated tests are located inside js/src/jit-test/tests/binast/invalid. filter_utils.py provides some utility functions that is used by filter script, to handle tagged tuple/list structure in the JSON encoded AST. Actual testcases are added in Part 4. Differential Revision: https://phabricator.services.mozilla.com/D18560

# A utility to run filter program for given JSON format BinAST.

import json
import runpy


def run(filter_path, source):
    """
    Run filter script against JSON source.

    :param filter_path(string)
           The path to the filter script (python script)
    :param source(string)
           The string representation of JSON format of BinAST file
    :returns (string)
             The string representation of filtered JSON
    """
    ast = json.loads(source)

    # The filter script is executed with sys.path that has this directory in
    # the first element, so that it can import filter_utils.
    filter_global = runpy.run_path(filter_path)

    filtered_ast = filter_global['filter_ast'](ast)

    return json.dumps(filtered_ast)