Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

Commit

Permalink
Fixed dennwc#48
Browse files Browse the repository at this point in the history
  • Loading branch information
noartem committed Oct 23, 2019
1 parent 77e974c commit 8de3c9a
Showing 1 changed file with 75 additions and 79 deletions.
154 changes: 75 additions & 79 deletions js/bytes.go
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}
// }

1 comment on commit 8de3c9a

@noartem
Copy link
Member Author

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

Please sign in to comment.