diff options
Diffstat (limited to 'runtime/ops/io.rs')
-rw-r--r-- | runtime/ops/io.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/runtime/ops/io.rs b/runtime/ops/io.rs index f18624eb2..a357dd6f1 100644 --- a/runtime/ops/io.rs +++ b/runtime/ops/io.rs @@ -111,6 +111,10 @@ pub fn init_stdio(stdio: Stdio) -> Extension { let stdio = Rc::new(RefCell::new(Some(stdio))); Extension::builder() + .middleware(|op| match op.name { + "op_print" => op_print::decl(), + _ => op, + }) .state(move |state| { let stdio = stdio .borrow_mut() @@ -419,6 +423,24 @@ impl Resource for StdFileResource { } } +// override op_print to use the stdout and stderr in the resource table +#[op] +pub fn op_print( + state: &mut OpState, + msg: String, + is_err: bool, +) -> Result<(), AnyError> { + let rid = if is_err { 2 } else { 1 }; + StdFileResource::with(state, rid, move |r| match r { + Ok(std_file) => { + std_file.write_all(msg.as_bytes())?; + std_file.flush().unwrap(); + Ok(()) + } + Err(_) => Err(not_supported()), + }) +} + #[op] fn op_read_sync( state: &mut OpState, |