Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tests/except-0.c

    re9145a3 rfcc88a4  
    77#include <stdio.h>
    88#include <stdbool.h>
    9 
    10 #include "except-mac.h"
    11 TRIVIAL_EXCEPTION(yin)
    12 TRIVIAL_EXCEPTION(yang)
    13 TRIVIAL_EXCEPTION(zen)
    14 
    159
    1610// Local type to mark exits from scopes. (see ERROR)
     
    2923
    3024
    31 // Mark throws: make sure to only pass in exception types.
    32 forall(dtype T)
    33 void terminate(T * except_value) {
     25// Local Exception Types and manual vtable types.
     26//#define TRIVIAL_EXCEPTION(name) //TRIVAL_EXCEPTION(yin)
     27struct yin;
     28struct yin_vtable {
     29        struct exception_t_vtable const * parent;
     30        size_t size;
     31    void (*copy)(yin *this, yin * other);
     32    void (*free)(yin *this);
     33    const char (*msg)(yin *this);
     34};
     35struct yin {
     36        struct yin_vtable const * parent;
     37};
     38void yin_msg(yin) {
     39        return "in";
     40}
     41yin_vtable _yin_vtable_instance = {
     42        &_exception_t_vtable_instance, sizeof(yin), ?{}, ^?{}, yin_msg
     43}
     44
     45
     46void terminate(exception * except_value) {
    3447        signal_exit a = {"terminate function"};
    35         THROW(except_value);
     48        throw except_value;
    3649        printf("terminate returned\n");
    3750}
    3851
    39 forall(dtype T)
    40 void resume(T * except_value) {
     52void resume(exception * except_value) {
    4153        signal_exit a = {"resume function"};
    42         THROW_RESUME(except_value);
     54        throwResume except_value;
    4355        printf("resume returned\n");
    4456}
     
    4860        signal_exit a = {"bar function"};
    4961        try {
    50                 terminate(&(zen){});
    51         } catch (yin * error) {
    52                 printf("bar caught exception yin.\n");
     62                terminate(4);
     63        } catch (3) {
     64                printf("bar caught exception 3.\n");
    5365        }
    5466}
     
    5870        try {
    5971                bar();
    60         } catch (yang * error) {
    61                 printf("foo caught exception yang.\n");
    62         } catch (zen * error) {
    63                 printf("foo caught exception zen.\n");
     72        } catch (4) {
     73                printf("foo caught exception 4.\n");
     74        } catch (2) {
     75                printf("foo caught exception 2.\n");
    6476        }
    6577}
     
    6981        signal_exit a = {"beta function"};
    7082        try {
    71                 zen x;
    72                 resume(&x);
    73         } catchResume (yin * error) {
    74                 printf("beta caught exception yin\n");
     83                resume(4);
     84        } catchResume (3) {
     85                printf("beta caught exception 3\n");
    7586        }
    7687}
     
    8091        try {
    8192                beta();
    82         } catchResume (yang * error) {
    83                 printf("alpha caught exception yang\n");
    84         } catchResume (zen * error) {
    85                 printf("alpha caught exception zen\n");
     93        } catchResume (2) {
     94                printf("alpha caught exception 2\n");
     95        } catchResume (4) {
     96                printf("alpha caught exception 4\n");
    8697        }
    8798}
     
    106117void fallback() {
    107118        try {
    108                 zen x;
    109                 resume(&x);
    110         } catch (zen * error) {
    111                 printf("fallback caught termination zen\n");
     119                resume(2);
     120        } catch (2) {
     121                printf("fallback caught termination 2\n");
    112122        }
    113123}
     
    117127        signal_exit a = {"terminate_swap"};
    118128        try {
    119                 yin x;
    120                 terminate(&x);
    121         } catch (yin * error) {
    122                 yang y;
    123                 terminate(&y);
     129                terminate(2);
     130        } catch (2) {
     131                terminate(3);
    124132        }
    125133}
     
    129137        try {
    130138                terminate_swap();
    131         } catch (yang * error) {
    132                 printf("terminate_swapped caught exception yang\n");
     139        } catch (3) {
     140                printf("terminate_swapped caught exception 3\n");
    133141        }
    134142}
     
    138146        signal_exit a = {"resume_swap"};
    139147        try {
    140                 yin x;
    141                 resume(&x);
    142         } catchResume (yin * error) {
    143                 yang y;
    144                 resume(&y);
     148                resume(2);
     149        } catchResume (2) {
     150                resume(3);
    145151        }
    146152}
     
    149155        try {
    150156                resume_swap();
    151         } catchResume (yang * error) {
    152                 printf("resume_swapped caught exception yang\n");
     157        } catchResume (3) {
     158                printf("resume_swapped caught exception 3\n");
    153159        }
    154160}
     
    158164        try {
    159165                try {
    160                         zen x;
    161                         terminate(&x);
    162                 } catch (zen * error) {
    163                         printf("reterminate zen caught and "
    164                                "will rethrow exception zen\n");
     166                        terminate(2);
     167                } catch (2) {
     168                        printf("reterminate 2 caught and "
     169                               "will rethrow exception 2\n");
    165170                        throw;
    166171                }
    167         } catch (zen * error) {
    168                 printf("reterminate 1 caught exception zen\n");
     172        } catch (2) {
     173                printf("reterminate 1 caught exception 2\n");
    169174        }
    170175}
     
    174179        try {
    175180                try {
    176                         zen x;
    177                         resume(&x);
    178                 } catchResume (zen * error) {
    179                         printf("reresume zen caught and rethrows exception zen\n");
     181                        resume(2);
     182                } catchResume (2) {
     183                        printf("reresume 2 caught and rethrows exception 2\n");
    180184                        throwResume;
    181185                }
    182         } catchResume (zen * error) {
    183                 printf("reresume 1 caught exception zen\n");
     186        } catchResume (2) {
     187                printf("reresume 1 caught exception 2\n");
    184188        }
    185189}
     
    189193        // terminate block, call resume
    190194        try {
    191                 zen x;
    192                 resume(&x);
    193         } catch (zen * error) {
    194                 printf("fum caught exception zen\n");
     195                resume(3);
     196        } catch (3) {
     197                printf("fum caught exception 3\n");
    195198        }
    196199}
     
    199202        // resume block, call terminate
    200203        try {
    201                 zen y;
    202                 terminate(&y);
    203         } catchResume (zen * error) {
    204                 printf("foe caught exception zen\n");
     204                terminate(3);
     205        } catchResume (3) {
     206                printf("foe caught exception 3\n");
    205207        }
    206208}
     
    210212        try {
    211213                foe();
    212         } catch (zen * error) {
    213                 printf("fy caught exception zen\n");
     214        } catch (3) {
     215                printf("fy caught exception 3\n");
    214216                fum();
    215217        }
     
    220222        try {
    221223                fy();
    222         } catchResume (zen * error) {
    223                 printf("fee caught exception zen\n");
     224        } catchResume (3) {
     225                printf("fee caught exception 3\n");
    224226        }
    225227}
     
    240242        reresume(); printf("\n");
    241243        fee(); printf("\n");
    242 
    243244        // Uncaught termination test.
    244         printf("Throw uncaught.\n");
    245         yang z;
    246         terminate(&z);
    247 }
     245        terminate(7);
     246}
Note: See TracChangeset for help on using the changeset viewer.