LUA_INLINE int lua_compare(lua_State* L, int index1, int index2, int op) { if (op == LUA_OPEQ) { return lua_equal(L, index1, index2); } else if (op == LUA_OPLT) { return lua_lessthan(L, index1, index2); } else if (op == LUA_OPLE) { return lua_lessthan(L, index1, index2) || lua_equal(L, index1, index2); } else { return 0; } }
LUA_API int lua_compare(lua_State *L, int idx1, int idx2, int op) { switch(op) { case LUA_OPEQ: return lua_equal(L,idx1,idx2); case LUA_OPLT: return lua_lessthan(L, idx1, idx2); case LUA_OPLE: return !lua_lessthan(L, idx2, idx1); } assert(0); return 0; }
int lessthan(lua_State * L, int idx1, int idx2) { #if LUA_VERSION_NUM < 502 return lua_lessthan(L, idx1, idx2); #else return lua_compare(L, idx1, idx2, LUA_OPLT); #endif }
// __lt (less-than) metamethod handler // Provided so that a list of localised strings can be sorted, which is used // to create nice user-interface listing of strings. Note that this will mean // that persist->change language->depersist will result in a "random" ordering // of the resulting list, but this is generally acceptable. static int l_str_lt(lua_State *L) { luaL_checkany(L, 1); luaL_checkany(L, 2); lua_settop(L, 2); aux_push_weak_table(L, 0); lua_pushvalue(L, 1); lua_rawget(L, 3); lua_pushvalue(L, 2); lua_rawget(L, 3); lua_pushboolean(L, lua_lessthan(L, 4, 5)); return 1; }
static int sort_comp (lua_State *L, int a, int b) { if (!lua_isnil(L, 2)) { /* function? */ int res; lua_pushvalue(L, 2); lua_pushvalue(L, a-1); /* -1 to compensate function */ lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ lua_call(L, 2, 1); res = lua_toboolean(L, -1); lua_pop(L, 1); return res; } else /* a < b? */ return lua_lessthan(L, a, b); }
static int sort_comp (lua_State *L, int a, int b) { /* WARNING: the caller (auxsort) must ensure stack space */ if (!lua_isnil(L, 2)) { /* function? */ int res; lua_pushvalue(L, 2); lua_pushvalue(L, a-1); /* -1 to compensate function */ lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ lua_rawcall(L, 2, 1); res = !lua_isnil(L, -1); lua_pop(L, 1); return res; } else /* a < b? */ return lua_lessthan(L, a, b); }
int lua_compare (lua_State *L, int idx1, int idx2, int op) { int result = 0; switch (op) { case LUA_OPEQ: return lua_equal(L, idx1, idx2); case LUA_OPLT: return lua_lessthan(L, idx1, idx2); case LUA_OPLE: luaL_checkstack(L, 5, "not enough stack slots"); idx1 = lua_absindex(L, idx1); idx2 = lua_absindex(L, idx2); lua_pushvalue(L, idx1); lua_pushvalue(L, idx2); compat52_call_lua(L, compat52_compare_code, sizeof(compat52_compare_code)-1, 2, 1); result = lua_toboolean(L, -1); lua_pop(L, 1); return result; default: luaL_error(L, "invalid 'op' argument for lua_compare"); } return 0; }
JNIEXPORT jint JNICALL Java_m_lua_Lua_lessthan (JNIEnv* env, jobject thiz, jlong nativeObj, jint idx1, jint idx2) { pushJNIEnv(env, nativeObj); return (jint) lua_lessthan((lua_State*) nativeObj, idx1, idx2); }