mirror of
https://github.com/exoticorn/curlywas.git
synced 2026-01-20 11:46:43 +01:00
return dependencies for compiling cwa source
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{collections::HashSet, fs::File};
|
||||
|
||||
use crate::ast;
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
pub fn resolve_includes(script: &mut ast::Script, path: &Path) -> Result<()> {
|
||||
pub fn resolve_includes(
|
||||
script: &mut ast::Script,
|
||||
dependencies: &mut HashSet<PathBuf>,
|
||||
path: &Path,
|
||||
) -> Result<()> {
|
||||
let script_dir = path.parent().expect("Script path has no parent");
|
||||
for data in &mut script.data {
|
||||
for values in &mut data.data {
|
||||
@@ -21,6 +25,7 @@ pub fn resolve_includes(script: &mut ast::Script, path: &Path) -> Result<()> {
|
||||
anyhow!("Failed to load data from {}: {}", full_path.display(), e)
|
||||
})?
|
||||
.read_to_end(data)?;
|
||||
dependencies.insert(full_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
20
src/lib.rs
20
src/lib.rs
@@ -1,7 +1,8 @@
|
||||
use anyhow::{bail, Result};
|
||||
use parser::Sources;
|
||||
use std::collections::HashSet;
|
||||
use std::ffi::OsStr;
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
mod ast;
|
||||
mod constfold;
|
||||
@@ -22,7 +23,14 @@ impl Options {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn compile_file<P: AsRef<Path>>(path: P, options: Options) -> Result<Vec<u8>> {
|
||||
pub struct CompiledModule {
|
||||
pub wasm: Vec<u8>,
|
||||
pub dependencies: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
pub fn compile_file<P: AsRef<Path>>(path: P, options: Options) -> Result<CompiledModule> {
|
||||
let mut dependencies = HashSet::new();
|
||||
|
||||
let path = path.as_ref();
|
||||
let mut script = ast::Script::default();
|
||||
|
||||
@@ -32,12 +40,13 @@ pub fn compile_file<P: AsRef<Path>>(path: P, options: Options) -> Result<Vec<u8>
|
||||
while let Some((path, span)) = pending_files.pop() {
|
||||
match sources.add(&path) {
|
||||
Ok((id, true)) => {
|
||||
dependencies.insert(path.clone());
|
||||
let mut new_script = match parser::parse(&sources, id) {
|
||||
Ok(script) => script,
|
||||
Err(_) => bail!("Parse failed"),
|
||||
};
|
||||
|
||||
includes::resolve_includes(&mut new_script, &path)?;
|
||||
includes::resolve_includes(&mut new_script, &mut dependencies, &path)?;
|
||||
|
||||
for include in std::mem::take(&mut new_script.includes) {
|
||||
let mut path = path
|
||||
@@ -76,5 +85,8 @@ pub fn compile_file<P: AsRef<Path>>(path: P, options: Options) -> Result<Vec<u8>
|
||||
.to_string_lossy(),
|
||||
&options,
|
||||
);
|
||||
Ok(wasm)
|
||||
Ok(CompiledModule {
|
||||
wasm,
|
||||
dependencies: dependencies.into_iter().collect(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -15,12 +15,12 @@ fn main() -> Result<()> {
|
||||
|
||||
let mut filename = args.free_from_os_str::<PathBuf, bool>(|s| Ok(s.into()))?;
|
||||
|
||||
let wasm = compile_file(&filename, options)?;
|
||||
let module = compile_file(&filename, options)?;
|
||||
|
||||
wasmparser::validate(&wasm)?;
|
||||
wasmparser::validate(&module.wasm)?;
|
||||
|
||||
filename.set_extension("wasm");
|
||||
File::create(filename)?.write_all(&wasm)?;
|
||||
File::create(filename)?.write_all(&module.wasm)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user