Skip to content

Commit

Permalink
Merge pull request #1297 from polywrap/issue-900-wasm-rs-print-macros
Browse files Browse the repository at this point in the history
feat: wasm/rs support println! & print! macros
  • Loading branch information
dOrgJelli authored Oct 3, 2022
2 parents 826b192 + b03a1d7 commit d341657
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 1 deletion.
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
}

0 comments on commit d341657

Please sign in to comment.