static int luaC_array__index(lua_State *L) { Array *A = lunum_checkarray1(L, 1); // Figure out what is the format of the input index. If it's a number or a // table of numbers, then pass it along to _get_index. If it's an array of bools, // then use it as a mask. // --------------------------------------------------------------------------- if (lunum_hasmetatable(L, 2, "array")) { Array *M = lunum_checkarray1(L, 2); if (M->dtype != ARRAY_TYPE_BOOL) { return luaL_error(L, "index array must be of type bool"); } Array B = array_new_from_mask(A, M); lunum_pusharray1(L, &B); return 1; } /* try to index into array */ int success; const size_t m = _get_index(L, A, &success); if (success) { _push_value(L, A->dtype, (char*)A->data + array_sizeof(A->dtype)*m); return 1; } /* check metatable */ lua_getmetatable(L, 1); lua_pushvalue(L, 2); if (lua_gettable(L, -2) != LUA_TNIL) { return 1; } return 0; }
int luaC_array__index(lua_State *L) { struct Array *A = lunum_checkarray1(L, 1); // Figure out what is the format of the input index. If it's a number or a // table of numbers, then pass it along to _get_index. If it's a table of // tables or numbers, then assume it's a slice. If it's an array of bools, // then use it as a mask. // --------------------------------------------------------------------------- if (lunum_hasmetatable(L, 2, "array")) { struct Array *M = lunum_checkarray1(L, 2); if (M->dtype != ARRAY_TYPE_BOOL) { luaL_error(L, "index array must be of type bool"); } struct Array B = array_new_from_mask(A, M); lunum_pusharray1(L, &B); return 1; } else if (lua_type(L, 2) == LUA_TTABLE || lua_type(L, 2) == LUA_TSTRING) { lua_getglobal(L, "lunum"); lua_getfield(L, -1, "__build_slice"); lua_remove(L, -2); lua_pushvalue(L, 1); lua_pushvalue(L, 2); lua_call(L, 2, 1); return 1; } const int m = _get_index(L, A); _push_value(L, A->dtype, (char*)A->data + array_sizeof(A->dtype)*m); return 1; }