static int math_sqrt (lua_State *L) { lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); return 1; }
static int math_exp (lua_State *L) { lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); return 1; }
static int math_floor (lua_State *L) { lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1))); return 1; }
static int math_fmod (lua_State *L) { lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); return 1; }
static int math_atan2 (lua_State *L) { lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1), luaL_checknumber(L, 2))); return 1; }
static int math_ceil (lua_State *L) { lua_pushnumber(L, l_mathop(ceil)(luaL_checknumber(L, 1))); return 1; }
static int math_ldexp (lua_State *L) { lua_Number x = luaL_checknumber(L, 1); int ep = luaL_checkint(L, 2); lua_pushnumber(L, l_mathop(ldexp)(x, ep)); return 1; }
void Runtime::InitFunctions() { #define ADDFUNCTION(function, ret, ...) { \ auto type = llvm::FunctionType::get(ret, {__VA_ARGS__}, false); \ auto funcptr = reinterpret_cast<void*>(function); \ AddFunction(STRINGFY2(function), type, funcptr); } auto tstate = types_["lua_State"]; auto tclosure = types_["LClosure"]; auto ttvalue = types_["TValue"]; auto tci = types_["CallInfo"]; auto ttable = types_["Table"]; auto ttstring = types_["TString"]; auto tupval = types_["UpVal"]; auto tluanumber = types_["lua_Number"]; auto tluanumberptr = llvm::PointerType::get(tluanumber, 0); auto tluainteger = types_["lua_Integer"]; auto tluaintegerptr = llvm::PointerType::get(tluainteger, 0); auto tvoid = llvm::Type::getVoidTy(context_); auto tint = MakeIntT(sizeof(int)); // LLL ADDFUNCTION(LLLNumMod, tluanumber, tluanumber, tluanumber); // Deprecated lll ADDFUNCTION(lll_checkcg, tvoid, tstate, tci, ttvalue); ADDFUNCTION(lll_newtable, ttable, tstate, ttvalue); ADDFUNCTION(lll_upvalbarrier, tvoid, tstate, tupval); ADDFUNCTION(lll_forprep, tvoid, tstate, ttvalue); ADDFUNCTION(lll_setlist, tvoid, tstate, ttvalue, tint, tint); ADDFUNCTION(lll_closure, tvoid, tstate, tclosure, ttvalue, ttvalue, tint); ADDFUNCTION(lll_checkstack, tvoid, tstate, tint); // math.h ADDFUNCTION(l_mathop(floor), tluanumber, tluanumber); ADDFUNCTION(l_mathop(pow), tluanumber, tluanumber, tluanumber); // ldo.h ADDFUNCTION(luaD_callnoyield, tvoid, tstate, ttvalue, tint); // lfunc.h ADDFUNCTION(luaF_close, tvoid, tstate, ttvalue); // lgc.h ADDFUNCTION(luaC_barrierback_, tvoid, tstate, ttable); // ltable.h ADDFUNCTION(luaH_getint, ttvalue, ttable, tluainteger); ADDFUNCTION(luaH_getshortstr, ttvalue, ttable, ttstring); ADDFUNCTION(luaH_getstr, ttvalue, ttable, ttstring); ADDFUNCTION(luaH_get, ttvalue, ttable, ttvalue); ADDFUNCTION(luaH_resize, tvoid, tstate, ttable, tint, tint); // ltm.h ADDFUNCTION(luaT_gettm, ttvalue, ttable, tint, ttstring); ADDFUNCTION(luaT_trybinTM, tvoid, tstate, ttvalue, ttvalue, ttvalue, tint); // lvm.h ADDFUNCTION(luaV_concat, tvoid, tstate, tint); ADDFUNCTION(luaV_div, tluainteger, tstate, tluainteger, tluainteger); ADDFUNCTION(luaV_equalobj, tint, tstate, ttvalue, ttvalue); ADDFUNCTION(luaV_finishget, tvoid, tstate, ttvalue, ttvalue, ttvalue, ttvalue); ADDFUNCTION(luaV_finishset, tvoid, tstate, ttvalue, ttvalue, ttvalue, ttvalue); ADDFUNCTION(luaV_lessequal, tint, tstate, ttvalue, ttvalue); ADDFUNCTION(luaV_lessthan, tint, tstate, ttvalue, ttvalue); ADDFUNCTION(luaV_mod, tluainteger, tstate, tluainteger, tluainteger); ADDFUNCTION(luaV_objlen, tvoid, tstate, ttvalue, ttvalue) ADDFUNCTION(luaV_shiftl, tluainteger, tluainteger, tluainteger); ADDFUNCTION(luaV_tointeger, tint, ttvalue, tluaintegerptr, tint); ADDFUNCTION(luaV_tonumber_, tint, ttvalue, tluanumberptr); }
static int math_log10 (lua_State *L) { lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); return 1; }
static int math_frexp (lua_State *L) { int e; lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); lua_pushinteger(L, e); return 2; }
static int math_atan (lua_State *L) { lua_Number y = luaL_checknumber(L, 1); lua_Number x = luaL_optnumber(L, 2, 1); lua_pushnumber(L, l_mathop(atan2)(y, x)); return 1; }
static int math_rad (lua_State *L) { lua_pushnumber(L, luaL_checknumber(L, 1) * (PI / l_mathop(180.0))); return 1; }
static int math_deg (lua_State *L) { lua_pushnumber(L, luaL_checknumber(L, 1) * (l_mathop(180.0) / PI)); return 1; }
static int math_tanh (lua_State *L) { lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); return 1; }
static int math_acos (lua_State *L) { lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); return 1; }
static int math_pow (lua_State *L) { lua_Number x = luaL_checknumber(L, 1); lua_Number y = luaL_checknumber(L, 2); lua_pushnumber(L, l_mathop(pow)(x, y)); return 1; }
/* ** Add integer part of 'x' to buffer and return new 'x' */ static lua_Number adddigit (char *buff, int n, lua_Number x) { lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */ int d = (int)dd; buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */ return x - dd; /* return what is left */ }