-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IOStream as stdin, stdout and stderr? #19
Comments
I tried to implemente this, but I encountered a problem. pub struct OutputStream<'a>{
pub env: &'a JNIEnv<'a>,
pub stream: &'a JObject<'a>
}
#[async_trait::async_trait]
impl WasiFile for OutputStream<'_>{
//many lines.....
} Error:
|
Yeah, because of In order to make a java object movable among threads, we need to create a global reference out of it, also bring around I've also looked into some code in wasmtime, and found that instead of implementing Here's a snippet that I've confirmed it works as expected. struct JavaOutputStreamWrite {
jvm: JavaVM,
obj_ref: GlobalRef,
}
impl JavaOutputStreamWrite {
fn new(jvm: JavaVM, obj_ref: GlobalRef) -> Self {
Self { jvm, obj_ref }
}
}
impl std::io::Write for JavaOutputStreamWrite {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
// TODO: proper error conversion
let env = self.jvm.attach_current_thread().unwrap();
let array = env.byte_array_from_slice(buf).unwrap();
env.call_method(self.obj_ref.as_obj(), "write", "([B)V", &[array.into()])
.unwrap();
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
let env = self.jvm.attach_current_thread().unwrap();
env.call_method(self.obj_ref.as_obj(), "flush", "()V", &[])
.unwrap();
Ok(())
}
}
fn native_build(
env: &JNIEnv,
_clazz: JClass,
envs: jobjectArray,
args: jobjectArray,
inherit_stdin: jboolean,
stdin_path: JString,
inherit_stdout: jboolean,
stdout_path: JString,
stdout: JObject,
inherit_stderr: jboolean,
stderr_path: JString,
preopen_dirs: jobjectArray,
) -> Result<jlong, Self::Error> {
...
if inherit_stdout != 0 {
builder = builder.inherit_stdout();
} else if !stdout_path.is_null() {
let file = wasi_utils::open_wasi_file(utils::get_string(env, stdout_path.into())?)?;
builder = builder.stdout(Box::new(file));
} else if !stdout.is_null() {
let jvm = env.get_java_vm()?;
let global_ref = env.new_global_ref(stdout)?;
let pipe = WritePipe::new(JavaOutputStreamWrite::new(jvm, global_ref));
builder = builder.stdout(Box::new(pipe));
} public WasiCtxBuilder stdout(OutputStream out) {
stdout = out;
stdoutPath = null;
inheritStdout = false;
return this;
} |
@kawamuray Hello, I encountered a strange problem when I tried to implement stdout:
my code is here. |
Can you try spotting which btw I saw your code uses |
@kawamuray It will be a nice advance if we can use a stream as stdin, stdout and stderr.
Currently, I'm trying to transfer the output of my users' wasm program to browser via websocket. This advance will reduce unnecessary io.
The text was updated successfully, but these errors were encountered: