X Tutup
Skip to content

Commit 26ac507

Browse files
authored
Function apply without arguments should not lead to exception (#1228)
1 parent 5d66aa4 commit 26ac507

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/transformation/builtins/function.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ export function transformFunctionPrototypeCall(
2323
const expressionName = expression.name.text;
2424
switch (expressionName) {
2525
case "apply":
26-
return lua.createCallExpression(
27-
caller,
28-
[params[0], createUnpackCall(context, params[1], node.arguments[1])],
29-
node
30-
);
26+
const nonContextArgs = params.length > 1 ? [createUnpackCall(context, params[1], node.arguments[1])] : [];
27+
return lua.createCallExpression(caller, [params[0], ...nonContextArgs], node);
3128
case "bind":
3229
return transformLuaLibFunction(context, LuaLibFeature.FunctionBind, node, caller, ...params);
3330
case "call":

test/unit/functions/functions.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ test("Function apply", () => {
173173
`.expectToMatchJsResult();
174174
});
175175

176+
// Fix #1226: https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1226
177+
test("function apply without arguments should not lead to exception", () => {
178+
util.testFunction`
179+
const f = function (this: number) { return this + 3; }
180+
return f.apply(4);
181+
`.expectToMatchJsResult();
182+
});
183+
176184
test("Function call", () => {
177185
util.testFunction`
178186
const abc = function (this: { a: number }, a: string) { return this.a + a; }

0 commit comments

Comments
 (0)
X Tutup