misc: small fix or general refactoring i did not bother commenting

This commit is contained in:
Simone Margaritelli 2024-09-22 17:40:23 +02:00
commit 209725d623
5 changed files with 43 additions and 2 deletions

View file

@ -95,6 +95,27 @@ func jsOnEventFunc(call otto.FunctionCall) otto.Value {
return js.NullValue
}
func jsSaveToFileFunc(call otto.FunctionCall) otto.Value {
argv := call.ArgumentList
argc := len(argv)
if argc != 2 {
return js.ReportError("saveToFile accepts two string arguments")
} else if argv[0].IsString() == false {
return js.ReportError("saveToFile accepts two string arguments")
} else if argv[1].IsString() == false {
return js.ReportError("saveToFile accepts two string arguments")
}
fileName := argv[0].String()
data := argv[1].String()
if err := ioutil.WriteFile(fileName, []byte(data), os.ModePerm); err != nil {
return js.ReportError("error writing to '%s': %v", fileName, err)
}
return js.NullValue
}
func jsSaveJSONFunc(call otto.FunctionCall) otto.Value {
argv := call.ArgumentList
argc := len(argv)

View file

@ -328,6 +328,7 @@ func (s *Session) Start() error {
plugin.Defines["fileExists"] = jsFileExistsFunc
plugin.Defines["loadJSON"] = jsLoadJSONFunc
plugin.Defines["saveJSON"] = jsSaveJSONFunc
plugin.Defines["saveToFile"] = jsSaveToFileFunc
plugin.Defines["onEvent"] = jsOnEventFunc
plugin.Defines["session"] = s