Skip to content

Commit

Permalink
Merge pull request #70 from ewasm/entry-macro
Browse files Browse the repository at this point in the history
Macro for declaring entry point
  • Loading branch information
axic authored Jun 4, 2019
2 parents a72f2b4 + 2c051ce commit 61d2ff9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
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};
//!
//! #[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

0 comments on commit 61d2ff9

Please sign in to comment.