public errordomain SyntheticStacktraceError {
    FAIL
}

public static void __phpefc_dynamic_include_2_1() throws Error {
    PhpefRuntimeCore.throw_exception("Error", PhpefRuntimeCore.Zval.from_string("missing include"));
}

public static void phpef_file_smoke_php() throws Error {
    __phpefc_dynamic_include_2_1();
}

public static void phpef_file_example_php__foo() throws Error {
    PhpefRuntimeCore.throw_exception("Error", PhpefRuntimeCore.Zval.from_string("unregistered generated-looking function"));
}

public static PhpefRuntimeCore.Zval phpef_file_example_php__registered(PhpefRuntimeCore.Zval[] args) throws Error {
    PhpefRuntimeCore.throw_exception("Error", PhpefRuntimeCore.Zval.from_string("registered generated handler"));
    return PhpefRuntimeCore.Zval.from_null();
}

public static void phpef_file_registered_driver_php() throws Error {
    PhpefRuntimeCore.call_function("foo", new PhpefRuntimeCore.Zval[] {});
}

public class phpef_classes_php__testclass {
    public static PhpefRuntimeCore.Zval testfunc(PhpefRuntimeCore.Zval[] args) throws Error {
        PhpefRuntimeCore.throw_exception("Error", PhpefRuntimeCore.Zval.from_string("registered static method handler"));
        return PhpefRuntimeCore.Zval.from_null();
    }
}

public static void phpef_file_registered_method_driver_php() throws Error {
    PhpefRuntimeCore.call_static_method("testclass", "testfunc", new PhpefRuntimeCore.Zval[] {});
}

public static void phpef_file_missing_function_driver_php() throws Error {
    PhpefRuntimeCore.call_function("missing_function", new PhpefRuntimeCore.Zval[] {});
}

public static PhpefRuntimeCore.Zval phpef_classes_php__testclass_instance(PhpefRuntimeCore.Zval this_object, PhpefRuntimeCore.Zval[] args) throws Error {
    return PhpefRuntimeCore.Zval.from_null();
}

int main(string[] args) {
    PhpefRuntimeCore.init(args);
    var mode = args.length > 1 ? args[1] : "helper";

    try {
        switch (mode) {
            case "optimized-aliases":
                PhpefRuntimeCore.register_stacktrace_frame("phpef_file_smoke_php", "{main}");
                if (PhpefRuntimeCore.lookup_stacktrace_frame("phpef_file_smoke_php.isra.0") != "{main}") {
                    stderr.printf("optimized explicit frame alias lookup failed\n");
                    return 10;
                }
                PhpefRuntimeCore.register_function("foo", phpef_file_example_php__registered, "phpef_file_example_php__registered", 0, 0);
                var function_metadata = PhpefRuntimeCore.lookup_registered_function_by_handler("phpef_file_example_php__registered.isra.0");
                if (function_metadata == null || function_metadata.php_name != "foo") {
                    stderr.printf("optimized function handler alias lookup failed\n");
                    return 11;
                }
                PhpefRuntimeCore.register_class("testclass");
                PhpefRuntimeCore.register_static_method("testclass", "testfunc", phpef_classes_php__testclass.testfunc, "phpef_classes_php__testclass.testfunc", 0, 0);
                var static_metadata = PhpefRuntimeCore.lookup_registered_static_method_by_handler("phpef_classes_php__testclass_testfunc.isra.0");
                if (static_metadata == null || static_metadata.php_name != "testclass::testfunc") {
                    stderr.printf("optimized static method handler alias lookup failed\n");
                    return 12;
                }
                PhpefRuntimeCore.register_instance_method("testclass", "instance", phpef_classes_php__testclass_instance, "phpef_classes_php__testclass.instance", 0, 0);
                var instance_metadata = PhpefRuntimeCore.lookup_registered_instance_method_by_handler("phpef_classes_php__testclass_instance.isra.0");
                if (instance_metadata == null || instance_metadata.php_name != "testclass::instance") {
                    stderr.printf("optimized instance method handler alias lookup failed\n");
                    return 13;
                }
                return 0;
            case "stripped-summary":
                throw new SyntheticStacktraceError.FAIL("Exception: synthetic\n\nStacktrace at throw-time:\n#1 in '??' at /tmp/hallowelt\n#2 in '??' at /tmp/hallowelt\n#3 in '??' at /usr/lib64/libc.so.6\n#4 in '__libc_start_main' at /usr/lib64/libc.so.6\n#5 in '??' at /tmp/hallowelt");
            case "helper":
                PhpefRuntimeCore.register_stacktrace_frame("__phpefc_dynamic_include_2_1", "{include}");
                PhpefRuntimeCore.register_stacktrace_frame("phpef_file_smoke_php", "{main}");
                phpef_file_smoke_php();
                break;
            case "unregistered":
                phpef_file_example_php__foo();
                break;
            case "registered":
                PhpefRuntimeCore.register_function(
                    "foo",
                    phpef_file_example_php__registered,
                    "phpef_file_example_php__registered",
                    0,
                    0
                );
                PhpefRuntimeCore.register_stacktrace_frame("phpef_file_registered_driver_php", "{main}");
                phpef_file_registered_driver_php();
                break;
            case "registered-method":
                PhpefRuntimeCore.register_class("testclass");
                PhpefRuntimeCore.register_static_method(
                    "testclass",
                    "testfunc",
                    phpef_classes_php__testclass.testfunc,
                    "phpef_classes_php__testclass.testfunc",
                    0,
                    0
                );
                PhpefRuntimeCore.register_stacktrace_frame("phpef_file_registered_method_driver_php", "{main}");
                phpef_file_registered_method_driver_php();
                break;
            case "missing-function":
                PhpefRuntimeCore.register_stacktrace_frame("phpef_file_missing_function_driver_php", "{main}");
                phpef_file_missing_function_driver_php();
                break;
            default:
                stderr.printf("unknown mode: %s\n", mode);
                return 2;
        }
        return 0;
    } catch (Error e) {
        return PhpefRuntimeCore.handle_uncaught(e);
    }
}
