mirror of
https://github.com/Reuh/anselme.git
synced 2025-10-27 16:49:31 +00:00
Add scoped function injections
This commit is contained in:
parent
c4fb2649c2
commit
428042c5d9
2 changed files with 39 additions and 10 deletions
19
anselme.lua
19
anselme.lua
|
|
@ -368,7 +368,7 @@ local vm_mt = {
|
|||
self.state.builtin_aliases["🏁"] = reached
|
||||
return self
|
||||
end,
|
||||
--- set some code that will be added at the start of every function defined after this is called
|
||||
--- set some code that will be added at the start of every non-scoped function defined after this is called
|
||||
-- nil to disable
|
||||
-- can typically be used to define variables for every function like 👁️
|
||||
-- return self
|
||||
|
|
@ -376,6 +376,13 @@ local vm_mt = {
|
|||
self.state.inject.function_start = code
|
||||
return self
|
||||
end,
|
||||
--- same as injectfunctionstart, but inject code at the start of every scoped function
|
||||
-- nil to disable
|
||||
-- return self
|
||||
injectscopedfunctionstart = function(self, code)
|
||||
self.state.inject.scoped_function_start = code
|
||||
return self
|
||||
end,
|
||||
--- same as injectfunctionstart, but inject code at the start of every checkpoint
|
||||
-- nil to disable
|
||||
-- return self
|
||||
|
|
@ -383,13 +390,20 @@ local vm_mt = {
|
|||
self.state.inject.checkpoint_start = code
|
||||
return self
|
||||
end,
|
||||
--- same as injectfunctionstart, but inject code at the end of every function
|
||||
--- same as injectfunctionstart, but inject code at the end of every non-scoped function
|
||||
-- nil to disable
|
||||
-- return self
|
||||
injectfunctionend = function(self, code)
|
||||
self.state.inject.function_end = code
|
||||
return self
|
||||
end,
|
||||
--- same as injectfunctionstart, but inject code at the end of every scoped function
|
||||
-- nil to disable
|
||||
-- return self
|
||||
injectscopedfunctionend = function(self, code)
|
||||
self.state.inject.scoped_function_end = code
|
||||
return self
|
||||
end,
|
||||
--- same as injectfunctionend, but inject code at the end of every checkpoint
|
||||
-- nil to disable
|
||||
-- return self
|
||||
|
|
@ -593,6 +607,7 @@ return setmetatable(anselme, {
|
|||
local state = {
|
||||
inject = {
|
||||
function_start = nil, function_end = nil,
|
||||
scoped_function_start = nil, scoped_function_end = nil,
|
||||
checkpoint_start = nil, checkpoint_end = nil
|
||||
},
|
||||
feature_flags = {
|
||||
|
|
|
|||
|
|
@ -199,6 +199,18 @@ local function parse_line(line, state, namespace)
|
|||
table.insert(line.children, 1, { content = ":🔖=()", source = line.source })
|
||||
end
|
||||
-- custom code injection
|
||||
if r.scoped then
|
||||
if state.inject.scoped_function_start then
|
||||
for i, ll in ipairs(state.inject.scoped_function_start) do
|
||||
table.insert(line.children, 1+i, ll)
|
||||
end
|
||||
end
|
||||
if state.inject.scoped_function_end then
|
||||
for _, ll in ipairs(state.inject.scoped_function_end) do
|
||||
table.insert(line.children, ll)
|
||||
end
|
||||
end
|
||||
else
|
||||
if state.inject.function_start then
|
||||
for i, ll in ipairs(state.inject.function_start) do
|
||||
table.insert(line.children, 1+i, ll)
|
||||
|
|
@ -209,6 +221,7 @@ local function parse_line(line, state, namespace)
|
|||
table.insert(line.children, ll)
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif r.type == "checkpoint" then
|
||||
-- define 🏁 variable
|
||||
local reached_alias = state.global_state.builtin_aliases["🏁"]
|
||||
|
|
@ -459,6 +472,7 @@ local function parse(state, s, name, source)
|
|||
local state_proxy = {
|
||||
inject = {
|
||||
function_start = nil, function_end = nil,
|
||||
scoped_function_start = nil, scoped_function_end = nil,
|
||||
checkpoint_start = nil, checkpoint_end = nil
|
||||
},
|
||||
aliases = setmetatable({}, { __index = state.aliases }),
|
||||
|
|
@ -480,7 +494,7 @@ local function parse(state, s, name, source)
|
|||
global_state = state
|
||||
}
|
||||
-- parse injects
|
||||
for _, inject in ipairs{"function_start", "function_end", "checkpoint_start", "checkpoint_end"} do
|
||||
for _, inject in ipairs{"function_start", "function_end", "scoped_function_start", "scoped_function_end", "checkpoint_start", "checkpoint_end"} do
|
||||
if state.inject[inject] then
|
||||
local inject_indented, err = parse_indented(state.inject[inject], nil, "injected "..inject:gsub("_", " "))
|
||||
if not inject_indented then return nil, err end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue