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
9 changes: 9 additions & 0 deletions benchmark/src/runtime_benchmarks/array_concat_array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function arrayConcat(): number[] {
const arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const n = 50000;
for (let i = 0; i < n; i++) {
arr1.concat(arr2);
}
return arr1;
}
9 changes: 9 additions & 0 deletions benchmark/src/runtime_benchmarks/array_concat_spread.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function arrayConcat(): number[] {
const arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const n = 50000;
for (let i = 0; i < n; i++) {
arr1.concat(...arr2);
}
return arr1;
}
7 changes: 7 additions & 0 deletions benchmark/src/runtime_benchmarks/array_every.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function arrayEvery() {
const n = 200000;
const array = [1, 2, 3, 4, 5, 6, 7, 8, 7, 10];
for (let i = 0; i < n; i++) {
array.every((item, index) => item > index);
}
}
7 changes: 7 additions & 0 deletions benchmark/src/runtime_benchmarks/array_filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function arrayFilter() {
const n = 100000;
const array = [1, 2, 3, 4, 3, 6, 7, 8, 7, 10];
for (let i = 0; i < n; i++) {
array.filter((item, index) => item > index);
}
}
9 changes: 9 additions & 0 deletions benchmark/src/runtime_benchmarks/array_find.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function arrayFind() {
const n = 50000;
const array = [1, 2, 3, 4, 3, 6, 7, 8, 9, 10];
for (let i = 0; i < n; i++) {
for (let j = 0; j < 10; j++) {
array.find(value => value === j);
}
}
}
9 changes: 9 additions & 0 deletions benchmark/src/runtime_benchmarks/array_findIndex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function arrayFindIndex() {
const n = 50000;
const array = [1, 2, 3, 4, 3, 6, 7, 8, 9, 10];
for (let i = 0; i < n; i++) {
for (let j = 0; j < 10; j++) {
array.findIndex(value => value === j);
}
}
}
7 changes: 7 additions & 0 deletions benchmark/src/runtime_benchmarks/array_flat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function arrayFlat() {
const n = 50000;
const array = [1, 2, [3, [4, 5], 6], 7, [8, 9], 10];
for (let i = 0; i < n; i++) {
array.flat(2);
}
}
13 changes: 13 additions & 0 deletions benchmark/src/runtime_benchmarks/array_flatMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function arrayFlatMap() {
const n = 50000;
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let i = 0; i < n; i++) {
array.flatMap((el, index) => {
if (index < 5) {
return [el, el + 1];
} else {
return el + 2;
}
});
}
}
10 changes: 10 additions & 0 deletions benchmark/src/runtime_benchmarks/array_foreach.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default function arrayForeach() {
const n = 200000;
const array = [1, 2, 3, 4, 3, 6, 7, 8, 9, 10];
for (let i = 0; i < n; i++) {
array.forEach(value => {
let foo = value * 2;
foo = foo;
});
}
}
9 changes: 9 additions & 0 deletions benchmark/src/runtime_benchmarks/array_includes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function arrayIncludes() {
const n = 50000;
const array = [1, 2, 3, 4, 3, 6, 7, 8, 9, 10];
for (let i = 0; i < n; i++) {
for (let j = 0; j < 10; j++) {
array.includes(j);
}
}
}
9 changes: 9 additions & 0 deletions benchmark/src/runtime_benchmarks/array_indexOf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function arrayIndexOf() {
const n = 50000;
const array = [1, 2, 3, 4, 3, 6, 7, 8, 9, 10];
for (let i = 0; i < n; i++) {
for (let j = 0; j < 10; j++) {
array.indexOf(j);
}
}
}
8 changes: 8 additions & 0 deletions benchmark/src/runtime_benchmarks/array_join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const array = [1, 2, "3", 4, 3, "6", { foo: 3 }, 8, 9, 10];

export default function arrayJoin() {
const n = 3000;
for (let i = 0; i < n; i++) {
array.join("|");
}
}
7 changes: 7 additions & 0 deletions benchmark/src/runtime_benchmarks/array_map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function arrayMap() {
const n = 100000;
const array = [1, 2, 3, 4, 3, 6, 7, 8, 9, 10];
for (let i = 0; i < n; i++) {
array.map(value => value * 2);
}
}
9 changes: 9 additions & 0 deletions benchmark/src/runtime_benchmarks/array_push_array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function arrayPush(): number[] {
const n = 200000;
const numberList: number[] = [];
const numbers = [1, 2, 3];
for (let i = 0; i < n; i++) {
numberList.push(...numbers);
}
return numberList;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default function arrayPush(): number[] {
const n = 1000000;
const n = 200000;
const numberList: number[] = [];
for (let i = 0; i < n; i++) {
numberList[numberList.length] = i * i;
numberList.push(i * i, i + 1);
}
return numberList;
}
8 changes: 8 additions & 0 deletions benchmark/src/runtime_benchmarks/array_push_single.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function arrayPush(): number[] {
const n = 500000;
const numberList: number[] = [];
for (let i = 0; i < n; i++) {
numberList.push(i * i);
}
return numberList;
}
12 changes: 12 additions & 0 deletions benchmark/src/runtime_benchmarks/array_push_vararg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function arrayPush(): number[] {
const n = 200000;
const numberList: number[] = [];
function pushArgs(...args: number[]): void {
numberList.push(...args);
}

for (let i = 0; i < n; i++) {
pushArgs(3, 4);
}
return numberList;
}
7 changes: 7 additions & 0 deletions benchmark/src/runtime_benchmarks/array_reduce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function arrayReduce() {
const n = 200000;
const array = [1, 2, 3, 4, 5, 6, 7, 8, 7, 10];
for (let i = 0; i < n; i++) {
array.reduce((prev, cur, i) => prev + cur + i, 1);
}
}
7 changes: 7 additions & 0 deletions benchmark/src/runtime_benchmarks/array_reduceRight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function arrayReduce() {
const n = 200000;
const array = [1, 2, 3, 4, 5, 6, 7, 8, 7, 10];
for (let i = 0; i < n; i++) {
array.reduceRight((prev, cur, i) => prev + cur + i, 1);
}
}
7 changes: 7 additions & 0 deletions benchmark/src/runtime_benchmarks/array_reverse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function arrayReverse(): void {
const n = 500000;
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (let i = 0; i < n; i++) {
numbers.reverse();
}
}
9 changes: 9 additions & 0 deletions benchmark/src/runtime_benchmarks/array_slice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function arraySlice() {
const n = 50000;
const array = [1, 2, 3, 4, 3, 6, 7, 8, 9, 10];
for (let i = 0; i < n; i++) {
for (let j = 0; j < 6; j++) {
array.slice(j, -j);
}
}
}
7 changes: 7 additions & 0 deletions benchmark/src/runtime_benchmarks/array_some.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function arraySome() {
const n = 200000;
const array = [1, 2, 3, 4, 5, 6, 7, 8, 7, 10];
for (let i = 0; i < n; i++) {
array.some((item, index) => item < index);
}
}
9 changes: 9 additions & 0 deletions benchmark/src/runtime_benchmarks/array_splice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function arraySplice() {
const n = 20000;
const array = [1, 2, 3, 4, 3, 6, 7, 8, 9, 10];
for (let i = 0; i < n; i++) {
for (let j = 0; j < 10; j++) {
array.splice(j, 2, 1, 2);
}
}
}
9 changes: 9 additions & 0 deletions benchmark/src/runtime_benchmarks/array_unshift.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function arrayUnshift(): number[] {
const n = 2000;
const numberList: number[] = [];
const numbers = [1, 2, 3];
for (let i = 0; i < n; i++) {
numberList.unshift(...numbers);
}
return numberList;
}
8 changes: 8 additions & 0 deletions benchmark/src/runtime_benchmarks/string_concat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function stringReplace() {
const str = "one";
const n = 50000;
for (let i = 0; i < n; i++) {
str.concat("two", "three", "four", "five");
}
return str;
}
8 changes: 8 additions & 0 deletions benchmark/src/runtime_benchmarks/string_replace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function stringReplace() {
const str = "Hello, World!";
const n = 50000;
for (let i = 0; i < n; i++) {
str.replace("World", "Universe");
}
return str;
}
8 changes: 8 additions & 0 deletions benchmark/src/runtime_benchmarks/string_replaceAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function stringReplaceAll() {
const str = "hello, hello world!";
const n = 50000;
for (let i = 0; i < n; i++) {
str.replaceAll("Hello", "Goodbye");
}
return str;
}
8 changes: 8 additions & 0 deletions benchmark/src/runtime_benchmarks/string_split.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default function stringReplaceAll() {
const str = "This is a string";
const n = 50000;
for (let i = 0; i < n; i++) {
str.split(" ");
}
return str;
}
30 changes: 18 additions & 12 deletions src/LuaAST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,33 @@ export type Operator = UnaryOperator | BinaryOperator;

export type SymbolId = number & { _symbolIdBrand: any };

export enum NodeFlags {
None = 0,
Inline = 1 << 0, // Keep function body on same line
Declaration = 1 << 1, // Prefer declaration syntax `function foo()` over assignment syntax `foo = function()`
TableUnpackCall = 1 << 2, // This is a table.unpack call
}

export interface TextRange {
line?: number;
column?: number;
}

export interface Node extends TextRange {
kind: SyntaxKind;
flags: NodeFlags;
}

export function createNode(kind: SyntaxKind, tsOriginal?: ts.Node): Node {
if (tsOriginal === undefined) {
return { kind };
return { kind, flags: NodeFlags.None };
}

const sourcePosition = getSourcePosition(tsOriginal);
if (sourcePosition) {
return { kind, line: sourcePosition.line, column: sourcePosition.column };
return { kind, line: sourcePosition.line, column: sourcePosition.column, flags: NodeFlags.None };
} else {
return { kind };
return { kind, flags: NodeFlags.None };
}
}

Expand Down Expand Up @@ -190,6 +198,11 @@ export function getOriginalPos(node: Node): TextRange {
return { line: node.line, column: node.column };
}

export function setNodeFlags<T extends Node>(node: T, flags: NodeFlags): T {
node.flags = flags;
return node;
}

export interface File extends Node {
kind: SyntaxKind.File;
statements: Statement[];
Expand Down Expand Up @@ -578,18 +591,11 @@ export function isLiteral(
);
}

export enum FunctionExpressionFlags {
None = 1 << 0,
Inline = 1 << 1, // Keep function body on same line
Declaration = 1 << 2, // Prefer declaration syntax `function foo()` over assignment syntax `foo = function()`
}

export interface FunctionExpression extends Expression {
kind: SyntaxKind.FunctionExpression;
params?: Identifier[];
dots?: DotsLiteral;
body: Block;
flags: FunctionExpressionFlags;
}

export function isFunctionExpression(node: Node): node is FunctionExpression {
Expand All @@ -600,7 +606,7 @@ export function createFunctionExpression(
body: Block,
params?: Identifier[],
dots?: DotsLiteral,
flags = FunctionExpressionFlags.None,
flags = NodeFlags.None,
tsOriginal?: ts.Node
): FunctionExpression {
const expression = createNode(SyntaxKind.FunctionExpression, tsOriginal) as FunctionExpression;
Expand Down Expand Up @@ -819,6 +825,6 @@ export function isInlineFunctionExpression(expression: FunctionExpression): expr
expression.body.statements?.length === 1 &&
isReturnStatement(expression.body.statements[0]) &&
expression.body.statements[0].expressions !== undefined &&
(expression.flags & FunctionExpressionFlags.Inline) !== 0
(expression.flags & NodeFlags.Inline) !== 0
);
}
3 changes: 1 addition & 2 deletions src/LuaLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export enum LuaLibFeature {
ArrayJoin = "ArrayJoin",
ArrayMap = "ArrayMap",
ArrayPush = "ArrayPush",
ArrayPushArray = "ArrayPushArray",
ArrayReduce = "ArrayReduce",
ArrayReduceRight = "ArrayReduceRight",
ArrayReverse = "ArrayReverse",
ArrayShift = "ArrayShift",
ArrayUnshift = "ArrayUnshift",
ArraySort = "ArraySort",
ArraySlice = "ArraySlice",
Expand Down Expand Up @@ -80,7 +80,6 @@ export enum LuaLibFeature {
StringAccess = "StringAccess",
StringCharAt = "StringCharAt",
StringCharCodeAt = "StringCharCodeAt",
StringConcat = "StringConcat",
StringEndsWith = "StringEndsWith",
StringIncludes = "StringIncludes",
StringPadEnd = "StringPadEnd",
Expand Down
5 changes: 1 addition & 4 deletions src/LuaPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,7 @@ export class LuaPrinter {

chunks.push(this.indent());

if (
lua.isFunctionDefinition(statement) &&
(statement.right[0].flags & lua.FunctionExpressionFlags.Declaration) !== 0
) {
if (lua.isFunctionDefinition(statement) && (statement.right[0].flags & lua.NodeFlags.Declaration) !== 0) {
// Use `function foo()` instead of `foo = function()`
const name = this.printExpression(statement.left[0]);
if (isValidLuaFunctionDeclarationName(name.toString(), this.options)) {
Expand Down
Loading