diff src/libmlk-core-js/core/js-game.c @ 355:3b2eed504c61

js: simplify game/state mechanism
author David Demelier <markand@malikania.fr>
date Fri, 22 Oct 2021 10:19:35 +0200
parents 323d13f49233
children 26636fdb735d
line wrap: on
line diff
--- a/src/libmlk-core-js/core/js-game.c	Thu Oct 21 13:45:36 2021 +0200
+++ b/src/libmlk-core-js/core/js-game.c	Fri Oct 22 10:19:35 2021 +0200
@@ -23,8 +23,6 @@
 #include "js-game.h"
 #include "js-state.h"
 
-#define TABLE DUK_HIDDEN_SYMBOL("Mlk.Game.table")
-
 /*
  * TODO: determine if it's worth it to add handle, update and draw functions.
  */
@@ -34,18 +32,13 @@
 {
 	struct state *state = js_state_require(ctx, 0);
 
-	if (game.state == &game.states[GAME_STATE_MAX])
+	if (game.state == &game.states[GAME_STATE_MAX]) {
+		state_finish(state);
 		duk_error(ctx, DUK_ERR_RANGE_ERROR, "too many states");
+	}
 
 	game_push(state);
 
-	/* Store the state to avoid destruction. */
-	duk_push_global_stash(ctx);
-	duk_get_prop_string(ctx, -1, TABLE);
-	duk_dup(ctx, 0);
-	duk_put_prop_index(ctx, -2, duk_get_length(ctx, -2));
-	duk_pop_n(ctx, 2);
-
 	return 0;
 }
 
@@ -55,11 +48,7 @@
 	if (game.state == &game.states[0])
 		return 0;
 
-	/* Remove the stored reference. */
-	duk_push_global_stash(ctx);
-	duk_get_prop_string(ctx, -1, TABLE);
-	duk_del_prop_index(ctx, -1, duk_get_length(ctx, -1) - 1);
-	duk_pop_n(ctx, 2);
+	game_pop();
 
 	return 0;
 }
@@ -100,8 +89,4 @@
 	duk_push_object(ctx);
 	duk_put_function_list(ctx, -1, functions);
 	duk_put_global_string(ctx, "Game");
-	duk_push_global_stash(ctx);
-	duk_push_array(ctx);
-	duk_put_prop_string(ctx, -2, TABLE);
-	duk_pop(ctx);
 }