Improve parsing and error handling in js bindings.

This commit is contained in:
☸️ 2024-12-05 12:49:54 +01:00 committed by GitHub
commit 3cea30a277
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -9,24 +9,46 @@ import (
) )
func btoa(call otto.FunctionCall) otto.Value { func btoa(call otto.FunctionCall) otto.Value {
varValue := base64.StdEncoding.EncodeToString([]byte(call.Argument(0).String())) argv := call.ArgumentList
v, err := otto.ToValue(varValue) argc := len(argv)
if argc != 1 {
return ReportError("btoa: expected 1 argument, %d given instead.", argc)
}
arg := argv[0]
if (!arg.IsString()) {
return ReportError("btoa: single argument must be a string.")
}
encoded := base64.StdEncoding.EncodeToString([]byte(arg.String()))
v, err := otto.ToValue(encoded)
if err != nil { if err != nil {
return ReportError("Could not convert to string: %s", varValue) return ReportError("btoa: could not convert to string: %s", err.Error())
} }
return v return v
} }
func atob(call otto.FunctionCall) otto.Value { func atob(call otto.FunctionCall) otto.Value {
varValue, err := base64.StdEncoding.DecodeString(call.Argument(0).String()) argv := call.ArgumentList
if err != nil { argc := len(argv)
return ReportError("Could not decode string: %s", call.Argument(0).String()) if argc != 1 {
return ReportError("atob: expected 1 argument, %d given instead.", argc)
} }
v, err := otto.ToValue(string(varValue)) arg := argv[0]
if (!arg.IsString()) {
return ReportError("btoa: single argument must be a string.")
}
decoded, err := base64.StdEncoding.DecodeString(arg.String())
if err != nil { if err != nil {
return ReportError("Could not convert to string: %s", varValue) return ReportError("atob: could not decode string: %s", err.Error())
}
v, err := otto.ToValue(string(decoded))
if err != nil {
return ReportError("atob: could not convert to string: %s", decoded)
} }
return v return v
@ -39,7 +61,12 @@ func gzipCompress(call otto.FunctionCall) otto.Value {
return ReportError("gzipCompress: expected 1 argument, %d given instead.", argc) return ReportError("gzipCompress: expected 1 argument, %d given instead.", argc)
} }
uncompressedBytes := []byte(argv[0].String()) arg := argv[0]
if (!arg.IsString()) {
return ReportError("btoa: single argument must be a string.")
}
uncompressedBytes := []byte(arg.String())
var writerBuffer bytes.Buffer var writerBuffer bytes.Buffer
gzipWriter := gzip.NewWriter(&writerBuffer) gzipWriter := gzip.NewWriter(&writerBuffer)
@ -53,7 +80,7 @@ func gzipCompress(call otto.FunctionCall) otto.Value {
v, err := otto.ToValue(string(compressedBytes)) v, err := otto.ToValue(string(compressedBytes))
if err != nil { if err != nil {
return ReportError("Could not convert to string: %s", err.Error()) return ReportError("gzipCompress: could not convert to string: %s", err.Error())
} }
return v return v
@ -83,7 +110,7 @@ func gzipDecompress(call otto.FunctionCall) otto.Value {
decompressedBytes := decompressedBuffer.Bytes() decompressedBytes := decompressedBuffer.Bytes()
v, err := otto.ToValue(string(decompressedBytes)) v, err := otto.ToValue(string(decompressedBytes))
if err != nil { if err != nil {
return ReportError("Could not convert to string: %s", err.Error()) return ReportError("gzipDecompress: could not convert to string: %s", err.Error())
} }
return v return v