Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .luarc.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"new",
"current",
"async",
"super"
"super",
"isInstanceOf"
]
}
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ Atomic will automatically load available packages.

Optional dependencies:
- `MySQL` support / [MySQLOO](https://github.com/FredyH/MySQLOO)
- `git` support / [gm_git](https://github.com/TeamMeadows/gm_git)

---

Expand Down
67 changes: 48 additions & 19 deletions lua/atomic/libraries/class.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,48 @@ function classMt:__classname()
return tostring(self._classname)
end

--- Calls a current method of the parent class
---
--- # Example
--- ```lua
--- local Player = package:getClass("Player")
--- local User = package:class("User", Player) -- creating new class `User` that inherits class `Player`
---
--- function User:init(userData)
--- self:super(userData) -- calling `Player:init` with argument `userData`
--- end
---
--- function User:say(message)
--- self:super(message) -- calling `Player:say` with argument `message`
--- end
---
--- function User:die(reason)
--- -- you can also use the global `super` function
--- -- essentially, it does the same thing as `Class.super`
--- -- this function is needed to maintain compatibility with older versions
--- super(self, reason)
--- end
--- ```
---@protected
---@vararg any
function classMt:super(...)
local stack = debug.getinfo(2)
local methodName = stack.name

if (not methodName) then
return
end

local parent = atomic.class.getParent(self)
local method = parent and parent[methodName]

if (not parent or not method) then
return
end

method(self, ...)
end

--- Creates new class
---@param name string
---@param parent Atomic.Class?
Expand Down Expand Up @@ -173,24 +215,11 @@ function atomic.class.getParent(class)
return parent and parent.__index
end

--- Calls the `init` method in the parent class
---
--- # Example
--- ```lua
--- local Player = package:getClass("Player")
--- local User = package:class("User", Player) -- creating new class `User` that inherits class `Player`
---
--- function User:init()
--- super(self) -- calling Player:init() method
--- end
--- ```
---@param instance Atomic.Class
---@vararg ...
function atomic.class.super(instance, ...)
local parent = atomic.class.getParent(instance)

assert(parent, "class has no parent")
assert(isfunction(parent.init), "parent class has no init method")
-- just compatibility for older versions
super = classMt.super

parent.init(instance, ...)
---@param instance any
---@param class Atomic.Class
function atomic.class.isInstanceOf(instance, class)
return getmetatable(instance) == class
end
68 changes: 0 additions & 68 deletions lua/atomic/libraries/git.lua

This file was deleted.

1 change: 1 addition & 0 deletions lua/atomic/utils/aliases.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ new = atomic.class.new
super = atomic.class.super
current = atomic.package.current
async = coroutine.start
isInstanceOf = atomic.class.isInstanceOf

Instant = atomic.time.newInstant
Duration = atomic.time.newDuration
Expand Down
3 changes: 1 addition & 2 deletions lua/autorun/atomic_autorun.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ atomic = {
meta = {
author = "smokingplaya",
versionName = "Cherry",
version = "1.0.0-alpha.4",
version = "1.0.0-alpha.5",
},
_config = {}
}
Expand Down Expand Up @@ -45,7 +45,6 @@ client("atomic/libraries/webview/class.lua")
client("atomic/libraries/webview/common.lua")
client("atomic/libraries/webview/basicfuncs.lua")
server("atomic/libraries/command/common.lua")
server("atomic/libraries/git.lua")
-- package loading should be one of the latest
shared("atomic/libraries/package/common.lua")
server("atomic/libraries/mysql.lua")
Expand Down