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

Macro for declaring entry point #70

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ In your project, include the prelude:
use ewasm_api::prelude::*;
```

Additionally there is support for some macros to make creating contracts easier:
```rust
#[macro_use]
extern crate ewasm_api;

use ewasm_api::prelude::*;

fn entry() {
// The actual contract code goes here.
}

ewasm_entry_point!(entry);
```

Other modules are available as well, outside of the prelude. Refer to the documentation for more info.

`ewasm-rust-api` builds with various feature sets:
Expand Down
32 changes: 28 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,29 @@
//! - `experimental`: Exposes the experimental bignum system library API.
//!
//! # Examples
//! ```ignore
//! extern crate ewasm_api;
//! ```
//! use ewasm_api::prelude::*;
//!
//! fn entry() {
//! let a: Hash = block_hash(1);
//! finish_data(&a.bytes);
//! }
//!
//! ewasm_entry_point!(entry);
//! ```
//!
//! use ewasm_api::{Hash, block_hash, finish_data};
//! Using lower-level primitives:
//! ```ignore
//! use ewasm_api::{types::Hash, block_hash, finish_data};
Copy link
Member Author

Choose a reason for hiding this comment

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

It seems after recent changes types isn't exposed anymore unless the prelude is used.

//!
//! #[cfg(target_arch = "wasm32")]
//! #[no_mangle]
//! pub extern "C" fn main() {
//! let a: Hash = block_hash(1);
//! let a: types::Hash = block_hash(1);
//! finish_data(&a.bytes);
//! }
//! ```
//!

#[macro_use]
extern crate cfg_if;
Expand Down Expand Up @@ -85,6 +96,19 @@ pub mod prelude {
pub use crate::eth2;
}

/// Declare entry point for a contract. Expects a Rust function name to be executed.
/// This will only compile in when using the wasm32 target.
#[macro_export]
macro_rules! ewasm_entry_point {
($name:ident) => {
#[cfg(target_arch = "wasm32")]
#[no_mangle]
pub extern "C" fn main() {
$name()
}
};
}

/// Enum representing an error code for EEI calls. Currently used by `codeCopy`, `callDataCopy`,
/// `externalCodeCopy`, and `returnDataCopy`.
pub enum Error {
Expand Down