Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global functions get transpiled with a nil first argument #1

Open
r-o-b-o-t-o opened this issue Jun 7, 2021 · 3 comments
Open

Global functions get transpiled with a nil first argument #1

r-o-b-o-t-o opened this issue Jun 7, 2021 · 3 comments

Comments

@r-o-b-o-t-o
Copy link
Member

r-o-b-o-t-o commented Jun 7, 2021

It seems that some functions that are available from the global scope are incorrectly transpiled to Lua.
For example:

const map = GetMapById(1);
print(map.GetName());

transpiles to

local map = GetMapById(nil, 1)
print(
    map:GetName()
)

notice the nil before the 1, which causes an error: bad argument #1 to 'GetMapById' (number expected, got nil)

Same behavior with

const query = CharDBQuery("...");

which transpiles to

local query = CharDBQuery(nil, "...")

and therefore gives the error bad argument #1 to 'CharDBQuery' (string expected, got nil)

I'm using the following versions:

  • @azerothcore/eluna-ts-lib: ^1.0.13
  • typescript: 4.2.4
  • typescript-to-lua: ^0.39.3
  • Node v14.16.0
@FrancescoBorzi
Copy link

I wonder if it's a bug of the underlying https://github.com/TypeScriptToLua/TypeScriptToLua

@r-o-b-o-t-o
Copy link
Member Author

It might be, but I'm not sure how to pinpoint the exact cause

@r-o-b-o-t-o
Copy link
Member Author

Found it!
Static functions need a void argument in the type declaration.
So for example you need to change CharDBQuery in global.d.ts from

declare function CharDBQuery(sql: string): ElunaQuery;

to

declare function CharDBQuery(this: void, sql: string): ElunaQuery;

then it works properly 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants