Skip to content

Commit

Permalink
Msgpack: Userdata packer. Huge hack.
Browse files Browse the repository at this point in the history
  • Loading branch information
vifino committed Feb 15, 2016
1 parent 58d69d7 commit a526bce
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 7 deletions.
2 changes: 2 additions & 0 deletions builtin/ConfGlue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ args = args and luar.slice2table(args)
srv = srv or require("wrappers.srv")

require("wrappers.mw")

require("wrappers.init")
16 changes: 16 additions & 0 deletions builtin/libs/msgpack.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ msgpack.packers['table'] = function(buffer, t)
end
end

-- Userdata
msgpack.packers['userdata'] = function(buffer, ud)
-- Hack. A big one, too.
local idpool = "tmp:msgpack:ud-tmp"
kvstore.inc(idpool)
local numid = kvstore.get(idpool)
local id = idpool..":"..tostring(numid)
kvstore.set(id, ud)
msgpack.packers['ext'](buffer, 43, tostring(numid))
end

-- Unpacker for both
msgpack.build_ext = function (tag, data)
if tag == 7 then -- Function
Expand All @@ -49,6 +60,11 @@ msgpack.build_ext = function (tag, data)
local _, t = f()
local _, mt = f()
return setmetatable(t, mt)
elseif tag == 43 then -- Userdata. Big hack.
local id = "tmp:msgpack:ud-tmp:"..data
local ud = kvstore.get(id)
kvstore.del(id)
return ud
end
end

Expand Down
2 changes: 1 addition & 1 deletion builtin/libs/thread.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function thread.spawn(code, bindings, buffer, dontusemsgpack)
elseif type(code) == "string" then
fn, err = loadstring(code)
if err then
error(err)
error(err, 0)
end
end
local buffer = tonumber(buffer) or -1
Expand Down
2 changes: 2 additions & 0 deletions builtin/libs/wrappers/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Init Glue for Config State
kvstore.set("tmp:msgpack:ud-tmp", 0)
35 changes: 29 additions & 6 deletions modules/glue/generated_lua.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions tests/thread.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
thread = require("thread")
local a = "test"
thread.spawn(function()
print(a)
end)
os.sleep(10)

0 comments on commit a526bce

Please sign in to comment.