client: Fix loading of extensions (extensions were loaded twice; one instance for the sandbox and one for other extensions, which broke things)

This commit is contained in:
Perttu Ahola 2014-10-04 19:48:31 +03:00
parent 79255e301f
commit f9d2e825cc
3 changed files with 15 additions and 5 deletions

View File

@ -3,8 +3,14 @@
-- Copyright 2014 Perttu Ahola <celeron55@gmail.com>
local log = buildat.Logger("__client/extensions")
function __buildat_load_extension(name)
log:debug("__buildat_load_extension(\""..name.."\")")
-- Extension interfaces, indexed by extension name
local loaded_extensions = {}
function __buildat_require_extension(name)
log:debug("__buildat_require_extension(\""..name.."\")")
if loaded_extensions[name] then
return loaded_extensions[name]
end
local path = __buildat_extension_path(name).."/init.lua"
local script, err = loadfile(path)
if script == nil then
@ -16,6 +22,7 @@ function __buildat_load_extension(name)
log:error("Extension returned nil: "..name.." at "..path)
return nil
end
loaded_extensions[name] = interface
return interface
end
@ -27,7 +34,7 @@ function require(name)
log:debug("require called with name=\""..name.."\"")
local m = string.match(name, '^buildat/extension/([a-zA-Z0-9_]+)$')
if m then
return __buildat_load_extension(m)
return __buildat_require_extension(m)
end
return old_require(name)
end

View File

@ -58,7 +58,7 @@ __buildat_sandbox_environment.require = function(name)
-- Allow loading extensions
local m = string.match(name, '^buildat/extension/([a-zA-Z0-9_]+)$')
if m then
local unsafe = __buildat_load_extension(m)
local unsafe = __buildat_require_extension(m)
if unsafe == nil then
error("require: Cannot load extension: \""..m.."\"")
end

View File

@ -213,8 +213,9 @@ setmetatable(Safe, {
local sandbox_callback_to_global_function_name = {}
local next_sandbox_global_function_i = 1
log:error("FOO!")
function Safe.SubscribeToEvent(x, y, z)
log:debug("Safe.SubscribeToEvent("..dump(x)..", "..dump(y)..", "..dump(z)..")")
local object = x
local sub_event_type = y
local callback = z
@ -290,10 +291,12 @@ function Safe.SubscribeToEvent(x, y, z)
else
SubscribeToEvent(sub_event_type, global_callback_name)
end
log:debug("-> global_callback_name="..dump(global_callback_name))
return global_callback_name
end
function Safe.UnsubscribeFromEvent(sub_event_type, cb_name)
log:debug("Safe.UnsubscribeFromEvent("..dump(sub_event_type)..", "..dump(cb_name)..")")
UnsubscribeFromEvent(sub_event_type, cb_name)
-- TODO: Delete the generated global callback
end