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

Use wasm_import_module and remove function name prefix #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "ewasm_api"
name = "ss_ewasm_api"
version = "0.11.0"
authors = ["Alex Beregszaszi <alex@rtfs.hu>", "Jake Lang <jak3lang@gmail.com>"]
authors = ["Alex Beregszaszi <alex@rtfs.hu>", "Jake Lang <jak3lang@gmail.com>", "Antonio Yang <yanganto@gmail.com>"]
license = "Apache-2.0"
repository = "https://github.com/ewasm/ewasm-rust-api"
repository = "https://github.com/second-state/ewasm-rust-api"
description = "ewasm API for Rust"
edition = "2018"

Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# ewasm-rust-api
# ss-ewasm-rust-api

A fork from ewasm/ewasm-rust-api

### Why we fork on this?

We open a [PR](https://github.com/ewasm/ewasm-rust-api/pull/80) to upstream to use the `wasm_import_module` and remove the function prefix,
and it still pending not merge.
This modification will affect on using WasmEdge in [SewUp](https://github.com/second-state/SewUp), and block the release of SewUp.
So we fork and release it into crate.io.


![Build](https://circleci.com/gh/ewasm/ewasm-rust-api.svg?style=shield&circle-token=:circle-token)
![Version](https://img.shields.io/crates/v/ewasm-api.svg)
Expand Down
9 changes: 5 additions & 4 deletions src/bignum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use crate::types::Uint256;
/// The low-level interface to the system library. Use the wrapper functions unless you know what
/// you're doing.
pub mod native {
#[link(wasm_import_module = "bignum")]
extern "C" {
pub fn bignum_mul256(a: *const u32, b: *const u32, ret: *const u32);
pub fn bignum_umulmod256(a: *const u32, b: *const u32, modulo: *const u32, ret: *const u32);
pub fn mul256(a: *const u32, b: *const u32, ret: *const u32);
pub fn umulmod256(a: *const u32, b: *const u32, modulo: *const u32, ret: *const u32);
}
}

Expand All @@ -15,7 +16,7 @@ pub fn mul256(a: &Uint256, b: &Uint256) -> Uint256 {
let mut ret = Uint256::default();

unsafe {
native::bignum_mul256(
native::mul256(
a.bytes.as_ptr() as *const u32,
b.bytes.as_ptr() as *const u32,
ret.bytes.as_mut_ptr() as *const u32,
Expand All @@ -30,7 +31,7 @@ pub fn umulmod256(a: &Uint256, b: &Uint256, modulo: &Uint256) -> Uint256 {
let mut ret = Uint256::default();

unsafe {
native::bignum_umulmod256(
native::umulmod256(
a.bytes.as_ptr() as *const u32,
b.bytes.as_ptr() as *const u32,
modulo.bytes.as_ptr() as *const u32,
Expand Down
27 changes: 14 additions & 13 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,48 @@ use crate::types::StorageKey;

/// The native interface for debugging functions.
mod native {
#[link(wasm_import_module = "debug")]
extern "C" {
pub fn debug_print32(value: u32);
pub fn debug_print64(value: u64);
pub fn debug_printMem(offset: *const u32, len: u32);
pub fn debug_printMemHex(offset: *const u32, len: u32);
pub fn debug_printStorage(pathOffset: *const u32);
pub fn debug_printStorageHex(pathOffset: *const u32);
pub fn print32(value: u32);
pub fn print64(value: u64);
pub fn printMem(offset: *const u32, len: u32);
pub fn printMemHex(offset: *const u32, len: u32);
pub fn printStorage(pathOffset: *const u32);
pub fn printStorageHex(pathOffset: *const u32);
}
}

/// Prints a string.
pub fn log(msg: &str) {
unsafe { native::debug_printMem(msg.as_ptr() as *const u32, msg.len() as u32) }
unsafe { native::printMem(msg.as_ptr() as *const u32, msg.len() as u32) }
}

/// Prints an unsigned 32-bit int.
pub fn print32(value: u32) {
unsafe { native::debug_print32(value) }
unsafe { native::print32(value) }
}

/// Prints an unsigned 64-bit int.
pub fn print64(value: u64) {
unsafe { native::debug_print64(value) }
unsafe { native::print64(value) }
}

/// Prints the contents of a slice.
pub fn print_mem(slice: &[u8]) {
unsafe { native::debug_printMem(slice.as_ptr() as *const u32, slice.len() as u32) }
unsafe { native::printMem(slice.as_ptr() as *const u32, slice.len() as u32) }
}

/// Prints the contents of a slice in hexadecimal format.
pub fn print_mem_hex(slice: &[u8]) {
unsafe { native::debug_printMemHex(slice.as_ptr() as *const u32, slice.len() as u32) }
unsafe { native::printMemHex(slice.as_ptr() as *const u32, slice.len() as u32) }
}

/// Prints the value of a storage key.
pub fn print_storage(key: &StorageKey) {
unsafe { native::debug_printStorage(key.bytes.as_ptr() as *const u32) }
unsafe { native::printStorage(key.bytes.as_ptr() as *const u32) }
}

/// Prints the value of a storage key in hexadecimal format.
pub fn print_storage_hex(key: &StorageKey) {
unsafe { native::debug_printStorageHex(key.bytes.as_ptr() as *const u32) }
unsafe { native::printStorageHex(key.bytes.as_ptr() as *const u32) }
}
25 changes: 13 additions & 12 deletions src/eth2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,40 @@
//! unimplemented!()
//! }
//!
//! eth2_shard_script!(process_block);
//! shard_script!(process_block);
//! ```

use super::*;

mod native {
#[link(wasm_import_module = "eth2")]
extern "C" {
pub fn eth2_loadPreStateRoot(offset: *const u32);
pub fn eth2_blockDataSize() -> u32;
pub fn eth2_blockDataCopy(outputOfset: *const u32, offset: u32, length: u32);
pub fn eth2_savePostStateRoot(offset: *const u32);
pub fn eth2_pushNewDeposit(offset: *const u32, length: u32);
pub fn loadPreStateRoot(offset: *const u32);
pub fn blockDataSize() -> u32;
pub fn blockDataCopy(outputOfset: *const u32, offset: u32, length: u32);
pub fn savePostStateRoot(offset: *const u32);
pub fn pushNewDeposit(offset: *const u32, length: u32);
}
}

/// Load current state root.
pub fn load_pre_state_root() -> Bytes32 {
let mut ret = Bytes32::default();

unsafe { native::eth2_loadPreStateRoot(ret.bytes.as_mut_ptr() as *const u32) }
unsafe { native::loadPreStateRoot(ret.bytes.as_mut_ptr() as *const u32) }

ret
}

/// Returns the length of the "block data" supplied with the current block.
pub fn block_data_size() -> usize {
unsafe { native::eth2_blockDataSize() as usize }
unsafe { native::blockDataSize() as usize }
}

/// Copies a slices from the "block data", but does not check for overflow.
pub fn unsafe_block_data_copy(from: usize, length: usize, ret: &mut [u8]) {
unsafe {
native::eth2_blockDataCopy(ret.as_mut_ptr() as *const u32, from as u32, length as u32);
native::blockDataCopy(ret.as_mut_ptr() as *const u32, from as u32, length as u32);
}
}

Expand Down Expand Up @@ -70,20 +71,20 @@ pub fn block_data_copy(from: usize, length: usize, ret: &mut [u8]) -> Result<(),

/// Push new deposit receipt.
pub fn push_new_deposit(deposit: &[u8]) {
unsafe { native::eth2_pushNewDeposit(deposit.as_ptr() as *const u32, deposit.len() as u32) }
unsafe { native::pushNewDeposit(deposit.as_ptr() as *const u32, deposit.len() as u32) }
}

/// Save new state root.
pub fn save_post_state_root(state: &Bytes32) {
unsafe { native::eth2_savePostStateRoot(state.bytes.as_ptr() as *const u32) }
unsafe { native::savePostStateRoot(state.bytes.as_ptr() as *const u32) }
}

/// Create shard script entry point. Expects a function to process blocks with the signature:
/// ```ignore
/// fn process_block(pre_state_root: &Bytes32, block_data: &[u8]) -> (Bytes32, Vec<u8>) {}
/// ```
#[macro_export]
macro_rules! eth2_shard_script {
macro_rules! shard_script {
($process_block:ident) => {
#[cfg(target_arch = "wasm32")]
#[no_mangle]
Expand Down
Loading