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

[WIP] pass strings between host and wasm #39

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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: 11 additions & 3 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ ENV TZ=Asian/Shanghai

RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install --no-install-recommends -y cmake
&& apt-get install --no-install-recommends -y clang llvm cmake

# Fix "Unable to find libclang" issue
RUN apt-get install --no-install-recommends -y clang-11
#
# wasm-tools
ARG WASM_TOOLS_VER=1.205.0

WORKDIR /opt
RUN wget https://github.com/bytecodealliance/wasm-tools/releases/download/v${WASM_TOOLS_VER}/wasm-tools-${WASM_TOOLS_VER}-x86_64-linux.tar.gz
RUN tar -xvf wasm-tools-${WASM_TOOLS_VER}-x86_64-linux.tar.gz \
&& mv wasm-tools-${WASM_TOOLS_VER}-x86_64-linux wasm-tools \
&& rm wasm-tools-${WASM_TOOLS_VER}-x86_64-linux.tar.gz \
&& ln -s /opt/wasm-tools/wasm-tools /usr/local/bin/wasm-tools
Comment on lines +10 to +19
Copy link

Choose a reason for hiding this comment

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

Simple way

RUN cargo install cargo-binstall && cargo binstall wasm-tools

2 changes: 2 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
// Configure tool-specific properties.
"customizations": {
"settings": {
"editor.formatOnSave": true,
"editor.formatOnType": true,
"lldb.executable": "/usr/bin/lldb",
"terminal.integrated.shell.linux": "/bin/bash"
},
Expand Down
8 changes: 5 additions & 3 deletions .devcontainer/finalize.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ printf "Running 'postCreateCommand' Script\n"
printf "Installing Rust Targets\n"
rustup update stable --no-self-update
rustup default stable
rustup target add wasm32-unknown-unknown
#rustup target add wasm32-unknown-unknown
rustup target add wasm32-wasi
rustup component add clippy
rustup component add rustfmt

cargo install cargo-readme
cargo install rustfilt

# Install Python stuff
printf "Installing Python Dependencies"
printf "Installing Python Dependencies\n"

# Install NPM dependencies
printf "Installing NPM Dependencies"
printf "Installing NPM Dependencies\n"
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
members = ["crates/wamr-sys"]
exclude = [
"examples/wasi-hello",
"resources/test/gcd",
"resources/test/add-extra",
"resources/test/gcd",
"resources/test/string-concat",
".devcontainer",
".github",
]
Expand Down
2 changes: 1 addition & 1 deletion crates/wamr-sys/wasm-micro-runtime
8 changes: 8 additions & 0 deletions resources/test/string-concat/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

[target.wasm32-wasi]
rustflags = [
"-C", "link-arg=--export=malloc",
"-C", "link-arg=--export=free",
]
150 changes: 150 additions & 0 deletions resources/test/string-concat/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions resources/test/string-concat/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

[package]
name = "string_concat"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rustfilt = "0.2.1"
43 changes: 43 additions & 0 deletions resources/test/string-concat/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use std::string::String;
use std::alloc::{alloc, dealloc, Layout};

#[no_mangle]
#[export_name = "my_malloc"]
pub unsafe fn my_alloc(size: usize) -> *mut u8 {
let align = std::mem::align_of::<usize>();
let layout = Layout::from_size_align_unchecked(size, align);
alloc(layout)
}

#[no_mangle]
#[export_name = "my_free"]
pub unsafe fn my_dealloc(ptr: *mut u8, size: usize) {
let align = std::mem::align_of::<usize>();
let layout = Layout::from_size_align_unchecked(size, align);
dealloc(ptr, layout);
}

#[no_mangle]
#[export_name = "my_strcat"]
pub fn my_strcat(s1: &str, s2: &str) -> String {
println!("-=-|> s1: {}, s2: {}", s1, s2);

let mut result = String::with_capacity(s1.len() + s2.len());
let ret_ref = &result;
println!(" --> the address of result: {:p}", ret_ref as *const String);

result.push_str(s1);
result.push_str(s2);
println!(" --> result: {result}");

result
}

fn main() {
let result = my_strcat("hello", "world");
println!("-> {}", result);
let result = my_strcat(&result, "from, xxx,");
println!("-> {}", result);
let result = my_strcat(&result, "main function");
println!("-> {}", result);
}
Loading
Loading