• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/examples/exception-handler.c

  1 /*
  2  * Copyright (C) 2021 Jo-Philipp Wich <jo@mein.io>
  3  *
  4  * Permission to use, copy, modify, and/or distribute this software for any
  5  * purpose with or without fee is hereby granted, provided that the above
  6  * copyright notice and this permission notice appear in all copies.
  7  *
  8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15  */
 16 
 17 #include <stdio.h>
 18 
 19 #include <ucode/compiler.h>
 20 #include <ucode/lib.h>
 21 #include <ucode/vm.h>
 22 
 23 
 24 #define MULTILINE_STRING(...) #__VA_ARGS__
 25 
 26 static const char *program_code = MULTILINE_STRING(
 27         {%
 28                 function fail() {
 29                         /* invoke not existing function to raise runtime error */
 30                         doesnotexist();
 31                 }
 32 
 33                 map([1], x => fail(x));
 34         %}
 35 );
 36 
 37 static uc_parse_config_t config = {
 38         .strict_declarations = false,
 39         .lstrip_blocks = true,
 40         .trim_blocks = true
 41 };
 42 
 43 static void
 44 log_exception(uc_vm_t *vm, uc_exception_t *ex)
 45 {
 46         char *trace = ucv_to_jsonstring_formatted(vm, ex->stacktrace, ' ', 2);
 47 
 48         printf("Program raised an exception:\n");
 49         printf("  type=%d\n", ex->type);
 50         printf("  message=%s\n", ex->message);
 51         printf("  stacktrace=%s\n", trace);
 52 
 53         free(trace);
 54 }
 55 
 56 int main(int argc, char **argv)
 57 {
 58         int exit_code = 0;
 59 
 60         /* create a source buffer containing the program code */
 61         uc_source_t *src = uc_source_new_buffer("my program", strdup(program_code), strlen(program_code));
 62 
 63         /* compile source buffer into function */
 64         char *syntax_error = NULL;
 65         uc_program_t *program = uc_compile(&config, src, &syntax_error);
 66 
 67         /* release source buffer */
 68         uc_source_put(src);
 69 
 70         /* check if compilation failed */
 71         if (!program) {
 72                 fprintf(stderr, "Failed to compile program: %s\n", syntax_error);
 73 
 74                 return 1;
 75         }
 76 
 77         /* initialize VM context */
 78         uc_vm_t vm = { 0 };
 79         uc_vm_init(&vm, &config);
 80 
 81         /* load standard library into global VM scope */
 82         uc_stdlib_load(uc_vm_scope_get(&vm));
 83 
 84         /* register custom exception handler */
 85         uc_vm_exception_handler_set(&vm, log_exception);
 86 
 87         /* execute program function */
 88         int return_code = uc_vm_execute(&vm, program, NULL);
 89 
 90         /* handle return status */
 91         if (return_code == ERROR_COMPILE || return_code == ERROR_RUNTIME) {
 92                 printf("An error occurred while running the program\n");
 93                 exit_code = 1;
 94         }
 95 
 96         /* free VM context */
 97         uc_vm_free(&vm);
 98 
 99         return exit_code;
100 }
101 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt