Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"benchmark/exit_code_function_zero",
"benchmark/exit_code_function_one",
"benchmark/log_truncation_function",
"benchmark/exports",
]

[package]
Expand Down
Binary file added benchmark/build/exports.wasm
Binary file not shown.
10 changes: 10 additions & 0 deletions benchmark/exports/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "exports"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib"]

[dependencies]
7 changes: 7 additions & 0 deletions benchmark/exports/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use std::io::Write;

#[no_mangle]
pub extern "C" fn export1() {
std::io::stdout().write("export1".as_bytes()).unwrap();
std::io::stdout().flush().unwrap();
}
12 changes: 9 additions & 3 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn import_modules(
pub fn run(
function_path: PathBuf,
input: Vec<u8>,
export: &str,
profile_opts: Option<&ProfileOpts>,
) -> Result<FunctionRunResult> {
let engine = Engine::new(
Expand Down Expand Up @@ -90,9 +91,7 @@ pub fn run(
linker.module(&mut store, "Function", &module)?;
let instance = linker.instantiate(&mut store, &module)?;

let func = instance
.get_typed_func::<(), ()>(store.as_context_mut(), "_start")
.unwrap();
let func = instance.get_typed_func::<(), ()>(store.as_context_mut(), export)?;

let module_result;
(module_result, profile_data) = if let Some(profile_opts) = profile_opts {
Expand Down Expand Up @@ -193,13 +192,15 @@ mod tests {
use std::path::Path;

const LINEAR_MEMORY_USAGE: u64 = 159 * 64;
const DEFAULT_EXPORT: &str = "_start";

#[test]
fn test_js_function() {
let input = include_bytes!("../benchmark/build/js_function_input.json").to_vec();
let function_run_result = run(
Path::new("benchmark/build/js_function.wasm").to_path_buf(),
input,
DEFAULT_EXPORT,
None,
);

Expand All @@ -212,6 +213,7 @@ mod tests {
let function_run_result = run(
Path::new("benchmark/build/exit_code_function_zero.wasm").to_path_buf(),
input,
DEFAULT_EXPORT,
None,
)
.unwrap();
Expand All @@ -225,6 +227,7 @@ mod tests {
let function_run_result = run(
Path::new("benchmark/build/exit_code_function_one.wasm").to_path_buf(),
input,
DEFAULT_EXPORT,
None,
)
.unwrap();
Expand All @@ -238,6 +241,7 @@ mod tests {
let function_run_result = run(
Path::new("benchmark/build/linear_memory_function.wasm").to_path_buf(),
input,
DEFAULT_EXPORT,
None,
)
.unwrap();
Expand All @@ -251,6 +255,7 @@ mod tests {
let function_run_result = run(
Path::new("benchmark/build/log_truncation_function.wasm").to_path_buf(),
input,
DEFAULT_EXPORT,
None,
)
.unwrap();
Expand All @@ -266,6 +271,7 @@ mod tests {
let function_run_result = run(
Path::new("benchmark/build/size_function.wasm").to_path_buf(),
input,
DEFAULT_EXPORT,
None,
)
.unwrap();
Expand Down
11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ struct Opts {
/// Path to json file containing Function input; if omitted, stdin is used
input: Option<PathBuf>,

/// Name of the export to invoke.
#[clap(short, long, default_value = "_start")]
export: String,

/// Log the run result as a JSON object
#[clap(short, long)]
json: bool,
Expand Down Expand Up @@ -94,7 +98,12 @@ fn main() -> Result<()> {
.map_err(|e| anyhow!("Invalid input JSON: {}", e))?;

let profile_opts = opts.profile_opts();
let function_run_result = run(opts.function, buffer, profile_opts.as_ref())?;
let function_run_result = run(
opts.function,
buffer,
opts.export.as_ref(),
profile_opts.as_ref(),
)?;

if opts.json {
println!("{}", function_run_result.to_json());
Expand Down
25 changes: 25 additions & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,31 @@ mod tests {
Ok(())
}

#[test]
fn exports() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("function-runner")?;
cmd.args(["--function", "benchmark/build/exports.wasm"])
.args(["--export", "export1"])
.arg("benchmark/build/product_discount.json");

cmd.assert().success().stdout(contains("export1"));

Ok(())
}

#[test]
fn missing_export() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("function-runner")?;
cmd.args(["--function", "benchmark/build/exports.wasm"])
.arg("benchmark/build/product_discount.json");

cmd.assert()
.failure()
.stderr(contains(" failed to find function export `_start`"));

Ok(())
}

fn profile_base_cmd_in_temp_dir(
) -> Result<(Command, assert_fs::TempDir), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("function-runner")?;
Expand Down