1 #ifndef __R_SCRIPT_H 2 #define __R_SCRIPT_H 3 4 /* 5 Copyright 2012 Jared Krinke. 6 7 Permission is hereby granted, free of charge, to any person obtaining a copy 8 of this software and associated documentation files (the "Software"), to deal 9 in the Software without restriction, including without limitation the rights 10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 copies of the Software, and to permit persons to whom the Software is 12 furnished to do so, subject to the following conditions: 13 14 The above copyright notice and this permission notice shall be included in 15 all copies or substantial portions of the Software. 16 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 THE SOFTWARE. 24 */ 25 26 #include "r_state.h" 27 #include "r_object_ref.h" 28 29 /* Macros for verifying script stack invariants from C code */ 30 #ifdef R_DEBUG 31 #define R_SCRIPT_ENTER() int __r_script_enter_base_index = lua_gettop(rs->script_state) 32 #define R_SCRIPT_EXIT(delta) { int __r_script_exit_base_index = lua_gettop(rs->script_state); R_ASSERT(__r_script_exit_base_index == __r_script_enter_base_index + (delta)); } 33 #else 34 #define R_SCRIPT_ENTER() 35 #define R_SCRIPT_EXIT(delta) 36 #endif 37 38 /* Set script error return point (this macro expands to a function call that can return twice) */ 39 #define R_SCRIPT_SET_ERROR_CONTEXT(rs, panic) (lua_atpanic(rs->script_state, panic), setjmp(rs->script_error_return_point)) 40 41 typedef struct 42 { 43 int script_type; 44 r_object_type_t object_type; 45 } r_script_argument_t; 46 47 typedef enum 48 { 49 R_SCRIPT_NODE_TYPE_TABLE = 1, 50 R_SCRIPT_NODE_TYPE_FUNCTION, 51 R_SCRIPT_NODE_TYPE_MAX 52 } r_script_node_type_t; 53 54 typedef struct _r_script_node 55 { 56 const char *name; 57 r_script_node_type_t type; 58 const struct _r_script_node *table_children; 59 lua_CFunction function_func; 60 } r_script_node_t; 61 62 typedef struct 63 { 64 int index; 65 r_object_ref_t *ref; 66 r_script_node_t node; 67 } r_script_node_root_t; 68 69 extern r_status_t r_script_start(r_state_t *rs); 70 extern void r_script_end(r_state_t *rs); 71 extern r_status_t r_script_setup(r_state_t *rs); 72 73 extern r_status_t r_script_call(r_state_t *rs, int argument_count, int result_count); 74 extern r_state_t *r_script_get_r_state(lua_State *ls); 75 76 /* TODO: Use this everywhere it can be used */ 77 extern r_status_t r_script_verify_arguments(r_state_t *rs, int expected_argument_count, const r_script_argument_t *expected_arguments); 78 extern r_status_t r_script_verify_arguments_with_optional(r_state_t *rs, int min_argument_count, int max_argument_count, const r_script_argument_t *expected_arguments); 79 80 extern r_status_t r_script_register_node(r_state_t *rs, const r_script_node_t *node, int root_index); 81 82 extern r_status_t r_script_register_nodes(r_state_t *rs, const r_script_node_root_t *node_roots); 83 84 /* TODO: Unregister node functions need to be created and used everywhere! */ 85 86 /* Script panic function */ 87 extern int l_panic(lua_State *ls); 88 89 #endif 90 91