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

feat(dan): basic template macro #4358

Merged
merged 11 commits into from
Jul 29, 2022
15 changes: 13 additions & 2 deletions dan_layer/engine/tests/hello_world/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dan_layer/engine/tests/hello_world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"

[dependencies]
tari_template_abi = { path = "../../../template_abi" }
tari_template_macros = { path = "../macros" }
common = { path = "../common" }

[profile.release]
Expand Down
37 changes: 8 additions & 29 deletions dan_layer/engine/tests/hello_world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,14 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// TODO: we should only use stdlib if the template dev needs to include it e.g. use core::mem when stdlib is not
// available
use tari_template_macros::template;

use common::{generate_abi, generate_main, TemplateImpl};
use tari_template_abi::{encode_with_len, FunctionDef, Type};
template! {
Copy link
Member

@sdbondi sdbondi Jul 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scoping is why we'll need to put something like

#[tari::contract]
mod hello_world {
   pub struct HelloWorld { etc.}
}

If we want to avoid wrapping in template! {...}, unless there's another way to get the scoping (I can't think of any) - the main reason is that code completion on some editors breaks when wrapped in declarative macros.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine IMO

It also allows some flexibility, because you can also write your contract in my_contract.rs, and then in lib.rs have

#[tari::contract]
mod my_contract;

struct HelloWorld {}

// TODO: Macro generated code
#[no_mangle]
extern "C" fn HelloWorld_abi() -> *mut u8 {
let template_name = "HelloWorld".to_string();

let functions = vec![FunctionDef {
name: "greet".to_string(),
arguments: vec![],
output: Type::String,
}];

generate_abi(template_name, functions)
}

#[no_mangle]
extern "C" fn HelloWorld_main(call_info: *mut u8, call_info_len: usize) -> *mut u8 {
let mut template_impl = TemplateImpl::new();

template_impl.add_function("greet".to_string(), Box::new(|_| encode_with_len(&"Hello World!")));

generate_main(call_info, call_info_len, template_impl)
}

extern "C" {
pub fn tari_engine(op: u32, input_ptr: *const u8, input_len: usize) -> *mut u8;
impl HelloWorld {
pub fn greet() -> String {
"Hello World!".to_string()
}
}
}
185 changes: 185 additions & 0 deletions dan_layer/engine/tests/macros/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions dan_layer/engine/tests/macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[workspace]
[package]
name = "tari_template_macros"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
proc-macro = true

[dependencies]
tari_template_abi = { path = "../../../template_abi" }
syn = { version = "1.0.98", features = ["full"] }
proc-macro2 = "1.0.42"
quote = "1.0.20"
Loading