1
0
Fork 0

Adding upstream version 0.0~git20250409.f7acab6.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-22 11:36:18 +02:00
parent b9b5d88025
commit 21b930d007
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
51 changed files with 11229 additions and 0 deletions

84
goutil/argtypes.go Normal file
View file

@ -0,0 +1,84 @@
package goutil
import (
"math/big"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/errors"
)
func RequiredIntegerArgument(r *goja.Runtime, call goja.FunctionCall, name string, argIndex int) int64 {
arg := call.Argument(argIndex)
if goja.IsNumber(arg) {
return arg.ToInteger()
}
if goja.IsUndefined(arg) {
panic(errors.NewTypeError(r, errors.ErrCodeInvalidArgType, "The \"%s\" argument is required.", name))
}
panic(errors.NewArgumentNotNumberTypeError(r, name))
}
func RequiredFloatArgument(r *goja.Runtime, call goja.FunctionCall, name string, argIndex int) float64 {
arg := call.Argument(argIndex)
if goja.IsNumber(arg) {
return arg.ToFloat()
}
if goja.IsUndefined(arg) {
panic(errors.NewTypeError(r, errors.ErrCodeInvalidArgType, "The \"%s\" argument is required.", name))
}
panic(errors.NewArgumentNotNumberTypeError(r, name))
}
func CoercedIntegerArgument(call goja.FunctionCall, argIndex int, defaultValue int64, typeMistMatchValue int64) int64 {
arg := call.Argument(argIndex)
if goja.IsNumber(arg) {
return arg.ToInteger()
}
if goja.IsUndefined(arg) {
return defaultValue
}
return typeMistMatchValue
}
func OptionalIntegerArgument(r *goja.Runtime, call goja.FunctionCall, name string, argIndex int, defaultValue int64) int64 {
arg := call.Argument(argIndex)
if goja.IsNumber(arg) {
return arg.ToInteger()
}
if goja.IsUndefined(arg) {
return defaultValue
}
panic(errors.NewArgumentNotNumberTypeError(r, name))
}
func RequiredBigIntArgument(r *goja.Runtime, call goja.FunctionCall, name string, argIndex int) *big.Int {
arg := call.Argument(argIndex)
if goja.IsUndefined(arg) {
panic(errors.NewTypeError(r, errors.ErrCodeInvalidArgType, "The \"%s\" argument is required.", name))
}
if !goja.IsBigInt(arg) {
panic(errors.NewArgumentNotBigIntTypeError(r, name))
}
n, _ := arg.Export().(*big.Int)
if n == nil {
n = new(big.Int)
}
return n
}
func RequiredStringArgument(r *goja.Runtime, call goja.FunctionCall, name string, argIndex int) string {
arg := call.Argument(argIndex)
if goja.IsString(arg) {
return arg.String()
}
if goja.IsUndefined(arg) {
panic(errors.NewTypeError(r, errors.ErrCodeInvalidArgType, "The \"%s\" argument is required.", name))
}
panic(errors.NewArgumentNotStringTypeError(r, name))
}