X Tutup
Skip to content

Implemented support for properties on functions#1180

Merged
Perryvw merged 1 commit intoTypeScriptToLua:masterfrom
tomblind:function-properties
Dec 2, 2021
Merged

Implemented support for properties on functions#1180
Perryvw merged 1 commit intoTypeScriptToLua:masterfrom
tomblind:function-properties

Conversation

@tomblind
Copy link
Collaborator

Closes #388

Functions with properties are now supported (including via declaration merging with namespaces). This is done by wrapping the function definition in a "callable" table (using the __call metamethod).

Example:

function foo(s: string) { return s; }
foo.bar = "bar";
local foo = setmetatable(
    {},
    {__call = function(____, self, s)
        return s
    end}
)
foo.bar = "bar"

Additionally, this fixes #1174 and fixes #1179 as I needed those working for tests.

case "call":
return lua.createCallExpression(caller, params, node);
default:
case "toString":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a mistake, this should be default to catch everything we have not handled

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since functions can have any properties, we don't want to throw errors on them being used. So I changed this to only error on JS lib functions that we don't handle.

@Perryvw Perryvw merged commit c955a63 into TypeScriptToLua:master Dec 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants

X Tutup