Skip to content

Commit bf28004

Browse files
Update WrapInFunction.lua
1 parent 5cf8d7e commit bf28004

1 file changed

Lines changed: 43 additions & 9 deletions

File tree

src/prometheus/steps/WrapInFunction.lua

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,53 @@ WrapInFunction.SettingsDescriptor = {
2525

2626
function WrapInFunction:init(_) end
2727

28+
local function createWrapper(scope, body, mode)
29+
if mode == 1 then
30+
return Ast.Block({
31+
Ast.ReturnStatement({
32+
Ast.FunctionCallExpression(
33+
Ast.FunctionLiteralExpression({Ast.VarargExpression()}, body),
34+
{Ast.VarargExpression()}
35+
)
36+
})
37+
}, scope)
38+
39+
elseif mode == 2 then
40+
local f = scope:addVariable()
41+
return Ast.Block({
42+
Ast.LocalVariableDeclaration(scope, {f}, {
43+
Ast.FunctionLiteralExpression({Ast.VarargExpression()}, body)
44+
}),
45+
Ast.ReturnStatement({
46+
Ast.FunctionCallExpression(
47+
Ast.VariableExpression(scope, f),
48+
{Ast.VarargExpression()}
49+
)
50+
})
51+
}, scope)
52+
53+
else
54+
return Ast.Block({
55+
Ast.ReturnStatement({
56+
Ast.FunctionCallExpression(
57+
Ast.FunctionLiteralExpression({Ast.VarargExpression()}, body),
58+
{Ast.VarargExpression()}
59+
)
60+
})
61+
}, scope)
62+
end
63+
end
64+
2865
function WrapInFunction:apply(ast)
2966
for i = 1, self.Iterations, 1 do
30-
local body = ast.body;
67+
local body = ast.body
68+
local scope = Scope:new(ast.globalScope)
69+
body.scope:setParent(scope)
3170

32-
local scope = Scope:new(ast.globalScope);
33-
body.scope:setParent(scope);
71+
local mode = math.random(1, 3)
3472

35-
ast.body = Ast.Block({
36-
Ast.ReturnStatement({
37-
Ast.FunctionCallExpression(Ast.FunctionLiteralExpression({Ast.VarargExpression()}, body), {Ast.VarargExpression()})
38-
});
39-
}, scope);
73+
ast.body = createWrapper(scope, body, mode)
4074
end
4175
end
4276

43-
return WrapInFunction;
77+
return WrapInFunction;

0 commit comments

Comments
 (0)