-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wasm
- Loading branch information
Showing
11 changed files
with
160 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
#include <errno.h> | ||
|
||
// zig cc --target=wasm32-wasi main.c -o c.wasm | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
ssize_t n, m; | ||
char buf[BUFSIZ]; | ||
|
||
int in = STDIN_FILENO; | ||
int out = STDOUT_FILENO; | ||
|
||
while ((n = read(in, buf, BUFSIZ)) > 0) | ||
{ | ||
char *ptr = buf; | ||
while (n > 0) | ||
{ | ||
m = write(out, ptr, (size_t)n); | ||
if (m < 0) | ||
{ | ||
fprintf(stderr, "write error: %s\n", strerror(errno)); | ||
exit(1); | ||
} | ||
n -= m; | ||
ptr += m; | ||
} | ||
} | ||
|
||
if (n < 0) | ||
{ | ||
fprintf(stderr, "read error: %s\n", strerror(errno)); | ||
exit(1); | ||
} | ||
|
||
return EXIT_SUCCESS; | ||
} |
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,27 @@ | ||
//go:build ignore | ||
|
||
// compile with: | ||
// GOOS=wasip1 GOARCH=wasm go build -o go.wasm go.go | ||
// or | ||
// tinygo build -o tgo.wasm -target=wasi -scheduler=none -no-debug main.go | ||
|
||
package main | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
) | ||
|
||
func main() { | ||
reader := bufio.NewReader(os.Stdin) | ||
writer := bufio.NewWriter(os.Stdout) | ||
|
||
for { | ||
input, err := reader.ReadString('\n') | ||
writer.WriteString(input) | ||
if err != nil { | ||
break | ||
} | ||
} | ||
writer.Flush() | ||
} |
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,10 @@ | ||
//rustc main.rs --target wasm32-wasip1 -o rust.wasm | ||
|
||
use std::io::{self, Read, Write}; | ||
|
||
fn main() { | ||
let mut buffer = Vec::new(); | ||
let _ = io::stdin().read_to_end(&mut buffer); | ||
let _ = io::stdout().write_all(&buffer); | ||
() | ||
} |
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,17 @@ | ||
// zig cc --target=wasm32-wasi main.zig -o zig.wasm | ||
const std = @import("std"); | ||
|
||
pub fn main() !void { | ||
var stdin = std.io.getStdIn().reader(); | ||
var stdout = std.io.getStdOut().writer(); | ||
|
||
const bufferSize: usize = 4096; | ||
var buffer: [bufferSize]u8 = undefined; | ||
|
||
while (true) { | ||
const bytesRead = try stdin.read(buffer[0..]); | ||
if (bytesRead == 0) break; | ||
|
||
_ = try stdout.write(buffer[0..bytesRead]); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,62 @@ | ||
package mutators | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/batmac/ccat/pkg/log" | ||
|
||
"github.com/tetratelabs/wazero" | ||
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" | ||
"github.com/tetratelabs/wazero/sys" | ||
// "github.com/tetratelabs/wazero/api" | ||
) | ||
|
||
// Yaegi is a mutator that executes a yaegi script | ||
|
||
func init() { | ||
singleRegister("wasm", wasm, withDescription("a wasi (wasm) module to apply (path as first argument"), | ||
withConfigBuilder(stdConfigString), | ||
withCategory("plugin"), | ||
) | ||
} | ||
|
||
func wasm(w io.WriteCloser, r io.ReadCloser, arg any) (int64, error) { | ||
ctx := context.Background() | ||
moduleFile := arg.(string) | ||
|
||
log.Debugln("Create a new WebAssembly runtime") | ||
runtime := wazero.NewRuntime(ctx) | ||
defer runtime.Close(ctx) | ||
|
||
log.Debugln("Instantiate WASI in the runtime") | ||
wasi_snapshot_preview1.MustInstantiate(ctx, runtime) | ||
|
||
log.Debugln("Load your WASM module") | ||
wasmBytes, err := os.ReadFile(moduleFile) | ||
if err != nil { | ||
panic(fmt.Sprintf("Failed to read WASM module: %v", err)) | ||
} | ||
|
||
log.Debugln("Define the configuration with the in-memory file system") | ||
config := wazero.NewModuleConfig(). | ||
WithStdin(r). | ||
WithStdout(w). | ||
WithStderr(os.Stderr) // Set stderr to the standard error of the host process | ||
|
||
log.Debugln("Instantiate the WASM module with the configured input/output") | ||
if _, err := runtime.InstantiateWithConfig(ctx, wasmBytes, config); err != nil { | ||
var exitErr *sys.ExitError | ||
if errors.As(err, &exitErr) && exitErr.ExitCode() != 0 { | ||
log.Debugf("exit_code: %d\n", exitErr.ExitCode()) | ||
} else { | ||
log.Fatal(err) | ||
} | ||
} | ||
w.Close() | ||
|
||
return 0, nil | ||
} |