lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 17 March 2017 at 17:19, Sean Conner <sean@conman.org> wrote:
> It was thus said that the Great Laurent FAILLIE once stated:
>> Very interesting discussion, indeed :)
>
>> When I started with Lua, I made a parallel with the BASIC (the one from
>> 80', the real one, not VB with is far away vs original BASIC concept) :
>
>   Even the version in the 80s wasn't the "real one"---the real one would be
> BASIC from Dartmouth College in 1964.
>
>> Lua syntax is simple and accessible (for basic tasks), learning curve is
>> very fast and even non programmer can progress easily.
>
>> But I faced rapidly 2 pitfalls :- the 1st one is strings manipulation :
>> sooner, you need to master RegEx to do even simple tasks with strings and
>> I think it's a showstopper for beginners.
>
>
>> Lua is missing simple but
>> powerful string manipulation functions as the Basic has.
>
>   What?  All I recall of BASIC string manipulations are LEFT$(), RIGHT$(),
> MID$() and STRING$(), all of which can be replaced with string.sub() in Lua.
> I recall those being pretty typical of the time.
>
>> Finally, I succeeded and I'm "proud" of results :
>> https://github.com/destroyedlolo/Selene and
>> https://github.com/destroyedlolo/Marcel which are matching my
>> objectives.As you can see, I'm sticking with 5.1 ... because
>> luaL_register() has been deprecated and I was unable to find a clear way
>> to migrate code like
>>
>> | static const struct luaL_reg SelSurfaceLib [] = { |
>> |  {"BlittingFlagsConst", BlittingFlagsConst}, |
>> | ... , |
>> |  {"CircleQuarterConst", CircleQuarterConst}, |
>> |  {"create", createsurface}, |
>> |  {NULL, NULL} |
>> | }; |
>>
>> | static const struct luaL_reg SelSurfaceM [] = { |
>> |  {"Release", SurfaceRelease}, |
>> |  {"destroy", SurfaceRelease}, /* Alias */ |
>> | ... |
>> |  {"restore", SurfaceRestore}, |
>> |  {NULL, NULL} |
>> | }; |
>
>> | void _include_SelSurface( lua_State *L ){ |
>
>          ^ Just a note---identifiers starting with a leading underscore are
> reserved for the C compiler, not for user code.  That aside ...
>
>> |  luaL_newmetatable(L, "SelSurface"); |
>> |  lua_pushstring(L, "__index"); |
>> |  lua_pushvalue(L, -2); |
>> |  lua_settable(L, -3); /* metatable.__index = metatable */ |
>> |  luaL_register(L, NULL, SelSurfaceM); |
>> |  luaL_register(L,"SelSurface", SelSurfaceLib); |
>> }
>>
>> to newer Lua :(
>
>         void include_SelSurface(lua_State *L)
>         {
>           luaL_newmetatable(L,"SelSurface");
>           lua_pushvalue(L,-1);
>           lua_setfield(L,-2,"__index");
>           luaL_setfuncs(L,SelSurfaceM,0);
>           luaL_newlib(L,SelSurfaceLib);
>           lua_setglobal(L,"SelSurface");
>         }

And if you're making a separate module to be used with require(), name
it luaopen_<module name>, compile it as a shared library, and avoid
creating a global:

void luaopen_SelSurface(lua_State *L)
{
   luaL_newmetatable(L,"SelSurface");
   lua_pushvalue(L,-1);
   lua_setfield(L,-2,"__index");
   luaL_setfuncs(L,SelSurfaceM,0);
   luaL_newlib(L,SelSurfaceLib);
   return 1
}

and then in Lua,

   local SelSurface = require("SelSurface")

-- Hisham