lua-users home
lua-l archive

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


Roberto Ierusalimschy <roberto@inf.puc-rio.br> 于2020年12月23日周三 下午10:36写道:
> This has been in our list of improvements for some time, but the
> details still need some iron. For instance, what to do if one

Yes. I agree.

> tries to create a free-defined string which is too short?
> Raise an error? Create a short string and call the free function
> immediately? Would that free function need some kind of user value?

My opinion is:

1. Add a new sub-type of string, it can be any size, even it's short.
2. When we use a short user-string as a table key, (param for
luaH_get/luaH_set), we create/find an equivalent short string instead.

To support user-defined string, we don't need to specify the free
function for each user-string.

We may need to add two APIs :

   void (*lua_Free)(void *ud, void *obj);
   void lua_setfreef(lua_State *L, lua_Free f, void *ud);

   lua_pushustring(lua_State *L, const char * s, size_t l, void *obj);

If obj == NULL, we use lua_Alloc to free user-string object. So that
we can allocate the string object by lua_Alloc before pushing it into
VM manually.

If obj is specified, Lua_Free will free the user-string object by the
`void *obj` rather than the `const char * s`, it's more flexible.

-- 
http://blog.codingnow.com