Skip to content

Commit 8dc3b30

Browse files
committed
Update FlatBuffer schema and generation process
- Add all.fbs to include all schema files in one place - Restructure function_call_result.fbs to use Result-like union - Add HostError variant to ErrorCode enum in guest_error.fbs - Update flatbuffer generation command in Justfile to use all.fbs - Update documentation for new generation process Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 323ae41 commit 8dc3b30

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

Justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ build-fuzzer fuzz-target:
359359
###################
360360

361361
gen-all-fbs-rust-code:
362-
for fbs in `find src -name "*.fbs"`; do flatc -r --rust-module-root-file --gen-all -o ./src/hyperlight_common/src/flatbuffers/ $fbs; done
362+
flatc --rust --rust-module-root-file --gen-all -o ./src/hyperlight_common/src/flatbuffers/ ./src/schema/all.fbs
363363
just fmt-apply
364364

365365
install-vcpkg:

docs/how-to-use-flatbuffers.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,3 @@ We recommend building `flatc` from source. To generate rust code, use
1919
```console
2020
just gen-all-fbs-rust-code
2121
```
22-
23-
### Note about generated code
24-
25-
Because we invoke `flatc` multiple times when generating the Rust code, the `mod.rs` generated in `./src/hyperlight_common/src/flatbuffers` is overwritten multiple times and will likely be incorrect. Make sure to manually inspect and if necessary update this file before continuing with your changes as certain modules might be missing. After fixing `mod.rs`, you might need to re-run `just fmt`, since it might not have applied to all generated files if your `mod.rs` was invalid.
26-
27-
>`flatc` does support passing multiple schema files (e.g. it is possible to pass `.\src\schema\*.fbs`), so we could regenerate all the files each time a change was made, however that generates incorrect code (see [here](https://github.com/google/flatbuffers/issues/6800) for details).

src/schema/all.fbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include "function_types.fbs";
2+
include "function_call_result.fbs";
3+
include "function_call.fbs";
4+
include "guest_error.fbs";
5+
include "guest_log_data.fbs";
6+
include "host_function_definition.fbs";
7+
include "host_function_details.fbs";
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
include "function_types.fbs";
2+
include "guest_error.fbs";
23

34
namespace Hyperlight.Generated;
45

6+
// Wrapper so ReturnValue (a union) can be a single union variant
7+
table ReturnValueBox {
8+
value: ReturnValue (required);
9+
}
10+
11+
// Result-like union
12+
union FunctionCallResultType {
13+
ReturnValueBox,
14+
GuestError
15+
}
16+
517
table FunctionCallResult {
6-
return_value:ReturnValue(required);
18+
result: FunctionCallResultType (required);
719
}
820

921
root_type FunctionCallResult;

src/schema/guest_error.fbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ enum ErrorCode: ulong {
1616
MallocFailed = 13, // this error is set when malloc returns 0 bytes.
1717
GuestFunctionParameterTypeMismatch = 14, // The function call parameter type was not the expected type.
1818
GuestError = 15, // An error occurred in the guest Guest implementation should use this along with a message when calling setError.
19-
ArrayLengthParamIsMissing = 16 // Expected a int parameter to follow a byte array
19+
ArrayLengthParamIsMissing = 16, // Expected a int parameter to follow a byte array
20+
HostError = 17 // Guest called Host Function, which errored.
2021
}
2122

2223
table GuestError {

0 commit comments

Comments
 (0)