xiangnanscu / js2lua

js2lua Writing LuaJIT with the expressiveness of JavaScript.
https://xiangnanscu.github.io/js2lua/
3 stars 0 forks source link

Question: is there a way to invoke method wihout "passing" this? #2

Closed al1-ce closed 1 month ago

al1-ce commented 1 month ago

It'd be easier to explain in example:

let a = require("a");
a.f();
new a.f();
a.b.f();
new a.b.f();

let b = { f() {}, c: { f(){} } };
b.f();
new b.f();
b.c.f();
new b.c.f();

Would generate:

local a = require("a")
a:f() -- a.f()
a.f() -- new a.f()
(function()
    local __tmp = a.b
    return __tmp:f()
end)() -- a.b.f()
a.b.f() -- new a.b.f()

local b = {
    f = function()
    end,
    c = {
        f = function()
        end
    }
}
b:f() -- b.f()
b.f() -- new b.f()
(function()
    local __tmp = b.c
    return __tmp:f()
end)() -- b.c.f()
b.c.f() -- new b.c.f()

My question is - is there a way to get output of a.f() without prepending new?

al1-ce commented 1 month ago

So, still, what'd be syntax difference between calls that pass this and ones that do not? For example if I'd use external library there's some functions that do obj:method() and others obj.func(). How should I make it generate one or the other?

xiangnanscu commented 1 month ago

It's hard to take care of both, so when you want obj.func(), you write const {func: obj_func}=obj first and then obj_func()

al1-ce commented 1 month ago

I guess that would work, kinda would be like bindings in other languages which is possible to generate

al1-ce commented 1 month ago

I was thinking about it and what about being able to define non-class symbols? Code has to go through cli anyway so something like js2lua --non-class=api,other,thing file.js > file.lua would possibly work

xiangnanscu commented 1 month ago

I add an option useColonOnMethod, done by b78d0aad41ec9ed8903e274089bdad1e0acaaa7e