Files
gopeed/pkg/download/engine/inject/file/module.go
2023-10-16 09:47:31 +08:00

33 lines
715 B
Go

package file
import (
"errors"
"github.com/dop251/goja"
"io"
)
type File struct {
io.Reader `json:""`
io.Closer `json:""`
Name string `json:"name"`
Size int64 `json:"size"`
}
func NewJsFile(runtime *goja.Runtime) (goja.Value, error) {
fileCtor, ok := goja.AssertConstructor(runtime.Get("File"))
if !ok {
return nil, errors.New("file is not defined")
}
return fileCtor(nil)
}
func Enable(runtime *goja.Runtime) error {
file := runtime.ToValue(func(call goja.ConstructorCall) *goja.Object {
instance := &File{}
instanceValue := runtime.ToValue(instance).(*goja.Object)
instanceValue.SetPrototype(call.This.Prototype())
return instanceValue
})
return runtime.Set("File", file)
}