changeset 374:76ecec33dc9b

js: add Mlk.setPanic
author David Demelier <markand@malikania.fr>
date Mon, 25 Oct 2021 21:17:30 +0200
parents ebaf5490b32f
children d83b686dcf63
files src/libmlk-core-js/core/js-drawable.c src/libmlk-core-js/core/js-panic.c src/libmlk-core/core/panic.c src/libmlk-core/core/panic.h
diffstat 4 files changed, 44 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/libmlk-core-js/core/js-drawable.c	Mon Oct 25 14:43:14 2021 +0200
+++ b/src/libmlk-core-js/core/js-drawable.c	Mon Oct 25 21:17:30 2021 +0200
@@ -166,7 +166,7 @@
 	data->ctx = ctx;
 	data->dw.x = x;
 	data->dw.y = y;
-	data->dw.data = self;
+	data->dw.data = data;
 	data->dw.update = update;
 	data->dw.finish = finish;
 	data->dw.draw = draw;
@@ -174,7 +174,7 @@
 
 	duk_push_this(ctx);
 	data->ptr = duk_get_heapptr(ctx, -1);
-	duk_push_pointer(ctx, self);
+	duk_push_pointer(ctx, data);
 	duk_put_prop_string(ctx, -2, SIGNATURE);
 	duk_push_string(ctx, "x");
 	duk_push_c_function(ctx, Drawable_getX, 0);
--- a/src/libmlk-core-js/core/js-panic.c	Mon Oct 25 14:43:14 2021 +0200
+++ b/src/libmlk-core-js/core/js-panic.c	Mon Oct 25 21:17:30 2021 +0200
@@ -17,11 +17,32 @@
  */
 
 #include <assert.h>
+#include <stdio.h>
 
 #include <core/panic.h>
 
 #include "js-panic.h"
 
+#define PANIC_CB DUK_HIDDEN_SYMBOL("Mlk.panicHandler")
+
+static void
+handler(void)
+{
+	duk_context *ctx = panic_data;
+
+	duk_push_global_stash(ctx);
+	duk_get_prop_string(ctx, -1, PANIC_CB);
+	duk_remove(ctx, -2);
+
+	if (duk_is_callable(ctx, -1)) {
+		duk_call(ctx, 0);
+		duk_pop(ctx);
+	} else {
+		fprintf(stderr, "abort: javascript panic handler not callable\n");
+		exit(1);
+	}
+}
+
 static duk_ret_t
 Mlk_panic(duk_context *ctx)
 {
@@ -33,8 +54,26 @@
 	return 0;
 }
 
+static duk_ret_t
+Mlk_setPanic(duk_context *ctx)
+{
+	if (!duk_is_callable(ctx, 0))
+		return duk_error(ctx, DUK_ERR_TYPE_ERROR, "handler not callable");
+
+	duk_push_global_stash(ctx);
+	duk_dup(ctx, 0);
+	duk_put_prop_string(ctx, -2, PANIC_CB);
+	duk_pop(ctx);
+
+	panic_handler = handler;
+	panic_data = ctx;
+
+	return 0;
+}
+
 static const duk_function_list_entry functions[] = {
 	{ "panic",              Mlk_panic,              DUK_VARARGS     },
+	{ "setPanic",           Mlk_setPanic,           1               },
 	{ NULL,                 NULL,                   0               }
 };
 
@@ -46,4 +85,4 @@
 	duk_get_global_string(ctx, "Mlk");
 	duk_put_function_list(ctx, -1, functions);
 	duk_pop(ctx);
-}
\ No newline at end of file
+}
--- a/src/libmlk-core/core/panic.c	Mon Oct 25 14:43:14 2021 +0200
+++ b/src/libmlk-core/core/panic.c	Mon Oct 25 21:17:30 2021 +0200
@@ -33,6 +33,7 @@
 }
 
 void (*panic_handler)(void) = terminate;
+void *panic_data = NULL;
 
 void
 panicf(const char *fmt, ...)
--- a/src/libmlk-core/core/panic.h	Mon Oct 25 14:43:14 2021 +0200
+++ b/src/libmlk-core/core/panic.h	Mon Oct 25 21:17:30 2021 +0200
@@ -24,6 +24,7 @@
 #include "core.h"
 
 extern void (*panic_handler)(void);
+extern void *panic_data;
 
 CORE_BEGIN_DECLS