Skip to content

Commit

Permalink
Merge pull request #139 from wasmerio/pass-bindingsoptions-by-reference
Browse files Browse the repository at this point in the history
  • Loading branch information
ayys authored Jan 24, 2024
2 parents 3044670 + 26f1cb5 commit dcf446e
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ change, where applicable.
wasmer-pack in both `test_wasmer_pack.py` (pytest) and
`index.test.ts` (jest)

- Pass in `BindingsOptions` by reference so it's easier to implement builder
pattern later on.


## [0.7.1] - 2023-06-12

Expand Down
10 changes: 3 additions & 7 deletions crates/cli/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ impl Codegen {
input,
} = self;
let pkg = crate::utils::load(&input)?;

let options = wasmer_pack::BindingsOptions { name };
let files = match language {
Language::JavaScript => {
wasmer_pack::generate_javascript(&pkg, wasmer_pack::BindingsOptions { name })?
}
Language::Python => {
wasmer_pack::generate_python(&pkg, wasmer_pack::BindingsOptions { name })?
}
Language::JavaScript => wasmer_pack::generate_javascript(&pkg, &options)?,
Language::Python => wasmer_pack::generate_python(&pkg, &options)?,
};

let metadata = pkg.metadata();
Expand Down
10 changes: 4 additions & 6 deletions crates/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,17 @@ impl crate::wasmer_pack::Package for Package {
&self,
options: wasmer_pack::BindingsOptions,
) -> Result<Vec<wasmer_pack::File>, wasmer_pack::Error> {
let files = original::generate_javascript(
&self.0,
original::BindingsOptions { name: options.name },
)?;
let original_options = original::BindingsOptions { name: options.name };
let files = original::generate_javascript(&self.0, &original_options)?;
Ok(unwrap_files(files))
}

fn generate_python(
&self,
options: wasmer_pack::BindingsOptions,
) -> Result<Vec<wasmer_pack::File>, wasmer_pack::Error> {
let files =
original::generate_python(&self.0, original::BindingsOptions { name: options.name })?;
let original_options = original::BindingsOptions { name: options.name };
let files = original::generate_python(&self.0, &original_options)?;
Ok(unwrap_files(files))
}
}
Expand Down
9 changes: 5 additions & 4 deletions crates/wasmer-pack/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static TEMPLATES: Lazy<Environment> = Lazy::new(|| {
});

/// Generate JavaScript bindings for a package.
pub fn generate_javascript(package: &Package, options: BindingsOptions) -> Result<Files, Error> {
pub fn generate_javascript(package: &Package, options: &BindingsOptions) -> Result<Files, Error> {
let mut files = Files::new();

let ctx = Context::for_package(package);
Expand All @@ -56,8 +56,8 @@ pub fn generate_javascript(package: &Package, options: BindingsOptions) -> Resul

files.insert_child_directory("src", top_level(&ctx)?);
let mut metadata = package.metadata().clone();
if let Some(package_name) = options.name {
metadata.package_name.set_name(&package_name);
if let Some(package_name) = &options.name {
metadata.package_name.set_name(package_name);
}
let package_json = generate_package_json(package.requires_wasi(), &metadata);
files.insert("package.json", package_json);
Expand Down Expand Up @@ -378,8 +378,9 @@ mod tests {
imports: vec![browser],
}];
let pkg = Package::new(metadata, libraries, commands);
let options = BindingsOptions::default();

let files = generate_javascript(&pkg, BindingsOptions { name: None }).unwrap();
let files = generate_javascript(&pkg, &options).unwrap();

let actual_files: BTreeSet<_> = files.iter().map(|(p, _)| p).collect();
assert_eq!(actual_files, expected);
Expand Down
5 changes: 4 additions & 1 deletion crates/wasmer-pack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@
//! // finally, we've got all the information we need
//! let pkg = Package ::new(metadata, libraries, commands);
//!
//! // we setup the options that are passed to the generator
//! let options = wasmer_pack::BindingsOptions::default();
//!
//! // Now we can generate the bindings for our language
//! let js = wasmer_pack::generate_javascript(&pkg, wasmer_pack::BindingsOptions::default())?;
//! let js = wasmer_pack::generate_javascript(&pkg, &options)?;
//!
//! // And finally, save them to disk
//! js.save_to_disk("./out")?;
Expand Down
8 changes: 4 additions & 4 deletions crates/wasmer-pack/src/py/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ static TEMPLATES: Lazy<Environment> = Lazy::new(|| {
});

/// Generate Python bindings.
pub fn generate_python(package: &Package, options: BindingsOptions) -> Result<Files, Error> {
pub fn generate_python(package: &Package, options: &BindingsOptions) -> Result<Files, Error> {
let metadata = package.metadata();

// make sure the name is in snake-case
let package_name = if let Some(name) = options.name {
let package_name = if let Some(name) = &options.name {
name.to_snake_case()
} else {
metadata.package_name.name().to_string().to_snake_case()
Expand Down Expand Up @@ -400,8 +400,8 @@ mod tests {
imports: vec![browser],
}];
let package = Package::new(metadata, libraries, commands);

let files = generate_python(&package, BindingsOptions::default()).unwrap();
let options = BindingsOptions::default();
let files = generate_python(&package, &options).unwrap();

let actual_files: BTreeSet<_> = files.iter().map(|(p, _)| p).collect();
assert_eq!(actual_files, expected);
Expand Down
9 changes: 4 additions & 5 deletions crates/wasmer-pack/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ fn use_wasi_javascript_bindings() {
.join("wasmer-pack")
.join("javascript-wasi");
let _ = std::fs::remove_dir_all(&out_dir);

let js =
wasmer_pack::generate_javascript(&pkg, wasmer_pack::BindingsOptions::default()).unwrap();
let options = wasmer_pack::BindingsOptions::default();
let js = wasmer_pack::generate_javascript(&pkg, &options).unwrap();
js.save_to_disk(&out_dir).unwrap();

let js_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
Expand All @@ -40,8 +39,8 @@ fn use_wasi_python_bindings() {
.join("wasmer-pack")
.join("python-wasi");
let _ = std::fs::remove_dir_all(&out_dir);

let py = wasmer_pack::generate_python(&pkg, wasmer_pack::BindingsOptions::default()).unwrap();
let options = wasmer_pack::BindingsOptions::default();
let py = wasmer_pack::generate_python(&pkg, &options).unwrap();
py.save_to_disk(&out_dir).unwrap();

let python_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
Expand Down

0 comments on commit dcf446e

Please sign in to comment.