Skip to content

Commit

Permalink
Merge pull request gnunicorn#1 from bmcpt/shared_buffer
Browse files Browse the repository at this point in the history
Shared buffer
  • Loading branch information
gnunicorn authored Feb 16, 2022
2 parents 2b9c33c + fb3c19e commit 5c3ea85
Show file tree
Hide file tree
Showing 22 changed files with 1,374 additions and 88 deletions.
5 changes: 4 additions & 1 deletion example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ crate-type = ["cdylib"]
ffi-gen = { version = "0.1.0", path = ".." }

[dependencies]
anyhow = "1.0.51"
anyhow = "1.0.53"
ffi-gen = { version = "0.1.0", path = ".." }
ffi-gen-macro = { version = "0.1.0", path = "../macro" }
futures = "0.3.17"
ureq = "2.4.0"
paste = "1.0.6"
17 changes: 4 additions & 13 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
## build
```sh
cargo build --target wasm32-unknown-unknown && cp ../target/wasm32-unknown-unknown/debug/api.wasm js
```
Added a new data type called 'buffer'. Example demonstrates how to use it.

### run in node
```sh
node js/index.mjs
```

### run in the browser
```sh
npx --yes http-server js
```
cd example/dart
cargo build
LD_LIBRARY_PATH=../../target/debug dart bin/simple_bench.dart
22 changes: 19 additions & 3 deletions example/api.rsh
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
/// Prints a friendly greeting to stdout.
fn hello_world();

/// Returns a future that prints a friendly
/// greeting to stdout.
fn async_hello_world() -> Future<Result<u8>>;

fn get_image() -> buffer<u8>;

fn create(n: usize) -> DataTest;
object DataTest {
fn get_copy() -> Vec<u8>;
fn get_shmem() -> buffer<u8>;
}

fn get_u8_counting(n: usize) -> buffer<u8>;
fn get_u16_counting(n: usize) -> buffer<u16>;
fn get_u32_counting(n: usize) -> buffer<u32>;
fn get_u64_counting(n: usize) -> buffer<u64>;
fn get_i8_counting(n: usize) -> buffer<i8>;
fn get_i16_counting(n: usize) -> buffer<i16>;
fn get_i32_counting(n: usize) -> buffer<i32>;
fn get_i64_counting(n: usize) -> buffer<i64>;
fn get_f32_counting(n: usize) -> buffer<f32>;
fn get_f64_counting(n: usize) -> buffer<f64>;
16 changes: 11 additions & 5 deletions example/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ffi_gen::FfiGen;
use ffi_gen::{Abi, FfiGen};
use std::path::PathBuf;

fn main() {
Expand All @@ -8,11 +8,17 @@ fn main() {
"cargo:rerun-if-changed={}",
path.as_path().to_str().unwrap()
);
println!(
"cargo:rerun-if-changed={}",
dir.join("build.rs").as_path().to_str().unwrap()
);
let ffigen = FfiGen::new(&path).unwrap();
let rust = dir.join("src").join("bindings.rs");
std::fs::write(rust, format!("use crate::*;\nuse futures::Stream;\n{}", ffigen.generate_rust(Abi::Native64).unwrap())).unwrap();
let dart = dir.join("dart").join("lib").join("bindings.dart");
ffigen.generate_dart(dart, "api", "api").unwrap();
let js = dir.join("js").join("bindings.mjs");
ffigen.generate_js(js).unwrap();
let ts = dir.join("js").join("bindings.d.ts");
ffigen.generate_ts(ts).unwrap();
// let js = dir.join("js").join("bindings.mjs");
// ffigen.generate_js(js).unwrap();
// let ts = dir.join("js").join("bindings.d.ts");
// ffigen.generate_ts(ts).unwrap();
}
10 changes: 10 additions & 0 deletions example/dart/bin/image_fetch.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'dart:io';

import '../lib/bindings.dart';

void main() async {
final api = Api.load();
final img = await api.getImage();
print(img.toUint8List().length);
File.fromUri(Uri.parse('t.jpg')).writeAsBytes(img.toUint8List());
}
6 changes: 0 additions & 6 deletions example/dart/bin/main.dart

This file was deleted.

15 changes: 15 additions & 0 deletions example/dart/bin/simple_bench.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import '../lib/bindings.dart';

void main() async {
final n = 1024 * 1024 * 10;

final api = Api.load();
final data = api.create(n);
final t1 = DateTime.now();
print(data.getCopy().length);
final t2 = DateTime.now();
print(t2.difference(t1).inMilliseconds);
print(data.getShmem().toUint8List().length);
final t3 = DateTime.now();
print(t3.difference(t2).inMilliseconds);
}
33 changes: 33 additions & 0 deletions example/dart/bin/typed_buffers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import '../lib/bindings.dart';

void main() async {
final api = Api.load();
final u8s = api.getU8Counting(20).toUint8List();
final u16s = api.getU16Counting(20).toUint16List();
final u32s = api.getU32Counting(20).toUint32List();
final u64s = api.getU64Counting(20).toUint64List();
final i8s = api.getI8Counting(20).toInt8List();
final i16s = api.getI16Counting(20).toInt16List();
final i32s = api.getI32Counting(20).toInt32List();
final i64s = api.getI64Counting(20).toInt64List();
final f32s = api.getF32Counting(20).toFloat32List();
final f64s = api.getF64Counting(20).toFloat64List();
for (final i in positiveIntegers.take(20)) {
assert(u8s[i] == i);
assert(u16s[i] == i);
assert(u32s[i] == i);
assert(u64s[i] == i);
assert(i8s[i] == i);
assert(i16s[i] == i);
assert(i32s[i] == i);
assert(i64s[i] == i);
assert(f32s[i] == i);
assert(f64s[i] == i);
}
print('ok');
}

Iterable<int> get positiveIntegers sync* {
int i = 0;
while (true) yield i++;
}
Loading

0 comments on commit 5c3ea85

Please sign in to comment.