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

fix(class): fixed private access in method calls #553

Merged
merged 2 commits into from
Apr 28, 2024

Conversation

deFuzz77
Copy link
Contributor

I created issue (#549) about not expecting value from private table. Commit dcdfe6e partially fixed this, but it creates a new issue.

Now, class documentation has one error, actually jesse.private.year returns 3, not nill.

-- code from https://overextended.dev/ox_lib/Modules/Class/Shared#example

CreateThread(function()
    local jesse = Student:new('Jesse', 2)
 
    jesse:introduceSelf() -- Hi! I'm Jesse, and I'm in year 2.
    jesse:setYear(3)
    jesse:introduceSelf() -- Hi! I'm Jesse, and I'm in year 3.
    print(jesse.private.year) -- nil  [now, this returns 3, not nil]
    print(getmetatable(jesse.private)) -- private
    jesse.private.year = 4 -- error
end)

I solve this with this simple if statement, and it works fine:

  __index = function(self, index)
      local di = getinfo(2, 'n')
  
      if di.namewhat ~= 'method' and di.namewhat ~= '' then return end
  
      return private[index]
  end,

@LukeWasTakenn LukeWasTakenn requested a review from thelindat April 27, 2024 13:35
@thelindat thelindat merged commit 75f4ecb into overextended:master Apr 28, 2024
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

Successfully merging this pull request may close these issues.

2 participants