Skip to content

Commit

Permalink
Use a polyfill to make the JS tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed May 22, 2023
1 parent ac43ee5 commit 4cb8694
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
5 changes: 2 additions & 3 deletions lib/wasi/src/runtime/resolver/filesystem_source.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::{Context, Error};
use url::Url;
use webc::compat::Container;

use crate::runtime::resolver::{
Expand Down Expand Up @@ -29,8 +28,8 @@ impl Source for FileSystemSource {
let container = Container::from_disk(&path)
.with_context(|| format!("Unable to parse \"{}\"", path.display()))?;

let url = Url::from_file_path(&path)
.map_err(|_| anyhow::anyhow!("Unable to turn \"{}\" into a URL", path.display()))?;
let url = crate::runtime::resolver::polyfills::url_from_file_path(&path)
.ok_or_else(|| anyhow::anyhow!("Unable to turn \"{}\" into a URL", path.display()))?;

let summary = PackageSummary {
pkg: PackageInfo::from_manifest(container.manifest())?,
Expand Down
6 changes: 4 additions & 2 deletions lib/wasi/src/runtime/resolver/in_memory_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ impl Source for InMemorySource {
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use url::Url;

use crate::runtime::resolver::{
inputs::{DistributionInfo, PackageInfo},
Expand Down Expand Up @@ -162,7 +161,10 @@ mod tests {
entrypoint: None,
},
dist: DistributionInfo {
webc: Url::from_file_path(bash.canonicalize().unwrap()).unwrap(),
webc: crate::runtime::resolver::polyfills::url_from_file_path(
bash.canonicalize().unwrap()
)
.unwrap(),
webc_sha256: [
7, 226, 190, 131, 173, 231, 130, 245, 207, 185, 51, 189, 86, 85, 222, 37,
27, 163, 170, 27, 25, 24, 211, 136, 186, 233, 174, 119, 66, 15, 134, 9
Expand Down
7 changes: 4 additions & 3 deletions lib/wasi/src/runtime/resolver/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ impl PackageSummary {
let path = path.as_ref().canonicalize()?;
let container = Container::from_disk(&path)?;
let webc_sha256 = WebcHash::for_file(&path)?;
let url = Url::from_file_path(&path).map_err(|_| {
anyhow::anyhow!("Unable to turn \"{}\" into a file:// URL", path.display())
})?;
let url =
crate::runtime::resolver::polyfills::url_from_file_path(&path).ok_or_else(|| {
anyhow::anyhow!("Unable to turn \"{}\" into a file:// URL", path.display())
})?;

let pkg = PackageInfo::from_manifest(container.manifest())?;
let dist = DistributionInfo {
Expand Down
5 changes: 3 additions & 2 deletions lib/wasi/src/runtime/resolver/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
mod filesystem_source;
mod in_memory_source;
mod inputs;
mod multi_source_registry;
mod outputs;
mod registry;
mod resolve;
mod source;
pub(crate) mod polyfills;
mod wapm_source;
mod web_source;
mod filesystem_source;

pub use self::{
in_memory_source::InMemorySource,
filesystem_source::FileSystemSource,
in_memory_source::InMemorySource,
inputs::{
Command, Dependency, DistributionInfo, PackageInfo, PackageSpecifier, PackageSummary,
WebcHash,
Expand Down
50 changes: 50 additions & 0 deletions lib/wasi/src/runtime/resolver/polyfills.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::path::Path;

use url::Url;

/// Polyfill for [`Url::from_file_path()`] that works on `wasm32-unknown-unknown`.
pub(crate) fn url_from_file_path(path: impl AsRef<Path>) -> Option<Url> {
let path = path.as_ref();

if !path.is_absolute() {
return None;
}

let mut buffer = String::new();

for component in path {
if !buffer.ends_with('/') {
buffer.push('/');
}

buffer.push_str(component.to_str()?);
}

buffer.insert_str(0, "file://");

buffer.parse().ok()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[cfg(unix)]
fn behaviour_is_identical() {
let inputs = [
"/",
"/path",
"/path/to/file.txt",
"./path/to/file.txt",
".",
"",
];

for path in inputs {
let got = url_from_file_path(path);
let expected = Url::from_file_path(path).ok();
assert_eq!(got, expected, "Mismatch for \"{path}\"");
}
}
}
8 changes: 6 additions & 2 deletions lib/wasi/src/state/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,10 +856,14 @@ impl WasiEnv {
/// Make all the commands in a [`BinaryPackage`] available to the WASI
/// instance.
///
/// The [`BinaryPackageCommand::atom()`] will be saved to `/bin/command`.
/// The [`BinaryPackageCommand::atom()`][cmd-atom] will be saved to
/// `/bin/command`.
///
/// This will also merge the command's filesystem
/// ([`BinaryPackage::webc_fs`]) into the current filesystem.
/// ([`BinaryPackage::webc_fs`][pkg-fs]) into the current filesystem.
///
/// [cmd-atom]: crate::bin_factory::BinaryPackageCommand::atom()
/// [pkg-fs]: crate::bin_factory::BinaryPackage::webc_fs
pub fn use_package(&self, pkg: &BinaryPackage) -> Result<(), WasiStateCreationError> {
// PERF: We should avoid all these copies in the WasiFsRoot::Backing case.

Expand Down

0 comments on commit 4cb8694

Please sign in to comment.