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

feat: wasm/rs support println! & print! macros #1297

Merged
merged 2 commits into from
Oct 3, 2022
Merged
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
25 changes: 25 additions & 0 deletions packages/js/client/src/__tests__/e2e/wasm-rs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,31 @@ describe("wasm-rs test cases", () => {
wrapperUri
);
});

it("override rust print macros", async () => {
const wrapperPath = `${GetPathToTestWrappers()}/wasm-rs/println-logging`;
const wrapperUri = `fs/${wrapperPath}/build`;
await buildWrapper(wrapperPath);

console.debug = jest.fn();
const message = "foo bar baz";

const client = await getClient();
const result = await client.invoke<boolean>({
uri: wrapperUri,
method: "logMessage",
args: {
message,
}
});

expect(result.ok).toBeTruthy();
if (!result.ok) return;
expect(result.value).toBeTruthy();
expect((console.debug as any).mock.calls[0][0]).toBe("__wrap_debug_log: " + message);
expect((console.debug as any).mock.calls[1][0]).toBe("__wrap_debug_log: " + message);
jest.clearAllMocks();
});
});

describe.skip("Wasm-rs benchmarking", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@ pub use {{#detectKeyword}}{{#toLower}}{{type}}{{/toLower}}{{/detectKeyword}}::{
Args{{#detectKeyword}}{{#toUpper}}{{name}}{{/toUpper}}{{/detectKeyword}}{{^last}},{{/last}}
{{/methods}}
};
{{/moduleType}}
{{/moduleType}}

// Override print!(...) & println!(...) macros
#[macro_export]
macro_rules! println { ($($args:tt)*) => { polywrap_wasm_rs::wrap_debug_log(format!($($args)*).as_str()); } }
#[macro_export]
macro_rules! print { ($($args:tt)*) => { polywrap_wasm_rs::wrap_debug_log(format!($($args)*).as_str()); } }
6 changes: 6 additions & 0 deletions packages/test-cases/cases/bind/sanity/output/wasm-rs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ pub use module::{
if_wrapped,
ArgsIf
};

// Override print!(...) & println!(...) macros
#[macro_export]
macro_rules! println { ($($args:tt)*) => { polywrap_wasm_rs::wrap_debug_log(format!($($args)*).as_str()); } }
#[macro_export]
macro_rules! print { ($($args:tt)*) => { polywrap_wasm_rs::wrap_debug_log(format!($($args)*).as_str()); } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "query"
version = "0.1.0"
description = "Query module of bigint-type e2e test"
authors = [
"Kobby Pentangeli <kobbypentangeli@gmail.com>",
"Jordan Ellis <jelli@dorg.tech>"
]
repository = "https://github.com/polywrap/monorepo"
license = "MIT"
edition = "2021"

[dependencies]
polywrap-wasm-rs = { path = "../../../../../wasm/rs" }
serde = { version = "1.0", features = ["derive"] }

[lib]
crate-type = ["cdylib"]

[profile.release]
opt-level = 's'
lto = true
panic = 'abort'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
format: 0.1.0
docker:
name: println-logging-wasm-rs
linked_packages:
- name: polywrap-wasm-rs
path: ../../../../../wasm/rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
format: 0.2.0
project:
name: println-logging
type: wasm/rust
source:
schema: ./schema.graphql
module: ./Cargo.toml
extensions:
build: ./polywrap.build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Module {
logMessage(
message: String!
): Boolean!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub mod wrap;
pub use wrap::*;

pub fn log_message(args: ArgsLogMessage) -> bool {
let message = args.message.as_str();

println!("{}", message);
print!("{}", message);

true
}