Check-in [239dbfb881]

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

Overview
Comment:simplified jimff_getcallback() and jimff_getlibrary(), also added update argument to update the object if string search succeeds
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:239dbfb8815848829dcd9ce2a6243ad8ed15c4d29af70e7fd16707a34291208c
User & Date: grable 2018-11-01 18:44:00
Context
2018-11-01
18:50
moved tests to own directory check-in: fdeca0a0a0 user: grable tags: trunk
18:44
simplified jimff_getcallback() and jimff_getlibrary(), also added update argument to update the object if string search succeeds check-in: 239dbfb881 user: grable tags: trunk
16:43
added exists command as a quicker and easier way to find symbols than info check-in: 4c5284048d user: grable tags: trunk
Changes

Changes to jimff-call.c.

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
				if(sym->typeinfo.pointersizes[numptrs] && sym->typeinfo.pointersizes[numptrs] != size) goto err_invalid_ptrsize;
				numptrs++;
				av_ptr( AV, void*, p);
				break;
			}
			
			case 'x': {
				struct jimff_callback* cb = jimff_getcallback( ctx, argv[arg]);
				if(!cb || !(cb->flags & JIMFF_FLAG_REGISTERED)) goto err_invalid_callback;
				//TODO: find a better way to compare callback typetags
				if(sym->typeinfo.callbacks[numcbs] && strcmp( cb->typeinfo.typetag, sym->typeinfo.callbacks[numcbs])) goto err_invalid_callback_typetag;
				numcbs++;
				av_ptr( AV, callback_t, cb->fn);
				break;
			}







|







123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
				if(sym->typeinfo.pointersizes[numptrs] && sym->typeinfo.pointersizes[numptrs] != size) goto err_invalid_ptrsize;
				numptrs++;
				av_ptr( AV, void*, p);
				break;
			}
			
			case 'x': {
				struct jimff_callback* cb = jimff_getcallback( ctx, argv[arg], 1);
				if(!cb || !(cb->flags & JIMFF_FLAG_REGISTERED)) goto err_invalid_callback;
				//TODO: find a better way to compare callback typetags
				if(sym->typeinfo.callbacks[numcbs] && strcmp( cb->typeinfo.typetag, sym->typeinfo.callbacks[numcbs])) goto err_invalid_callback_typetag;
				numcbs++;
				av_ptr( AV, callback_t, cb->fn);
				break;
			}

Changes to jimff-callback.c.

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
	
	cb->flags |= JIMFF_FLAG_REGISTERED;
	cb->command = command;
	Jim_IncrRefCount(command);	
	
	Jim_Obj* obj = Jim_NewObj(ctx->interp);
	obj->bytes = NULL;
	obj->typePtr = &jimff_callback_ObjType;
	OBJ_SET_CALLBACK(obj);
	OBJ_CALLBACK(obj) = cb;
	OBJ_CALLBACK_EPOCH(obj) = ctx->epoch;
	
	return obj;
}

//TODO: maybe add callback typechecking here
JIMFF_API struct jimff_callback* jimff_getcallback( struct jimff_context* ctx, Jim_Obj* obj) {
	assert( ctx );
	assert( obj );
	
#define PREFIXLEN	(sizeof(JIMFF_CALLBACK_OBJ_PREFIX) - 1)	
	
	struct jimff_callback* cb;
	int strl;
	const char* strp;

	if(OBJ_IS_CALLBACK(obj)) {
		if(OBJ_CALLBACK_EPOCH(obj) != ctx->epoch) return NULL;
		return OBJ_CALLBACK(obj);
	}

	//TODO: should we lookup both string representaions of a callbacks or at all?
	strp = Jim_GetString( obj, &strl);
	if(strl == 0) return NULL;
	cb = jimff_find_callback( ctx, strp);
	if(cb) goto got_callback;
	if(strl > PREFIXLEN && (cb = jimff_find_callback( ctx, strp + PREFIXLEN))) goto got_callback;
	return NULL;
	
got_callback:
	//TODO: should we update the jimobj to a callback objtype on success?

	Jim_FreeIntRep( ctx->interp, obj);
	OBJ_SET_CALLBACK(obj);
	OBJ_CALLBACK(obj) = cb;
	OBJ_CALLBACK_EPOCH(obj) = ctx->epoch;
	return cb;
	
#undef PREFIXLEN
}

/*
//
// helpers for calling script defined debug procs
//
static Jim_Obj* call_errorInfo( Jim_Interp* interp, Jim_Obj* msg, Jim_Obj* stacktrace) {
	Jim_Obj args[3] = { Jim_NewStringObj( interp, "errorInfo", -1), msg, stacktrace };
	if(Jim_EvalObjVector( interp, stacktrace ? 3 : 2, args) != JIM_OK) return interp->emptyObj;
	return Jim_GetResult(interp);
}

static Jim_Obj* call_stacktrace( Jim_Interp* interp) {
	Jim_Obj args[1] = { Jim_NewStringObj( interp, "stacktrace", -1) };
	if(Jim_EvalObjVector( interp, 1, args) != JIM_OK) return interp->emptyObj;
	return Jim_GetResult(interp);
}
*/

//NOTE: this function marshalls data between a callback and a jimtcl command
static void cb_callback( void* data, va_alist valist) {
	struct jimff_callback* cb = data;
	union jimff_result tmp;
	union jimff_result *result = NULL;
	Jim_Obj* jimresult;
	Jim_Obj* args[1 + cb->typeinfo.argc];	







<








|






<
<






<
<
<
|
<
<
|
<
<
<
>
|
|
|
|
<
|
<
<
<
<
<
<
<
<
<
<
|


<
<
<
<
<
<
<







36
37
38
39
40
41
42

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57


58
59
60
61
62
63



64


65



66
67
68
69
70

71










72
73
74







75
76
77
78
79
80
81
	
	cb->flags |= JIMFF_FLAG_REGISTERED;
	cb->command = command;
	Jim_IncrRefCount(command);	
	
	Jim_Obj* obj = Jim_NewObj(ctx->interp);
	obj->bytes = NULL;

	OBJ_SET_CALLBACK(obj);
	OBJ_CALLBACK(obj) = cb;
	OBJ_CALLBACK_EPOCH(obj) = ctx->epoch;
	
	return obj;
}

//TODO: maybe add callback typechecking here
JIMFF_API struct jimff_callback* jimff_getcallback( struct jimff_context* ctx, Jim_Obj* obj, int update) {
	assert( ctx );
	assert( obj );
	
#define PREFIXLEN	(sizeof(JIMFF_CALLBACK_OBJ_PREFIX) - 1)	
	
	struct jimff_callback* cb;



	if(OBJ_IS_CALLBACK(obj)) {
		if(OBJ_CALLBACK_EPOCH(obj) != ctx->epoch) return NULL;
		return OBJ_CALLBACK(obj);
	}




	cb = jimff_find_callback( ctx, Jim_String(obj));


	if(!cb) return NULL;



	if(update) {
		Jim_FreeIntRep( ctx->interp, obj);
		OBJ_SET_CALLBACK(obj);
		OBJ_CALLBACK(obj) = cb;
		OBJ_CALLBACK_EPOCH(obj) = ctx->epoch;

	}










	return cb;
}








//NOTE: this function marshalls data between a callback and a jimtcl command
static void cb_callback( void* data, va_alist valist) {
	struct jimff_callback* cb = data;
	union jimff_result tmp;
	union jimff_result *result = NULL;
	Jim_Obj* jimresult;
	Jim_Obj* args[1 + cb->typeinfo.argc];	

Changes to jimff-commands.c.

244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
...
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
....
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
	if(JIMFF_LIST_LEN(ctx->libs) == 0) {
		Jim_SetResultFormatted( interp, "%#s: cant import symbols, no libraries are loaded", argv0);
		return JIM_ERR;
	}
	
	// check for library
	if(argc == 4) {
		lib = jimff_getlibrary( ctx, argv[1]);
		if(!lib && Jim_CompareStringImmediate( interp, argv[1], JIMFF_SELFLIBRARY_NAME)) {
			lib = jimff_find_library( ctx, NULL);
		}
		if(!lib) {
			Jim_SetResultFormatted( interp, "%#s: library \"%#s\" not found", argv0, argv[1]);
			return JIM_ERR;
		}
................................................................................
	const char* name;
	struct jimff_callback* cb;
	Jim_Obj* obj;
	int update = 0;
	
	if(argc >= 2 && Jim_CompareStringImmediate( interp, argv[1], "-update")) {
		if(argc != 4) goto wrongnumargs;
		struct jimff_callback* cb = jimff_getcallback( ctx, argv[2]);
		if(!cb) goto err_invalid_callback;
		if(cb->command) Jim_DecrRefCount( interp, cb->command);
		Jim_IncrRefCount(argv[3]);
		cb->command = argv[3];
		Jim_SetResult( interp, argv[2]);
		return JIM_OK;
	}
................................................................................
		Jim_WrongNumArgs( interp, 1, argv,	"?-library | -symbol | -function | -variable | -callback? ?library? name");
		return JIM_ERR;
	}
	
	switch(mode) {
		case EXISTS_LIBRARY:
			if(argc != 2) goto wrongnumargs;
			Jim_SetResultBool( interp, jimff_getlibrary( ctx, argv[1]) != NULL);
			return JIM_OK;
		
		case EXISTS_SYMBOL:
		case EXISTS_FUNCTION:
		case EXISTS_VARIABLE:
			if(argc == 3) {
				lib = jimff_getlibrary( ctx, argv[1]);
				if(!lib) goto err_library_not_found;
				narg++;
			} else if(argc != 2) {
				goto wrongnumargs;
			}
			if(!lib) {
				for JIMFF_LIST_EACH( lib, ctx->libs) {







|







 







|







 







|






|







244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
...
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
....
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
	if(JIMFF_LIST_LEN(ctx->libs) == 0) {
		Jim_SetResultFormatted( interp, "%#s: cant import symbols, no libraries are loaded", argv0);
		return JIM_ERR;
	}
	
	// check for library
	if(argc == 4) {
		lib = jimff_getlibrary( ctx, argv[1], 1);
		if(!lib && Jim_CompareStringImmediate( interp, argv[1], JIMFF_SELFLIBRARY_NAME)) {
			lib = jimff_find_library( ctx, NULL);
		}
		if(!lib) {
			Jim_SetResultFormatted( interp, "%#s: library \"%#s\" not found", argv0, argv[1]);
			return JIM_ERR;
		}
................................................................................
	const char* name;
	struct jimff_callback* cb;
	Jim_Obj* obj;
	int update = 0;
	
	if(argc >= 2 && Jim_CompareStringImmediate( interp, argv[1], "-update")) {
		if(argc != 4) goto wrongnumargs;
		struct jimff_callback* cb = jimff_getcallback( ctx, argv[2], 1);
		if(!cb) goto err_invalid_callback;
		if(cb->command) Jim_DecrRefCount( interp, cb->command);
		Jim_IncrRefCount(argv[3]);
		cb->command = argv[3];
		Jim_SetResult( interp, argv[2]);
		return JIM_OK;
	}
................................................................................
		Jim_WrongNumArgs( interp, 1, argv,	"?-library | -symbol | -function | -variable | -callback? ?library? name");
		return JIM_ERR;
	}
	
	switch(mode) {
		case EXISTS_LIBRARY:
			if(argc != 2) goto wrongnumargs;
			Jim_SetResultBool( interp, jimff_getlibrary( ctx, argv[1], 1) != NULL);
			return JIM_OK;
		
		case EXISTS_SYMBOL:
		case EXISTS_FUNCTION:
		case EXISTS_VARIABLE:
			if(argc == 3) {
				lib = jimff_getlibrary( ctx, argv[1], 1);
				if(!lib) goto err_library_not_found;
				narg++;
			} else if(argc != 2) {
				goto wrongnumargs;
			}
			if(!lib) {
				for JIMFF_LIST_EACH( lib, ctx->libs) {

Changes to jimff-library.c.

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

60
61
62
63

64
65
66
67
	obj->bytes = NULL;
	OBJ_SET_LIBRARY(obj);
	OBJ_LIBRARY(obj) = lib;
	OBJ_LIBRARY_EPOCH(obj) = ctx->epoch;
	return obj;
}

JIMFF_API struct jimff_library* jimff_getlibrary( struct jimff_context* ctx, Jim_Obj* obj) {
	assert( ctx );
	assert( obj );
	
	struct jimff_library* lib;
	int strl;
	const char* strp;

	if(OBJ_IS_LIBRARY(obj)) {
		if(OBJ_LIBRARY_EPOCH(obj) != ctx->epoch) return NULL;
		return OBJ_LIBRARY(obj);
	}

	//TODO: should we lookup string the stringrep of libraries?
	strp = Jim_GetString( obj, &strl);
	if(strl == 0) return NULL;
	lib = jimff_find_library( ctx, strp);
	if(lib) goto got_library;
	return NULL;
	
got_library:
	//TODO: should we update the jimobj to a library objtype on success?

	Jim_FreeIntRep( ctx->interp, obj);
	OBJ_SET_LIBRARY(obj);
	OBJ_LIBRARY(obj) = lib;
	OBJ_LIBRARY_EPOCH(obj) = ctx->epoch;

	return lib;
	
#undef PREFIXLEN
}







|




<
<






<
<
<
|
<
|
<
<
<
>
|
|
|
|
>

|
<
<
31
32
33
34
35
36
37
38
39
40
41
42


43
44
45
46
47
48



49

50



51
52
53
54
55
56
57
58


	obj->bytes = NULL;
	OBJ_SET_LIBRARY(obj);
	OBJ_LIBRARY(obj) = lib;
	OBJ_LIBRARY_EPOCH(obj) = ctx->epoch;
	return obj;
}

JIMFF_API struct jimff_library* jimff_getlibrary( struct jimff_context* ctx, Jim_Obj* obj, int update) {
	assert( ctx );
	assert( obj );
	
	struct jimff_library* lib;



	if(OBJ_IS_LIBRARY(obj)) {
		if(OBJ_LIBRARY_EPOCH(obj) != ctx->epoch) return NULL;
		return OBJ_LIBRARY(obj);
	}




	lib = jimff_find_library( ctx, Jim_String(obj));

	if(!lib) return NULL;



	if(update) {
		Jim_FreeIntRep( ctx->interp, obj);
		OBJ_SET_LIBRARY(obj);
		OBJ_LIBRARY(obj) = lib;
		OBJ_LIBRARY_EPOCH(obj) = ctx->epoch;
	}
	return lib;
}