aboutsummaryrefslogtreecommitdiff
path: root/tests/manual_register.lua
blob: 52cc4ed28016e29773d65322cb1ec0f912747b81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
local RequireAModuleThatExportsGlobalFunctions = function(type_)
	-- grab some module that exports C functions, this is good enough for our purpose
	local due = require "deep_userdata_example"
	-- make one of these a global
	GlobalFunc = due.get_deep_count
	-- we need to register it so that it is transferable
	local lanes = require "lanes"
	lanes.register( "GlobalFunc", GlobalFunc)
	print(type_, "RequireAModuleThatExportsGlobalFunctions done:", lanes.nameof(GlobalFunc))
end


local lanes = require "lanes".configure{on_state_create = RequireAModuleThatExportsGlobalFunctions}

-- load some module that adds C functions to the global space
RequireAModuleThatExportsGlobalFunctions("main")

local GlobalFuncUpval = GlobalFunc

-- a lane that makes use of the above global
local f = function()
	GlobalFuncUpval("foo")
	print("f done:", lanes.nameof(GlobalFuncUpval))
	return 33
end


local g = lanes.gen( "*", f)

-- generate a lane, this will transfer f, which pulls GlobalFunc.
local h = g()
assert(h[1] == 33)