mirror of
https://github.com/gfx-rs/wgpu.git
synced 2025-12-08 21:26:17 +00:00
Allow workspace to be built on wasm (#3840)
This commit is contained in:
parent
3f0aed789d
commit
4aff9b67af
@ -9,7 +9,9 @@ description = "CTS runner for wgpu"
|
||||
license.workspace = true
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
# We make all dependencies conditional on not being wasm,
|
||||
# so the whole workspace can built as wasm.
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||
deno_console.workspace = true
|
||||
deno_core.workspace = true
|
||||
deno_url.workspace = true
|
||||
|
||||
@ -1,30 +1,27 @@
|
||||
use std::{
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
mod native {
|
||||
use std::{
|
||||
env, fmt,
|
||||
io::{Read, Write},
|
||||
rc::Rc,
|
||||
};
|
||||
};
|
||||
|
||||
use deno_core::anyhow::anyhow;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::op;
|
||||
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_core::ZeroCopyBuf;
|
||||
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::op;
|
||||
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_core::ZeroCopyBuf;
|
||||
use deno_web::BlobStore;
|
||||
use termcolor::Ansi;
|
||||
use termcolor::Color::Red;
|
||||
use termcolor::ColorSpec;
|
||||
use termcolor::WriteColor;
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() {
|
||||
unwrap_or_exit(run().await)
|
||||
}
|
||||
|
||||
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
|
||||
@ -82,9 +79,9 @@ async fn run() -> Result<(), AnyError> {
|
||||
rx.await.unwrap()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn extension() -> deno_core::Extension {
|
||||
fn extension() -> deno_core::Extension {
|
||||
deno_core::Extension::builder(env!("CARGO_PKG_NAME"))
|
||||
.ops(vec![
|
||||
op_exit::decl(),
|
||||
@ -93,31 +90,31 @@ fn extension() -> deno_core::Extension {
|
||||
])
|
||||
.esm(deno_core::include_js_files!("bootstrap.js",))
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
#[op]
|
||||
fn op_exit(code: i32) -> Result<(), AnyError> {
|
||||
#[op]
|
||||
fn op_exit(code: i32) -> Result<(), AnyError> {
|
||||
std::process::exit(code)
|
||||
}
|
||||
}
|
||||
|
||||
#[op]
|
||||
fn op_read_file_sync(path: String) -> Result<ZeroCopyBuf, AnyError> {
|
||||
#[op]
|
||||
fn op_read_file_sync(path: String) -> Result<ZeroCopyBuf, AnyError> {
|
||||
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(ZeroCopyBuf::from(buf))
|
||||
}
|
||||
}
|
||||
|
||||
#[op]
|
||||
fn op_write_file_sync(path: String, buf: ZeroCopyBuf) -> Result<(), AnyError> {
|
||||
#[op]
|
||||
fn op_write_file_sync(path: String, buf: ZeroCopyBuf) -> Result<(), AnyError> {
|
||||
let path = std::path::Path::new(&path);
|
||||
let mut file = std::fs::File::create(path)?;
|
||||
file.write_all(&buf)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn get_error_class_name(e: &AnyError) -> &'static str {
|
||||
fn get_error_class_name(e: &AnyError) -> &'static str {
|
||||
deno_core::error::get_custom_error_class(e)
|
||||
.or_else(|| deno_webgpu::error::get_error_class_name(e))
|
||||
.unwrap_or_else(|| {
|
||||
@ -129,9 +126,9 @@ fn get_error_class_name(e: &AnyError) -> &'static str {
|
||||
.collect::<String>()
|
||||
);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
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) => {
|
||||
@ -139,30 +136,42 @@ fn unwrap_or_exit<T>(result: Result<T, AnyError>) -> T {
|
||||
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
|
||||
}
|
||||
|
||||
fn check_unstable(&self, _state: &deno_core::OpState, _api_name: &'static str) {}
|
||||
}
|
||||
}
|
||||
|
||||
#[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!");
|
||||
}
|
||||
|
||||
@ -16,14 +16,16 @@ path = "lib.rs"
|
||||
[features]
|
||||
surface = ["wgpu-core/raw-window-handle", "dep:raw-window-handle"]
|
||||
|
||||
[dependencies]
|
||||
# We make all dependencies conditional on not being wasm,
|
||||
# so the whole workspace can built as wasm.
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
|
||||
deno_core.workspace = true
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
tokio = { workspace = true, features = ["full"] }
|
||||
wgpu-types = { workspace = true, features = ["trace", "replay", "serde"] }
|
||||
raw-window-handle = { workspace = true, optional = true }
|
||||
|
||||
[dependencies.wgpu-core]
|
||||
[target.'cfg(not(target_arch = "wasm32"))'.dependencies.wgpu-core]
|
||||
workspace = true
|
||||
features = ["trace", "replay", "serde", "strict_asserts", "wgsl", "gles"]
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
#![cfg(not(target_arch = "wasm32"))]
|
||||
#![warn(unsafe_op_in_unsafe_fn)]
|
||||
|
||||
use deno_core::error::AnyError;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user