diff options
author | Benoit Germain <bnt.germain@gmail.com> | 2018-11-19 09:16:49 +0100 |
---|---|---|
committer | Benoit Germain <bnt.germain@gmail.com> | 2018-11-19 09:16:49 +0100 |
commit | 7b4f59c5ebc84e426e2876906b24d7dd73342f07 (patch) | |
tree | f05748fc2d75c43c25865be01677271fff5d86e4 /src/macros_and_utils.h | |
parent | 01f83215a2ad235fbf306f591c6c0547b1bb7047 (diff) | |
download | lanes-7b4f59c5ebc84e426e2876906b24d7dd73342f07.tar.gz lanes-7b4f59c5ebc84e426e2876906b24d7dd73342f07.tar.bz2 lanes-7b4f59c5ebc84e426e2876906b24d7dd73342f07.zip |
Internal code tweaks
* Registry access code utility macros
* CONFIG_REGKEY and LOOKUP_REGKEY are now lightuserdata instead of strings
* Stack checking debug macros improvements
Diffstat (limited to 'src/macros_and_utils.h')
-rw-r--r-- | src/macros_and_utils.h | 68 |
1 files changed, 54 insertions, 14 deletions
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h index 550fc0a..acbe690 100644 --- a/src/macros_and_utils.h +++ b/src/macros_and_utils.h | |||
@@ -46,26 +46,52 @@ extern char const* debugspew_indent; | |||
46 | 46 | ||
47 | #ifdef NDEBUG | 47 | #ifdef NDEBUG |
48 | 48 | ||
49 | #define _ASSERT_L(lua,c) /*nothing*/ | 49 | #define _ASSERT_L(lua,c) //nothing |
50 | #define STACK_CHECK(L) /*nothing*/ | 50 | #define STACK_CHECK(L,o) //nothing |
51 | #define STACK_MID(L,c) /*nothing*/ | 51 | #define STACK_CHECK_ABS(L,o) //nothing |
52 | #define STACK_END(L,c) /*nothing*/ | 52 | #define STACK_MID(L,c) //nothing |
53 | #define STACK_DUMP(L) /*nothing*/ | 53 | #define STACK_END(L,c) //nothing |
54 | #define STACK_DUMP(L) //nothing | ||
54 | 55 | ||
55 | #else // NDEBUG | 56 | #else // NDEBUG |
56 | 57 | ||
57 | #define _ASSERT_L( L, cond_) if( (cond_) == 0) { (void) luaL_error( L, "ASSERT failed: %s:%d '%s'", __FILE__, __LINE__, #cond_);} | 58 | #define _ASSERT_L( L, cond_) if( (cond_) == 0) { (void) luaL_error( L, "ASSERT failed: %s:%d '%s'", __FILE__, __LINE__, #cond_);} |
58 | 59 | ||
59 | #define STACK_CHECK(L) { int const _oldtop_##L = lua_gettop( L) | 60 | #define STACK_CHECK( L, offset_) \ |
60 | #define STACK_MID(L,change) \ | ||
61 | do \ | ||
62 | { \ | 61 | { \ |
63 | int stack_check_a = lua_gettop( L) - _oldtop_##L; \ | 62 | int const L##_delta = offset_; \ |
64 | int stack_check_b = (change); \ | 63 | if( (L##_delta < 0) || (lua_gettop( L) < L##_delta)) \ |
65 | if( stack_check_a != stack_check_b) \ | 64 | { \ |
66 | luaL_error( L, "STACK ASSERT failed (%d not %d): %s:%d", stack_check_a, stack_check_b, __FILE__, __LINE__ ); \ | 65 | assert( FALSE); \ |
67 | } while( 0) | 66 | (void) luaL_error( L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop( L), L##_delta, __FILE__, __LINE__); \ |
68 | #define STACK_END(L,change) STACK_MID(L,change); } | 67 | } \ |
68 | int const L##_oldtop = lua_gettop( L) - L##_delta | ||
69 | |||
70 | #define STACK_CHECK_ABS( L, offset_) \ | ||
71 | { \ | ||
72 | int const L##_pos = offset_; \ | ||
73 | if( lua_gettop( L) < L##_pos) \ | ||
74 | { \ | ||
75 | assert( FALSE); \ | ||
76 | (void) luaL_error( L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop( L), L##_pos, __FILE__, __LINE__); \ | ||
77 | } \ | ||
78 | int const L##_oldtop = 0 | ||
79 | |||
80 | #define STACK_MID( L, change) \ | ||
81 | do \ | ||
82 | { \ | ||
83 | int stack_check_a = lua_gettop( L) - L##_oldtop; \ | ||
84 | int stack_check_b = (change); \ | ||
85 | if( stack_check_a != stack_check_b) \ | ||
86 | { \ | ||
87 | assert( FALSE); \ | ||
88 | luaL_error( L, "STACK ASSERT failed (%d not %d): %s:%d", stack_check_a, stack_check_b, __FILE__, __LINE__); \ | ||
89 | } \ | ||
90 | } while( 0) | ||
91 | |||
92 | #define STACK_END( L, change) \ | ||
93 | STACK_MID( L, change); \ | ||
94 | } | ||
69 | 95 | ||
70 | #define STACK_DUMP( L) luaG_dump( L) | 96 | #define STACK_DUMP( L) luaG_dump( L) |
71 | 97 | ||
@@ -75,4 +101,18 @@ extern char const* debugspew_indent; | |||
75 | 101 | ||
76 | #define STACK_GROW( L, n) do { if (!lua_checkstack(L,(int)(n))) luaL_error( L, "Cannot grow stack!" ); } while( 0) | 102 | #define STACK_GROW( L, n) do { if (!lua_checkstack(L,(int)(n))) luaL_error( L, "Cannot grow stack!" ); } while( 0) |
77 | 103 | ||
104 | // non-string keyed registry access | ||
105 | #define REGISTRY_SET( L, key_, value_) \ | ||
106 | { \ | ||
107 | push_unique_key( L, key_); \ | ||
108 | value_; \ | ||
109 | lua_rawset( L, LUA_REGISTRYINDEX); \ | ||
110 | } | ||
111 | |||
112 | #define REGISTRY_GET( L, key_) \ | ||
113 | { \ | ||
114 | push_unique_key( L, key_); \ | ||
115 | lua_rawget( L, LUA_REGISTRYINDEX); \ | ||
116 | } | ||
117 | |||
78 | #endif // MACROS_AND_UTILS_H | 118 | #endif // MACROS_AND_UTILS_H |