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

[feature request] close-variables for Lua versions below 5.4 #143

Closed
SkyyySi opened this issue Jul 30, 2023 · 1 comment
Closed

[feature request] close-variables for Lua versions below 5.4 #143

SkyyySi opened this issue Jul 30, 2023 · 1 comment

Comments

@SkyyySi
Copy link

SkyyySi commented Jul 30, 2023

Lua 5.4 added support for using <close> on a variable to allow calling a destructor function as soon as it goes out of scope. I think that this feature could also be made to work with target versions below 5.4.

Let's assume we have this Yuescript code:

do
    close db_connection = { <close>: => @disconnect() }

    -- do stuff...

It could compile to something like this:

do
    local db_connection = setmetatable({}, { __close = function(self) self:disconnect() end })
    local _close_0 = assert(getmetatable(db_connection).__close)
    local _err_0 = false
    local _msg_0
    xpcall(function()

        -- do stuff...

    end, function(err)
        _err_0 = true
        _msg_0 = err
    end)
    _close_0(db_connection)
    if _err_0 then
        error(_msg_0)
    end
end

Also, I'd just like to say: Thank you for taking your time to address most of my issues and for being so responsive.

pigpigyyy added a commit that referenced this issue Aug 7, 2023
@pigpigyyy
Copy link
Owner

pigpigyyy commented Aug 7, 2023

Tried to add this feature as:

f = ->
  close db_connection =
    disconnect: -> print "disconnect"
    <close>: => @disconnect!
  1, nil, 2, nil, 3

print f!

compiles to Lua target below 5.4:

local f
f = function()
  local db_connection = setmetatable({
    disconnect = function()
      return print("disconnect")
    end,
  }, {
    __close = function(self)
      return self:disconnect()
    end
  })
  local _close_0 = assert(getmetatable(db_connection).__close)
  return (function(_arg_0, ...)
    local _ok_0 = _arg_0
    _close_0(db_connection)
    if _ok_0 then
      return ...
    else
      return error(...)
    end
  end)(pcall(function()
    return 1, nil, 2, nil, 3
  end))
end
return print(f())

compiles to Lua target 5.4:

local f
f = function()
  local db_connection <close> = setmetatable({
    disconnect = function()
      return print("disconnect")
    end,
  }, {
    __close = function(self)
      return self:disconnect()
    end
  })
  return 1, nil, 2, nil, 3
end
return print(f())

Sorry for being late this time. It took me some effort trying out your good idea. And I made some modification according to the way that the variadic items can be handled, mentioned in issue #144.

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