diff --git a/api.go b/api.go index bdab365..94263a2 100644 --- a/api.go +++ b/api.go @@ -107,17 +107,17 @@ func hilbishLoad(rtm *rt.Runtime) (rt.Value, func()) { The nice lil shell for {blue}Lua{reset} fanatics! Check out the {blue}{bold}guide{reset} command to get started. ` - util.SetField(rtm, mod, "ver", rt.StringValue(version), "Hilbish version") - util.SetField(rtm, mod, "user", rt.StringValue(username), "Username of user") - util.SetField(rtm, mod, "host", rt.StringValue(host), "Host name of the machine") - util.SetField(rtm, mod, "home", rt.StringValue(curuser.HomeDir), "Home directory of the user") - util.SetField(rtm, mod, "dataDir", rt.StringValue(dataDir), "Directory for Hilbish's data files") - util.SetField(rtm, mod, "interactive", rt.BoolValue(interactive), "If this is an interactive shell") - util.SetField(rtm, mod, "login", rt.BoolValue(login), "Whether this is a login shell") - util.SetField(rtm, mod, "greeting", rt.StringValue(greeting), "Hilbish's welcome message for interactive shells. It has Lunacolors formatting.") - util.SetField(rtm, mod, "vimMode", rt.NilValue, "Current Vim mode of Hilbish (nil if not in Vim mode)") - util.SetField(rtm, mod, "exitCode", rt.IntValue(0), "Exit code of last exected command") - util.Document(mod, "Hilbish's core API, containing submodules and functions which relate to the shell itself.") + util.SetFieldProtected(fakeMod, mod, "ver", rt.StringValue(version), "Hilbish version") + util.SetFieldProtected(fakeMod, mod, "user", rt.StringValue(username), "Username of user") + util.SetFieldProtected(fakeMod, mod, "host", rt.StringValue(host), "Host name of the machine") + util.SetFieldProtected(fakeMod, mod, "home", rt.StringValue(curuser.HomeDir), "Home directory of the user") + util.SetFieldProtected(fakeMod, mod, "dataDir", rt.StringValue(dataDir), "Directory for Hilbish's data files") + util.SetFieldProtected(fakeMod, mod, "interactive", rt.BoolValue(interactive), "If this is an interactive shell") + util.SetFieldProtected(fakeMod, mod, "login", rt.BoolValue(login), "Whether this is a login shell") + util.SetFieldProtected(fakeMod, mod, "greeting", rt.StringValue(greeting), "Hilbish's welcome message for interactive shells. It has Lunacolors formatting.") + util.SetFieldProtected(fakeMod, mod, "vimMode", rt.NilValue, "Current Vim mode of Hilbish (nil if not in Vim mode)") + util.SetFieldProtected(fakeMod, mod, "exitCode", rt.IntValue(0), "Exit code of last exected command") + util.Document(fakeMod, "Hilbish's core API, containing submodules and functions which relate to the shell itself.") // hilbish.userDir table hshuser := rt.NewTable() diff --git a/util/util.go b/util/util.go index e46d4b2..6b5860e 100644 --- a/util/util.go +++ b/util/util.go @@ -25,14 +25,20 @@ func Document(module *rt.Table, doc string) { // It is accessible via the __docProp metatable. It is a table of the names of the fields. func SetField(rtm *rt.Runtime, module *rt.Table, field string, value rt.Value, doc string) { // TODO: ^ rtm isnt needed, i should remove it + SetFieldDoc(module, field, doc) + module.Set(rt.StringValue(field), value) +} + +// SetFieldDoc sets the __docProp metatable for a field on the +// module. +func SetFieldDoc(module *rt.Table, field, doc string) { mt := module.Metatable() - + if mt == nil { mt = rt.NewTable() - module.SetMetatable(mt) } - + docProp := mt.Get(rt.StringValue("__docProp")) if docProp == rt.NilValue { docPropTbl := rt.NewTable() @@ -41,7 +47,15 @@ func SetField(rtm *rt.Runtime, module *rt.Table, field string, value rt.Value, d } docProp.AsTable().Set(rt.StringValue(field), rt.StringValue(doc)) - module.Set(rt.StringValue(field), value) +} + +// SetFieldProtected sets a field in a protected table. A protected table +// is one which has a metatable proxy to ensure no overrides happen to it. +// It sets the field in the table and sets the __docProp metatable on the +// user facing table. +func SetFieldProtected(module, realModule *rt.Table, field string, value rt.Value, doc string) { + SetFieldDoc(module, field, doc) + realModule.Set(rt.StringValue(field), value) } // DoString runs the code string in the Lua runtime.