summary refs log tree commit diff stats
path: root/src/cratera.cratera.d/lib.cratera
diff options
context:
space:
mode:
Diffstat (limited to 'src/cratera.cratera.d/lib.cratera')
-rw-r--r--src/cratera.cratera.d/lib.cratera53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/cratera.cratera.d/lib.cratera b/src/cratera.cratera.d/lib.cratera
index 69ca476..7e04c83 100644
--- a/src/cratera.cratera.d/lib.cratera
+++ b/src/cratera.cratera.d/lib.cratera
@@ -16,12 +16,16 @@
     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 --]]
 
+-- (post-)bootstrap bits
+
 local cratera_env = _G
+-- the "old" loader
 local cratera_loader_base = cratera_env.load
 
 -- only use setfenv if lua load doesn't support env argument
 local setfenv = (lua.loadstring or lua.load)("return setfenv", nil, nil, {})()
 
+-- replace the loader
 function cratera_env.load(chunk, chunkname, mode, env)
     if env == nil then
         env = cratera_env
@@ -41,3 +45,52 @@ end
 -- cratera opts not to support dostring/dofile.
 cratera_env.dostring = false
 cratera_env.dofile = false
+
+-- "proper" stdlib from now on
+
+local setmetatable = setmetatable
+
+-- traits aren't (currently) special, they're just unique tables that can be
+-- used as keys
+function cratera_env.mktrait()
+    return {}
+end
+
+-- all structs implement the Struct trait
+cratera_env.Struct = mktrait()
+-- the Struct trait has no members of its own but just points at the struct
+-- type itself.
+
+-- (should be upvalue, not global)
+local Struct = cratera_env.Struct
+-- structs are a little special
+function cratera_env.mkstruct(constructor, meta)
+    local struct = {}
+    struct[Struct] = struct
+    meta = meta or {}
+    meta.__index = struct
+    setmetatable(struct, {__call=function(...)
+        return setmetatable(constructor(...), meta)
+    end})
+    return struct
+end
+
+cratera_env.Iter = mktrait()
+
+function Iter:iter()
+    return [[
+Returns an iterator function, a state, and an initial value, for use in a
+regular generic `for` loop.
+
+# Examples
+
+```cratera
+local bucketlib = require "cratera.lib.bucket"
+
+local map = bucketlib.BucketMap()
+
+for k,v in map:[Iter].iter() do
+end
+```
+]]
+end