示例#1
0
文件: table.c 项目: eric-zhu/ktap
/*
 * main search function
 */
const Tvalue *kp_table_get(Table *t, const Tvalue *key)
{
	switch (ttype(key)) {
	case KTAP_TNIL:
		return ktap_nilobject;
	case KTAP_TSHRSTR:
		return kp_table_getstr(t, rawtsvalue(key));
	case KTAP_TNUMBER: {
		ktap_Number n = nvalue(key);
		int k = (int)n;
		if ((ktap_Number)k == nvalue(key)) /* index is int? */
			return kp_table_getint(t, k);  /* use specialized version */
		/* else go through */
	}
	default: {
		Node *n = mainposition(t, key);
		do {  /* check whether `key' is somewhere in the chain */
			if (rawequalobj(gkey(n), key))
				return gval(n);  /* that's it */
			else
				n = gnext(n);
		} while (n);

		return ktap_nilobject;
	}
	}
}
示例#2
0
文件: vm.c 项目: joelagnel/ktap
static int cfunction_cache_getindex(ktap_state *ks, ktap_value *fname)
{
	const ktap_value *gt = kp_table_getint(hvalue(&G(ks)->registry),
				KTAP_RIDX_GLOBALS);
	const ktap_value *cfunc;
	int nr, i;

	nr = G(ks)->nr_builtin_cfunction;
	cfunc = kp_table_get(hvalue(gt), fname);

	for (i = 0; i < nr; i++) {
		if (rawequalobj(&G(ks)->cfunction_tbl[i], cfunc))
			return i;
	}

	return -1;
}