Switch cts_runner to clearer no_main style of wasm compat

This commit is contained in:
Connor Fitzgerald 2025-04-25 18:16:37 -04:00 committed by Andreas Reich
parent 267f14632f
commit 65eb10ed5a

View File

@ -1,27 +1,28 @@
#[cfg(not(target_arch = "wasm32"))]
mod native {
use std::sync::Arc;
use std::{
#![cfg_attr(target_arch = "wasm32", no_main)]
#![cfg(not(target_arch = "wasm32"))]
use std::sync::Arc;
use std::{
env, fmt,
io::{Read, Write},
rc::Rc,
};
};
use deno_core::anyhow::anyhow;
use deno_core::error::AnyError;
use deno_core::op2;
use deno_core::resolve_url_or_path;
use deno_core::serde_json::json;
use deno_core::v8;
use deno_core::JsRuntime;
use deno_core::RuntimeOptions;
use deno_web::BlobStore;
use termcolor::Ansi;
use termcolor::Color::Red;
use termcolor::ColorSpec;
use termcolor::WriteColor;
use deno_core::anyhow::anyhow;
use deno_core::error::AnyError;
use deno_core::op2;
use deno_core::resolve_url_or_path;
use deno_core::serde_json::json;
use deno_core::v8;
use deno_core::JsRuntime;
use deno_core::RuntimeOptions;
use deno_web::BlobStore;
use termcolor::Ansi;
use termcolor::Color::Red;
use termcolor::ColorSpec;
use termcolor::WriteColor;
pub async fn run() -> Result<(), AnyError> {
pub async fn run() -> Result<(), AnyError> {
let mut args_iter = env::args();
let _ = args_iter.next();
let url = args_iter
@ -74,9 +75,9 @@ mod native {
result.await?;
Ok(())
}
}
deno_core::extension!(
deno_core::extension!(
cts_runner,
deps = [deno_webidl, deno_web],
ops = [op_exit, op_read_file_sync, op_write_file_sync],
@ -85,35 +86,32 @@ mod native {
state = |state| {
state.put(Permissions {});
}
);
);
#[op2(fast)]
fn op_exit(code: i32) {
#[op2(fast)]
fn op_exit(code: i32) {
std::process::exit(code)
}
}
#[op2]
#[buffer]
fn op_read_file_sync(#[string] path: &str) -> Result<Vec<u8>, std::io::Error> {
#[op2]
#[buffer]
fn op_read_file_sync(#[string] path: &str) -> Result<Vec<u8>, std::io::Error> {
let path = std::path::Path::new(path);
let mut file = std::fs::File::open(path)?;
let mut buf = Vec::new();
file.read_to_end(&mut buf)?;
Ok(buf)
}
}
#[op2(fast)]
fn op_write_file_sync(
#[string] path: &str,
#[buffer] buf: &[u8],
) -> Result<(), std::io::Error> {
#[op2(fast)]
fn op_write_file_sync(#[string] path: &str, #[buffer] buf: &[u8]) -> Result<(), std::io::Error> {
let path = std::path::Path::new(path);
let mut file = std::fs::File::create(path)?;
file.write_all(buf)?;
Ok(())
}
}
pub fn unwrap_or_exit<T>(result: Result<T, AnyError>) -> T {
pub fn unwrap_or_exit<T>(result: Result<T, AnyError>) -> T {
match result {
Ok(value) => value,
Err(error) => {
@ -121,40 +119,33 @@ mod native {
std::process::exit(1);
}
}
}
}
fn style<S: AsRef<str>>(s: S, colorspec: ColorSpec) -> impl fmt::Display {
fn style<S: AsRef<str>>(s: S, colorspec: ColorSpec) -> impl fmt::Display {
let mut v = Vec::new();
let mut ansi_writer = Ansi::new(&mut v);
ansi_writer.set_color(&colorspec).unwrap();
ansi_writer.write_all(s.as_ref().as_bytes()).unwrap();
ansi_writer.reset().unwrap();
String::from_utf8_lossy(&v).into_owned()
}
}
fn red_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
fn red_bold<S: AsRef<str>>(s: S) -> impl fmt::Display {
let mut style_spec = ColorSpec::new();
style_spec.set_fg(Some(Red)).set_bold(true);
style(s, style_spec)
}
}
// NOP permissions
struct Permissions;
// NOP permissions
struct Permissions;
impl deno_web::TimersPermission for Permissions {
impl deno_web::TimersPermission for Permissions {
fn allow_hrtime(&mut self) -> bool {
false
}
}
}
#[cfg(not(target_arch = "wasm32"))]
#[tokio::main(flavor = "current_thread")]
async fn main() {
native::unwrap_or_exit(native::run().await)
}
#[cfg(target_arch = "wasm32")]
fn main() {
panic!("This is a native only module. It can't be run on the web!");
unwrap_or_exit(run().await)
}