Index: jimff-commands.c ================================================================== --- jimff-commands.c +++ jimff-commands.c @@ -490,10 +490,27 @@ err_not_dynamic_pointer: Jim_SetResultFormatted( ctx->interp, "%#s %s: argument #1 \"%#s\" is not a valid dynamic pointer", argv0, modes[mode], argv[1]); return JIM_ERR; } +//NOTE: macros used by wrap/unwrap commands +#define struct_ptr(t) ((t*)(ptr + offs)) +#define struct_elem_ptr(t,sz) ({ t* _ptr = struct_ptr(t); offs += (sz); _ptr; }) + +#define struct_get(t) ({ t _val = *struct_ptr(t); offs += sizeof(t); _val; }) +#define struct_set(t,v) ({*struct_ptr(t) = (v); offs += sizeof(t); }) + +#define struct_get_ptrflags(tag) ((*t == tag) ? JIMFF_DYNAMIC_POINTER_FLAGS : 0) + +#define struct_get_s( tag, type) ((*t == tag) ? struct_get(signed type) : struct_get(unsigned type)) +#define struct_get_u( tag, type) ((*t == tag) ? struct_get(unsigned type) : struct_get(signed type)) +#define struct_get_f(tag) ((*t == tag) ? struct_get(float) : struct_get(double)) + +#define struct_set_s( tag, type, val) ((*t == tag) ? struct_set( signed type, val) : struct_set( unsigned type, val)) +#define struct_set_u( tag, type, val) ((*t == tag) ? struct_set( unsigned type, val) : struct_set( signed type, val)) +#define struct_set_f( tag, val) ((*t == tag) ? struct_set( float, val) : struct_set( double, val)) + static int fn_pointer_unwrap( Jim_Interp* interp, int argc, Jim_Obj* const argv[]) { Jim_Obj* argv0 = argv[0]; struct jimff_context* ctx = Jim_CmdPrivData(interp); int packed = 0; void* ptr; @@ -535,21 +552,11 @@ list = Jim_NewListObj( interp, NULL, 0); } else { list = NULL; index = 3; } - -#define struct_ptr(t) ((t*)(ptr + offs)) -#define struct_elem_ptr(t,sz) ({ t* _ptr = struct_ptr(t); offs += (sz); _ptr; }) -#define struct_get(t) ({ t _val = *struct_ptr(t); offs += sizeof(t); _val; }) - -#define struct_get_s( tag, type) ((*t == tag) ? struct_get(signed type) : struct_get(unsigned type)) -#define struct_get_u( tag, type) ((*t == tag) ? struct_get(unsigned type) : struct_get(signed type)) -#define struct_get_f(tag) ((*t == tag) ? struct_get(float) : struct_get(double)) -#define struct_get_ptrflags(tag) ((*t == tag) ? JIMFF_DYNAMIC_POINTER_FLAGS : 0) - #define GET_ELEM(v) ({ lastobj = (v); \ if(list) Jim_ListAppendElement( interp, list, lastobj); \ else Jim_SetVariable( interp, argv[index++], lastobj); }) offs = 0; @@ -751,10 +758,12 @@ return JIM_ERR; err_too_many_args: Jim_SetResultFormatted( interp, "%#s: there are more arguments than elements in the typetag", argv0); return JIM_ERR; + +#undef GET_ELEM } //TODO: rework wrap command the same way as the new unwrap command, with array support. static int fn_pointer_wrap( Jim_Interp* interp, int argc, Jim_Obj* const argv[]) { Jim_Obj* argv0 = argv[0]; @@ -802,16 +811,10 @@ index = 0; } else { list = NULL; index = 3; } - -#define struct_set(t,v) ({*struct_ptr(t) = (v); offs += sizeof(t); }) - -#define struct_set_s( tag, type, val) ((*t == tag) ? struct_set( signed type, val) : struct_set( unsigned type, val)) -#define struct_set_u( tag, type, val) ((*t == tag) ? struct_set( unsigned type, val) : struct_set( signed type, val)) -#define struct_set_f( tag, val) ((*t == tag) ? struct_set( float, val) : struct_set( double, val)) #define GET_ARG() (lastobj = (list ? Jim_ListGetIndex( interp, list, index++) : argv[index++])) offs = 0; for( const char* t = typetag; *t;) { @@ -1082,13 +1085,26 @@ err_too_many_args: Jim_SetResultFormatted( interp, "%#s: too many arguments for typetag \"%#s\"", argv0, argv[2]); return JIM_ERR; -#undef struct_elem #undef GET_ARG } + +//NOTE: wrap/unwrap macros +#define struct_ptr +#define struct_elem_ptr +#define struct_get +#define struct_set +#define struct_get_ptrflags +#define struct_get_s +#define struct_get_u +#define struct_get_f +#define struct_set_s +#define struct_set_u +#define struct_set_f + // out of bounds checking used by various raw pointer commands, if len=0 it is ignored #define check_oob( name, ptr, size, offs, len) \ void* name ## _start = (ptr) + (offs); \ void* name ## _stop = (ptr) + (size); \