Ignore:
File:
1 edited

Legend:

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

    rfcc88a4 re9145a3  
    77#include <stdio.h>
    88#include <stdbool.h>
     9
     10#include "except-mac.h"
     11TRIVIAL_EXCEPTION(yin)
     12TRIVIAL_EXCEPTION(yang)
     13TRIVIAL_EXCEPTION(zen)
     14
    915
    1016// Local type to mark exits from scopes. (see ERROR)
     
    2329
    2430
    25 // Local Exception Types and manual vtable types.
    26 //#define TRIVIAL_EXCEPTION(name) //TRIVAL_EXCEPTION(yin)
    27 struct yin;
    28 struct 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 };
    35 struct yin {
    36         struct yin_vtable const * parent;
    37 };
    38 void yin_msg(yin) {
    39         return "in";
    40 }
    41 yin_vtable _yin_vtable_instance = {
    42         &_exception_t_vtable_instance, sizeof(yin), ?{}, ^?{}, yin_msg
    43 }
    44 
    45 
    46 void terminate(exception * except_value) {
     31// Mark throws: make sure to only pass in exception types.
     32forall(dtype T)
     33void terminate(T * except_value) {
    4734        signal_exit a = {"terminate function"};
    48         throw except_value;
     35        THROW(except_value);
    4936        printf("terminate returned\n");
    5037}
    5138
    52 void resume(exception * except_value) {
     39forall(dtype T)
     40void resume(T * except_value) {
    5341        signal_exit a = {"resume function"};
    54         throwResume except_value;
     42        THROW_RESUME(except_value);
    5543        printf("resume returned\n");
    5644}
     
    6048        signal_exit a = {"bar function"};
    6149        try {
    62                 terminate(4);
    63         } catch (3) {
    64                 printf("bar caught exception 3.\n");
     50                terminate(&(zen){});
     51        } catch (yin * error) {
     52                printf("bar caught exception yin.\n");
    6553        }
    6654}
     
    7058        try {
    7159                bar();
    72         } catch (4) {
    73                 printf("foo caught exception 4.\n");
    74         } catch (2) {
    75                 printf("foo caught exception 2.\n");
     60        } catch (yang * error) {
     61                printf("foo caught exception yang.\n");
     62        } catch (zen * error) {
     63                printf("foo caught exception zen.\n");
    7664        }
    7765}
     
    8169        signal_exit a = {"beta function"};
    8270        try {
    83                 resume(4);
    84         } catchResume (3) {
    85                 printf("beta caught exception 3\n");
     71                zen x;
     72                resume(&x);
     73        } catchResume (yin * error) {
     74                printf("beta caught exception yin\n");
    8675        }
    8776}
     
    9180        try {
    9281                beta();
    93         } catchResume (2) {
    94                 printf("alpha caught exception 2\n");
    95         } catchResume (4) {
    96                 printf("alpha caught exception 4\n");
     82        } catchResume (yang * error) {
     83                printf("alpha caught exception yang\n");
     84        } catchResume (zen * error) {
     85                printf("alpha caught exception zen\n");
    9786        }
    9887}
     
    117106void fallback() {
    118107        try {
    119                 resume(2);
    120         } catch (2) {
    121                 printf("fallback caught termination 2\n");
     108                zen x;
     109                resume(&x);
     110        } catch (zen * error) {
     111                printf("fallback caught termination zen\n");
    122112        }
    123113}
     
    127117        signal_exit a = {"terminate_swap"};
    128118        try {
    129                 terminate(2);
    130         } catch (2) {
    131                 terminate(3);
     119                yin x;
     120                terminate(&x);
     121        } catch (yin * error) {
     122                yang y;
     123                terminate(&y);
    132124        }
    133125}
     
    137129        try {
    138130                terminate_swap();
    139         } catch (3) {
    140                 printf("terminate_swapped caught exception 3\n");
     131        } catch (yang * error) {
     132                printf("terminate_swapped caught exception yang\n");
    141133        }
    142134}
     
    146138        signal_exit a = {"resume_swap"};
    147139        try {
    148                 resume(2);
    149         } catchResume (2) {
    150                 resume(3);
     140                yin x;
     141                resume(&x);
     142        } catchResume (yin * error) {
     143                yang y;
     144                resume(&y);
    151145        }
    152146}
     
    155149        try {
    156150                resume_swap();
    157         } catchResume (3) {
    158                 printf("resume_swapped caught exception 3\n");
     151        } catchResume (yang * error) {
     152                printf("resume_swapped caught exception yang\n");
    159153        }
    160154}
     
    164158        try {
    165159                try {
    166                         terminate(2);
    167                 } catch (2) {
    168                         printf("reterminate 2 caught and "
    169                                "will rethrow exception 2\n");
     160                        zen x;
     161                        terminate(&x);
     162                } catch (zen * error) {
     163                        printf("reterminate zen caught and "
     164                               "will rethrow exception zen\n");
    170165                        throw;
    171166                }
    172         } catch (2) {
    173                 printf("reterminate 1 caught exception 2\n");
     167        } catch (zen * error) {
     168                printf("reterminate 1 caught exception zen\n");
    174169        }
    175170}
     
    179174        try {
    180175                try {
    181                         resume(2);
    182                 } catchResume (2) {
    183                         printf("reresume 2 caught and rethrows exception 2\n");
     176                        zen x;
     177                        resume(&x);
     178                } catchResume (zen * error) {
     179                        printf("reresume zen caught and rethrows exception zen\n");
    184180                        throwResume;
    185181                }
    186         } catchResume (2) {
    187                 printf("reresume 1 caught exception 2\n");
     182        } catchResume (zen * error) {
     183                printf("reresume 1 caught exception zen\n");
    188184        }
    189185}
     
    193189        // terminate block, call resume
    194190        try {
    195                 resume(3);
    196         } catch (3) {
    197                 printf("fum caught exception 3\n");
     191                zen x;
     192                resume(&x);
     193        } catch (zen * error) {
     194                printf("fum caught exception zen\n");
    198195        }
    199196}
     
    202199        // resume block, call terminate
    203200        try {
    204                 terminate(3);
    205         } catchResume (3) {
    206                 printf("foe caught exception 3\n");
     201                zen y;
     202                terminate(&y);
     203        } catchResume (zen * error) {
     204                printf("foe caught exception zen\n");
    207205        }
    208206}
     
    212210        try {
    213211                foe();
    214         } catch (3) {
    215                 printf("fy caught exception 3\n");
     212        } catch (zen * error) {
     213                printf("fy caught exception zen\n");
    216214                fum();
    217215        }
     
    222220        try {
    223221                fy();
    224         } catchResume (3) {
    225                 printf("fee caught exception 3\n");
     222        } catchResume (zen * error) {
     223                printf("fee caught exception zen\n");
    226224        }
    227225}
     
    242240        reresume(); printf("\n");
    243241        fee(); printf("\n");
     242
    244243        // Uncaught termination test.
    245         terminate(7);
    246 }
     244        printf("Throw uncaught.\n");
     245        yang z;
     246        terminate(&z);
     247}
Note: See TracChangeset for help on using the changeset viewer.