lua-users home
lua-l archive

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

Stefan <> 于2020年4月17日周五 上午2:18写道:
> This sounds interesting!
> I've added your modified Lua 5.3 to the test repository:

Thanks for your interresting.

I patched lua 5.4 yesterday with a little different way. (Because lua
5.4 use the whole 8bits of ->marked)

You may have a look here :

There are 5 parts:

1. Add a shared status to the GC object, it can prevent gc cycle to
mark the object shared from outside.
2. Add a small thread lib for multithread environment, becuase we may
run multi lua vm in different os threads.
3. Add an id in TString
( )
4. Add lua_clonefunction  to share function prototype from outside.
5. Add a small test case.

You can use it as the regular lua, but I provied some new C APIs :

LUA_API void (lua_clonefunction) (lua_State *L, const void * fp);
LUA_API void (lua_sharefunction) (lua_State *L, int index);
LUA_API void (lua_sharestring) (lua_State *L, int index);
LUA_API void (lua_clonetable) (lua_State *L, const void * t);

See test/* for the detail.

local sharetable = require "sharetable"
local multivm = require "multivm"
local vm =
local testp, tablep = vm:dostring [[
  local sharetable = require "sharetable"
  _G.test = { "Hello", "World" }
  local testp = sharetable.mark_shared(_G.test)
  local tablep = sharetable.mark_shared(_G.table)
  return testp, tablep

local shared_test = sharetable.clone(testp)
local shared_table = sharetable.clone(tablep)

-- Use table.unpack in vm to unpack _G.test in vm.

If you use multiple lua VMs in the same process, you can access the
table/function without upvalues from outside (in the other VM)

For example, you can share the base library table
table/string/utf8/... by `lua_clonetable`, it's much faster than
luaL_openlibs . And if you loaded a .lua file from a vm before,
loading the same file from other VM would be much faster , too.