コード例 #1
0
ファイル: test.py プロジェクト: richardhills/lockdown
    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))
コード例 #2
0
ファイル: function.py プロジェクト: richardhills/lockdown
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
コード例 #3
0
ファイル: test.py プロジェクト: richardhills/lockdown
    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")
コード例 #4
0
ファイル: test.py プロジェクト: richardhills/lockdown
    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
ファイル: test.py プロジェクト: richardhills/lockdown
 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")
コード例 #6
0
ファイル: test.py プロジェクト: richardhills/lockdown
    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")
コード例 #7
0
ファイル: test.py プロジェクト: richardhills/lockdown
 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
ファイル: test.py プロジェクト: richardhills/lockdown
    def test_misc2(self):
        _, result = bootstrap_function(
            function_lit(
                no_value_type(), infer_all(), inferred_type(),
                prepared_function(
                    object_type({
                        "foo": int_type(),
                        "bar": int_type()
                    }),
                    return_op(
                        addition_op(dereference("argument.foo"),
                                    dereference("argument.bar")))),
                return_op(
                    invoke_op(
                        dereference("local"),
                        object_template_op({
                            "foo": literal_op(39),
                            "bar": literal_op(3)
                        }),
                    ))))

        self.assertEquals(result.caught_break_mode, "return")
        self.assertEquals(result.value, 42)
コード例 #9
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)))
コード例 #10
0
ファイル: bootstrap.py プロジェクト: richardhills/lockdown
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")