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

Removes text parser and renames project to wabin #2

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Improves experience of commands like `make format` on Windows
* text=auto eol=lf
68 changes: 68 additions & 0 deletions .github/workflows/commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Test
on:
pull_request:
branches: [main]
paths-ignore:
- '**/*.md'
push:
branches: [main]
paths-ignore:
- '**/*.md'

env: # Update this prior to requiring a higher minor version in go.mod
GO_VERSION: "1.18" # 1.xx == latest patch of 1.xx

defaults:
run: # use bash for all operating systems unless overridden
shell: bash

jobs:
check:
name: Pre-commit check, Go-${{ matrix.go-version }}
runs-on: ubuntu-20.04
strategy:
matrix: # use latest available versions and be consistent on all workflows!
go-version:
- "1.18" # == ${{ env.GO_VERSION }} because matrix cannot expand env variables
- "1.19"

steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
cache: true

- run: make check

test_amd64:
name: amd64, ${{ matrix.os }}, Go-${{ matrix.go-version }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false # don't fail fast as sometimes failures are arch/OS specific
matrix: # use latest available versions and be consistent on all workflows!
os: [ubuntu-20.04, macos-12, windows-2022]
go-version:
- "1.18" # == ${{ env.GO_VERSION }} because matrix cannot expand env variables
- "1.19"

steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
cache: true

- run: make test

- name: "Generate coverage report" # only once (not per OS)
if: runner.os == 'Linux'
run: make coverage

- name: "Upload coverage report" # only on main push and only once (not per OS)
if: github.event_name == 'push' && github.ref == 'refs/heads/main' && runner.os == 'Linux'
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: bash <(curl -s https://codecov.io/bash)
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

# Goland
.idea

# codecov.io
/coverage.txt

.DS_Store
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2020-2021 wazero authors
Copyright 2020-2022 wazero authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
63 changes: 63 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Make functions strip spaces and use commas to separate parameters. The below variables escape these characters.
comma := ,
space :=
space +=

goimports := golang.org/x/tools/cmd/goimports@v0.1.12
golangci_lint := github.com/golangci/golangci-lint/cmd/golangci-lint@v1.49.0

# Make 3.81 doesn't support '**' globbing: Set explicitly instead of recursion.
all_sources := $(wildcard *.go */*.go */*/*.go */*/*/*.go */*/*/*.go */*/*/*/*.go)
all_testdata := $(wildcard testdata/* */testdata/* */*/testdata/* */*/testdata/*/* */*/*/testdata/*)
all_testing := $(wildcard internal/testing/* internal/testing/*/* internal/testing/*/*/*)
# main_sources exclude any test or example related code
main_sources := $(wildcard $(filter-out %_test.go $(all_testdata) $(all_testing), $(all_sources)))
# main_packages collect the unique main source directories (sort will dedupe).
# Paths need to all start with ./, so we do that manually vs foreach which strips it.
main_packages := $(sort $(foreach f,$(dir $(main_sources)),$(if $(findstring ./,$(f)),./,./$(f))))

.PHONY: test
test:
@go test ./... -timeout 120s

.PHONY: coverage
coverpkg = $(subst $(space),$(comma),$(main_packages))
coverage: ## Generate test coverage
@go test -coverprofile=coverage.txt -covermode=atomic --coverpkg=$(coverpkg) $(main_packages)
@go tool cover -func coverage.txt

golangci_lint_path := $(shell go env GOPATH)/bin/golangci-lint

$(golangci_lint_path):
@go install $(golangci_lint)

golangci_lint_goarch ?= $(shell go env GOARCH)

.PHONY: lint
lint: $(golangci_lint_path)
@GOARCH=$(golangci_lint_goarch) CGO_ENABLED=0 $(golangci_lint_path) run --timeout 5m

.PHONY: format
format:
@find . -type f -name '*.go' | xargs gofmt -s -w
@for f in `find . -name '*.go'`; do \
awk '/^import \($$/,/^\)$$/{if($$0=="")next}{print}' $$f > /tmp/fmt; \
mv /tmp/fmt $$f; \
done
@go run $(goimports) -w -local github.com/tetratelabs/wabin `find . -name '*.go'`

.PHONY: check
check:
@$(MAKE) lint golangci_lint_goarch=arm64
@$(MAKE) lint golangci_lint_goarch=amd64
@$(MAKE) format
@go mod tidy
@if [ ! -z "`git status -s`" ]; then \
echo "The following differences will fail CI until committed:"; \
git diff --exit-code; \
fi

.PHONY: clean
clean: ## Ensure a clean build
@rm -rf dist build coverage.txt
@go clean -testcache
11 changes: 11 additions & 0 deletions RATIONALE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# wabin rationale

## Why wabin?
wabin is "wa" as it WebAssembly and "bin" as in Binary format.

This is intentionally similarly named to [wabt][1], although there will be a
small amount of overlap. In both cases, the names are puns, how [Elmer Fudd][2]
would say the word "rabbit" and "robin" respectively.

[1]: https://github.com/WebAssembly/wabt
[2]: https://en.wikipedia.org/wiki/Elmer_Fudd#Elmer-speak
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# watzero
# wabin: WebAssembly Binary Format in Go

watzero extracts functionality from a canceled WebAssembly Text Format feature
in wazero. This is not yet tested, and will only continue based on community
demand and help. Otherwise, it will be archived.
wabin includes WebAssembly an WebAssembly data model and binary encoder. Most
won't use this library. It mainly supports advanced manipulation of WebAssembly
binaries prior to instantiation with wazero. Notably, this has no dependencies,
so is cleaner to use in Go projects.
6 changes: 3 additions & 3 deletions binary/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"io"
"math"

"github.com/tetratelabs/watzero/leb128"
"github.com/tetratelabs/watzero/wasm"
"github.com/tetratelabs/wabin/leb128"
"github.com/tetratelabs/wabin/wasm"
)

func decodeCode(r *bytes.Reader) (*wasm.Code, error) {
Expand Down Expand Up @@ -79,7 +79,7 @@ func decodeCode(r *bytes.Reader) (*wasm.Code, error) {
return &wasm.Code{Body: body, LocalTypes: localTypes}, nil
}

// encodeCode returns the wasm.Code encoded in WebAssembly 1.0 (20191205) Binary Format.
// encodeCode returns the wasm.Code encoded in WebAssembly Binary Format.
//
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-code
func encodeCode(c *wasm.Code) []byte {
Expand Down
3 changes: 2 additions & 1 deletion binary/code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tetratelabs/watzero/wasm"

"github.com/tetratelabs/wabin/wasm"
)

var addLocalZeroLocalTwo = []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 2, wasm.OpcodeI32Add, wasm.OpcodeEnd}
Expand Down
14 changes: 7 additions & 7 deletions binary/const_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"bytes"
"fmt"

"github.com/tetratelabs/watzero/ieee754"
"github.com/tetratelabs/watzero/leb128"
"github.com/tetratelabs/watzero/wasm"
"github.com/tetratelabs/wabin/ieee754"
"github.com/tetratelabs/wabin/leb128"
"github.com/tetratelabs/wabin/wasm"
)

func decodeConstantExpression(r *bytes.Reader, enabledFeatures wasm.Features) (*wasm.ConstantExpression, error) {
func decodeConstantExpression(r *bytes.Reader, features wasm.CoreFeatures) (*wasm.ConstantExpression, error) {
b, err := r.ReadByte()
if err != nil {
return nil, fmt.Errorf("read opcode: %v", err)
Expand All @@ -33,7 +33,7 @@ func decodeConstantExpression(r *bytes.Reader, enabledFeatures wasm.Features) (*
case wasm.OpcodeGlobalGet:
_, _, err = leb128.DecodeUint32(r)
case wasm.OpcodeRefNull:
if err := enabledFeatures.Require(wasm.FeatureBulkMemoryOperations); err != nil {
if err := features.RequireEnabled(wasm.CoreFeatureBulkMemoryOperations); err != nil {
return nil, fmt.Errorf("ref.null is not supported as %w", err)
}
reftype, err := r.ReadByte()
Expand All @@ -43,13 +43,13 @@ func decodeConstantExpression(r *bytes.Reader, enabledFeatures wasm.Features) (*
return nil, fmt.Errorf("invalid type for ref.null: 0x%x", reftype)
}
case wasm.OpcodeRefFunc:
if err := enabledFeatures.Require(wasm.FeatureBulkMemoryOperations); err != nil {
if err := features.RequireEnabled(wasm.CoreFeatureBulkMemoryOperations); err != nil {
return nil, fmt.Errorf("ref.func is not supported as %w", err)
}
// Parsing index.
_, _, err = leb128.DecodeUint32(r)
case wasm.OpcodeVecPrefix:
if err := enabledFeatures.Require(wasm.FeatureSIMD); err != nil {
if err := features.RequireEnabled(wasm.CoreFeatureSIMD); err != nil {
return nil, fmt.Errorf("vector instructions are not supported as %w", err)
}
opcode, err = r.ReadByte()
Expand Down
25 changes: 13 additions & 12 deletions binary/const_expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/tetratelabs/watzero/wasm"

"github.com/tetratelabs/wabin/wasm"
)

func TestDecodeConstantExpression(t *testing.T) {
Expand Down Expand Up @@ -84,7 +85,7 @@ func TestDecodeConstantExpression(t *testing.T) {
tc := tt
t.Run(strconv.Itoa(i), func(t *testing.T) {
actual, err := decodeConstantExpression(bytes.NewReader(tc.in),
wasm.FeatureBulkMemoryOperations|wasm.FeatureSIMD)
wasm.CoreFeatureBulkMemoryOperations|wasm.CoreFeatureSIMD)
require.NoError(t, err)
require.Equal(t, tc.exp, actual)
})
Expand All @@ -95,22 +96,22 @@ func TestDecodeConstantExpression_errors(t *testing.T) {
tests := []struct {
in []byte
expectedErr string
features wasm.Features
features wasm.CoreFeatures
}{
{
in: []byte{
wasm.OpcodeRefFunc,
0,
},
expectedErr: "look for end opcode: EOF",
features: wasm.FeatureBulkMemoryOperations,
features: wasm.CoreFeatureBulkMemoryOperations,
},
{
in: []byte{
wasm.OpcodeRefNull,
},
expectedErr: "read reference type for ref.null: EOF",
features: wasm.FeatureBulkMemoryOperations,
features: wasm.CoreFeatureBulkMemoryOperations,
},
{
in: []byte{
Expand All @@ -119,7 +120,7 @@ func TestDecodeConstantExpression_errors(t *testing.T) {
wasm.OpcodeEnd,
},
expectedErr: "invalid type for ref.null: 0xff",
features: wasm.FeatureBulkMemoryOperations,
features: wasm.CoreFeatureBulkMemoryOperations,
},
{
in: []byte{
Expand All @@ -128,7 +129,7 @@ func TestDecodeConstantExpression_errors(t *testing.T) {
wasm.OpcodeEnd,
},
expectedErr: "ref.null is not supported as feature \"bulk-memory-operations\" is disabled",
features: wasm.Features20191205,
features: wasm.CoreFeaturesV1,
},
{
in: []byte{
Expand All @@ -137,7 +138,7 @@ func TestDecodeConstantExpression_errors(t *testing.T) {
wasm.OpcodeEnd,
},
expectedErr: "ref.func is not supported as feature \"bulk-memory-operations\" is disabled",
features: wasm.Features20191205,
features: wasm.CoreFeaturesV1,
},
{
in: []byte{
Expand All @@ -148,14 +149,14 @@ func TestDecodeConstantExpression_errors(t *testing.T) {
wasm.OpcodeEnd,
},
expectedErr: "vector instructions are not supported as feature \"simd\" is disabled",
features: wasm.Features20191205,
features: wasm.CoreFeaturesV1,
},
{
in: []byte{
wasm.OpcodeVecPrefix,
},
expectedErr: "read vector instruction opcode suffix: EOF",
features: wasm.FeatureSIMD,
features: wasm.CoreFeatureSIMD,
},
{
in: []byte{
Expand All @@ -165,7 +166,7 @@ func TestDecodeConstantExpression_errors(t *testing.T) {
wasm.OpcodeEnd,
},
expectedErr: "invalid vector opcode for const expression: 0x1",
features: wasm.FeatureSIMD,
features: wasm.CoreFeatureSIMD,
},
{
in: []byte{
Expand All @@ -174,7 +175,7 @@ func TestDecodeConstantExpression_errors(t *testing.T) {
1, 1, 1, 1, 1, 1, 1, 1,
},
expectedErr: "read vector const instruction immediates: needs 16 bytes but was 8 bytes",
features: wasm.FeatureSIMD,
features: wasm.CoreFeatureSIMD,
},
}

Expand Down
Loading