示例#1
0
    def test_return_with_dereference4(self):
        _, result = bootstrap_function(function_lit(
            int_type(),
            build_break_types(
                object_type({
                    "foo": any_type(),
                    "bar": any_type()
                })),
            comma_op(
                return_op(
                    object_template_op({
                        "foo":
                        literal_op(42),
                        "bar":
                        dereference_op(context_op(), literal_op("argument"),
                                       True)
                    })))),
                                       argument=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)
        self.assertEquals(result.value._get("bar"), 42)
示例#2
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)
示例#3
0
    def test_to_match_with_one_of_type_combo(self):
        func = function_lit(
            one_of_type([string_type(), int_type(),
                         bool_type()]),
            return_op(
                match_op(dereference("argument"), [
                    prepared_function(int_type(),
                                      literal_op("int is not a string")),
                    prepared_function(bool_type(),
                                      literal_op("bool is not a string")),
                    prepared_function(inferred_type(), dereference("argument"))
                ])))

        _, result = bootstrap_function(func, argument=2)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "int is not a string")
        _, result = bootstrap_function(func, argument=True)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "bool is not a string")
        _, result = bootstrap_function(func, argument="hello world")
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "hello world")

        prepared_func = prepare(func, PythonObject({}), FrameManager())
        self.assertEquals(len(prepared_func.break_types), 1)
        self.assertTrue("return" in prepared_func.break_types)
        for return_break_type in prepared_func.break_types["return"]:
            self.assertTrue(StringType().is_copyable_from(
                return_break_type["out"], DUMMY_REASONER))
示例#4
0
    def test_interesting(self):
        func = function_lit(
            any_type(), infer_all(),
            match_op(dereference("argument"), [
                prepared_function(
                    object_type({"foo": int_type()}),
                    return_op(
                        addition_op(dereference("argument.foo"),
                                    literal_op(3)))),
                prepared_function(any_type(), return_op(literal_op("invalid")))
            ]))

        _, result = bootstrap_function(func,
                                       argument=PythonObject(
                                           {"foo": 39},
                                           bind=UniversalObjectType(
                                               {"foo": IntegerType()})))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)

        _, result = bootstrap_function(func,
                                       argument=PythonObject({"foo": "hello"}))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "invalid")
示例#5
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)
示例#6
0
    def test_to_string_from_int(self):
        func = function_lit(
            any_type(),
            return_op(
                match_op(dereference("argument"), [
                    prepared_function(unit_type(1), literal_op("one")),
                    prepared_function(unit_type(2), literal_op("two")),
                    prepared_function(unit_type(3), literal_op("three")),
                    prepared_function(unit_type(4), literal_op("four")),
                    prepared_function(any_type(), literal_op("invalid"))
                ])))

        _, result = bootstrap_function(func, argument=1)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "one")
        _, result = bootstrap_function(func, argument=2)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "two")
        _, result = bootstrap_function(func, argument=3)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "three")
        _, result = bootstrap_function(func, argument=4)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "four")
        _, result = bootstrap_function(func, argument=5)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "invalid")
示例#7
0
    def test_static_value_dereference(self):
        _, result = bootstrap_function(
            function_lit(
                return_op(static_op(addition_op(literal_op(5),
                                                literal_op(37))))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#8
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)
示例#9
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")
示例#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_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)
示例#13
0
    def test_doubler(self):
        func = function_lit(
            int_type(), build_break_types(int_type()),
            return_op(
                addition_op(
                    dereference_op(context_op(), literal_op("argument"), True),
                    dereference_op(context_op(), literal_op("argument"),
                                   True))))

        _, result = bootstrap_function(func, argument=21)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
示例#14
0
 def visitObjectPropertyPair(self, ctx):
     if ctx.SYMBOL():
         return [
             literal_op(ctx.SYMBOL().getText()),
             self.visit(ctx.expression()[0])
         ]
     elif ctx.NUMBER():
         return [
             literal_op(json.loads(ctx.NUMBER().getText())),
             self.visit(ctx.expression()[0])
         ]
     else:
         key, value = ctx.expression()
         return [self.visit(key), self.visit(value)]
示例#15
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)
示例#16
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)
示例#17
0
    def test_assignment_from_argument(self):
        _, result = bootstrap_function(function_lit(
            int_type(), build_break_types(int_type()), int_type(),
            literal_op(0),
            comma_op(
                assignment_op(
                    context_op(), literal_op("local"),
                    dereference_op(context_op(), literal_op("argument"),
                                   True)),
                return_op(
                    dereference_op(context_op(), literal_op("local"), True)))),
                                       argument=43,
                                       check_safe_exit=True)

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 43)
示例#18
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)
示例#19
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)
示例#20
0
    def visitStaticValueDeclaration(self, ctx):
        remaining_code = self.visit(ctx.codeBlock())

        name, value = self.visit(ctx.symbolInitialization())

        return CodeBlockBuilder(extra_statics={
            literal_op(name): value
        }).chain(remaining_code, get_debug_info(ctx))
示例#21
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)
示例#22
0
    def visitDictionaryType(self, ctx):
        key_type, value_type = ctx.expression()
        key_type = self.visit(key_type)
        value_type = self.visit(value_type)

        return composite_type([
            object_template_op({
                "type":
                literal_op("get-wildcard"),
                "params":
                list_template_op([key_type, value_type,
                                  literal_op(True)])
            }),
            object_template_op({
                "type":
                literal_op("set-wildcard"),
                "params":
                list_template_op([
                    key_type,
                    value_type,
                    literal_op(False),
                    literal_op(False),
                ])
            }),
            object_template_op({
                "type":
                literal_op("delete-wildcard"),
                "params":
                list_template_op([key_type, literal_op(True)])
            }),
        ])
示例#23
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)
示例#24
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)
示例#25
0
    def visitTypedef(self, ctx):
        remaining_code = self.visit(ctx.codeBlock())

        value = self.visit(ctx.expression())
        name = ctx.SYMBOL().getText()

        return CodeBlockBuilder(extra_statics={
            literal_op(name): value
        }, ).chain(remaining_code, get_debug_info(ctx))
示例#26
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)
示例#27
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)
示例#28
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)
示例#29
0
    def __call__(self, expression):
        from lockdown.executor.raw_code_factories import dereference_op, assignment_op, \
            literal_op, dereference, context_op

        debug_info = get_debug_info_from_opcode(expression)

        if expression._get("opcode", None) == "unbound_dereference":
            reference = expression.reference
            bound_countext_op, is_static = self.search_for_reference(
                reference, debug_info)

            if bound_countext_op:
                new_dereference = dereference_op(bound_countext_op,
                                                 literal_op(reference), True,
                                                 **debug_info)
                if is_static:
                    new_dereference = static_op(new_dereference)
                get_manager(new_dereference).add_composite_type(
                    DEFAULT_READONLY_COMPOSITE_TYPE)
                return new_dereference
            else:
                new_dereference = dynamic_dereference_op(
                    reference, **debug_info)
                get_manager(new_dereference).add_composite_type(
                    DEFAULT_READONLY_COMPOSITE_TYPE)
                return new_dereference

        if expression._get("opcode", None) == "unbound_assignment":
            reference = expression.reference
            bound_countext_op, _ = self.search_for_reference(
                reference, debug_info)

            if bound_countext_op:
                new_assignment = assignment_op(bound_countext_op,
                                               literal_op(reference),
                                               expression.rvalue, **debug_info)
                get_manager(new_assignment).add_composite_type(
                    DEFAULT_READONLY_COMPOSITE_TYPE)
                return new_assignment
            else:
                raise FatalError()  # TODO, dynamic assignment

        return expression
示例#30
0
 def test_return_with_dereference5(self):
     with self.assertRaises(Exception):
         bootstrap_function(function_lit(
             int_type(),
             build_break_types(
                 object_type({
                     "foo": any_type(),
                     "bar": unit_type(42)
                 })),
             return_op(
                 object_template_op({
                     "foo":
                     literal_op(42),
                     "bar":
                     dereference_op(context_op(), literal_op("argument"),
                                    True)
                 }))),
                            argument=42,
                            check_safe_exit=True)