mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-17 18:30:45 +00:00
This changes the []byte <-> Uint8Array conversion to use an ArrayBuffer, avoiding inefficient copying of the slice data in Goja. Co-authored-by: Sina Mahmoodi <1591639+s1na@users.noreply.github.com> Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
parent
900b333241
commit
241ad1d635
1 changed files with 4 additions and 11 deletions
|
|
@ -54,11 +54,7 @@ type fromBufFn = func(vm *goja.Runtime, buf goja.Value, allowString bool) ([]byt
|
||||||
|
|
||||||
func toBuf(vm *goja.Runtime, bufType goja.Value, val []byte) (goja.Value, error) {
|
func toBuf(vm *goja.Runtime, bufType goja.Value, val []byte) (goja.Value, error) {
|
||||||
// bufType is usually Uint8Array. This is equivalent to `new Uint8Array(val)` in JS.
|
// bufType is usually Uint8Array. This is equivalent to `new Uint8Array(val)` in JS.
|
||||||
res, err := vm.New(bufType, vm.ToValue(val))
|
return vm.New(bufType, vm.ToValue(vm.NewArrayBuffer(val)))
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return vm.ToValue(res), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString bool) ([]byte, error) {
|
func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString bool) ([]byte, error) {
|
||||||
|
|
@ -69,6 +65,7 @@ func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString b
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return common.FromHex(obj.String()), nil
|
return common.FromHex(obj.String()), nil
|
||||||
|
|
||||||
case "Array":
|
case "Array":
|
||||||
var b []byte
|
var b []byte
|
||||||
if err := vm.ExportTo(buf, &b); err != nil {
|
if err := vm.ExportTo(buf, &b); err != nil {
|
||||||
|
|
@ -80,10 +77,7 @@ func fromBuf(vm *goja.Runtime, bufType goja.Value, buf goja.Value, allowString b
|
||||||
if !obj.Get("constructor").SameAs(bufType) {
|
if !obj.Get("constructor").SameAs(bufType) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
var b []byte
|
b := obj.Get("buffer").Export().(goja.ArrayBuffer).Bytes()
|
||||||
if err := vm.ExportTo(buf, &b); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("invalid buffer type")
|
return nil, fmt.Errorf("invalid buffer type")
|
||||||
|
|
@ -764,7 +758,7 @@ func (co *contractObj) GetValue() goja.Value {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (co *contractObj) GetInput() goja.Value {
|
func (co *contractObj) GetInput() goja.Value {
|
||||||
input := co.contract.Input
|
input := common.CopyBytes(co.contract.Input)
|
||||||
res, err := co.toBuf(co.vm, input)
|
res, err := co.toBuf(co.vm, input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
co.vm.Interrupt(err)
|
co.vm.Interrupt(err)
|
||||||
|
|
@ -883,7 +877,6 @@ func (r *callframeResult) GetError() goja.Value {
|
||||||
return r.vm.ToValue(r.err.Error())
|
return r.vm.ToValue(r.err.Error())
|
||||||
}
|
}
|
||||||
return goja.Undefined()
|
return goja.Undefined()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *callframeResult) setupObject() *goja.Object {
|
func (r *callframeResult) setupObject() *goja.Object {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue