Skip to content

Commit 61d2ff9

Browse files
authored
Merge pull request #70 from ewasm/entry-macro
Macro for declaring entry point
2 parents a72f2b4 + 2c051ce commit 61d2ff9

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ In your project, include the prelude:
1818
use ewasm_api::prelude::*;
1919
```
2020

21+
Additionally there is support for some macros to make creating contracts easier:
22+
```rust
23+
#[macro_use]
24+
extern crate ewasm_api;
25+
26+
use ewasm_api::prelude::*;
27+
28+
fn entry() {
29+
// The actual contract code goes here.
30+
}
31+
32+
ewasm_entry_point!(entry);
33+
```
34+
2135
Other modules are available as well, outside of the prelude. Refer to the documentation for more info.
2236

2337
`ewasm-rust-api` builds with various feature sets:

src/lib.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,29 @@
1414
//! - `experimental`: Exposes the experimental bignum system library API.
1515
//!
1616
//! # Examples
17-
//! ```ignore
18-
//! extern crate ewasm_api;
17+
//! ```
18+
//! use ewasm_api::prelude::*;
19+
//!
20+
//! fn entry() {
21+
//! let a: Hash = block_hash(1);
22+
//! finish_data(&a.bytes);
23+
//! }
24+
//!
25+
//! ewasm_entry_point!(entry);
26+
//! ```
1927
//!
20-
//! use ewasm_api::{Hash, block_hash, finish_data};
28+
//! Using lower-level primitives:
29+
//! ```ignore
30+
//! use ewasm_api::{types::Hash, block_hash, finish_data};
2131
//!
2232
//! #[cfg(target_arch = "wasm32")]
2333
//! #[no_mangle]
2434
//! pub extern "C" fn main() {
25-
//! let a: Hash = block_hash(1);
35+
//! let a: types::Hash = block_hash(1);
2636
//! finish_data(&a.bytes);
2737
//! }
2838
//! ```
39+
//!
2940
3041
#[macro_use]
3142
extern crate cfg_if;
@@ -85,6 +96,19 @@ pub mod prelude {
8596
pub use crate::eth2;
8697
}
8798

99+
/// Declare entry point for a contract. Expects a Rust function name to be executed.
100+
/// This will only compile in when using the wasm32 target.
101+
#[macro_export]
102+
macro_rules! ewasm_entry_point {
103+
($name:ident) => {
104+
#[cfg(target_arch = "wasm32")]
105+
#[no_mangle]
106+
pub extern "C" fn main() {
107+
$name()
108+
}
109+
};
110+
}
111+
88112
/// Enum representing an error code for EEI calls. Currently used by `codeCopy`, `callDataCopy`,
89113
/// `externalCodeCopy`, and `returnDataCopy`.
90114
pub enum Error {

0 commit comments

Comments
 (0)