go-ethereum/vendor/github.com/go-interpreter/wagon/wasm/encode.go
Guillaume Ballet 9927596822 core/vm: Add wagon/wasm as an interpreter
* Implement the ewasm module and the EEI.
 * Add an ewasm standalone binary to test standalone files
 * use reflection to declare EEI module exports
 * check the module name when importing EEI functions
 * make eeiFuncs a function to avoid an init loop issue in call
 * Add a reference to the ewasm tests
 * wagon implementation of the Interpreter interface
 * update to wagon's PR #59 and uses the Process structure
 * Adapt to endianness change
 * All tests but 1 passing
 * CanRun only checks for the header
 * Add a "debug" module to support "printHex" in contracts

Notes:
  * There are very little checks, e.g. of
     the call depths and so on.
2018-10-05 15:15:41 +02:00

70 lines
1.5 KiB
Go

// Copyright 2018 The go-interpreter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package wasm
import (
"bytes"
"encoding/binary"
"io"
"github.com/go-interpreter/wagon/wasm/leb128"
)
const currentVersion = 0x01
// EncodeModule writes a provided module to w using WASM binary encoding.
func EncodeModule(w io.Writer, m *Module) error {
if err := writeU32(w, Magic); err != nil {
return err
}
if err := writeU32(w, currentVersion); err != nil {
return err
}
sections := m.Sections
buf := new(bytes.Buffer)
for _, s := range sections {
if _, err := leb128.WriteVarUint32(w, uint32(s.SectionID())); err != nil {
return err
}
buf.Reset()
if err := s.WritePayload(buf); err != nil {
return err
}
if _, err := leb128.WriteVarUint32(w, uint32(buf.Len())); err != nil {
return err
}
if _, err := buf.WriteTo(w); err != nil {
return err
}
}
return nil
}
func writeStringUint(w io.Writer, s string) error {
return writeBytesUint(w, []byte(s))
}
func writeBytesUint(w io.Writer, p []byte) error {
_, err := leb128.WriteVarUint32(w, uint32(len(p)))
if err != nil {
return err
}
_, err = w.Write(p)
return err
}
func writeU32(w io.Writer, n uint32) error {
var buf [4]byte
binary.LittleEndian.PutUint32(buf[:], n)
_, err := w.Write(buf[:])
return err
}
func writeU64(w io.Writer, n uint64) error {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], n)
_, err := w.Write(buf[:])
return err
}