|
8 | 8 | "strings" |
9 | 9 |
|
10 | 10 | "github.com/tidwall/tile38/internal/field" |
| 11 | + "github.com/tidwall/tile38/internal/log" |
11 | 12 | lua "github.com/yuin/gopher-lua" |
12 | 13 | luajson "layeh.com/gopher-json" |
13 | 14 | ) |
@@ -160,41 +161,50 @@ func luaSetField(tbl *lua.LTable, name string, val field.Value) { |
160 | 161 | } |
161 | 162 |
|
162 | 163 | func (whereeval whereevalT) match(fieldsWithNames map[string]field.Value, |
163 | | - id string, props string) (bool, error, |
| 164 | + id string, props string) (ok bool, err error, |
164 | 165 | ) { |
165 | 166 | fieldsTbl := whereeval.luaState.CreateTable(0, len(fieldsWithNames)) |
166 | 167 | for name, val := range fieldsWithNames { |
167 | 168 | luaSetField(fieldsTbl, name, val) |
168 | 169 | } |
169 | | - var lprops lua.LValue |
| 170 | + propsTbl := lua.LValue(whereeval.luaState.CreateTable(0, 0)) |
170 | 171 | if props != "" { |
171 | 172 | var err error |
172 | | - lprops, err = luajson.Decode(whereeval.luaState, []byte(props)) |
173 | | - if err != nil { |
174 | | - lprops = lua.LNil |
| 173 | + tbl, err := luajson.Decode(whereeval.luaState, []byte(props)) |
| 174 | + if err == nil { |
| 175 | + propsTbl = tbl |
175 | 176 | } |
176 | 177 | } |
177 | | - // lua.LTString |
178 | 178 | luaSetRawGlobals( |
179 | 179 | whereeval.luaState, map[string]lua.LValue{ |
180 | 180 | "ID": lua.LString(id), |
181 | 181 | "FIELDS": fieldsTbl, |
182 | | - "PROPERTIES": lprops, |
183 | | - }) |
184 | | - defer luaSetRawGlobals( |
185 | | - whereeval.luaState, map[string]lua.LValue{ |
186 | | - "ID": lua.LNil, |
187 | | - "FIELDS": lua.LNil, |
188 | | - "PROPERTIES": lua.LNil, |
| 182 | + "PROPERTIES": propsTbl, |
189 | 183 | }) |
| 184 | + defer func() { |
| 185 | + luaSetRawGlobals( |
| 186 | + whereeval.luaState, map[string]lua.LValue{ |
| 187 | + "ID": lua.LNil, |
| 188 | + "FIELDS": lua.LNil, |
| 189 | + "PROPERTIES": lua.LNil, |
| 190 | + }) |
| 191 | + }() |
190 | 192 |
|
191 | 193 | whereeval.luaState.Push(whereeval.fn) |
192 | 194 | if err := whereeval.luaState.PCall(0, 1, nil); err != nil { |
| 195 | + emsg := err.Error() |
| 196 | + if strings.Contains(emsg, "attempt to index a non-table") { |
| 197 | + log.Debugf("Lua error: %v", emsg) |
| 198 | + return false, nil |
| 199 | + } |
193 | 200 | return false, err |
194 | 201 | } |
195 | 202 | ret := whereeval.luaState.Get(-1) |
196 | 203 | whereeval.luaState.Pop(1) |
197 | 204 |
|
| 205 | + if ret == nil { |
| 206 | + return false, nil |
| 207 | + } |
198 | 208 | // Make bool out of returned lua value |
199 | 209 | switch ret.Type() { |
200 | 210 | case lua.LTNil: |
|
0 commit comments