3 ms·
I'm guessing the code was intended to be equivalent to: lua_newtable(L); lua_pushstring(L, "five"); lua_pushnumber(L, 5); lua_settable(L, -3);
by omaranto 8y ago
I'm guessing the code was intended to be equivalent to:
lua_newtable(L);
lua_pushstring(L, "five");
lua_pushnumber(L, 5);
lua_settable(L, -3);
- wahern 8y agoAh, I see. So it's more a preference for a richer standard library, like, lua_newtable(L); luaL_setfieldi(L, -1, "five", 5); rather than having to roll your own. Part of the function of the stack protocol is to minimize the number of unique functions needed to load and store values across language boundaries. So there's a single [low-level] function for communicating a number value in a type-safe manner, rather than a multitude of functions--one routine for setting a number value for a table string key, another for a number value for a table number key, another for a string value for a table number key, etc. The permutations explode, and unless the host language has some sort of generics capability then it adds significant complexity both to the interface and to implementation maintenance. (See Perl XS.) Alternatively you can reduce the number of permuted interfaces by using printf-style formatting strings and va_lists, but that's not type safe. (See Py_BuildValue.) Notably, Lua 5.3 removed lua_pushunsigned, lua_tounsigned, luaL_checkint, luaL_checklong, and many similar routines as there was never an end to the number of precomposed type coercing, constraint checking auxiliary routines people demanded. They decided that the only way to win that game was to not play at all. But with C11 _Generic and a dash of compiler extension magic you can actually implement a richer set of type agnostic (but type safe) numeric load/store interfaces in just a handful of auxiliary routines. For better or worse, though, Lua doesn't target C11 nor wish to depend on compiler extensions. "Batteries not included" is a whole 'nother debate in the Lua universe.