withSymbols returns copy of module with selected symbols
(L *lua.LState, enabledSymbols []string)
| 50 | |
| 51 | // withSymbols returns copy of module with selected symbols |
| 52 | func (m luaModule) withSymbols(L *lua.LState, enabledSymbols []string) luaModule { |
| 53 | // gopher-lua does not have API to select enabled symbols, |
| 54 | // see https://github.com/yuin/gopher-lua/discussions/393 |
| 55 | // |
| 56 | // Instead collect symbols to disable as difference |
| 57 | // between all and enabled module symbols |
| 58 | allSymbols := make(map[string]struct{}) |
| 59 | |
| 60 | m.load(L) |
| 61 | m.table(L).ForEach(func(k, _ lua.LValue) { |
| 62 | if name, ok := k.(lua.LString); ok { |
| 63 | allSymbols[name.String()] = struct{}{} |
| 64 | } |
| 65 | }) |
| 66 | |
| 67 | for _, s := range enabledSymbols { |
| 68 | delete(allSymbols, s) |
| 69 | } |
| 70 | |
| 71 | result := luaModule{m.name, m.loader, nil} |
| 72 | for s := range allSymbols { |
| 73 | result.disabledSymbols = append(result.disabledSymbols, s) |
| 74 | } |
| 75 | return result |
| 76 | } |
| 77 | |
| 78 | func (m luaModule) table(L *lua.LState) *lua.LTable { |
| 79 | name := m.name |
no test coverage detected