Skip to content

Commit 893cc7a

Browse files
committed
fix throwing strings and update tests
1 parent 21c27ea commit 893cc7a

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

src/LuaTransformer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2851,6 +2851,7 @@ export class LuaTransformer {
28512851

28522852
if (statement.expression) {
28532853
parameters.push(this.transformExpression(statement.expression));
2854+
parameters.push(tstl.createNumericLiteral(0));
28542855
}
28552856

28562857
return tstl.createExpressionStatement(

src/lualib/Error.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function __TS__GetErrorString(this: void, error: Error): string {
2424
return error.message !== "" ? `${error.name}: ${error.message}` : error.name;
2525
}
2626

27-
function __TS__InitErrorClass<T>(Type: ErrorType<T>, name?: string): any {
27+
function __TS__InitErrorClass<T>(Type: ErrorType<T>, name: string): any {
2828
if (name) {
2929
Type.name = name;
3030
}
@@ -34,17 +34,19 @@ function __TS__InitErrorClass<T>(Type: ErrorType<T>, name?: string): any {
3434
}
3535

3636
Error = __TS__InitErrorClass(
37-
class Error {
37+
class {
3838
public name = "Error";
3939
public message: string;
4040
public stack: string;
4141

4242
constructor(message = "") {
4343
this.message = message;
4444
this.stack = __TS__GetErrorStack((this.constructor as any).new);
45-
getmetatable(this).__tostring = __TS__GetErrorString;
45+
const mt = getmetatable(this);
46+
mt.__tostring = mt.__tostring || __TS__GetErrorString;
4647
}
47-
}
48+
},
49+
"Error"
4850
);
4951

5052
for (const errorName of ["RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError"]) {

test/unit/error.spec.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,18 @@ test("return from nested finally", () => {
286286
expect(util.transpileAndExecute(code)).toBe("finally AB");
287287
});
288288

289-
test("throw and catch custom error object", () => {
290-
util.testFunction`
289+
test.each([`"Hello error string!"`, `42`, `true`, `false`, `undefined`, `{ x: "Hello error object!" }`])(
290+
"throw and catch custom error object",
291+
error => {
292+
util.testFunction`
291293
try {
292-
throw { x: "Hello error object!" };
294+
throw ${error};
293295
} catch (error) {
294-
return error.x;
296+
return error;
295297
}
296298
`.expectToMatchJsResult();
297-
});
299+
}
300+
);
298301

299302
test.each(["Error", "RangeError", "ReferenceError", "SyntaxError", "TypeError", "URIError"])(
300303
"throw builtin Errors as classes",
@@ -304,7 +307,7 @@ test.each(["Error", "RangeError", "ReferenceError", "SyntaxError", "TypeError",
304307
throw new ${errorType}("message")
305308
} catch (error) {
306309
if (error instanceof Error) {
307-
return \`\${error}\`;
310+
return error.toString();
308311
} else {
309312
throw Error("This error serves to prevent false positives from .expectNoExecutionError()");
310313
}
@@ -321,7 +324,7 @@ test.each(["Error", "RangeError", "ReferenceError", "SyntaxError", "TypeError",
321324
throw ${errorType}("message")
322325
} catch (error) {
323326
if (error instanceof Error) {
324-
return \`\${error}\`;
327+
return error.toString();
325328
} else {
326329
throw Error("This error serves to prevent false positives from .expectNoExecutionError()");
327330
}
@@ -333,14 +336,14 @@ test.each(["Error", "RangeError", "ReferenceError", "SyntaxError", "TypeError",
333336
test("subclass Error", () => {
334337
util.testFunction`
335338
class MyError extends Error {
336-
name: "MyError"
339+
public name = "MyError";
337340
}
338341
339342
try {
340343
throw new MyError();
341344
} catch (error) {
342345
if (error instanceof Error) {
343-
return \'\$error\';
346+
return error.toString();
344347
} else {
345348
throw Error("This error serves to prevent false positives from .expectNoExecutionError()");
346349
}

0 commit comments

Comments
 (0)