Class: Wasmtime::WasiCtx

Inherits:
Object
  • Object
show all
Defined in:
ext/src/ruby_api/wasi_ctx.rs

Overview

WASI context to be sent as Store.new’s wasi_ctx keyword argument.

Instance methods mutate the current object and return self.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.deterministicWasiCtx

Create a new deterministic Wasmtime::WasiCtx. See github.com/Shopify/deterministic-wasi-ctx for more details

Returns:



35
36
37
38
39
# File 'ext/src/ruby_api/wasi_ctx.rs', line 35

pub fn deterministic() -> Self {
    Self {
        inner: RefCell::new(wasi_deterministic_ctx()),
    }
}

Instance Method Details

#set_stderr_buffer(buffer, capacity) ⇒ WasiCtx

Set stderr to write to a string buffer. If the string buffer is frozen, Wasm execution will raise a Wasmtime::Error error. No encoding checks are done on the resulting string, it is the caller’s responsibility to ensure the string contains a valid encoding

Parameters:

  • buffer (String)

    The string buffer to write to.

  • capacity (Integer)

    The maximum number of bytes that can be written to the output buffer.

Returns:



115
116
117
118
119
120
# File 'ext/src/ruby_api/wasi_ctx.rs', line 115

fn set_stderr_buffer(rb_self: RbSelf, buffer: RString, capacity: usize) -> RbSelf {
    let inner = rb_self.inner.borrow_mut();
    let pipe = WritePipe::new(OutputLimitedBuffer::new(buffer.into(), capacity));
    inner.set_stderr(Box::new(pipe));
    rb_self
}

#set_stderr_file(path) ⇒ WasiCtx

Set stderr to write to a file. Will truncate the file if it exists, otherwise try to create it.

Parameters:

  • path (String)

    The path of the file to write to.

Returns:



100
101
102
103
104
105
# File 'ext/src/ruby_api/wasi_ctx.rs', line 100

fn set_stderr_file(rb_self: RbSelf, path: RString) -> RbSelf {
    let inner = rb_self.inner.borrow_mut();
    let cs = file_w(path).map(wasi_file).unwrap();
    inner.set_stderr(cs);
    rb_self
}

#set_stdin_file(path) ⇒ WasiCtxBuilder

Set stdin to read from the specified file.

Parameters:

  • path (String)

    The path of the file to read from.

Returns:



46
47
48
49
50
51
# File 'ext/src/ruby_api/wasi_ctx.rs', line 46

fn set_stdin_file(rb_self: RbSelf, path: RString) -> RbSelf {
    let inner = rb_self.inner.borrow_mut();
    let cs = file_r(path).map(wasi_file).unwrap();
    inner.set_stdin(cs);
    rb_self
}

#set_stdin_string(content) ⇒ WasiCtx

Set stdin to the specified String.

Parameters:

  • content (String)

Returns:



58
59
60
61
62
63
64
# File 'ext/src/ruby_api/wasi_ctx.rs', line 58

fn set_stdin_string(rb_self: RbSelf, content: RString) -> RbSelf {
    let inner = rb_self.inner.borrow_mut();
    let str = unsafe { content.as_slice() };
    let pipe = ReadPipe::from(str);
    inner.set_stdin(Box::new(pipe));
    rb_self
}

#set_stdout_buffer(buffer, capacity) ⇒ WasiCtx

Set stdout to write to a string buffer. If the string buffer is frozen, Wasm execution will raise a Wasmtime::Error error. No encoding checks are done on the resulting string, it is the caller’s responsibility to ensure the string contains a valid encoding

Parameters:

  • buffer (String)

    The string buffer to write to.

  • capacity (Integer)

    The maximum number of bytes that can be written to the output buffer.

Returns:



87
88
89
90
91
92
# File 'ext/src/ruby_api/wasi_ctx.rs', line 87

fn set_stdout_buffer(rb_self: RbSelf, buffer: RString, capacity: usize) -> RbSelf {
    let inner = rb_self.inner.borrow_mut();
    let pipe = WritePipe::new(OutputLimitedBuffer::new(buffer.into(), capacity));
    inner.set_stdout(Box::new(pipe));
    rb_self
}

#set_stdout_file(path) ⇒ WasiCtx

Set stdout to write to a file. Will truncate the file if it exists, otherwise try to create it.

Parameters:

  • path (String)

    The path of the file to write to.

Returns:



72
73
74
75
76
77
# File 'ext/src/ruby_api/wasi_ctx.rs', line 72

fn set_stdout_file(rb_self: RbSelf, path: RString) -> RbSelf {
    let inner = rb_self.inner.borrow_mut();
    let cs = file_w(path).map(wasi_file).unwrap();
    inner.set_stdout(cs);
    rb_self
}