Check-in [f53309743a]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:cleanup wrap/unwrap macros
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:f53309743a0ae3a2bdccd8bc5cea8d0c301e5bb74e9616259a69c0365f44c3b9
User & Date: grable 2018-11-01 04:38:02
Context
2018-11-01
04:39
removed old todo check-in: 175cec980d user: grable tags: trunk
04:38
cleanup wrap/unwrap macros check-in: f53309743a user: grable tags: trunk
04:30
reworked wrap command for parity with unwrap, though pointer sizes remain check-in: 592303ad28 user: grable tags: trunk
Changes

Changes to jimff-commands.c.

488
489
490
491
492
493
494

















495
496
497
498
499
500
501
...
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
...
749
750
751
752
753
754
755


756
757
758
759
760
761
762
...
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
....
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089














1090
1091
1092
1093
1094
1095
1096
	return JIM_ERR;
	
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;
}


















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;
	int size;
	int len;
................................................................................
	
	if(argc == 3) {
		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;
	for( const char* t = typetag; *t;) {
		if(!list && index >= argc) {
................................................................................
err_structs_unsupported:
	Jim_SetResultFormatted( interp, "%#s: structs not supported", argv0);
	return JIM_ERR;
	
err_too_many_args:
	Jim_SetResultFormatted( interp, "%#s: there are more arguments than elements in the typetag", argv0);
	return JIM_ERR;


}

//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];
	struct jimff_context* ctx = Jim_CmdPrivData(interp);
	int packed = 0;
................................................................................
	if(argc == 4 && len > 1) {
		list = argv[3];
		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;) {
		if(list) {
			if(index >= Jim_ListLength( interp, list)) goto err_not_enough_args;
................................................................................
	Jim_SetResultFormatted( interp, "%#s: not enough arguments for typetag \"%#s\"", argv0, argv[2]);
	return JIM_ERR;
	
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
}















// 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);												\
		if(name ## _start < (ptr)) goto err_offset_oob;									\
		if(name ## _stop > (ptr)) {																\







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
<
<
<
<
<
<
<
<
<
<







 







>
>







 







<
<
<
<
<
<







 







<


>
>
>
>
>
>
>
>
>
>
>
>
>
>







488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
...
550
551
552
553
554
555
556
557










558
559
560
561
562
563
564
...
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
...
809
810
811
812
813
814
815






816
817
818
819
820
821
822
....
1083
1084
1085
1086
1087
1088
1089

1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
	return JIM_ERR;
	
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;
	int size;
	int len;
................................................................................
	
	if(argc == 3) {
		list = Jim_NewListObj( interp, NULL, 0);
	} else {
		list = NULL;
		index = 3;
	}
	










#define GET_ELEM(v)		({ lastobj = (v); 																\
										if(list) Jim_ListAppendElement( interp, list, lastobj);			\
										else Jim_SetVariable( interp, argv[index++], lastobj); })
	
	offs = 0;
	for( const char* t = typetag; *t;) {
		if(!list && index >= argc) {
................................................................................
err_structs_unsupported:
	Jim_SetResultFormatted( interp, "%#s: structs not supported", argv0);
	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];
	struct jimff_context* ctx = Jim_CmdPrivData(interp);
	int packed = 0;
................................................................................
	if(argc == 4 && len > 1) {
		list = argv[3];
		index = 0;
	} else {
		list = NULL;
		index = 3;
	}






	
#define GET_ARG()			(lastobj = (list ? Jim_ListGetIndex( interp, list, index++) : argv[index++]))
	
	offs = 0;
	for( const char* t = typetag; *t;) {
		if(list) {
			if(index >= Jim_ListLength( interp, list)) goto err_not_enough_args;
................................................................................
	Jim_SetResultFormatted( interp, "%#s: not enough arguments for typetag \"%#s\"", argv0, argv[2]);
	return JIM_ERR;
	
err_too_many_args:
	Jim_SetResultFormatted( interp, "%#s: too many arguments for typetag \"%#s\"", argv0, argv[2]);
	return JIM_ERR;
	

#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);												\
		if(name ## _start < (ptr)) goto err_offset_oob;									\
		if(name ## _stop > (ptr)) {																\