This repository was archived by the owner on Jul 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmain.rs
More file actions
475 lines (411 loc) · 17.7 KB
/
main.rs
File metadata and controls
475 lines (411 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#![allow(dead_code)]
use bytes::Bytes;
use fp_bindgen::{prelude::*, types::CargoDependency};
use once_cell::sync::Lazy;
use redux_example::{ReduxAction, StateUpdate};
use serde_bytes::ByteBuf;
use std::collections::{BTreeMap, BTreeSet};
// Referencing types using their full module path can be problematic in some
// edge cases. If you want to use types from other modules in your protocol,
// it's best to import them with a `use` statement and refer to them by their
// name only.
mod types;
use types::*;
fp_import! {
// Aliases need to be explicitly mentioned in either `fp_import!` or
// `fp_export!`.
//
// See `types/aliases.rs` for more info.
type Body = ByteBuf;
type FloatingPoint = Point<f64>;
type HttpResult = Result<Response, RequestError>;
type Int64 = u64;
// Types that are not referenced by any of the protocol functions (either
// directly as argument or return type, or indirectly through other types)
// need to be explicitly "used" in order to be generated as part of the
// generated bindings. Nested modules are supported in `use` statements.
//
// See `types/dead_code.rs` for more info.
use ExplicitedlyImportedType;
use submodule::{nested::GroupImportedType1, GroupImportedType2};
use types::{DocExampleEnum, DocExampleStruct};
// ===============================================================
// Imported functions that we call as part of the end-to-end tests
// ===============================================================
// No arguments, no return type:
fn import_void_function();
// No arguments, empty return type:
fn import_void_function_empty_return() -> ();
// No arguments, generic empty result type:
fn import_void_function_empty_result() -> Result<(), u32>;
// Passing primitives:
fn import_primitive_bool_negate(arg: bool) -> bool;
fn import_primitive_f32_add_one(arg: f32) -> f32;
fn import_primitive_f64_add_one(arg: f64) -> f64;
// NOTICE: This is a workaround for a bug in wasmer 2.3, where imported functions
// receive 0.0 instead of the the float value they were called with.
// This bug is fixed in wasmer 3, but in the meantime, you can use this workaround.
// See https://github.com/fiberplane/fp-bindgen/issues/180
fn import_primitive_f32_add_one_wasmer2(arg: [f32; 1]) -> f32;
fn import_primitive_f64_add_one_wasmer2(arg: [f64; 1]) -> f64;
fn import_primitive_i8_add_one(arg: i8) -> i8;
fn import_primitive_i16_add_one(arg: i16) -> i16;
fn import_primitive_i32_add_one(arg: i32) -> i32;
fn import_primitive_i64_add_one(arg: i64) -> i64;
fn import_primitive_u8_add_one(arg: u8) -> u8;
fn import_primitive_u16_add_one(arg: u16) -> u16;
fn import_primitive_u32_add_one(arg: u32) -> u32;
fn import_primitive_u64_add_one(arg: u64) -> u64;
// Passing arrays:
fn import_array_u8(arg: [u8; 3]) -> [u8; 3];
fn import_array_u16(arg: [u16; 3]) -> [u16; 3];
fn import_array_u32(arg: [u32; 3]) -> [u32; 3];
fn import_array_i8(arg: [i8; 3]) -> [i8; 3];
fn import_array_i16(arg: [i16; 3]) -> [i16; 3];
fn import_array_i32(arg: [i32; 3]) -> [i32; 3];
fn import_array_f32(arg: [f32; 3]) -> [f32; 3];
fn import_array_f64(arg: [f64; 3]) -> [f64; 3];
// Passing strings:
fn import_string(arg: String) -> String;
// Multiple arguments:
fn import_multiple_primitives(arg1: i8, arg2: String) -> i64;
// Integration with the `time` crate:
fn import_timestamp(arg: MyDateTime) -> MyDateTime;
// Passing custom types with flattened properties.
//
// See `types/flattening.rs` for more info.
fn import_fp_flatten(arg: FpFlatten) -> FpFlatten;
fn import_serde_flatten(arg: SerdeFlatten) -> SerdeFlatten;
// Generics.
//
// See `types/generics.rs` for more info.
fn import_generics(arg: StructWithGenerics<u64>) -> StructWithGenerics<u64>;
fn import_explicit_bound_point(arg: ExplicitBoundPoint<u64>);
// Options
fn import_struct_with_options(arg: StructWithOptions) -> StructWithOptions;
// Custom type in a generic position.
fn import_get_bytes() -> Result<Bytes, String>;
fn import_get_serde_bytes() -> Result<ByteBuf, String>;
// Passing custom types with property/variant renaming.
//
// See `types/renaming.rs` for more info.
fn import_fp_struct(arg: FpPropertyRenaming) -> FpPropertyRenaming;
fn import_fp_enum(arg: FpVariantRenaming) -> FpVariantRenaming;
fn import_serde_struct(arg: SerdePropertyRenaming) -> SerdePropertyRenaming;
fn import_serde_enum(arg: SerdeVariantRenaming) -> SerdeVariantRenaming;
// Passing custom enums with different tagging options.
//
// See `types/tagged_enums.rs` for more info.
fn import_fp_internally_tagged(arg: FpInternallyTagged) -> FpInternallyTagged;
fn import_fp_adjacently_tagged(arg: FpAdjacentlyTagged) -> FpAdjacentlyTagged;
fn import_fp_untagged(arg: FpUntagged) -> FpUntagged;
fn import_serde_internally_tagged(arg: SerdeInternallyTagged) -> SerdeInternallyTagged;
fn import_serde_adjacently_tagged(arg: SerdeAdjacentlyTagged) -> SerdeAdjacentlyTagged;
fn import_serde_untagged(arg: SerdeUntagged) -> SerdeUntagged;
// Passing primitives as async:
async fn import_primitive_bool_negate_async(arg: bool) -> bool;
async fn import_primitive_f32_add_one_async(arg: f32) -> f32;
async fn import_primitive_f64_add_one_async(arg: f64) -> f64;
async fn import_primitive_i8_add_one_async(arg: i8) -> i8;
async fn import_primitive_i16_add_one_async(arg: i16) -> i16;
async fn import_primitive_i32_add_one_async(arg: i32) -> i32;
async fn import_primitive_i64_add_one_async(arg: i64) -> i64;
async fn import_primitive_u8_add_one_async(arg: u8) -> u8;
async fn import_primitive_u16_add_one_async(arg: u16) -> u16;
async fn import_primitive_u32_add_one_async(arg: u32) -> u32;
async fn import_primitive_u64_add_one_async(arg: u64) -> u64;
// Test that void return works with async as well.
// Intentionally explicit unit struct return in only one declaration, to test both variants.
async fn import_reset_global_state() -> ();
async fn import_increment_global_state();
// Async function:
async fn import_fp_struct(arg1: FpPropertyRenaming, arg2: u64) -> FpPropertyRenaming;
/// Logs a message to the (development) console.
fn log(message: String);
/// Example how a runtime could expose a `Fetch`-like function to plugins.
///
/// See `types/http.rs` for more info.
async fn make_http_request(request: Request) -> HttpResult;
}
fp_export! {
// ===============================================================
// Exported functions that we call as part of the end-to-end tests
// ===============================================================
// No arguments, no return type:
fn export_void_function();
// Passing primitives:
fn export_primitive_bool_negate(arg: bool) -> bool;
fn export_primitive_f32_add_three(arg: f32) -> f32;
fn export_primitive_f64_add_three(arg: f64) -> f64;
fn export_primitive_f32_add_three_wasmer2(arg: f32) -> f32;
fn export_primitive_f64_add_three_wasmer2(arg: f64) -> f64;
fn export_primitive_i8_add_three(arg: i8) -> i8;
fn export_primitive_i16_add_three(arg: i16) -> i16;
fn export_primitive_i32_add_three(arg: i32) -> i32;
fn export_primitive_i64_add_three(arg: i64) -> i64;
fn export_primitive_u8_add_three(arg: u8) -> u8;
fn export_primitive_u16_add_three(arg: u16) -> u16;
fn export_primitive_u32_add_three(arg: u32) -> u32;
fn export_primitive_u64_add_three(arg: u64) -> u64;
// Passing arrays:
fn export_array_u8(arg: [u8; 3]) -> [u8; 3];
fn export_array_u16(arg: [u16; 3]) -> [u16; 3];
fn export_array_u32(arg: [u32; 3]) -> [u32; 3];
fn export_array_i8(arg: [i8; 3]) -> [i8; 3];
fn export_array_i16(arg: [i16; 3]) -> [i16; 3];
fn export_array_i32(arg: [i32; 3]) -> [i32; 3];
fn export_array_f32(arg: [f32; 3]) -> [f32; 3];
fn export_array_f64(arg: [f64; 3]) -> [f64; 3];
// Passing strings:
fn export_string(arg: String) -> String;
// Multiple arguments:
fn export_multiple_primitives(arg1: i8, arg2: String) -> i64;
// Integration with the `time` crate:
fn export_timestamp(arg: MyDateTime) -> MyDateTime;
// Passing custom types with flattened properties.
//
// See `types/flattening.rs` for more info.
fn export_fp_flatten(arg: FpFlatten) -> FpFlatten;
fn export_serde_flatten(arg: SerdeFlatten) -> SerdeFlatten;
// Generics.
//
// See `types/generics.rs` for more info.
fn export_generics(arg: StructWithGenerics<u64>) -> StructWithGenerics<u64>;
// Options
fn export_struct_with_options(arg: StructWithOptions) -> StructWithOptions;
// Custom type in a generic position.
fn export_get_bytes() -> Result<Bytes, String>;
fn export_get_serde_bytes() -> Result<ByteBuf, String>;
// Passing custom types with property/variant renaming.
//
// See `types/renaming.rs` for more info.
fn export_fp_struct(arg: FpPropertyRenaming) -> FpPropertyRenaming;
fn export_fp_enum(arg: FpVariantRenaming) -> FpVariantRenaming;
fn export_serde_struct(arg: SerdePropertyRenaming) -> SerdePropertyRenaming;
fn export_serde_enum(arg: SerdeVariantRenaming) -> SerdeVariantRenaming;
// Passing custom enums with different tagging options.
//
// See `types/tagged_enums.rs` for more info.
fn export_fp_internally_tagged(arg: FpInternallyTagged) -> FpInternallyTagged;
fn export_fp_adjacently_tagged(arg: FpAdjacentlyTagged) -> FpAdjacentlyTagged;
fn export_fp_untagged(arg: FpUntagged) -> FpUntagged;
fn export_serde_internally_tagged(arg: SerdeInternallyTagged) -> SerdeInternallyTagged;
fn export_serde_adjacently_tagged(arg: SerdeAdjacentlyTagged) -> SerdeAdjacentlyTagged;
fn export_serde_untagged(arg: SerdeUntagged) -> SerdeUntagged;
// Passing primitives with async:
async fn export_primitive_bool_negate_async(arg: bool) -> bool;
async fn export_primitive_f32_add_three_async(arg: f32) -> f32;
async fn export_primitive_f64_add_three_async(arg: f64) -> f64;
async fn export_primitive_i8_add_three_async(arg: i8) -> i8;
async fn export_primitive_i16_add_three_async(arg: i16) -> i16;
async fn export_primitive_i32_add_three_async(arg: i32) -> i32;
async fn export_primitive_i64_add_three_async(arg: i64) -> i64;
async fn export_primitive_u8_add_three_async(arg: u8) -> u8;
async fn export_primitive_u16_add_three_async(arg: u16) -> u16;
async fn export_primitive_u32_add_three_async(arg: u32) -> u32;
async fn export_primitive_u64_add_three_async(arg: u64) -> u64;
// Test that void return works with async as well.
// Intentionally explicit unit struct return in only one declaration, to test both variants.
async fn export_reset_global_state() -> ();
async fn export_increment_global_state();
// Async function:
async fn export_async_struct(arg1: FpPropertyRenaming, arg2: u64) -> FpPropertyRenaming;
/// Example how plugin could expose async data-fetching capabilities.
async fn fetch_data(r#type: String) -> Result<String, String>;
/// Example that shows how to make parallel requests from the guest module.
async fn make_parallel_requests() -> String;
/// Called on the plugin to give it a chance to initialize.
fn init();
/// Example how plugin could expose a reducer.
fn reducer_bridge(action: ReduxAction) -> StateUpdate;
}
const VERSION: &str = "1.0.0";
const NAME: &str = "example-bindings";
const DESCRIPTION: &str = "Bindings to the fp-bindgen example protocol";
const LICENSE: &str = "MIT OR Apache-2.0";
fn authors() -> Vec<String> {
vec!["Fiberplane <info@fiberplane.com>".to_string()]
}
static PLUGIN_DEPENDENCIES: Lazy<BTreeMap<&str, CargoDependency>> = Lazy::new(|| {
BTreeMap::from([
(
"redux-example",
CargoDependency::with_path("../../../redux-example"),
),
(
"fp-bindgen-support",
CargoDependency::with_path_and_features(
"../../../../fp-bindgen-support",
BTreeSet::from(["async", "guest"]),
),
),
(
"time",
CargoDependency::with_version_and_features("0.3", BTreeSet::from(["macros"])),
),
])
});
fn main() {
for bindings_type in [
BindingsType::RustPlugin(
RustPluginConfig::builder()
.name(NAME)
.authors(authors())
.version(VERSION)
.description(DESCRIPTION)
.license(LICENSE)
.dependencies(PLUGIN_DEPENDENCIES.clone())
.build(),
),
BindingsType::RustWasmer2Runtime,
BindingsType::RustWasmer2WasiRuntime,
BindingsType::TsRuntime(
TsRuntimeConfig::new()
.with_msgpack_module("https://unpkg.com/@msgpack/msgpack@2.7.2/mod.ts")
.with_raw_export_wrappers()
.without_streaming_instantiation(),
),
] {
let output_path = format!("bindings/{bindings_type}");
fp_bindgen!(BindingConfig {
bindings_type,
path: &output_path,
});
println!("Generated bindings written to `{output_path}/`.");
}
}
#[test]
fn test_generate_rust_plugin() {
static FILES: &[(&str, &[u8])] = &[
(
"bindings/rust-plugin/src/types.rs",
include_bytes!("assets/rust_plugin_test/expected_types.rs"),
),
(
"bindings/rust-plugin/src/lib.rs",
include_bytes!("assets/rust_plugin_test/expected_lib.rs"),
),
(
"bindings/rust-plugin/src/export.rs",
include_bytes!("assets/rust_plugin_test/expected_export.rs"),
),
(
"bindings/rust-plugin/src/import.rs",
include_bytes!("assets/rust_plugin_test/expected_import.rs"),
),
(
"bindings/rust-plugin/Cargo.toml",
include_bytes!("assets/rust_plugin_test/expected_Cargo.toml"),
),
];
fp_bindgen!(BindingConfig {
bindings_type: BindingsType::RustPlugin(
RustPluginConfig::builder()
.name(NAME)
.authors(authors())
.version(VERSION)
.description(DESCRIPTION)
.license(RustPluginConfigValue::Workspace)
.dependencies(PLUGIN_DEPENDENCIES.clone())
.readme("README.md")
.build()
),
path: "bindings/rust-plugin",
});
for (path, expected) in FILES {
tests::assert_file_eq(path, expected)
}
}
#[test]
fn test_generate_rust_plugin_without_some_fields() {
fp_bindgen!(BindingConfig {
bindings_type: BindingsType::RustPlugin(
RustPluginConfig::builder()
.name(NAME)
.authors(authors())
.version(VERSION)
.dependencies(PLUGIN_DEPENDENCIES.clone())
.build()
),
path: "bindings/rust-plugin-no-optionals",
});
tests::assert_file_eq(
"bindings/rust-plugin-no-optionals/Cargo.toml",
include_bytes!("assets/rust_plugin_test/expected_Cargo_no_optionals.toml"),
);
}
#[test]
fn test_generate_rust_wasmer2_runtime() {
static FILES: &[(&str, &[u8])] = &[
(
"bindings/rust-wasmer2-runtime/bindings.rs",
include_bytes!("assets/rust_wasmer2_runtime_test/expected_bindings.rs"),
),
(
"bindings/rust-wasmer2-runtime/types.rs",
include_bytes!("assets/rust_wasmer2_runtime_test/expected_types.rs"),
),
];
fp_bindgen!(BindingConfig {
bindings_type: BindingsType::RustWasmer2Runtime,
path: "bindings/rust-wasmer2-runtime",
});
for (path, expected) in FILES {
tests::assert_file_eq(path, expected)
}
}
#[test]
fn test_generate_rust_wasmer2_wasi_runtime() {
static FILES: &[(&str, &[u8])] = &[
(
"bindings/rust-wasmer2-wasi-runtime/bindings.rs",
include_bytes!("assets/rust_wasmer2_wasi_runtime_test/expected_bindings.rs"),
),
(
"bindings/rust-wasmer2-wasi-runtime/types.rs",
include_bytes!("assets/rust_wasmer2_wasi_runtime_test/expected_types.rs"),
),
];
fp_bindgen!(BindingConfig {
bindings_type: BindingsType::RustWasmer2WasiRuntime,
path: "bindings/rust-wasmer2-wasi-runtime",
});
for (path, expected) in FILES {
tests::assert_file_eq(path, expected)
}
}
#[test]
fn test_generate_ts_runtime() {
static FILES: &[(&str, &[u8])] = &[
(
"bindings/ts-runtime/types.ts",
include_bytes!("assets/ts_runtime_test/expected_types.ts"),
),
(
"bindings/ts-runtime/index.ts",
include_bytes!("assets/ts_runtime_test/expected_index.ts"),
),
];
fp_bindgen!(BindingConfig {
bindings_type: BindingsType::TsRuntime(
TsRuntimeConfig::new()
.with_msgpack_module("https://unpkg.com/@msgpack/msgpack@2.7.2/mod.ts")
.with_raw_export_wrappers()
),
path: "bindings/ts-runtime",
});
for (path, expected) in FILES {
tests::assert_file_eq(path, expected)
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
pub fn assert_file_eq(path_of_actual: impl AsRef<Path>, expected_bytes: &[u8]) {
let actual = std::fs::read_to_string(path_of_actual).expect("Cannot read `actual` file");
let expected_code = String::from_utf8_lossy(expected_bytes);
let actual_lines = actual.lines().collect::<Vec<_>>();
let expected_lines = expected_code.lines().collect::<Vec<_>>();
pretty_assertions::assert_eq!(actual_lines, expected_lines);
}
}