X Tutup
Skip to content

Commit 6066a7e

Browse files
committed
set __tostring metamethod in constructor
1 parent a0b63e1 commit 6066a7e

File tree

3 files changed

+18
-24
lines changed

3 files changed

+18
-24
lines changed

src/LuaLib.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export enum LuaLibFeature {
6464
const luaLibDependencies: { [lib in LuaLibFeature]?: LuaLibFeature[] } = {
6565
ArrayFlat: [LuaLibFeature.ArrayConcat],
6666
ArrayFlatMap: [LuaLibFeature.ArrayConcat],
67+
Error: [LuaLibFeature.FunctionCall],
6768
InstanceOf: [LuaLibFeature.Symbol],
6869
Iterator: [LuaLibFeature.Symbol],
6970
ObjectFromEntries: [LuaLibFeature.Iterator, LuaLibFeature.Symbol],

src/lualib/Error.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ function __TS__GetErrorStack(constructor: Function): string {
2020
return debug.traceback(undefined, level);
2121
}
2222

23-
function __TS__GetErrorString(this: void, errorObj: Error): string {
24-
const description = errorObj.message !== "" ? `${errorObj.name}: ${errorObj.message}` : errorObj.name;
25-
const caller = debug.getinfo(3, "f");
26-
if (_VERSION === "Lua 5.1" || (caller && caller.func !== error)) {
27-
return description;
28-
} else {
29-
return `${description}\n${errorObj.stack}`;
30-
}
23+
function __TS__WrapErrorToString<T extends Error>(getDescription: (this: T) => string): (this: T) => string {
24+
return function(this: Error): string {
25+
const description = getDescription.call(this);
26+
const caller = debug.getinfo(3, "f");
27+
if (_VERSION === "Lua 5.1" || (caller && caller.func !== error)) {
28+
return description;
29+
} else {
30+
return `${description}\n${this.stack}`;
31+
}
32+
};
3133
}
3234

3335
function __TS__InitErrorClass<T>(Type: ErrorType<T>, name: string): any {
@@ -44,10 +46,15 @@ Error = __TS__InitErrorClass(
4446

4547
constructor(public message = "") {
4648
this.stack = __TS__GetErrorStack((this.constructor as any).new);
49+
const metatable = getmetatable(this);
50+
if (!metatable.__errorToStringPatched) {
51+
metatable.__errorToStringPatched = true;
52+
metatable.__tostring = __TS__WrapErrorToString(metatable.__tostring);
53+
}
4754
}
4855

4956
public toString(): string {
50-
return __TS__GetErrorString(this);
57+
return this.message !== "" ? `${this.name}: ${this.message}` : this.name;
5158
}
5259
},
5360
"Error"
@@ -57,10 +64,6 @@ for (const errorName of ["RangeError", "ReferenceError", "SyntaxError", "TypeErr
5764
globalThis[errorName] = __TS__InitErrorClass(
5865
class extends Error {
5966
public name = errorName;
60-
61-
public toString(): string {
62-
return __TS__GetErrorString(this);
63-
}
6467
},
6568
errorName
6669
);

test/unit/error.spec.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -325,17 +325,7 @@ test.each([
325325
])("test builtin error properties", errorType => {
326326
util.testFunction`
327327
const error = ${errorType}();
328-
return error.name;
329-
`.expectToMatchJsResult();
330-
331-
util.testFunction`
332-
const error = ${errorType}();
333-
return error.message;
334-
`.expectToMatchJsResult();
335-
336-
util.testFunction`
337-
const error = ${errorType}();
338-
return error.toString();
328+
return { name: error.name, message: error.message, string: error.toString() };
339329
`.expectToMatchJsResult();
340330
});
341331

0 commit comments

Comments
 (0)
X Tutup