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
2 changes: 2 additions & 0 deletions src/LuaLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export enum LuaLibFeature {
StringSplit = "StringSplit",
StringConcat = "StringConcat",
Symbol = "Symbol",
SymbolRegistry = "SymbolRegistry",
}

const luaLibDependencies: {[lib in LuaLibFeature]?: LuaLibFeature[]} = {
Expand All @@ -41,6 +42,7 @@ const luaLibDependencies: {[lib in LuaLibFeature]?: LuaLibFeature[]} = {
Set: [LuaLibFeature.InstanceOf, LuaLibFeature.Iterator, LuaLibFeature.Symbol],
WeakMap: [LuaLibFeature.InstanceOf, LuaLibFeature.Iterator, LuaLibFeature.Symbol],
WeakSet: [LuaLibFeature.InstanceOf, LuaLibFeature.Iterator, LuaLibFeature.Symbol],
SymbolRegistry: [LuaLibFeature.Symbol],
};

export class LuaLib {
Expand Down
31 changes: 31 additions & 0 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2815,6 +2815,11 @@ export class LuaTransformer {
parameters = this.transformArguments(node.arguments, signature);
}

const expressionType = this.checker.getTypeAtLocation(node.expression);
if (expressionType.symbol && expressionType.symbol.escapedName === "SymbolConstructor") {
return this.transformLuaLibFunction(LuaLibFeature.Symbol, node, ...parameters);
}

const callExpression = tstl.createCallExpression(callPath, parameters);
return wrapResult ? this.wrapInTable(callExpression) : callExpression;
}
Expand Down Expand Up @@ -2850,6 +2855,10 @@ export class LuaTransformer {
return this.transformObjectCallExpression(node);
}

if (ownerType.symbol && ownerType.symbol.escapedName === "SymbolConstructor") {
return this.transformSymbolCallExpression(node);
}

switch (ownerType.flags) {
case ts.TypeFlags.String:
case ts.TypeFlags.StringLiteral:
Expand Down Expand Up @@ -3300,6 +3309,28 @@ export class LuaTransformer {
}
}

// Transpile a Symbol._ property
public transformSymbolCallExpression(expression: ts.CallExpression): tstl.CallExpression {
const method = expression.expression as ts.PropertyAccessExpression;
const parameters = this.transformArguments(expression.arguments);
const methodName = method.name.escapedText;

switch (methodName) {
case "for":
case "keyFor":
this.importLuaLibFeature(LuaLibFeature.SymbolRegistry);
const upperMethodName = methodName[0].toUpperCase() + methodName.slice(1);
const functionIdentifier = tstl.createIdentifier(`__TS__SymbolRegistry${upperMethodName}`);
return tstl.createCallExpression(functionIdentifier, parameters, expression);
default:
throw TSTLErrors.UnsupportedForTarget(
`symbol property ${methodName}`,
this.options.luaTarget,
expression
);
}
}

public transformArrayCallExpression(node: ts.CallExpression): tstl.CallExpression {
const expression = node.expression as ts.PropertyAccessExpression;
const params = this.transformArguments(node.arguments);
Expand Down
19 changes: 18 additions & 1 deletion src/lualib/Symbol.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
declare function setmetatable<T extends object>(obj: T, metatable: any): T;

// tslint:disable-next-line: variable-name
const ____symbolMetatable = {
__tostring(): string {
if (this.description === undefined) {
return 'Symbol()';
} else {
return 'Symbol(' + this.description + ')';
}
},
};

function __TS__Symbol(description?: string | number): symbol {
return setmetatable({ description }, ____symbolMetatable) as any;
}

Symbol = {
iterator: {},
iterator: __TS__Symbol('Symbol.iterator'),
} as any;
16 changes: 16 additions & 0 deletions src/lualib/SymbolRegistry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// tslint:disable-next-line: variable-name
const ____symbolRegistry: Record<string, symbol> = {};

function __TS__SymbolRegistryFor(key: string): symbol {
if (!____symbolRegistry[key]) {
____symbolRegistry[key] = __TS__Symbol(key);
}

return ____symbolRegistry[key];
}

function __TS__SymbolRegistryKeyFor(sym: symbol): string {
for (const key in ____symbolRegistry) {
if (____symbolRegistry[key] === sym) return key;
}
}
89 changes: 89 additions & 0 deletions test/unit/lualib/symbol.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Expect, Test, TestCase } from "alsatian";
import * as util from "../../src/util";
import { CompilerOptions, LuaLibImportKind } from '../../../src/CompilerOptions';

export class SymbolTests {
private compilerOptions: CompilerOptions = {
lib: ["esnext"],
luaLibImport: LuaLibImportKind.Require,
};

@Test("symbol.toString()")
@TestCase()
@TestCase(1)
@TestCase("name")
public symbolToString(description?: string | number): void
{
const result = util.transpileAndExecute(`
return Symbol(${JSON.stringify(description)}).toString();
`, this.compilerOptions);

Expect(result).toBe(`Symbol(${description || ''})`);
}

@Test("symbol.description")
@TestCase()
@TestCase(1)
@TestCase("name")
public symbolDescription(description?: string | number): void
{
const result = util.transpileAndExecute(`
return Symbol(${JSON.stringify(description)}).description;
`, this.compilerOptions);

Expect(result).toBe(description);
}

@Test("symbol uniqueness")
public symbolUniqueness(): void
{
const result = util.transpileAndExecute(`
return Symbol("a") === Symbol("a");
`);

Expect(result).toBe(false);
}

@Test("Symbol.for")
public symbolFor(): void
{
const result = util.transpileAndExecute(`
return Symbol.for("name").description;
`, this.compilerOptions);

Expect(result).toBe("name");
}

@Test("Symbol.for non-uniqueness")
public symbolForNonUniqueness(): void
{
const result = util.transpileAndExecute(`
return Symbol.for("a") === Symbol.for("a");
`);

Expect(result).toBe(true);
}

@Test("Symbol.keyFor")
public symbolKeyFor(): void
{
const result = util.transpileAndExecute(`
const sym = Symbol.for("a");
Symbol.for("b");
return Symbol.keyFor(sym);
`);

Expect(result).toBe("a");
}

@Test("Symbol.keyFor empty")
public symbolKeyForEmpty(): void
{
const result = util.transpileAndExecute(`
Symbol.for("a");
return Symbol.keyFor(Symbol());
`);

Expect(result).toBe(undefined);
}
}