static int ktap_lib_pairs(ktap_state *ks) { ktap_value *v = kp_arg(ks, 1); ktap_table *t; if (G(ks)->mainthread != ks) { kp_error(ks, "only mainthread can call table pairs\n"); return -1; } if (ttistable(v)) { t = hvalue(v); } else if (ttisaggrtable(v)) { t = kp_aggrtable_synthesis(ks, ahvalue(v)); } else if (isnil(v)) { kp_error(ks, "table is nil in pairs\n"); return 0; } else { kp_error(ks, "wrong argument for pairs\n"); return 0; } setfvalue(ks->top++, ktap_lib_next); sethvalue(ks->top++, t); setnilvalue(ks->top++); return 3; }
/* function for register library */ void kp_register_lib(ktap_state *ks, const char *libname, const ktap_Reg *funcs) { int i; ktap_table *target_tbl; const ktap_value *gt = kp_table_getint(hvalue(&G(ks)->registry), KTAP_RIDX_GLOBALS); /* lib is null when register baselib function */ if (libname == NULL) target_tbl = hvalue(gt); else { ktap_value key, val; target_tbl = kp_table_new(ks); kp_table_resize(ks, target_tbl, 0, sizeof(*funcs) / sizeof(ktap_Reg)); setsvalue(&key, kp_tstring_new(ks, libname)); sethvalue(&val, target_tbl); kp_table_setvalue(ks, hvalue(gt), &key, &val); } for (i = 0; funcs[i].name != NULL; i++) { ktap_value func_name, cl; setsvalue(&func_name, kp_tstring_new(ks, funcs[i].name)); setfvalue(&cl, funcs[i].func); kp_table_setvalue(ks, target_tbl, &func_name, &cl); cfunction_cache_add(ks, &cl); } }
static int ktap_lib_pairs(ktap_State *ks) { Table *t = hvalue(GetArg(ks, 1)); setfvalue(ks->top++, ktap_lib_next); sethvalue(ks->top++, t); setnilvalue(ks->top++); return 3; }
/*選択*/ void selection(char gene[POOLSIZE][RULESIZE][LOCUSSIZE],char midgene[POOLSIZE*2][RULESIZE][LOCUSSIZE], char lines[MAXLINES][LINESIZE],int lineno) { int i,j,k ; int fvalue[POOLSIZE*2] ;//midgeneプールの適応度 int sumf ;//適応度の合計値 int point=0 ;//ルーレットのスタート場所 int midpoint ; setfvalue(midgene,fvalue,lines,lineno) ;//fvalueに値をセット sumf=sumupfvalue(fvalue) ;//適応度の合計値を計算 for(i=0;i<POOLSIZE;++i){//ルーレットにしたがって選択 midpoint=roulette(fvalue,sumf,point) ; for(j=0;j<RULESIZE;++j) for(k=0;k<LOCUSSIZE;++k) gene[i][j][k]=midgene[midpoint][j][k]; } }
LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { lua_lock(L); if (n == 0) { setfvalue(L->top, fn); } else { Closure *cl; api_checknelems(L, n); api_check(L, n <= MAXUPVAL, "upvalue index too large"); luaC_checkGC(L); cl = luaF_newCclosure(L, n); cl->c.f = fn; L->top -= n; while (n--) setobj2n(L, &cl->c.upvalue[n], L->top + n); setclCvalue(L, L->top, cl); } api_incr_top(L); lua_unlock(L); }
LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { int status; Closure *f = NULL; CallInfo *ci = NULL; void *plight = NULL; lua_lock(L); if (*what == '>') { StkId func = L->top - 1; luai_apicheck(L, ttisfunction(func) || ttislightfunction(func)); what++; /* skip the '>' */ if (ttisfunction(func)) f = clvalue(func); else plight = fvalue(func); L->top--; /* pop function */ } else if (ar->i_ci != 0) { /* no tail call? */ ci = L->base_ci + ar->i_ci; lua_assert(ttisfunction(ci->func) || ttislightfunction(ci->func)); if (ttisfunction(ci->func)) f = clvalue(ci->func); else plight = fvalue(ci->func); } status = auxgetinfo(L, what, ar, f, plight, ci); if (strchr(what, 'f')) { if (f != NULL) setclvalue(L, L->top, f) else if (plight != NULL) setfvalue(L->top, plight) else setnilvalue(L->top); incr_top(L); } if (strchr(what, 'L')) collectvalidlines(L, f); lua_unlock(L); return status; }