1+ import { __TS__New } from "./New" ;
2+
13interface ErrorType {
24 name : string ;
35 new ( ...args : any [ ] ) : Error ;
@@ -22,6 +24,10 @@ function getErrorStack(constructor: () => any): string | undefined {
2224
2325 if ( _VERSION . includes ( "Lua 5.0" ) ) {
2426 return debug . traceback ( `[Level ${ level } ]` ) ;
27+ } else if ( _VERSION === "Lua 5.1" ) {
28+ // Lua 5.1 and LuaJIT have a bug where it's not possible to specify the level without a message.
29+ // @ts -ignore Fails when compiled with Lua 5.0 types
30+ return string . sub ( debug . traceback ( "" , level ) , 2 ) ;
2531 } else {
2632 // @ts -ignore Fails when compiled with Lua 5.0 types
2733 return debug . traceback ( undefined , level ) ;
@@ -33,7 +39,7 @@ function wrapErrorToString<T extends Error>(getDescription: (this: T) => string)
3339 const description = getDescription . call ( this as T ) ;
3440 const caller = debug . getinfo ( 3 , "f" ) ;
3541 // @ts -ignore Fails when compiled with Lua 5.0 types
36- const isClassicLua = _VERSION . includes ( "Lua 5.0" ) || _VERSION === "Lua 5.1" ;
42+ const isClassicLua = _VERSION . includes ( "Lua 5.0" ) ;
3743 if ( isClassicLua || ( caller && caller . func !== error ) ) {
3844 return description ;
3945 } else {
@@ -55,7 +61,7 @@ export const Error: ErrorConstructor = initErrorClass(
5561 public stack ?: string ;
5662
5763 constructor ( public message = "" ) {
58- this . stack = getErrorStack ( ( this . constructor as any ) . new ) ;
64+ this . stack = getErrorStack ( __TS__New as any ) ;
5965 const metatable = getmetatable ( this ) ;
6066 if ( metatable && ! metatable . __errorToStringPatched ) {
6167 metatable . __errorToStringPatched = true ;
0 commit comments