X Tutup
Skip to content

Commit 6d20032

Browse files
authored
Don't use lualib for .call and .apply (#884)
* Don't use lualib for .call and .apply * Fix flaky array.join test
1 parent 912a230 commit 6d20032

File tree

6 files changed

+12
-23
lines changed

6 files changed

+12
-23
lines changed

src/LuaLib.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ export enum LuaLibFeature {
3131
Decorate = "Decorate",
3232
Descriptors = "Descriptors",
3333
Error = "Error",
34-
FunctionApply = "FunctionApply",
3534
FunctionBind = "FunctionBind",
36-
FunctionCall = "FunctionCall",
3735
Generator = "Generator",
3836
InstanceOf = "InstanceOf",
3937
InstanceOfObject = "InstanceOfObject",
@@ -75,10 +73,8 @@ export enum LuaLibFeature {
7573
const luaLibDependencies: Partial<Record<LuaLibFeature, LuaLibFeature[]>> = {
7674
ArrayFlat: [LuaLibFeature.ArrayConcat],
7775
ArrayFlatMap: [LuaLibFeature.ArrayConcat],
78-
Error: [LuaLibFeature.New, LuaLibFeature.Class, LuaLibFeature.FunctionCall],
79-
FunctionApply: [LuaLibFeature.Unpack],
76+
Error: [LuaLibFeature.New, LuaLibFeature.Class],
8077
FunctionBind: [LuaLibFeature.Unpack],
81-
FunctionCall: [LuaLibFeature.Unpack],
8278
Generator: [LuaLibFeature.Symbol],
8379
InstanceOf: [LuaLibFeature.Symbol],
8480
Iterator: [LuaLibFeature.Symbol],

src/lualib/FunctionApply.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/lualib/FunctionCall.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/transformation/builtins/function.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as lua from "../../LuaAST";
22
import { TransformationContext } from "../context";
33
import { unsupportedProperty, unsupportedSelfFunctionConversion } from "../utils/diagnostics";
44
import { ContextType, getFunctionContextType } from "../utils/function-context";
5+
import { createUnpackCall } from "../utils/lua-ast";
56
import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
67
import { PropertyCallExpression, transformArguments } from "../visitors/call";
78

@@ -21,11 +22,15 @@ export function transformFunctionPrototypeCall(
2122
const expressionName = expression.name.text;
2223
switch (expressionName) {
2324
case "apply":
24-
return transformLuaLibFunction(context, LuaLibFeature.FunctionApply, node, caller, ...params);
25+
return lua.createCallExpression(
26+
caller,
27+
[params[0], createUnpackCall(context, params[1], node.arguments[1])],
28+
node
29+
);
2530
case "bind":
2631
return transformLuaLibFunction(context, LuaLibFeature.FunctionBind, node, caller, ...params);
2732
case "call":
28-
return transformLuaLibFunction(context, LuaLibFeature.FunctionCall, node, caller, ...params);
33+
return lua.createCallExpression(caller, params, node);
2934
default:
3035
context.diagnostics.push(unsupportedProperty(expression.name, "function", expressionName));
3136
}

test/unit/annotations/__snapshots__/forRange.spec.ts.snap

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ exports[`invalid usage reference ("let array = [0, luaRange, 1];"): code 1`] = `
7070

7171
exports[`invalid usage reference ("let array = [0, luaRange, 1];"): diagnostics 1`] = `"main.ts(6,29): error TSTL: Invalid @forRange call: can be used only as an iterable in a for...of loop."`;
7272

73-
exports[`invalid usage reference ("luaRange.call(null, 0, 0, 0);"): code 1`] = `
74-
"require(\\"lualib_bundle\\");
75-
__TS__FunctionCall(luaRange, nil, 0, 0, 0)"
76-
`;
73+
exports[`invalid usage reference ("luaRange.call(null, 0, 0, 0);"): code 1`] = `"luaRange(nil, 0, 0, 0)"`;
7774

7875
exports[`invalid usage reference ("luaRange.call(null, 0, 0, 0);"): diagnostics 1`] = `"main.ts(6,13): error TSTL: Invalid @forRange call: can be used only as an iterable in a for...of loop."`;
7976

test/unit/builtins/array.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,12 @@ test.each([
430430
});
431431

432432
test('array.join (1, "2", {})', () => {
433-
util.testExpression`[1, "2", {}].join()`.expectToEqual("1,2,table: 0x168");
433+
const result = util.testExpression`[1, "2", {}].join()`.getLuaExecutionResult();
434+
expect(result).toMatch(/^1,2,table: 0x\d+$/);
434435
});
435436

436437
test('array.join (1, "2", Symbol("foo"))', () => {
437-
util.testExpression`[1, "2", Symbol("foo")].join()`.expectToEqual("1,2,Symbol(foo)");
438+
util.testExpression`[1, "2", Symbol("foo")].join(", ")`.expectToEqual("1, 2, Symbol(foo)");
438439
});
439440

440441
test("array.join without separator argument", () => {

0 commit comments

Comments
 (0)
X Tutup