-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #302 from dusk-network/apple-archs
Add support for most common developer environments
- Loading branch information
Showing
10 changed files
with
176 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
[workspace] | ||
members = [ | ||
"box", | ||
"c-example", | ||
"callcenter", | ||
"constructor", | ||
"counter", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "c_example" | ||
version = "0.1.0" | ||
authors = [ | ||
"Eduardo Leegwater Simões <[email protected]>", | ||
] | ||
edition = "2021" | ||
|
||
license = "MPL-2.0" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
// | ||
// Copyright (c) DUSK NETWORK. All rights reserved. | ||
|
||
use std::ptr; | ||
|
||
// Define the argument buffer used to communicate between contract and host | ||
#[no_mangle] | ||
pub static mut A: [u8; 65536] = [0; 65536]; | ||
|
||
// ==== Host functions ==== | ||
// | ||
// These functions are provided by the host. See `piecrust-uplink` for a full | ||
// list. Here we only declare the ones we will need. | ||
mod ext { | ||
extern "C" { | ||
pub fn c( | ||
contract_id: *const u8, | ||
fn_name: *const u8, | ||
fn_name_len: u32, | ||
fn_arg_len: u32, | ||
points_limit: u64, | ||
) -> i32; | ||
pub fn hd(name: *const u8, name_len: u32) -> u32; | ||
} | ||
} | ||
|
||
// ==== Helper functions ==== | ||
// | ||
// These will help us write the exported functions underneath | ||
|
||
// Reads a contract ID from the argument buffer | ||
unsafe fn read_contract_id(id: *mut u8) { | ||
ptr::copy(&A[0], id, 32); | ||
} | ||
|
||
// Calls the counter contract to increment the counter | ||
unsafe fn increment_counter(contract_id: *const u8) { | ||
let fn_name = b"increment"; | ||
ext::c(contract_id, fn_name.as_ptr(), fn_name.len() as u32, 0, 0); | ||
} | ||
|
||
// Reads a 64-bit from the argument buffer | ||
unsafe fn read_integer(i: *mut i64) { | ||
ptr::copy(&A[0], i.cast(), 8); | ||
} | ||
|
||
// Writes a 64-bit integer to the argument buffer | ||
unsafe fn write_integer(i: i64) { | ||
let i: *const i64 = &i; | ||
let i: *const u8 = i as _; | ||
ptr::copy(&*i, &mut A[0], 8); | ||
} | ||
|
||
// Calls the counter contract to read the counter | ||
unsafe fn read_counter(contract_id: *const u8) -> i64 { | ||
let fn_name = b"read_value"; | ||
ext::c(contract_id, fn_name.as_ptr(), fn_name.len() as u32, 0, 0); | ||
|
||
let mut i = 0; | ||
read_integer(&mut i); | ||
i | ||
} | ||
|
||
// ==== Exported functions ==== | ||
|
||
// Increments and reads the counter contract. The function expects the counter | ||
// contract ID to be written to the argument buffer before being called. | ||
#[no_mangle] | ||
unsafe fn increment_and_read(_: i32) -> i32 { | ||
let mut counter_id = [0u8; 32]; | ||
|
||
read_contract_id(&mut counter_id[0]); | ||
increment_counter(&counter_id[0]); | ||
|
||
write_integer(read_counter(&counter_id[0])); | ||
|
||
8 | ||
} | ||
|
||
// Calls the "hd" extern with an (almost) certainly out of bounds pointer, in an | ||
// effort to trigger an error. | ||
#[no_mangle] | ||
unsafe fn out_of_bounds(_: i32) -> i32 { | ||
ext::hd(4398046511103 as *const u8, 2); | ||
0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/bin/sh | ||
|
||
# The script should check whether the inputs are set, or print usage | ||
# information otherwise. | ||
if [ -z "$1" ] || [ -z "$2" ]; then | ||
echo "Usage: $0 <input-wasm> <output-wasm>" | ||
exit 1 | ||
fi | ||
|
||
TRIPLE=$(rustc -vV | sed -n 's|host: ||p') | ||
ARCH=$(echo "$TRIPLE" | cut -f 1 -d'-') | ||
|
||
# This is a bit of a mess because target triples are pretty inconsistent | ||
OS=$(echo "$TRIPLE" | cut -f 2 -d'-') | ||
# If OS is not currently linux, or apple, then we get it again | ||
if [ "$OS" != "linux" ] && [ "$OS" != "apple" ]; then | ||
OS=$(echo "$TRIPLE" | cut -f 3 -d'-') | ||
fi | ||
if [ "$OS" != "linux" ] && [ "$OS" != "apple" ]; then | ||
echo "OS not supported: $OS" | ||
exit 1 | ||
fi | ||
# If OS is apple, change it to macos | ||
if [ "$OS" = "apple" ]; then | ||
OS="macos" | ||
fi | ||
|
||
RELEASES_URL=https://github.com/bytecodealliance/wasm-tools/releases/download | ||
|
||
PROGRAM_VERSION=1.0.54 | ||
|
||
ARTIFACT_NAME=wasm-tools-$PROGRAM_VERSION-$ARCH-$OS.tar.gz | ||
ARTIFACT_URL=$RELEASES_URL/wasm-tools-$PROGRAM_VERSION/$ARTIFACT_NAME | ||
|
||
ARTIFACT_DIR=$PWD/target/wasm-tools/$PROGRAM_VERSION | ||
ARTIFACT_PATH=$ARTIFACT_DIR/$ARTIFACT_NAME | ||
|
||
# If the artifact doesn't already exist in the target directory, download it, | ||
# otherwise skip. | ||
if [ ! -f "$ARTIFACT_PATH" ]; then | ||
echo "Downloading wasm-tools version $PROGRAM_VERSION" | ||
mkdir -p "$ARTIFACT_DIR" | ||
curl -L "$ARTIFACT_URL" -o "$ARTIFACT_PATH" | ||
fi | ||
|
||
# Extract the tarball, if they aren't already extracted | ||
EXTRACTED_DIR=$ARTIFACT_DIR/extracted | ||
|
||
if [ ! -d "$EXTRACTED_DIR" ]; then | ||
mkdir -p "$EXTRACTED_DIR" | ||
tar -xzf "$ARTIFACT_PATH" -C "$EXTRACTED_DIR" --strip-components=1 | ||
fi | ||
|
||
"$EXTRACTED_DIR"/wasm-tools strip -a "$1" -o "$2" |