File size: 2,266 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
use anyhow::Result;
use serde::{Deserialize, Serialize};
use turbo_tasks::{NonLocalValue, ResolvedVc, TaskInput, Vc, trace::TraceRawVcs};
use turbo_tasks_fs::{File, FileContent};
use turbopack_core::{
asset::{Asset, AssetContent},
ident::AssetIdent,
source::Source,
};
#[derive(
PartialOrd,
Ord,
Eq,
PartialEq,
Hash,
Debug,
Copy,
Clone,
Serialize,
Deserialize,
TaskInput,
TraceRawVcs,
NonLocalValue,
)]
pub enum WebAssemblySourceType {
/// Binary WebAssembly files (.wasm).
Binary,
/// WebAssembly text format (.wat).
Text,
}
/// Returns the raw binary WebAssembly source or the assembled version of a text
/// format source.
#[turbo_tasks::value]
#[derive(Clone)]
pub struct WebAssemblySource {
source: ResolvedVc<Box<dyn Source>>,
source_ty: WebAssemblySourceType,
}
#[turbo_tasks::value_impl]
impl WebAssemblySource {
#[turbo_tasks::function]
pub fn new(source: ResolvedVc<Box<dyn Source>>, source_ty: WebAssemblySourceType) -> Vc<Self> {
Self::cell(WebAssemblySource { source, source_ty })
}
}
#[turbo_tasks::value_impl]
impl Source for WebAssemblySource {
#[turbo_tasks::function]
async fn ident(&self) -> Result<Vc<AssetIdent>> {
Ok(match self.source_ty {
WebAssemblySourceType::Binary => self.source.ident(),
WebAssemblySourceType::Text => self
.source
.ident()
.with_path(self.source.ident().path().await?.append("_.wasm")?),
})
}
}
#[turbo_tasks::value_impl]
impl Asset for WebAssemblySource {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<AssetContent>> {
let content = match self.source_ty {
WebAssemblySourceType::Binary => return Ok(self.source.content()),
WebAssemblySourceType::Text => self.source.content(),
};
let content = content.file_content().await?;
let FileContent::Content(file) = &*content else {
return Ok(AssetContent::file(FileContent::NotFound.cell()));
};
let bytes = file.content().to_bytes();
let parsed = wat::parse_bytes(&bytes)?;
Ok(AssetContent::file(File::from(&*parsed).into()))
}
}
|