Пример #1
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")
Пример #2
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))
Пример #3
0
def type_conditional_converter(expression):
    is_conditional = expression.opcode == "conditional"
    if not is_conditional:
        return expression
    condition_is_type_check = expression.condition.opcode == "is"
    if not condition_is_type_check:
        return expression
    lvalue_of_condition_is_dereference = expression.condition.expression.opcode == "unbound_dereference"
    if not lvalue_of_condition_is_dereference:
        return expression

    shadow_name = expression.condition.expression.reference

    new_match = match_op(expression.condition.expression, [
        prepared_function(
            expression.condition.type,
            invoke_op(prepared_function(
                object_type({shadow_name: expression.condition.type}),
                expression.when_true),
                      argument_expression=object_template_op(
                          {shadow_name: dereference("argument")}))),
        prepared_function(inferred_type(), expression.when_false)
    ])
    get_manager(new_match).add_composite_type(DEFAULT_READONLY_COMPOSITE_TYPE)
    return new_match
Пример #4
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")
Пример #5
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)
Пример #6
0
    def visitForGeneratorLoop(self, ctx):
        iterator_name = ctx.SYMBOL().getText()
        generator_expression = self.visit(ctx.expression())
        loop_code = self.visit(ctx.codeBlock())

        loop_code = CodeBlockBuilder(argument_type_expression=object_type(
            {iterator_name: inferred_type()})).chain(loop_code,
                                                     get_debug_info(ctx))

        loop_code = loop_code.create("second-class-function",
                                     get_debug_info(ctx))
        #        get_manager(loop_code).add_composite_type(READONLY_DEFAULT_OBJECT_TYPE)

        return transform_op(
            "break", "value",
            invoke_op(
                local_function(
                    object_template_op({"callback": generator_expression}),
                    loop_op(
                        invoke_op(
                            local_function(
                                transform(
                                    ("yield", "value"), ("value", "end"),
                                    reset_op(
                                        dereference("outer.local.callback",
                                                    **get_debug_info(ctx)),
                                        nop(), **get_debug_info(ctx)),
                                    **get_debug_info(ctx)),
                                comma_op(
                                    assignment_op(
                                        dereference("outer.local"),
                                        literal_op("callback"),
                                        dereference("local.continuation"),
                                        **get_debug_info(ctx)),
                                    invoke_op(
                                        prepare_function_lit(loop_code),
                                        object_template_op(
                                            {
                                                iterator_name:
                                                dereference("local.value")
                                            }, **get_debug_info(ctx)),
                                        **get_debug_info(ctx))),
                                **get_debug_info(ctx)), **get_debug_info(ctx)),
                        **get_debug_info(ctx)), **get_debug_info(ctx))))
Пример #7
0
 def test_slightly_strange_try_catch(self):
     # Function either throws the same string back at you, or returns an int +1
     func = function_lit(
         one_of_type([string_type(), int_type()]),
         try_catch_op(
             throw_op(dereference("argument")),
             prepared_function(
                 int_type(),
                 return_op(
                     addition_op(literal_op(1), dereference("argument")))),
             nop()))
     _, result = bootstrap_function(func,
                                    argument="hello world",
                                    check_safe_exit=False)
     self.assertEquals(result.caught_break_mode, "exception")
     self.assertEquals(result.value, "hello world")
     _, result = bootstrap_function(func,
                                    argument=41,
                                    check_safe_exit=False)
     self.assertEquals(result.caught_break_mode, "return")
     self.assertEquals(result.value, 42)
Пример #8
0
 def test_catch_real_exception(self):
     #  Function safely handles an internal exception
     func = function_lit(
         try_catch_op(
             dereference_op(context_op(), literal_op("foo"), True),
             prepared_function(
                 object_type({
                     "type": const_string_type(),
                     "message": const_string_type(),
                 }), return_op(dereference("argument.message"))), nop()))
     _, result = bootstrap_function(func)
     self.assertEquals(result.caught_break_mode, "return")
     self.assertEquals(result.value, "DereferenceOp: invalid_dereference")
Пример #9
0
    def visitToListDestructuring(self, ctx):
        lvalues = [
            self.visit(l) for l in ctx.assignmentOrInitializationLvalue()
        ]
        rvalue = self.visit(ctx.expression())

        code_block = ctx.codeBlock()
        if code_block:
            code_block = self.visit(code_block)

        assignments = [(i, n) for i, n in enumerate(lvalues)
                       if isinstance(n, basestring)]
        initializers = [(i, n) for i, n in enumerate(lvalues)
                        if isinstance(n, tuple)]

        local_variable_types = {}
        local_variable_initializers = {}

        for index, (type, name) in initializers:
            local_variable_types[name] = type
            local_variable_initializers[name] = dereference(
                "outer.local._temp", index)

        code_block = CodeBlockBuilder(
            local_variable_type=object_type(local_variable_types),
            local_initializer=object_template_op(
                local_variable_initializers)).chain(code_block,
                                                    get_debug_info(ctx))

        code_block = CodeBlockBuilder(
            local_variable_type=object_type(
                {"_temp": list_type([inferred_type()] * len(lvalues), None)}),
            local_initializer=object_template_op({"_temp": rvalue}),
            code_expressions=[
                unbound_assignment(name, dereference("local._temp", index))
                for index, name in assignments
            ]).chain(code_block, get_debug_info(ctx))

        return code_block
Пример #10
0
    def search_statics_for_reference(self, reference, context, prepend_context,
                                     debug_info):
        from lockdown.executor.raw_code_factories import dereference_op, assignment_op, \
            literal_op, dereference, context_op

        static = context._get("static", None)
        if static and hasattr(static, reference):
            return dereference(prepend_context, "static", **debug_info)

        prepare = context._get("prepare", None)
        if prepare:
            prepare_search = self.search_statics_for_reference(
                reference, prepare, prepend_context + ["prepare"], debug_info)
            if prepare_search:
                return prepare_search
Пример #11
0
    def search_context_type_area_for_reference(self, reference, area,
                                               context_type, prepend_context,
                                               debug_info):
        from lockdown.executor.raw_code_factories import dereference_op, assignment_op, \
            literal_op, dereference, context_op

        area_getter = context_type.get_micro_op_type(("get", area))
        if not area_getter:
            return None
        area_type = area_getter.value_type
        if not isinstance(area_type, CompositeType):
            return None
        getter = area_type.get_micro_op_type(("get", reference))
        if getter:
            return dereference(prepend_context, area, **debug_info)
Пример #12
0
    def visitArgumentDestructurings(self, ctx):
        initializers = [self.visit(l) for l in ctx.argumentDestructuring()]

        argument_types = [None] * len(initializers)
        local_variable_types = {}
        local_variable_initializers = {}

        for index, (type, name) in enumerate(initializers):
            argument_types[index] = type
            local_variable_types[name] = type
            local_variable_initializers[name] = dereference("argument", index)

        return CodeBlockBuilder(
            argument_type_expression=list_type(argument_types, None),
            local_variable_type=object_type(local_variable_types),
            local_initializer=object_template_op(local_variable_initializers))
Пример #13
0
    def test_silly_tostring_casing(self):
        func = function_lit(
            any_type(),
            try_catch_op(
                return_op(
                    match_op(dereference("argument"), [
                        prepared_function(unit_type(1), literal_op("one")),
                        prepared_function(unit_type(2), literal_op("two")),
                        prepared_function(
                            int_type(),
                            throw_op(
                                object_template_op(
                                    {"type": literal_op("UnknownInt")}))),
                        prepared_function(
                            any_type(),
                            throw_op(
                                object_template_op(
                                    {"type": literal_op("TypeError")})))
                    ])),
                prepared_function(
                    object_type({"type": unit_type("UnknownInt")}),
                    return_op(literal_op("unknown")))))

        _, result = bootstrap_function(func, argument=1, check_safe_exit=False)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "one")
        _, result = bootstrap_function(func, argument=2, check_safe_exit=False)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "two")
        _, result = bootstrap_function(func, argument=3, check_safe_exit=False)
        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, "unknown")
        _, result = bootstrap_function(func,
                                       argument="hello",
                                       check_safe_exit=False)
        self.assertEquals(result.caught_break_mode, "exception")
        self.assertIsInstance(result.value, Universal)
        self.assertEquals(result.value._get("type"), "TypeError")
Пример #14
0
    def visitForListLoop(self, ctx):
        iterator_name = ctx.SYMBOL().getText()
        composite_expression = self.visit(ctx.expression())
        loop_code = self.visit(ctx.codeBlock())

        loop_code = CodeBlockBuilder(argument_type_expression=object_type(
            {iterator_name: inferred_type()})).chain(loop_code,
                                                     get_debug_info(ctx))

        loop_code = loop_code.create("second-class-function",
                                     get_debug_info(ctx))

        return map_op(
            composite_expression,
            prepared_function(
                inferred_type(),
                invoke_op(
                    prepare_function_lit(loop_code, **get_debug_info(ctx)),
                    object_template_op(
                        {
                            iterator_name:
                            dereference("argument", **get_debug_info(ctx))
                        }, **get_debug_info(ctx)), **get_debug_info(ctx)),
                **get_debug_info(ctx)))
Пример #15
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")