fix(util): SetField on a table with a metatable causing panic

insensitive-tab^2
TorchedSammy 2022-04-21 20:33:32 -04:00
parent bd35e3b871
commit abfd4e5196
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 6 additions and 2 deletions

View File

@ -29,12 +29,16 @@ func SetField(rtm *rt.Runtime, module *rt.Table, field string, value rt.Value, d
if mt == nil { if mt == nil {
mt = rt.NewTable() mt = rt.NewTable()
docProp := rt.NewTable()
mt.Set(rt.StringValue("__docProp"), rt.TableValue(docProp))
module.SetMetatable(mt) module.SetMetatable(mt)
} }
docProp := mt.Get(rt.StringValue("__docProp")) docProp := mt.Get(rt.StringValue("__docProp"))
if docProp == rt.NilValue {
docPropTbl := rt.NewTable()
mt.Set(rt.StringValue("__docProp"), rt.TableValue(docPropTbl))
docProp = mt.Get(rt.StringValue("__docProp"))
}
docProp.AsTable().Set(rt.StringValue(field), rt.StringValue(doc)) docProp.AsTable().Set(rt.StringValue(field), rt.StringValue(doc))
module.Set(rt.StringValue(field), value) module.Set(rt.StringValue(field), value)