Example #1
0
// Helper fuctions
func nullFunc(helper *structure.Helper, values *structure.RequestData) []byte {
	// Check if the helper was defined in a plugin
	if plugins.LuaPool != nil {
		// Get a state map to execute and attach it to the request data
		if values.PluginVMs == nil {
			values.PluginVMs = plugins.LuaPool.Get(helper, values)
		}
		if values.PluginVMs[helper.Name] != nil {
			pluginResult, err := plugins.Execute(helper, values)
			if err != nil {
				return []byte{}
			}
			return evaluateEscape(pluginResult, helper.Unescaped)
		} else {
			// This helper is not implemented in a plugin. Get rid of the Lua VMs
			plugins.LuaPool.Put(values.PluginVMs)
			values.PluginVMs = nil
		}
	}
	log.Println("Warning: This helper is not implemented:", helper.Name)
	return []byte{}
}
Example #2
0
func Execute(helper *structure.Helper, values *structure.RequestData) ([]byte, error) {
	// Retrieve the lua state
	vm := values.PluginVMs[helper.Name]
	// Execute plugin
	err := vm.CallByParam(lua.P{Fn: vm.GetGlobal(helper.Name), NRet: 1, Protect: true})
	if err != nil {
		log.Println("Error while executing plugin for helper "+helper.Name+":", err)
		// Since the vm threw an error, close all vms and don't put the map back into the pool
		for _, luavm := range values.PluginVMs {
			luavm.Close()
		}
		values.PluginVMs = nil
		return []byte{}, err
	}
	// Get return value from vm
	ret := vm.ToString(-1)
	return []byte(ret), nil
}