From 7d487bfc986e2ba1f7197437f75aa26737d8bca0 Mon Sep 17 00:00:00 2001 From: sammyette Date: Thu, 11 Apr 2024 18:16:45 -0400 Subject: [PATCH] refactor: remove metatables protecting hilbish api and opts (#287) --- CHANGELOG.md | 2 ++ api.go | 61 ++++++++------------------------------------ nature/opts/init.lua | 16 +----------- 3 files changed, 14 insertions(+), 65 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 225770e..6b730b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ ### Changed - Remove usage of `hilbish.goro` in Greenhouse. +- Values in `hilbish` table are no longer protected. This means +they can be overridden. (#287) ## [2.2.1] - 2023-12-26 ### Fixed diff --git a/api.go b/api.go index 5f69eed..9470709 100644 --- a/api.go +++ b/api.go @@ -59,47 +59,8 @@ var hilbishLoader = packagelib.Loader{ } func hilbishLoad(rtm *rt.Runtime) (rt.Value, func()) { - fakeMod := rt.NewTable() - modmt := rt.NewTable() mod := rt.NewTable() - modIndex := func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - arg := c.Arg(1) - val := mod.Get(arg) - - return c.PushingNext1(t.Runtime, val), nil - } - modNewIndex := func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - k, err := c.StringArg(1) - if err != nil { - return nil, err - } - - v := c.Arg(2) - if k == "highlighter" { - var err error - // fine to assign, since itll be either nil or a closure - highlighter, err = c.ClosureArg(2) - if err != nil { - return nil, errors.New("hilbish.highlighter has to be a function") - } - } else if k == "hinter" { - var err error - hinter, err = c.ClosureArg(2) - if err != nil { - return nil, errors.New("hilbish.hinter has to be a function") - } - } else if modVal := mod.Get(rt.StringValue(k)); modVal != rt.NilValue { - return nil, errors.New("not allowed to override in hilbish table") - } - mod.Set(rt.StringValue(k), v) - - return c.Next(), nil - } - modmt.Set(rt.StringValue("__newindex"), rt.FunctionValue(rt.NewGoFunction(modNewIndex, "__newindex", 3, false))) - modmt.Set(rt.StringValue("__index"), rt.FunctionValue(rt.NewGoFunction(modIndex, "__index", 2, false))) - fakeMod.SetMetatable(modmt) - util.SetExports(rtm, mod, exports) hshMod = mod @@ -110,16 +71,16 @@ func hilbishLoad(rtm *rt.Runtime) (rt.Value, func()) { username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows } - util.SetFieldProtected(fakeMod, mod, "ver", rt.StringValue(getVersion())) - util.SetFieldProtected(fakeMod, mod, "goVersion", rt.StringValue(runtime.Version())) - util.SetFieldProtected(fakeMod, mod, "user", rt.StringValue(username)) - util.SetFieldProtected(fakeMod, mod, "host", rt.StringValue(host)) - util.SetFieldProtected(fakeMod, mod, "home", rt.StringValue(curuser.HomeDir)) - util.SetFieldProtected(fakeMod, mod, "dataDir", rt.StringValue(dataDir)) - util.SetFieldProtected(fakeMod, mod, "interactive", rt.BoolValue(interactive)) - util.SetFieldProtected(fakeMod, mod, "login", rt.BoolValue(login)) - util.SetFieldProtected(fakeMod, mod, "vimMode", rt.NilValue) - util.SetFieldProtected(fakeMod, mod, "exitCode", rt.IntValue(0)) + util.SetField(rtm, mod, "ver", rt.StringValue(getVersion())) + util.SetField(rtm, mod, "goVersion", rt.StringValue(runtime.Version())) + util.SetField(rtm, mod, "user", rt.StringValue(username)) + util.SetField(rtm, mod, "host", rt.StringValue(host)) + util.SetField(rtm, mod, "home", rt.StringValue(curuser.HomeDir)) + util.SetField(rtm, mod, "dataDir", rt.StringValue(dataDir)) + util.SetField(rtm, mod, "interactive", rt.BoolValue(interactive)) + util.SetField(rtm, mod, "login", rt.BoolValue(login)) + util.SetField(rtm, mod, "vimMode", rt.NilValue) + util.SetField(rtm, mod, "exitCode", rt.IntValue(0)) // hilbish.userDir table hshuser := userDirLoader(rtm) @@ -171,7 +132,7 @@ func hilbishLoad(rtm *rt.Runtime) (rt.Value, func()) { pluginModule := moduleLoader(rtm) mod.Set(rt.StringValue("module"), rt.TableValue(pluginModule)) - return rt.TableValue(fakeMod), nil + return rt.TableValue(mod), nil } func getenv(key, fallback string) string { diff --git a/nature/opts/init.lua b/nature/opts/init.lua index f4e9647..474ea3b 100644 --- a/nature/opts/init.lua +++ b/nature/opts/init.lua @@ -1,21 +1,7 @@ -local opts = {} hilbish.opts = {} -setmetatable(hilbish.opts, { - __newindex = function(_, k, v) - if opts[k] == nil then - error(string.format('opt %s does not exist', k)) - end - - opts[k] = v - end, - __index = function(_, k) - return opts[k] - end -}) - local function setupOpt(name, default) - opts[name] = default + hilbish.opts[name] = default pcall(require, 'nature.opts.' .. name) end