summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2017-07-05 16:52:50 +0200
committerAki <nthirtyone@gmail.com>2017-07-05 16:52:50 +0200
commiteacf0f60c0752616407fbca6cd9d8ca9757ce624 (patch)
tree0147b37fedd6f2df0116ddefcc12e3b76de8ec48
downloadobject-eacf0f60c0752616407fbca6cd9d8ca9757ce624.zip
object-eacf0f60c0752616407fbca6cd9d8ca9757ce624.tar.gz
object-eacf0f60c0752616407fbca6cd9d8ca9757ce624.tar.bz2
Initial commit
-rw-r--r--Object.lua83
-rw-r--r--example.lua32
-rw-r--r--license21
-rw-r--r--readme.md39
-rw-r--r--test.lua95
5 files changed, 270 insertions, 0 deletions
diff --git a/Object.lua b/Object.lua
new file mode 100644
index 0000000..9b9080b
--- /dev/null
+++ b/Object.lua
@@ -0,0 +1,83 @@
+local extends
+
+--- Metatable used for each class created.
+-- `__call` is an instance constructor.
+-- `__index` and `_index` are used when accessing variables inside of class and its hierarchy when accessing class itself.
+local metaclass do
+ local function _index (self, key)
+ local super = rawget(self, "__super")
+ if super then
+ local value = rawget(super, key)
+ if value ~= nil then
+ return value
+ end
+ return _index(super, key)
+ end
+ end
+ metaclass = {
+ __index = function (self, key)
+ if key == "extends" then
+ return extends
+ end
+ return _index(self, key)
+ end,
+ __call = function (self, ...)
+ local o = setmetatable({}, self)
+ self.new(o, ...)
+ return o
+ end
+ }
+end
+
+--- Method for checking if instance is of selected class.
+-- Usage: `instance:is(Object)`.
+-- Also works with classes: `Object:is(Object)`.
+local is do
+ local function _is (self, class)
+ if self then
+ if self == class then
+ return true
+ end
+ return _is(rawget(self, "__super"), class)
+ end
+ end
+ function is (self, class)
+ local mt = getmetatable(self)
+ if mt ~= metaclass then
+ return _is(mt, class)
+ end
+ return _is(self, class)
+ end
+end
+
+--- Indexing metamethod for instances.
+local index do
+ local function _index (self, key)
+ if self then
+ local value = rawget(self, key)
+ if value ~= nil then
+ return value
+ end
+ return _index(rawget(self, "__super"), key)
+ end
+ end
+ function index (self, key)
+ return _index(getmetatable(self), key)
+ end
+end
+
+--- Class creation/extension function.
+-- Class table is used as metatable for instance of that class. `Metaclass` table is used as metatable for classes.
+function extends (parent)
+ local self = setmetatable({}, metaclass)
+ rawset(self, "__class", self)
+ rawset(self, "__index", index)
+ rawset(self, "__super", parent)
+ return self
+end
+
+--- Base class used as root for class hierarchy.
+local Object = extends(nil)
+rawset(Object, "new", function () end)
+rawset(Object, "is", is)
+return Object
diff --git a/example.lua b/example.lua
new file mode 100644
index 0000000..47d25c6
--- /dev/null
+++ b/example.lua
@@ -0,0 +1,32 @@
+local Object = require "Object"
+
+local Point = Object:extends() -- creating new class by extending basic class
+function Point:new (x, y) -- defining function called by constructor for newly created class
+ self.x = x -- instance created by constructor is `self`
+ self.y = y
+end -- no returns needed
+
+Point.NUMBER = 10 -- setting some global values for class
+
+function Point:__tostring () -- metamethod used with `tostring()`
+ return "Point<" .. self.x .. ", " .. self.y .. ">"
+end
+
+local a = Point(3, 2) -- create new instance by calling class
+print(tostring(a))
+
+assert(a:is(Point)) -- you can also check if object is instance of class
+assert(a:is(Object)) -- use `is()` method for it
+
+local Foo = Point:extends() -- you can extend whatever class you want
+function Foo:new (str, x, y)
+ self.__super.new(self, x, y) -- call super constructor first
+ self.str = str -- do whatever else is left to be done
+end
+
+function Foo:__tostring () -- metamethods are not inherited
+ return "Foo(" .. self.str .. ")<" .. self.x .. ", " .. self.y .. ">"
+end
+
+local b = Foo("lorem", 7, -1)
+print(tostring(b))
diff --git a/license b/license
new file mode 100644
index 0000000..d547d96
--- /dev/null
+++ b/license
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2017 Aki
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/readme.md b/readme.md
new file mode 100644
index 0000000..77f7461
--- /dev/null
+++ b/readme.md
@@ -0,0 +1,39 @@
+# Object.lua
+## Description
+**Object.lua** is a really small lua module used for class creation that supports single-parent inheritance (Java-like) and metamethods. Initially it was created with three different approaches for fun but it eventually matured enough to be used. It was tested and works just fine on Lua 5.1 and up.
+## Example
+*This is content of `example.lua`.*
+```lua
+local Object = require "Object"
+
+local Point = Object:extends() -- creating new class by extending basic class
+function Point:new (x, y) -- defining function called by constructor for newly created class
+ self.x = x -- instance created by constructor is `self`
+ self.y = y
+end -- no returns needed
+
+Point.NUMBER = 10 -- setting some global values for class
+
+function Point:__tostring () -- metamethod used with `tostring()`
+ return "Point<" .. self.x .. ", " .. self.y .. ">"
+end
+
+local a = Point(3, 2) -- create new instance by calling class
+print(tostring(a))
+
+assert(a:is(Point)) -- you can also check if object is instance of class
+assert(a:is(Object)) -- use `is()` method for it
+
+local Foo = Point:extends() -- you can extend whatever class you want
+function Foo:new (str, x, y)
+ self.__super.new(self, x, y) -- call super constructor first
+ self.str = str -- do whatever else is left to be done
+end
+
+function Foo:__tostring () -- metamethods are not inherited
+ return "Foo(" .. self.str .. ")<" .. self.x .. ", " .. self.y .. ">"
+end
+
+local b = Foo("lorem", 7, -1)
+print(tostring(b))
+```
diff --git a/test.lua b/test.lua
new file mode 100644
index 0000000..e66bd55
--- /dev/null
+++ b/test.lua
@@ -0,0 +1,95 @@
+local Object = require "Object"
+
+assert(Object)
+assert(Object.extends)
+assert(Object.new)
+
+local object = Object()
+
+assert(object)
+assert(object:is(Object))
+assert(not object:is(Child))
+assert(not object:is(GrandChild))
+assert(not object:is(Sibling))
+assert(not object:is(Meta))
+assert(not object:is(MetaChild))
+
+local Child = Object:extends()
+local GrandChild = Child:extends()
+
+function Child:new (a)
+ self.a = a
+end
+function GrandChild:new (b, a)
+ self.__super.new(self, a)
+ self.b = b
+end
+
+Child.NUMBER = 3
+GrandChild.NUMBER = 11
+
+local a = 10
+local b = 15
+local child = Child(a)
+local grandChild = GrandChild(b, a)
+
+assert(child:is(Object))
+assert(child:is(Child))
+assert(not child:is(GrandChild))
+
+assert(grandChild:is(Object))
+assert(grandChild:is(Child))
+assert(grandChild:is(GrandChild))
+
+assert(child.NUMBER == Child.NUMBER)
+assert(not (child.NUMBER == GrandChild.NUMBER))
+assert(not (grandChild.NUMBER == Child.NUMBER))
+assert(grandChild.NUMBER == GrandChild.NUMBER)
+
+assert(child.a == a)
+assert(grandChild.a == a and grandChild.b == b)
+
+assert(GrandChild.__class == GrandChild)
+assert(GrandChild.__super == Child)
+assert(grandChild.__class == GrandChild)
+assert(grandChild.__super == Child)
+assert(grandChild.__class.__super == Child)
+assert(grandChild.__super.__super == Object)
+
+local Sibling = Object:extends()
+
+function Sibling:new (p)
+ self.p = p ^ 2
+end
+
+local p = 6
+local sibling = Sibling(p)
+
+assert(not sibling.NUMBER)
+assert(sibling.p == p ^ 2)
+
+local Meta = Object:extends()
+
+function Meta:new (x, y)
+ self.x = x
+ self.y = y
+end
+function Meta.__add (self, op)
+ return self.__class(self.x + op.x, self.y + op.y)
+end
+
+local x = 5
+local y = -2
+local meta = Meta(x, y)
+local meta2 = Meta(x, y)
+
+assert(rawget(getmetatable(meta), "__add"))
+
+local sum = meta + meta2
+
+assert(sum:is(Meta))
+assert((sum.x == meta.x + meta2.x) and (sum.y == meta.y + meta2.y))
+
+io.write("\27[1;32;40mSuccess\27[0m\n")
+io.flush()
+return true