Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/revamp host interaction #5

Merged
merged 17 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 align="center">Welcome to holium rust SDK 👋</h1>

> Holium Rust SDK is a tool used to compile rust code to proper Holium transformation. It leverages procedural macro to do so.
> Holium Rust SDK is a tool used to compile rust code to proper Holium transformations. It leverages procedural macro to do so.

### 🏠 [Homepage](https://holium.org/)

Expand All @@ -10,7 +10,7 @@ The project is divided in 4 main parts.

### Holium Rust SDK

Located in `./src`, the Holium Rust SDK is the crate that exposes the procdural macro to the rust code. It is also in
Located in `./crates/sdk`, the Holium Rust SDK is the crate that exposes the procedural macro to the rust code. It is also in
charge of exposing internal dependencies to ensure that the generated code works.

### Macro
Expand Down
15 changes: 11 additions & 4 deletions crates/backend/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl TryToTokens for ast::Export {
export_name = #exported_name,
)]
#[allow(clippy::all)]
pub extern "C" fn #holium_func_name() {
pub extern "C" fn #holium_func_name(ptr: *mut u8, len: usize) -> holium_rust_sdk::internal::memory::Slice {
#[derive(holium_rust_sdk::internal::serde::Serialize, holium_rust_sdk::internal::serde::Deserialize)]
#[serde( crate = "holium_rust_sdk::internal::serde")]
struct InputPayload {
Expand All @@ -209,15 +209,22 @@ impl TryToTokens for ast::Export {
}
}

let input: InputPayload = holium_rust_sdk::internal::api::get_payload().unwrap().into();
let payload_u8: &[u8] = unsafe { std::slice::from_raw_parts(ptr, len) };
let data_node: holium_rust_sdk::internal::data_tree::Node = holium_rust_sdk::internal::serde_cbor::from_slice(payload_u8).unwrap();

let input: InputPayload = data_node.into();

let output = #receiver(#(#converted_args),*);

let output_cbor = holium_rust_sdk::internal::serde_cbor::value::to_value(output).unwrap();
let output_cbor = holium_rust_sdk::internal::serde_cbor::value::to_value(vec![output]).unwrap();

let output_node = holium_rust_sdk::internal::data_tree::Node::new(output_cbor);
let output_node_u8 = holium_rust_sdk::internal::serde_cbor::to_vec(&output_node).unwrap();

holium_rust_sdk::internal::api::set_payload(&output_node).unwrap();
holium_rust_sdk::internal::memory::Slice {
ptr: output_node_u8.as_ptr() as u32,
len: output_node_u8.len() as u32
}
}
})
.to_tokens(into);
Expand Down
30 changes: 15 additions & 15 deletions crates/macro/tests/proc-macro-tests/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,62 @@ pub struct GoodStruct {
}

#[holium_bindgen]
pub fn good1() {}
pub fn pass1() {}

#[holium_bindgen]
pub fn good2() -> u32 {
pub fn pass2() -> u32 {
0
}

#[holium_bindgen]
pub fn good3(a: u32) -> u32 {
pub fn pass3(a: u32) -> u32 {
a
}

#[holium_bindgen]
pub fn good4(a: u32) -> GoodStruct {
pub fn pass4(a: u32) -> GoodStruct {
GoodStruct { number: a }
}

#[holium_bindgen]
pub fn good5(a: GoodStruct) -> GoodStruct {
pub fn pass5(a: GoodStruct) -> GoodStruct {
a
}

#[holium_bindgen]
pub fn good6(a: &GoodStruct) -> GoodStruct {
pub fn pass6(a: &GoodStruct) -> GoodStruct {
GoodStruct { number: a.number }
}

#[holium_bindgen]
pub fn good7(a: &mut GoodStruct) -> GoodStruct {
pub fn pass7(a: &mut GoodStruct) -> GoodStruct {
a.number += 10;
let new_struct = GoodStruct { number: a.number };
return new_struct;
}

#[holium_bindgen]
pub fn good8(a: &mut GoodStruct) -> (GoodStruct, u32) {
pub fn pass8(a: &mut GoodStruct) -> (GoodStruct, u32) {
let old_number = a.number;
a.number += 10;
let new_struct = GoodStruct { number: a.number };
return (new_struct, old_number);
}

#[holium_bindgen]
pub fn good9(a: &mut GoodStruct, b: String) -> (GoodStruct, String) {
pub fn pass9(a: &mut GoodStruct, b: String) -> (GoodStruct, String) {
a.number += 10;
let new_struct = GoodStruct { number: a.number };
return (new_struct, b);
}

#[holium_bindgen]
pub fn good10(a: Option<u32>) -> Option<u32> {
pub fn pass10(a: Option<u32>) -> Option<u32> {
a
}

#[holium_bindgen]
pub fn good11(a: Vec<u32>) -> Vec<u32> {
pub fn pass11(a: Vec<u32>) -> Vec<u32> {
a
}

Expand All @@ -72,7 +72,7 @@ struct BadStructNoMacro {
}

#[holium_bindgen]
pub fn bad1(a: BadStruct) -> BadStruct {
pub fn fail1(a: BadStructNoMacro) -> BadStructNoMacro {
a
}

Expand All @@ -82,19 +82,19 @@ struct BadStructOnlySerde {
}

#[holium_bindgen]
pub fn bad2(a: BadStructOnlySerde) -> BadStructOnlySerde {
pub fn fail2(a: BadStructOnlySerde) -> BadStructOnlySerde {
a
}

#[holium_bindgen]
pub fn bad3<'a>(x: &'a GoodStruct, y: &'a GoodStruct) -> &'a GoodStruct {
pub fn fail3<'a>(x: &'a GoodStruct, y: &'a GoodStruct) -> &'a GoodStruct {
GoodStruct {
number: x.number + y.number,
}
}

#[holium_bindgen]
pub fn bad4<T>(x: T) -> T {
pub fn fail4<T>(x: T) -> T {
x
}

Expand Down
77 changes: 57 additions & 20 deletions crates/macro/tests/proc-macro-tests/export.stderr
Original file line number Diff line number Diff line change
@@ -1,35 +1,72 @@
error: can't #[holium_bindgen] functions with lifetime or type parameters
--> $DIR/export.rs:90:12
--> tests/proc-macro-tests/export.rs:90:13
|
90 | pub fn bad3<'a>(x: &'a GoodStruct, y: &'a GoodStruct) -> &'a GoodStruct {
| ^^^^
90 | pub fn fail3<'a>(x: &'a GoodStruct, y: &'a GoodStruct) -> &'a GoodStruct {
| ^^^^

error: can't #[holium_bindgen] functions with lifetime or type parameters
--> $DIR/export.rs:97:12
--> tests/proc-macro-tests/export.rs:97:13
|
97 | pub fn bad4<T>(x: T) -> T {
| ^^^
97 | pub fn fail4<T>(x: T) -> T {
| ^^^

error[E0412]: cannot find type `BadStruct` in this scope
--> $DIR/export.rs:75:16
error[E0277]: the trait bound `BadStructNoMacro: Serialize` is not satisfied
--> tests/proc-macro-tests/export.rs:74:1
|
74 | #[holium_bindgen]
| ^^^^^^^^^^^^^^^^^ the trait `Serialize` is not implemented for `BadStructNoMacro`
|
::: $CARGO/serde_cbor-0.11.2/src/value/ser.rs
|
| T: Serialize,
| --------- required by this bound in `to_value`
|
= note: required because of the requirements on the impl of `Serialize` for `Vec<BadStructNoMacro>`
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no function or associated item named `generate_node` found for struct `BadStructNoMacro` in the current scope
--> tests/proc-macro-tests/export.rs:74:1
|
6 | pub struct GoodStruct {
| --------------------- similarly named struct `GoodStruct` defined here
70 | struct BadStructNoMacro {
| ----------------------- function or associated item `generate_node` not found for this
...
75 | pub fn bad1(a: BadStruct) -> BadStruct {
| ^^^^^^^^^ help: a struct with a similar name exists: `GoodStruct`
74 | #[holium_bindgen]
| ^^^^^^^^^^^^^^^^^ function or associated item not found in `BadStructNoMacro`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `generate_node`, perhaps you need to implement it:
candidate #1: `holium_rust_sdk::GenerateNode`
= note: this error originates in the attribute macro `holium_bindgen` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0412]: cannot find type `BadStruct` in this scope
--> $DIR/export.rs:75:30
error[E0277]: the trait bound `BadStructNoMacro: Serialize` is not satisfied
--> tests/proc-macro-tests/export.rs:74:1
|
6 | pub struct GoodStruct {
| --------------------- similarly named struct `GoodStruct` defined here
...
75 | pub fn bad1(a: BadStruct) -> BadStruct {
| ^^^^^^^^^ help: a struct with a similar name exists: `GoodStruct`
74 | #[holium_bindgen]
| ^^^^^^^^^^^^^^^^^ the trait `Serialize` is not implemented for `BadStructNoMacro`
|
= note: required by `_::_serde::ser::SerializeStruct::serialize_field`
= note: this error originates in the derive macro `holium_rust_sdk::internal::serde::Serialize` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `BadStructNoMacro: Deserialize<'_>` is not satisfied
--> tests/proc-macro-tests/export.rs:74:1
|
74 | #[holium_bindgen]
| ^^^^^^^^^^^^^^^^^ the trait `Deserialize<'_>` is not implemented for `BadStructNoMacro`
|
= note: required by `next_element`
= note: this error originates in the attribute macro `holium_bindgen` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `BadStructNoMacro: Deserialize<'_>` is not satisfied
--> tests/proc-macro-tests/export.rs:74:1
|
74 | #[holium_bindgen]
| ^^^^^^^^^^^^^^^^^ the trait `Deserialize<'_>` is not implemented for `BadStructNoMacro`
|
= note: required by `next_value`
= note: this error originates in the attribute macro `holium_bindgen` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no function or associated item named `generate_node` found for struct `BadStructOnlySerde` in the current scope
--> $DIR/export.rs:84:1
--> tests/proc-macro-tests/export.rs:84:1
|
80 | struct BadStructOnlySerde {
| ------------------------- function or associated item `generate_node` not found for this
Expand Down
57 changes: 0 additions & 57 deletions crates/sdk/src/internal/api.rs

This file was deleted.

74 changes: 0 additions & 74 deletions crates/sdk/src/internal/host_interface.rs

This file was deleted.

Loading