2014-09-23 18:06:30 +03:00
|
|
|
-- Buildat: extension/urho3d
|
2014-09-22 21:42:00 +03:00
|
|
|
-- http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
-- Copyright 2014 Perttu Ahola <celeron55@gmail.com>
|
2014-09-23 18:06:30 +03:00
|
|
|
local log = buildat.Logger("extension/urho3d")
|
|
|
|
local dump = buildat.dump
|
|
|
|
local M = {safe = {}}
|
2014-09-22 21:42:00 +03:00
|
|
|
|
|
|
|
-- Set every plain value in global environment to the sandbox
|
|
|
|
-- ...it's maybe safe enough...
|
|
|
|
for k, v in pairs(_G) do
|
|
|
|
if type(v) == 'number' or type(v) == 'string' then
|
|
|
|
--log:info("Setting sandbox["..k.."] = "..buildat.dump(v))
|
2014-09-23 18:06:30 +03:00
|
|
|
M.safe[k] = _G[k]
|
2014-09-22 21:42:00 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- TODO: Require explicit whitelisting of classes, method/function argument and
|
|
|
|
-- property types
|
|
|
|
|
|
|
|
local safe_globals = {
|
|
|
|
-- Instances
|
|
|
|
"cache",
|
|
|
|
"ui",
|
|
|
|
"renderer",
|
|
|
|
"input",
|
|
|
|
-- Types
|
|
|
|
"Scene",
|
|
|
|
"Text",
|
|
|
|
"Color",
|
|
|
|
"Vector3",
|
|
|
|
"Quaternion",
|
|
|
|
"Viewport",
|
|
|
|
"CustomGeometry",
|
|
|
|
"Texture",
|
|
|
|
"Material",
|
|
|
|
-- Functions
|
|
|
|
"Random",
|
|
|
|
"Clamp",
|
|
|
|
-- WTF properties
|
|
|
|
"KEY_W",
|
|
|
|
"KEY_S",
|
|
|
|
"KEY_A",
|
|
|
|
"KEY_D",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v in ipairs(safe_globals) do
|
2014-09-23 18:06:30 +03:00
|
|
|
M.safe[v] = _G[v]
|
2014-09-22 21:42:00 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
local sandbox_function_name_to_global_function_name = {}
|
|
|
|
local next_global_function_i = 1
|
|
|
|
|
2014-09-23 18:06:30 +03:00
|
|
|
function M.safe.SubscribeToEvent(event_name, function_name)
|
2014-09-23 21:11:41 +03:00
|
|
|
local caller_environment = getfenv(2)
|
|
|
|
local callback = caller_environment[function_name]
|
|
|
|
if type(callback) ~= 'function' then
|
2014-09-23 18:06:30 +03:00
|
|
|
error("SubscribeToEvent(): '"..function_name..
|
2014-09-23 21:11:41 +03:00
|
|
|
"' is not a global function in current sandbox environment")
|
2014-09-22 21:42:00 +03:00
|
|
|
end
|
|
|
|
local global_function_i = next_global_function_i
|
|
|
|
next_global_function_i = next_global_function_i + 1
|
|
|
|
local global_function_name = "__buildat_sandbox_callback_"..global_function_i
|
|
|
|
sandbox_function_name_to_global_function_name[function_name] = global_function_name
|
|
|
|
_G[global_function_name] = function(eventType, eventData)
|
|
|
|
local f = function()
|
2014-09-23 18:06:30 +03:00
|
|
|
callback(eventType, eventData)
|
2014-09-22 21:42:00 +03:00
|
|
|
end
|
|
|
|
__buildat_run_function_in_sandbox(f)
|
|
|
|
end
|
|
|
|
SubscribeToEvent(event_name, global_function_name)
|
|
|
|
end
|
2014-09-23 18:06:30 +03:00
|
|
|
|
|
|
|
return M
|