Skip to content

Commit

Permalink
Add lsusb example
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed Nov 12, 2023
1 parent 0f68083 commit 6b996b0
Show file tree
Hide file tree
Showing 11 changed files with 456 additions and 140 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ members = [
"deno_bindgen_ir",
"deno_bindgen_cli"
]
exclude = ["e2e_test/"]
exclude = ["e2e_test/", "example/"]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fmt:
cargo fmt
deno fmt --ignore=target/,e2e_test/target/,e2e_test/bindings/
deno fmt --ignore=target/,e2e_test/target/,e2e_test/bindings/,example/target/

build:
cargo build
Expand Down
12 changes: 9 additions & 3 deletions deno_bindgen_cli/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ impl Build {
let output = cmd.output()?;
if status.success() {
let reader = std::io::BufReader::new(output.stdout.as_slice());
let mut artifacts = vec![];
for message in cargo_metadata::Message::parse_stream(reader) {
match message.unwrap() {
cargo_metadata::Message::CompilerArtifact(artifact) => {
if artifact.target.kind.contains(&"cdylib".to_string()) {
return Ok(Artifact {
artifacts.push(Artifact {
path: PathBuf::from(artifact.filenames[0].to_string()),
manifest_path: PathBuf::from(
artifact.manifest_path.to_string(),
Expand All @@ -57,6 +58,12 @@ impl Build {
}
}

// TODO: Fix. Not an ideal way to get the artifact of the desired crate, but it
// works for most case.
if let Some(artifact) = artifacts.pop() {
return Ok(artifact);
}

Err(std::io::Error::new(
std::io::ErrorKind::Other,
"failed to parse cargo output",
Expand All @@ -72,9 +79,8 @@ impl Build {
}
}

pub fn metadata(path: &Path) -> Result<String> {
pub fn metadata() -> Result<String> {
let metadata = cargo_metadata::MetadataCommand::new()
.manifest_path(path)
.exec()
.map_err(|e| {
println!("failed to execute `cargo metadata`: {}", e);
Expand Down
10 changes: 5 additions & 5 deletions deno_bindgen_cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ fn main() -> std::io::Result<()> {
let opt = Opt::from_args();

let cwd = std::env::current_dir().unwrap();
let Artifact {
path,
manifest_path,
} = cargo::Build::new().release(opt.release).build(&cwd)?;
let Artifact { path, .. } =
cargo::Build::new().release(opt.release).build(&cwd)?;

let name = cargo::metadata(&manifest_path)?;
let name = cargo::metadata()?;
println!("Initializing {name}");

unsafe {
dlfcn::load_and_init(&PathBuf::from(path), opt.out, opt.lazy_init)?
};

println!("Ready {name}");
Ok(())
}
8 changes: 7 additions & 1 deletion e2e_test/bench.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { add, bytelen } from "./bindings/bindings.ts";
import { add, bytelen, Foo, make_foo } from "./bindings/mod.ts";

Deno.bench("add", () => add(1, 2));

const b = new Uint8Array([1, 2, 3, 4]);
Deno.bench("bytelen", () => bytelen(b));

Deno.bench("make_foo", () => make_foo(21));
Deno.bench("new Foo", () => new Foo(21));

const foo = new Foo(21);
Deno.bench("Foo#bar", () => foo.bar(1));
Loading

0 comments on commit 6b996b0

Please sign in to comment.