def main(shell: bool, packages_path: str, scripts: Tuple[str]): """ Execute ActionScript files. """ environment = {} for script in scripts: path = Path(script) try: execute( path.open('rt', encoding='utf-8').read(), str(path), environment) except Exception as e: print_exception(e) sys.exit(1) if shell: run_shell(environment)
def run_shell(environment: dict): session = PromptSession() style = style_from_pygments_cls(NativeStyle) click.echo(f'{click.style("Welcome to as3 shell!", fg="yellow")}') while True: line = session.prompt( '>>> ', lexer=PygmentsLexer(ActionScript3Lexer), style=style, auto_suggest=AutoSuggestFromHistory(), ) try: value = execute(line, '<shell>', environment) except Exception as e: print_exception(e) else: print( highlight(repr(value), ActionScript3Lexer(), TerminalFormatter()))
def test_simple_class(): object_ = execute('class X { }; new X()') assert isinstance(object_, dict) assert object_['__proto__'] == {} assert object_['__proto__'] is object_['constructor']['prototype']
def test_boolean_literal(source: str, expected: Any): assert execute(source) == expected
def test_get_property(source: str, expected: Any): assert execute(source, environment={'foo': {'baz': 2}}) == expected
def test_new_string(): assert execute('new String(2)') == '2'
def test_execute_deprecated(source: str, expected: Any) -> None: class FakeException(Exception): pass assert execute(source, '<ast>', {'FakeException': FakeException}) == expected
def test_initialized_class_field(): assert execute('class X { var a = 42 }; new X().a') == 42
def test_augmented_assignment(source: str, expected: Any): assert execute(source) == expected
def test_variable_definition(source: str, expected: Any): assert execute(source) == expected
def test_label(): # For the sake of simplicity a label is evaluated to `None`. assert execute('addr58:') is None
def test_null(): assert execute('null') is None
def test_conditional(source: str, expected: Any): assert execute(source) == expected
def test_float_literal(source: str, expected: Any): assert execute(source) == expected
def test_undefined(): assert execute('undefined') == undefined
def test_class_constructor(): assert execute('var expected; class X { function X() { expected = 42 } }; new X(); expected') == 42
def test_get_missing_class_field(): assert execute('class X { }; new X().a') is undefined
def test_addition(): assert execute('2 + 2') == 4
def test_priority(source: str, expected: Any): assert execute(source) == expected
def test_for(source: str, expected: Any): environment = {'foo': 0} assert execute(source, environment=environment) == expected
def test_new_string_generic(): assert execute('new String.<Whatever>(2)') == '2'
def test_function_parameter(source: str, expected: Any): assert execute(source) == expected
def test_math(source: str, expected: Any): assert execute(source) == expected
def test_function_return(source: str, expected: Any): assert execute(source) == expected
def test_call_function(source: str, expected: Any): assert execute(source, environment={ 'bar': make_function_object(lambda a, b: a + b), 'baz': make_function_object(lambda: 42), }) == expected
def test_assign_property(source: str, expected: Any): assert execute(source, environment={'dict_': {}}) == expected
def test_unary(source: str, expected: Any): assert execute(source) == expected
def test_reference_error(source: str): with raises(ASReferenceError): execute(source)