You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/11/12 18:10:39 UTC

[GitHub] [beam] damondouglas commented on a diff in pull request #24081: Implement embedded WebAssembly example

damondouglas commented on code in PR #24081:
URL: https://github.com/apache/beam/pull/24081#discussion_r1020789962


##########
sdks/go/examples/wasm/wasm.go:
##########
@@ -0,0 +1,159 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// wasm is an EXPERIMENTAL simple example that loads and executes a wasm file function.
+// greet.wasm, Cargo.toml and greet.rs were copied from the example provided by the wazero library:
+// https://github.com/tetratelabs/wazero/blob/v1.0.0-pre.3/examples/allocation/rust/greet.go
+//
+// New Concepts:
+// 1. Load a wasm file compiled from: cargo build --release --target wasm32-unknown-unknown
+// 2. Execute a wasm function within a DoFn
+package main
+
+import (
+	"context"
+	_ "embed"
+	"flag"
+	"fmt"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/io/textio"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/register"
+	"github.com/apache/beam/sdks/v2/go/pkg/beam/x/beamx"
+	"github.com/tetratelabs/wazero"
+	"github.com/tetratelabs/wazero/api"
+	"log"
+)
+
+const (
+	wasmFunctionName           = "greeting"
+	wasmAllocateFunctionName   = "allocate"
+	wasmDeallocateFunctionName = "deallocate"
+)
+
+//go:embed greet.wasm
+var greetWasm []byte
+
+var (
+	output = flag.String("output", "", "Output file (required).")
+)
+
+func init() {
+	// register.DoFnXxY registers a struct DoFn so that it can be correctly
+	// serialized and does some optimization to avoid runtime reflection. Since
+	// embeddedWasmFn's ProcessElement func has 2 inputs (context.Context) and 2 outputs (string, error),
+	// we use register.DoFn2x2 and provide its input and output types as its constraints.
+	// Struct DoFns must be registered for a pipeline to run.
+	register.DoFn2x2[context.Context, string, string, error](&embeddedWasmFn{})
+}
+
+func preRun() error {
+	if *output == "" {
+		return fmt.Errorf("--output is required")
+	}
+	return nil
+}
+
+func main() {
+	flag.Parse()
+	beam.Init()
+	ctx := context.Background()
+	if err := preRun(); err != nil {
+		panic(err)
+	}
+	if err := run(ctx); err != nil {
+		panic(err)
+	}
+}
+
+func run(ctx context.Context) error {
+	p, s := beam.NewPipelineWithRoot()
+
+	in := beam.Create(s, "a", "b", "c", "d", "e", "f", "g", "h")

Review Comment:
   Thank you :-) I replaced with `"Ada", "Lovelace", "World", "Beam", "Senior López"`. The last one is so we can see this with unicode and because Senior López is an important 🐕 member of the Beam community.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org