-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b873d99
commit 96773b3
Showing
11 changed files
with
252 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright © 2024 JR team | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
package wasm | ||
|
||
type Config struct { | ||
ModulePath string `json:"module_path"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright © 2024 JR team | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
package wasm | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" | ||
"os" | ||
"sync" | ||
|
||
"github.com/rs/zerolog/log" | ||
|
||
"github.com/tetratelabs/wazero" | ||
wazapi "github.com/tetratelabs/wazero/api" | ||
) | ||
|
||
type Producer struct { | ||
configuration Config | ||
lock sync.Mutex | ||
|
||
r wazero.Runtime | ||
m wazapi.Module | ||
f wazapi.Function | ||
|
||
stdin *bytes.Buffer | ||
stdout *bytes.Buffer | ||
} | ||
|
||
func (p *Producer) Initialize(ctx context.Context, configFile string) { | ||
cfgBytes, err := os.ReadFile(configFile) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to read config file") | ||
} | ||
|
||
config := Config{} | ||
if err := json.Unmarshal(cfgBytes, &config); err != nil { | ||
log.Fatal().Err(err).Msg("Failed to unmarshal config") | ||
} | ||
|
||
p.InitializeFromConfig(ctx, config) | ||
} | ||
|
||
func (p *Producer) InitializeFromConfig(ctx context.Context, config Config) { | ||
p.lock = sync.Mutex{} | ||
p.r = wazero.NewRuntime(ctx) | ||
|
||
// initialize WASI for stdin/out | ||
wasi_snapshot_preview1.MustInstantiate(ctx, p.r) | ||
|
||
moduleBytes, err := os.ReadFile(config.ModulePath) | ||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to read module file") | ||
} | ||
|
||
p.stdin = bytes.NewBuffer(nil) | ||
p.stdout = bytes.NewBuffer(nil) | ||
|
||
m, err := p.r.InstantiateWithConfig(ctx, moduleBytes, wazero.NewModuleConfig(). | ||
WithStdin(p.stdin). | ||
WithStdout(p.stdout), | ||
) | ||
|
||
if err != nil { | ||
log.Fatal().Err(err).Msg("unable to create WASM module") | ||
} | ||
|
||
p.m = m | ||
p.f = p.m.ExportedFunction("produce") | ||
} | ||
|
||
func (p *Producer) Produce(ctx context.Context, k []byte, v []byte, _ any) { | ||
p.lock.Lock() | ||
defer p.lock.Unlock() | ||
|
||
p.stdin.Reset() | ||
p.stdout.Reset() | ||
|
||
data, err := json.Marshal(map[string][]byte{ | ||
"k": k, | ||
"v": v, | ||
}) | ||
|
||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to serialize WASM request") | ||
} | ||
|
||
p.stdin.Write(data) | ||
res, err := p.f.Call(ctx, uint64(len(data))) | ||
|
||
if err != nil { | ||
log.Fatal().Err(err).Msg("Failed to invoke WASM function") | ||
} | ||
|
||
if res[0] > 0 { | ||
out := p.stdout.Bytes()[0:res[0]] | ||
log.Fatal().Err(errors.New(string(out))).Msg("Failed to execute WASM function") | ||
} | ||
|
||
} | ||
|
||
func (p *Producer) Close(ctx context.Context) error { | ||
if p.r == nil { | ||
if err := p.r.Close(ctx); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright © 2024 JR team | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
package wasm_test | ||
|
||
import ( | ||
"context" | ||
"github.com/jrnd-io/jr/pkg/producers/wasm" | ||
"testing" | ||
) | ||
|
||
func TestWASMProducer(t *testing.T) { | ||
|
||
testCases := []struct { | ||
name string | ||
config wasm.Config | ||
}{ | ||
{ | ||
name: "testprint", | ||
config: wasm.Config{ | ||
ModulePath: "wasm_producer_test_function.wasm", | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
someJson := `{"key": "value"}` | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
p := &wasm.Producer{} | ||
p.InitializeFromConfig(ctx, tc.config) | ||
p.Produce(ctx, []byte("somekey"), []byte(someJson), nil) | ||
}) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//go:build tinygo.wasm | ||
|
||
// tinygo build -o pkg/producers/wasm/wasm_producer_test_function.wasm -target=wasi pkg/producers/wasm/wasm_producer_test_function.go | ||
package main | ||
|
||
import ( | ||
"io" | ||
"os" | ||
) | ||
|
||
//export produce | ||
func _produce(size uint32) uint64 { | ||
b := make([]byte, size) | ||
|
||
_, err := io.ReadAtLeast(os.Stdin, b, int(size)) | ||
if err != nil { | ||
return 0 | ||
} | ||
|
||
// os.Stdout.WriteString("hello") | ||
// return uint64(len("hello")) | ||
return 0 | ||
} | ||
|
||
func main() {} |
Binary file not shown.