示例#1
0
    def test_single_restart(self):
        context = PythonObject({})
        frame_manager = FrameManager()

        func = prepare(
            function_lit(
                no_value_type(),
                build_break_types(any_type(),
                                  yield_types={
                                      "out": any_type(),
                                      "in": int_type()
                                  }),
                return_op(
                    addition_op(shift_op(literal_op("hello"), int_type()),
                                literal_op(40)))), context,
            frame_manager).close(None)

        def first():
            func.invoke(NO_VALUE, frame_manager)

        with frame_manager.capture("yield") as yielder:
            first()

        self.assertEquals(yielder.value, "hello")

        def second():
            yielder.create_continuation(first, {}).invoke(2, frame_manager)

        with frame_manager.capture("return") as returner:
            second()

        self.assertEquals(returner.value, 42)
示例#2
0
    def test_count_then_return(self):
        _, result = bootstrap_function(
            function_lit(
                no_value_type(), build_break_types(int_type()), int_type(),
                literal_op(0),
                loop_op(
                    comma_op(
                        assignment_op(
                            context_op(), literal_op("local"),
                            addition_op(
                                dereference_op(context_op(),
                                               literal_op("local"), True),
                                literal_op(1))),
                        condition_op(
                            equality_op(
                                dereference_op(context_op(),
                                               literal_op("local"), True),
                                literal_op(42)),
                            return_op(
                                dereference_op(context_op(),
                                               literal_op("local"))),
                            nop())))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#3
0
    def test_restart_into_local_initialization(self):
        context = PythonObject({})
        frame_manager = FrameManager()

        func = prepare(
            function_lit(
                no_value_type(),
                build_break_types(any_type(),
                                  yield_types={
                                      "out": any_type(),
                                      "in": int_type()
                                  }), int_type(),
                shift_op(literal_op("hello"), int_type()),
                return_op(
                    dereference_op(context_op(), literal_op("local"), True))),
            context, frame_manager).close(None)

        def start():
            func.invoke(NO_VALUE, frame_manager)

        with frame_manager.capture("yield") as yielder:
            start()

        self.assertEquals(yielder.value, "hello")

        def restart():
            yielder_restart_continuation = yielder.create_continuation(
                start,
                func.get_type().break_types)
            yielder_restart_continuation.invoke(32, frame_manager)

        with frame_manager.capture("return") as returner:
            restart()

        self.assertEquals(returner.value, 32)
示例#4
0
    def test_basic_inferrence(self):
        _, result = bootstrap_function(
            function_lit(no_value_type(), build_break_types(inferred_type()),
                         return_op(literal_op(42))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#5
0
    def test_infer_all(self):
        _, result = bootstrap_function(
            function_lit(no_value_type(), infer_all(),
                         return_op(literal_op(42))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#6
0
    def test_immediate_return(self):
        _, result = bootstrap_function(
            function_lit(no_value_type(), build_break_types(int_type()),
                         loop_op(return_op(literal_op(42)))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#7
0
    def test_comma(self):
        _, result = bootstrap_function(function_lit(
            no_value_type(), build_break_types(int_type()),
            return_op(comma_op(literal_op(5), literal_op(8), literal_op(42)))),
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#8
0
    def test_infer_exception(self):
        _, result = bootstrap_function(function_lit(
            no_value_type(), infer_all(),
            addition_op(literal_op("hello"), literal_op(5))),
                                       check_safe_exit=False)

        self.assertEquals(result.caught_break_mode, "exception")
        self.assertEquals(result.value.type, "TypeError")
示例#9
0
    def test_basic_function(self):
        func = function_lit(no_value_type(),
                            build_break_types(value_type=int_type()),
                            literal_op(42))

        _, result = bootstrap_function(func)

        self.assertEquals(result.caught_break_mode, "value")
        self.assertEquals(result.value, 42)
示例#10
0
    def test_addition(self):
        func = function_lit(
            no_value_type(), build_break_types(int_type()),
            return_op(addition_op(literal_op(40), literal_op(2))))

        _, result = bootstrap_function(func)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#11
0
 def test_basic_false(self):
     _, result = bootstrap_function(
         function_lit(
             no_value_type(), build_break_types(int_type()),
             return_op(
                 condition_op(literal_op(False), literal_op(34),
                              literal_op(53)))))
     self.assertEquals(result.caught_break_mode, "return")
     self.assertEquals(result.value, 53)
示例#12
0
    def test_basic_with_inferred_local_type(self):
        _, result = bootstrap_function(
            function_lit(
                no_value_type(), infer_all(), inferred_type(),
                close_op(
                    static_op(
                        prepare_op(
                            literal_op(
                                function_lit(no_value_type(), infer_all(),
                                             return_op(literal_op(42)))))),
                    context_op()),
                return_op(
                    invoke_op(
                        dereference_op(context_op(), literal_op("local"),
                                       True)))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#13
0
    def test_basic(self):
        _, result = bootstrap_function(
            function_lit(
                no_value_type(), build_break_types(int_type()),
                return_op(
                    invoke_op(
                        close_op(
                            static_op(
                                prepare_op(
                                    literal_op(
                                        function_lit(
                                            no_value_type(),
                                            build_break_types(int_type()),
                                            return_op(literal_op(42)))))),
                            context_op())))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#14
0
    def test_initialization(self):
        _, result = bootstrap_function(function_lit(
            no_value_type(), build_break_types(int_type()), int_type(),
            literal_op(42),
            comma_op(
                return_op(
                    dereference_op(context_op(), literal_op("local"), True)))),
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#15
0
    def test_return(self):
        _, result = bootstrap_function(function_lit(
            no_value_type(), build_break_types(object_type({"foo":
                                                            int_type()})),
            comma_op(return_op(object_template_op({"foo": literal_op(42)})))),
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertTrue(isinstance(result.value, Universal))
        get_manager(
            result.value).add_composite_type(DEFAULT_READONLY_COMPOSITE_TYPE)
        self.assertEquals(result.value._get("foo"), 42)
示例#16
0
    def test_restart_comma(self):
        context = PythonObject({})

        frame_manager = FrameManager()

        func = prepare(
            function_lit(
                no_value_type(),
                build_break_types(int_type(),
                                  yield_types={
                                      "out": any_type(),
                                      "in": int_type()
                                  }),
                return_op(
                    comma_op(literal_op(5),
                             shift_op(literal_op("first"), int_type()),
                             shift_op(literal_op("second"), int_type())))),
            context, frame_manager).close(None)

        def first():
            func.invoke(NO_VALUE, frame_manager)

        with frame_manager.capture("yield") as first_yielder:
            first()
        self.assertEquals(first_yielder.value, "first")

        def second():
            first_yield_restart_continuation = first_yielder.create_continuation(
                first,
                func.get_type().break_types)
            first_yield_restart_continuation.invoke(4, frame_manager)

        with frame_manager.capture("yield") as second_yielder:
            second()

        self.assertEquals(second_yielder.value, "second")

        def third():
            second_yield_restart_continuation = second_yielder.create_continuation(
                second,
                func.get_type().break_types)
            second_yield_restart_continuation.invoke(42, frame_manager)

        with frame_manager.capture("return") as returner:
            third()

        self.assertEquals(returner.value, 42)
示例#17
0
    def test_misc1(self):
        _, result = bootstrap_function(
            function_lit(
                no_value_type(), infer_all(),
                object_type({
                    "foo": int_type(),
                    "bar": int_type()
                }),
                object_template_op({
                    "foo": literal_op(39),
                    "bar": literal_op(3)
                }),
                return_op(
                    addition_op(dereference("local.foo"),
                                dereference("local.bar")))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#18
0
    def test_restart_into_local_initialization_and_code(self):
        context = PythonObject({})
        frame_manager = FrameManager()

        func = prepare(
            function_lit(
                no_value_type(),
                build_break_types(any_type(),
                                  yield_types={
                                      "out": any_type(),
                                      "in": int_type()
                                  }), int_type(),
                shift_op(literal_op("first"), int_type()),
                return_op(
                    addition_op(
                        dereference_op(context_op(), literal_op("local"),
                                       True),
                        shift_op(literal_op("second"), int_type())))), context,
            frame_manager).close(None)

        def first():
            func.invoke(NO_VALUE, frame_manager)

        with frame_manager.capture("yield") as first_yielder:
            first()
        self.assertEquals(first_yielder.value, "first")

        def second():
            first_restart_continuation = first_yielder.create_continuation(
                first,
                func.get_type().break_types)
            first_restart_continuation.invoke(40, frame_manager)

        with frame_manager.capture("yield") as second_yielder:
            second()
        self.assertEquals(second_yielder.value, "second")

        with frame_manager.capture("return") as returner:
            second_restart_continuation = second_yielder.create_continuation(
                first,
                func.get_type().break_types)
            second_restart_continuation.invoke(2, frame_manager)

        self.assertEquals(returner.value, 42)
示例#19
0
    def test_add_locals(self):
        func = function_lit(
            no_value_type(), build_break_types(int_type()),
            return_op(
                addition_op(
                    dereference_op(
                        dereference_op(
                            dereference_op(context_op(), literal_op("outer"),
                                           True), literal_op("local"), True),
                        literal_op(0), True),
                    dereference_op(
                        dereference_op(
                            dereference_op(context_op(), literal_op("outer"),
                                           True), literal_op("local"), True),
                        literal_op(1), True))))

        context = PythonObject(
            {
                "local":
                PythonList([39, 3]),
                "_types":
                PythonObject({
                    "local":
                    UniversalTupleType([IntegerType(),
                                        IntegerType()])
                })
            },
            bind=UniversalObjectType({
                "local":
                UniversalTupleType([IntegerType(),
                                    IntegerType()]),
                "_types":
                DEFAULT_READONLY_COMPOSITE_TYPE
            }))

        _, result = bootstrap_function(func, outer_context=context)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#20
0
    def test_repeated_restart_while_using_restart_values(self):
        context = PythonObject({})
        frame_manager = FrameManager()

        func = prepare(
            function_lit(
                no_value_type(),
                build_break_types(any_type(),
                                  yield_types={
                                      "out": any_type(),
                                      "in": int_type()
                                  }),
                return_op(
                    addition_op(shift_op(literal_op(30), int_type()),
                                shift_op(literal_op(10), int_type())))),
            context, frame_manager).close(None)

        def first():
            func.invoke(NO_VALUE, frame_manager)

        with frame_manager.capture("yield") as first_yielder:
            first()

        def second():
            first_yielder.create_continuation(first, {}).invoke(
                first_yielder.value + 1, frame_manager)

        with frame_manager.capture("yield") as second_yielder:
            second()

        def third():
            second_yielder.create_continuation(second, {}).invoke(
                second_yielder.value + 1, frame_manager)

        with frame_manager.capture("return") as returner:
            third()

        self.assertEquals(returner.value, 42)
示例#21
0
    def test_repeated_restart_with_outer_return_handling(self):
        context = PythonObject({})
        frame_manager = FrameManager()

        func = prepare(
            function_lit(
                no_value_type(),
                build_break_types(int_type(),
                                  yield_types={
                                      "out": any_type(),
                                      "in": int_type()
                                  }),
                return_op(
                    addition_op(shift_op(literal_op("first"), int_type()),
                                shift_op(literal_op("second"), int_type())))),
            context, frame_manager).close(None)

        with frame_manager.capture("return") as return_capturer:

            def first():
                func.invoke(NO_VALUE, frame_manager)

            with frame_manager.capture("yield") as first_yielder:
                first()
            self.assertEquals(first_yielder.value, "first")

            def second():
                first_yielder.create_continuation(first, {}).invoke(
                    39, frame_manager)

            with frame_manager.capture("yield") as second_yielder:
                second()
            self.assertEquals(second_yielder.value, "second")

            second_yielder.create_continuation(second,
                                               {}).invoke(3, frame_manager)

        self.assertEquals(return_capturer.value, 42)
示例#22
0
    def test_return_variable(self):
        func = function_lit(
            no_value_type(), build_break_types(int_type()),
            return_op(
                dereference_op(
                    dereference_op(context_op(), literal_op("outer"), True),
                    literal_op("local"), True)))

        context = PythonObject(
            {
                "local": 42,
                "_types": PythonObject({"local": IntegerType()})
            },
            bind=UniversalObjectType(
                {
                    "local": IntegerType(),
                    "_types": DEFAULT_READONLY_COMPOSITE_TYPE
                },
                wildcard_type=RICH_READONLY_TYPE))

        _, result = bootstrap_function(func, outer_context=context)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#23
0
 def test_without_infer_exception_fails(self):
     with self.assertRaises(Exception):
         bootstrap_function(
             function_lit(no_value_type(), build_break_types(int_type()),
                          addition_op(literal_op("hello"), literal_op(5))))
示例#24
0
def get_default_global_context():
    return PythonObject(
        {
            "static":
            PythonObject(
                {
                    "any":
                    PythonObject({"type": "Any"},
                                 debug_reason="default-global-context"),
                    "int":
                    PythonObject({"type": "Integer"},
                                 debug_reason="default-global-context"),
                    "bool":
                    PythonObject({"type": "Boolean"},
                                 debug_reason="default-global-context"),
                    "string":
                    PythonObject({"type": "String"},
                                 debug_reason="default-global-context"),
                    "void":
                    PythonObject({"type": "NoValue"},
                                 debug_reason="default-global-context"),
                    "var":
                    PythonObject({"type": "Inferred"},
                                 debug_reason="default-global-context"),
                    "range":
                    prepare(
                        function_lit(
                            list_type([int_type(), int_type()], None),
                            infer_all(), int_type(), dereference("argument.0"),
                            prepared_function(
                                loop_op(
                                    condition_op(
                                        binary_integer_op(
                                            "lt", dereference("outer.local"),
                                            dereference("outer.argument.1")),
                                        comma_op(
                                            shift_op(
                                                dereference("outer.local"),
                                                no_value_type()),
                                            assignment_op(
                                                dereference("outer"),
                                                literal_op("local"),
                                                addition_op(
                                                    dereference("outer.local"),
                                                    literal_op(1)))),
                                        transform_op("break"))))), NO_VALUE,
                        FrameManager()).close(NO_VALUE),
                    "list":
                    prepare(
                        function_lit(
                            list_type([
                                function_type(
                                    no_value_type(), {
                                        "yield":
                                        list_template_op([
                                            object_template_op(
                                                {
                                                    "in": no_value_type(),
                                                    "out": int_type()
                                                })
                                        ]),
                                        "value":
                                        list_template_op([
                                            object_template_op(
                                                {"out": no_value_type()})
                                        ]),
                                    }),
                            ], None), infer_all(), inferred_type(),
                            dereference("argument.0"),
                            loop_op(
                                invoke_op(
                                    local_function(
                                        transform(
                                            ("yield", "value"),
                                            ("value", "end"),
                                            reset_op(
                                                dereference("outer.local"),
                                                nop())),
                                        comma_op(
                                            assignment_op(
                                                dereference("outer"),
                                                literal_op("local"),
                                                dereference(
                                                    "local.continuation")),
                                            transform_op(
                                                "value", "continue",
                                                dereference("local.value"))))))
                        ), NO_VALUE, FrameManager()).close(NO_VALUE),
                    "max":
                    prepare(
                        function_lit(
                            list_type([int_type()], int_type()), infer_all(),
                            inferred_type(), dereference("argument.0"),
                            comma_op(
                                map_op(
                                    dereference("argument"),
                                    prepared_function(
                                        int_type(),
                                        condition_op(
                                            binary_integer_op(
                                                "gt", dereference("argument"),
                                                dereference("outer.local")),
                                            assignment_op(
                                                dereference("outer"),
                                                literal_op("local"),
                                                dereference("argument")),
                                            nop()))), dereference("local"))),
                        NO_VALUE, FrameManager()).close(NO_VALUE),
                },
                debug_reason="default-global-context")
        },
        bind=DEFAULT_READONLY_COMPOSITE_TYPE,
        debug_reason="default-global-context")
示例#25
0
    def create(self, output_mode, debug_info):
        if output_mode not in ("first-class-function", "second-class-function",
                               "expression"):
            raise FatalError()

        code_expressions = default(self.code_expressions, MISSING, [])

        for c in code_expressions:
            if not is_opcode(c):
                raise FatalError()

        if not self.requires_function() and output_mode == "expression":
            return combine_opcodes(code_expressions)

        if self.argument_type_expression is not MISSING:
            argument_type = self.argument_type_expression
        else:
            argument_type = no_value_type()

        if self.local_variable_type is not MISSING:
            local_type = self.local_variable_type
        else:
            local_type = object_type(
                {})  # For future python local variables...

        if self.local_initializer is not MISSING:
            local_initializer = self.local_initializer
        else:
            local_initializer = object_template_op({})

        if self.breaks_types is not MISSING:
            break_types = self.breaks_types
        else:
            break_types = infer_all()

        if self.extra_statics is not MISSING:
            extra_statics = self.extra_statics
        else:
            extra_statics = {}

        if output_mode == "first-class-function":
            # A function created by the user, which mangles returns as expected
            code = transform_op("return", "value",
                                combine_opcodes(code_expressions),
                                **debug_info)
            return function_lit(extra_statics, argument_type, break_types,
                                local_type, local_initializer, code,
                                **debug_info)
        if output_mode == "second-class-function":
            # A function created by the environment, which leaves returns unmangled
            code = combine_opcodes(code_expressions)
            return function_lit(extra_statics, argument_type, break_types,
                                local_type, local_initializer, code,
                                **debug_info)
        elif output_mode == "expression":
            return invoke_op(
                prepare_function_lit(
                    function_lit(extra_statics, argument_type, break_types,
                                 local_type, local_initializer,
                                 combine_opcodes(code_expressions),
                                 **debug_info), **debug_info), **debug_info)