move events library implementation to core:internal/events & disable access to core:internal modules outside of stdlib

This commit is contained in:
MihailRis 2025-08-08 00:29:12 +03:00
parent 5785b9fdda
commit 3760fb86f7
6 changed files with 67 additions and 59 deletions

View File

@ -256,7 +256,7 @@ function open_file_in_editor(filename, line, mutable)
end
function on_open(mode)
registry = require "core:internal/scripts_registry"
registry = __vc_scripts_registry
document.codePanel:setInterval(200, refresh_file_title)

View File

@ -43,11 +43,8 @@ function build_files_list(filenames, highlighted_part)
end
end
function on_open(mode)
registry = require "core:internal/scripts_registry"
local files_list = document.filesList
function on_open()
registry = __vc_scripts_registry
filenames = registry.filenames
table.sort(filenames)
build_files_list(filenames)

View File

@ -0,0 +1,48 @@
local events = {
handlers = {}
}
function events.on(event, func)
if events.handlers[event] == nil then
events.handlers[event] = {}
end
table.insert(events.handlers[event], func)
end
function events.reset(event, func)
if func == nil then
events.handlers[event] = nil
else
events.handlers[event] = {func}
end
end
function events.remove_by_prefix(prefix)
for name, handlers in pairs(events.handlers) do
local actualname = name
if type(name) == 'table' then
actualname = name[1]
end
if actualname:sub(1, #prefix+1) == prefix..':' then
events.handlers[actualname] = nil
end
end
end
function events.emit(event, ...)
local result = nil
local handlers = events.handlers[event]
if handlers == nil then
return nil
end
for _, func in ipairs(handlers) do
local status, newres = xpcall(func, __vc__error, ...)
if not status then
debug.error("error in event ("..event..") handler: "..newres)
else
result = result or newres
end
end
return result
end
return events

View File

@ -62,5 +62,4 @@ end
cache_names(block)
cache_names(item)
local scripts_registry = require "core:internal/scripts_registry"
scripts_registry.build_registry()
__vc_scripts_registry.build_registry()

View File

@ -169,61 +169,12 @@ function inventory.set_description(invid, slot, description)
inventory.set_data(invid, slot, "description", description)
end
------------------------------------------------
------------------- Events ---------------------
------------------------------------------------
events = {
handlers = {}
}
function events.on(event, func)
if events.handlers[event] == nil then
events.handlers[event] = {}
end
table.insert(events.handlers[event], func)
end
function events.reset(event, func)
if func == nil then
events.handlers[event] = nil
else
events.handlers[event] = {func}
end
end
function events.remove_by_prefix(prefix)
for name, handlers in pairs(events.handlers) do
local actualname = name
if type(name) == 'table' then
actualname = name[1]
end
if actualname:sub(1, #prefix+1) == prefix..':' then
events.handlers[actualname] = nil
end
end
end
events = require "core:internal/events"
function pack.unload(prefix)
events.remove_by_prefix(prefix)
end
function events.emit(event, ...)
local result = nil
local handlers = events.handlers[event]
if handlers == nil then
return nil
end
for _, func in ipairs(handlers) do
local status, newres = xpcall(func, __vc__error, ...)
if not status then
debug.error("error in event ("..event..") handler: "..newres)
else
result = result or newres
end
end
return result
end
gui_util = require "core:internal/gui_util"
Document = gui_util.Document
@ -319,11 +270,12 @@ entities.get_all = function(uids)
end
local bytearray = require "core:internal/bytearray"
Bytearray = bytearray.FFIBytearray
Bytearray_as_string = bytearray.FFIBytearray_as_string
Bytearray_construct = function(...) return Bytearray(...) end
__vc_scripts_registry = require "core:internal/scripts_registry"
file.open = require "core:internal/stream_providers/file"
file.open_named_pipe = require "core:internal/stream_providers/named_pipe"
@ -342,6 +294,7 @@ else
end
ffi = nil
__vc_lock_internal_modules()
math.randomseed(time.uptime() * 1536227939)

View File

@ -550,6 +550,8 @@ function reload_module(name)
end
end
local internal_locked = false
-- Load script with caching
--
-- path - script path `contentpack:filename`.
@ -559,6 +561,11 @@ end
function __load_script(path, nocache)
local packname, filename = parse_path(path)
if internal_locked and (packname == "res" or packname == "core")
and filename:starts_with("modules/internal") then
error("access to core:internal modules outside of [core]")
end
-- __cached_scripts used in condition because cached result may be nil
if not nocache and __cached_scripts[path] ~= nil then
return package.loaded[path]
@ -579,6 +586,10 @@ function __load_script(path, nocache)
return result
end
function __vc_lock_internal_modules()
internal_locked = true
end
function require(path)
if not string.find(path, ':') then
local prefix, _ = parse_path(_debug_getinfo(2).source)