This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
forked from dennwc/dom
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
75 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,91 @@ | ||
package js | ||
|
||
import ( | ||
"fmt" | ||
"syscall/js" | ||
) | ||
// It seems like this code doesn't used in any place so i just comment it to fix go1.12 -> go1.13 migration | ||
|
||
var _ Wrapper = TypedArray{} | ||
// var _ Wrapper = TypedArray{} | ||
|
||
// TypedArray represents a JavaScript typed array. | ||
type TypedArray struct { | ||
Value | ||
} | ||
// // TypedArray represents a JavaScript typed array. | ||
// type TypedArray struct { | ||
// Value | ||
// } | ||
|
||
// Release frees up resources allocated for the typed array. | ||
// The typed array and its buffer must not be accessed after calling Release. | ||
func (v TypedArray) Release() { | ||
v.Release() | ||
} | ||
// // Release frees up resources allocated for the typed array. | ||
// // The typed array and its buffer must not be accessed after calling Release. | ||
// func (v TypedArray) Release() { | ||
// v.Release() | ||
// } | ||
|
||
// TypedArrayOf returns a JavaScript typed array backed by the slice's underlying array. | ||
// | ||
// The supported types are []int8, []int16, []int32, []uint8, []uint16, []uint32, []float32 and []float64. | ||
// Passing an unsupported value causes a panic. | ||
// | ||
// TypedArray.Release must be called to free up resources when the typed array will not be used any more. | ||
func TypedArrayOf(o interface{}) TypedArray { | ||
v := js.TypedArrayOf(toJS(o)) | ||
return TypedArray{Value{v.Value}} | ||
} | ||
// // TypedArrayOf returns a JavaScript typed array backed by the slice's underlying array. | ||
// // | ||
// // The supported types are []int8, []int16, []int32, []uint8, []uint16, []uint32, []float32 and []float64. | ||
// // Passing an unsupported value causes a panic. | ||
// // | ||
// // TypedArray.Release must be called to free up resources when the typed array will not be used any more. | ||
// func TypedArrayOf(o interface{}) TypedArray { | ||
// v := js.TypedArrayOf(toJS(o)) | ||
// return TypedArray{Value{v.Value}} | ||
// } | ||
|
||
var _ Wrapper = (*Memory)(nil) | ||
// var _ Wrapper = (*Memory)(nil) | ||
|
||
type Memory struct { | ||
v TypedArray | ||
p []byte | ||
} | ||
// type Memory struct { | ||
// v TypedArray | ||
// p []byte | ||
// } | ||
|
||
func (m *Memory) Bytes() []byte { | ||
return m.p | ||
} | ||
// func (m *Memory) Bytes() []byte { | ||
// return m.p | ||
// } | ||
|
||
// CopyFrom copies binary data from JS object into Go buffer. | ||
func (m *Memory) CopyFrom(v Wrapper) error { | ||
var src Value | ||
switch v := v.(type) { | ||
case Value: | ||
src = v | ||
case TypedArray: | ||
src = v.Value | ||
default: | ||
src = Value{v.JSValue()} | ||
} | ||
// // CopyFrom copies binary data from JS object into Go buffer. | ||
// func (m *Memory) CopyFrom(v Wrapper) error { | ||
// var src Value | ||
// switch v := v.(type) { | ||
// case Value: | ||
// src = v | ||
// case TypedArray: | ||
// src = v.Value | ||
// default: | ||
// src = Value{v.JSValue()} | ||
// } | ||
|
||
switch { | ||
case src.InstanceOfClass("Uint8Array"): | ||
m.v.Call("set", src) | ||
return nil | ||
case src.InstanceOfClass("Blob"): | ||
r := New("FileReader") | ||
// switch { | ||
// case src.InstanceOfClass("Uint8Array"): | ||
// m.v.Call("set", src) | ||
// return nil | ||
// case src.InstanceOfClass("Blob"): | ||
// r := New("FileReader") | ||
|
||
cg := r.NewFuncGroup() | ||
defer cg.Release() | ||
done := cg.OneTimeTrigger("loadend") | ||
errc := cg.ErrorEventChan() | ||
// cg := r.NewFuncGroup() | ||
// defer cg.Release() | ||
// done := cg.OneTimeTrigger("loadend") | ||
// errc := cg.ErrorEventChan() | ||
|
||
r.Call("readAsArrayBuffer", src) | ||
select { | ||
case err := <-errc: | ||
return err | ||
case <-done: | ||
} | ||
cg.Release() | ||
arr := New("Uint8Array", r.Get("result")) | ||
return m.CopyFrom(arr) | ||
default: | ||
return fmt.Errorf("unsupported source type") | ||
} | ||
} | ||
// r.Call("readAsArrayBuffer", src) | ||
// select { | ||
// case err := <-errc: | ||
// return err | ||
// case <-done: | ||
// } | ||
// cg.Release() | ||
// arr := New("Uint8Array", r.Get("result")) | ||
// return m.CopyFrom(arr) | ||
// default: | ||
// return fmt.Errorf("unsupported source type") | ||
// } | ||
// } | ||
|
||
func (m *Memory) JSValue() Ref { | ||
return m.v.JSValue() | ||
} | ||
// func (m *Memory) JSValue() Ref { | ||
// return m.v.JSValue() | ||
// } | ||
|
||
func (m *Memory) Release() { | ||
m.v.Release() | ||
} | ||
// func (m *Memory) Release() { | ||
// m.v.Release() | ||
// } | ||
|
||
// MMap exposes memory of p to JS. | ||
// | ||
// Release must be called to free up resources when the memory will not be used any more. | ||
func MMap(p []byte) *Memory { | ||
v := TypedArrayOf(p) | ||
return &Memory{p: p, v: v} | ||
} | ||
// // MMap exposes memory of p to JS. | ||
// // | ||
// // Release must be called to free up resources when the memory will not be used any more. | ||
// func MMap(p []byte) *Memory { | ||
// return &Memory{p: p} | ||
// } |
8de3c9a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like this code doesn't used in any place so i just comment it to fix go1.12 -> go1.13 migration