Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/LuaLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum LuaLibFeature {
ArrayFindIndex = "ArrayFindIndex",
ArrayIncludes = "ArrayIncludes",
ArrayIndexOf = "ArrayIndexOf",
ArrayIsArray = "ArrayIsArray",
ArrayJoin = "ArrayJoin",
ArrayMap = "ArrayMap",
ArrayPush = "ArrayPush",
Expand Down Expand Up @@ -86,8 +87,9 @@ export enum LuaLibFeature {
}

const luaLibDependencies: Partial<Record<LuaLibFeature, LuaLibFeature[]>> = {
ArrayFlat: [LuaLibFeature.ArrayConcat],
ArrayFlatMap: [LuaLibFeature.ArrayConcat],
ArrayConcat: [LuaLibFeature.ArrayIsArray],
ArrayFlat: [LuaLibFeature.ArrayConcat, LuaLibFeature.ArrayIsArray],
ArrayFlatMap: [LuaLibFeature.ArrayConcat, LuaLibFeature.ArrayIsArray],
Decorate: [LuaLibFeature.CloneDescriptor],
Delete: [LuaLibFeature.ObjectGetOwnPropertyDescriptors],
Error: [LuaLibFeature.New, LuaLibFeature.Class],
Expand All @@ -102,6 +104,7 @@ const luaLibDependencies: Partial<Record<LuaLibFeature, LuaLibFeature[]>> = {
WeakMap: [LuaLibFeature.InstanceOf, LuaLibFeature.Iterator, LuaLibFeature.Symbol, LuaLibFeature.Class],
WeakSet: [LuaLibFeature.InstanceOf, LuaLibFeature.Iterator, LuaLibFeature.Symbol, LuaLibFeature.Class],
Spread: [LuaLibFeature.Iterator, LuaLibFeature.Unpack],
StringSplit: [LuaLibFeature.StringSubstring],
SymbolRegistry: [LuaLibFeature.Symbol],
};

Expand Down
5 changes: 2 additions & 3 deletions src/lualib/ArrayConcat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ function __TS__ArrayConcat(this: void, arr1: any[], ...args: any[]): any[] {
out[out.length] = val;
}
for (const arg of args) {
// Hack because we don't have an isArray function
if (pcall(() => (arg as any[]).length) && type(arg) !== "string") {
const argAsArray = arg as any[];
if (Array.isArray(arg)) {
const argAsArray = arg;
for (const val of argAsArray) {
out[out.length] = val;
}
Expand Down
8 changes: 1 addition & 7 deletions src/lualib/ArrayFlat.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
function __TS__ArrayFlat(this: void, array: any[], depth = 1): any[] {
let result: any[] = [];
for (const value of array) {
if (
depth > 0 &&
type(value) === "table" &&
// Workaround to determine if value is an array or not (fails in case of objects without keys)
// See discussion in: https://github.com/TypeScriptToLua/TypeScriptToLua/pull/737
(1 in value || (next as NextEmptyCheck)(value, undefined) === undefined)
) {
if (depth > 0 && Array.isArray(value)) {
result = result.concat(__TS__ArrayFlat(value, depth - 1));
} else {
result[result.length] = value;
Expand Down
7 changes: 1 addition & 6 deletions src/lualib/ArrayFlatMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ function __TS__ArrayFlatMap<T, U>(
let result: U[] = [];
for (let i = 0; i < array.length; i++) {
const value = callback(array[i], i, array);
if (
type(value) === "table" &&
// Workaround to determine if value is an array or not (fails in case of objects without keys)
// See discussion in: https://github.com/TypeScriptToLua/TypeScriptToLua/pull/737
(1 in value || (next as NextEmptyCheck)(value as any, undefined) === undefined)
) {
if (type(value) === "table" && Array.isArray(value)) {
result = result.concat(value);
} else {
result[result.length] = value as U;
Expand Down
5 changes: 5 additions & 0 deletions src/lualib/ArrayIsArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function __TS__ArrayIsArray(this: void, value: any): value is any[] {
// Workaround to determine if value is an array or not (fails in case of objects without keys)
// See discussion in: https://github.com/TypeScriptToLua/TypeScriptToLua/pull/7
return type(value) === "table" && (1 in value || (next as NextEmptyCheck)(value, undefined) === undefined);
}
17 changes: 17 additions & 0 deletions src/transformation/builtins/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ import { LuaLibFeature, transformLuaLibFunction } from "../utils/lualib";
import { PropertyCallExpression, transformArguments } from "../visitors/call";
import { isStringType, isNumberType } from "../utils/typescript";

export function transformArrayConstructorCall(
context: TransformationContext,
node: PropertyCallExpression
): lua.CallExpression | undefined {
const expression = node.expression;
const signature = context.checker.getResolvedSignature(node);
const params = transformArguments(context, node.arguments, signature);

const expressionName = expression.name.text;
switch (expressionName) {
case "isArray":
return transformLuaLibFunction(context, LuaLibFeature.ArrayIsArray, node, ...params);
default:
context.diagnostics.push(unsupportedProperty(expression.name, "Array", expressionName));
}
}

export function transformArrayPrototypeCall(
context: TransformationContext,
node: PropertyCallExpression
Expand Down
4 changes: 3 additions & 1 deletion src/transformation/builtins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from "../utils/typescript";
import { PropertyCallExpression } from "../visitors/call";
import { checkForLuaLibType } from "../visitors/class/new";
import { transformArrayProperty, transformArrayPrototypeCall } from "./array";
import { transformArrayConstructorCall, transformArrayProperty, transformArrayPrototypeCall } from "./array";
import { transformConsoleCall } from "./console";
import { transformFunctionPrototypeCall, transformFunctionProperty } from "./function";
import { transformGlobalCall } from "./global";
Expand Down Expand Up @@ -79,6 +79,8 @@ export function transformBuiltinCallExpression(
if (isStandardLibraryType(context, ownerType, undefined)) {
const symbol = ownerType.getSymbol();
switch (symbol?.name) {
case "ArrayConstructor":
return transformArrayConstructorCall(context, node);
case "Console":
return transformConsoleCall(context, node);
case "Math":
Expand Down
20 changes: 20 additions & 0 deletions test/unit/builtins/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,3 +618,23 @@ test.each(genericChecks)("array constrained generic length (%p)", signature => {
`;
expect(util.transpileAndExecute(code)).toBe(3);
});

test.each(["[]", '"hello"', "42", "[1, 2, 3]", '{ a: "foo", b: "bar" }'])(
"Array.isArray matches JavaScript (%p)",
valueString => {
util.testExpression`Array.isArray(${valueString})`.expectToMatchJsResult();
}
);

test("Array.isArray returns true for empty objects", () => {
// Important edge case we cannot handle correctly due to [] and {}
// being identical in Lua. We assume [] is more common than Array.isArray({}),
// so it is more important to handle [] right, sacrificing the result for {}.
// See discussion: https://github.com/TypeScriptToLua/TypeScriptToLua/pull/737
util.testExpression`Array.isArray({})`.expectToEqual(true);
});

// Test fix for https://github.com/TypeScriptToLua/TypeScriptToLua/issues/738
test("array.prototype.concat issue #738", () => {
util.testExpression`([] as any[]).concat(13, 323, {x: 3}, [2, 3])`.expectToMatchJsResult();
});
7 changes: 7 additions & 0 deletions test/unit/builtins/string.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LuaLibImportKind } from "../../../src";
import * as util from "../../util";

test("Supported lua string function", () => {
Expand Down Expand Up @@ -193,6 +194,12 @@ test.each([
util.testExpressionTemplate`${inp}.split(${separator})`.expectToMatchJsResult();
});

test("string.split inline", () => {
util.testExpression`"a, b, c".split(",")`
.setOptions({ luaLibImport: LuaLibImportKind.Inline })
.expectToMatchJsResult();
});

test.each([
{ inp: "hello test", index: 0 },
{ inp: "hello test", index: 1 },
Expand Down
2 changes: 1 addition & 1 deletion test/unit/spread.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("in array literal", () => {

test("of array literal /w OmittedExpression", () => {
util.testFunction`
const array = [1, 2, ...[3], , 5];
const array = [1, 2, ...[3], 5, , 6];
return { a: array[0], b: array[1], c: array[2], d: array[3] };
`.expectToMatchJsResult();
});
Expand Down