Skip to content

Commit 6684e45

Browse files
committed
wasm append output
1 parent 3f0a6fa commit 6684e45

File tree

5 files changed

+68
-20
lines changed

5 files changed

+68
-20
lines changed

.github/workflows/build.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ jobs:
5050
- name: Run Tests (univ)
5151
run: cargo test --features univ
5252
- name: Run Tests (wasm)
53-
run: wasm-pack test --node ./lib --features wasm
53+
run: |
54+
cargo install wasm-pack --force
55+
wasm-pack test --node ./lib --features wasm
5456
- name: Clippy Check
5557
run: cargo clippy --all-targets
5658
- name: Format Check

cmd/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "cmd"
33
version = "0.1.0"
4-
description = "basjoofan command"
4+
description = "continuous test"
55
repository = "https://github.com/basjoofan/lib"
66
license = "MIT OR Apache-2.0"
77
authors = ["Lamb <lamb@basjoofan.com>"]

cmd/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use lib::Source;
44
use std::{path::PathBuf, time::Duration};
55

66
#[derive(Parser)]
7-
#[command(version, about, long_about = None)]
7+
#[command(name = env!("CARGO_BIN_NAME"), version, about, long_about = None)]
88
struct Interface {
99
#[command(subcommand)]
1010
command: Option<Commands>,

lib/src/http/wasm/client.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ async fn fetch(request: &Request, content: Option<JsValue>, timeout: u32) -> Res
9090
#[cfg(test)]
9191
mod tests {
9292
use crate::http::Client;
93+
use js_sys::eval;
9394
use js_sys::BigInt;
9495
use wasm_bindgen::JsValue;
9596
use wasm_bindgen_test::*;
@@ -98,19 +99,19 @@ mod tests {
9899
// wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
99100

100101
fn setup() {
101-
let _ = js_sys::eval(
102+
let _ = eval(
102103
r#"
103-
globalThis.readFileContent = async function(filePath) {
104+
global.readFileContent = async function(filePath) {
104105
console.log(`Mock reading file in test: ${filePath}`);
105-
return new TextEncoder().encode(`Test content for ${filePath}`);
106+
return new TextEncoder().encode(`Mock content for ${filePath}`);
106107
};
107108
"#,
108109
);
109-
let _ = js_sys::eval(
110+
let _ = eval(
110111
r#"
111112
window.readFileContent = async function(filePath) {
112113
console.log(`Mock reading file in test: ${filePath}`);
113-
return new TextEncoder().encode(`Test content for ${filePath}`);
114+
return new TextEncoder().encode(`Mock content for ${filePath}`);
114115
};
115116
"#,
116117
);

lib/src/native.rs

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,67 @@
11
use crate::Context;
22
use crate::Value;
33

4-
pub fn println(values: Vec<Value>, context: &Context) -> Result<Value, String> {
5-
match format(values, context) {
6-
error @ Err(_) => error,
7-
Ok(value) => {
8-
println!("{value}");
9-
Ok(Value::Null)
4+
#[cfg(feature = "univ")]
5+
pub use univ::*;
6+
#[cfg(feature = "univ")]
7+
mod univ {
8+
use super::format;
9+
use crate::Context;
10+
use crate::Value;
11+
12+
pub fn println(values: Vec<Value>, context: &Context) -> Result<Value, String> {
13+
match format(values, context) {
14+
error @ Err(_) => error,
15+
Ok(value) => {
16+
println!("{value}");
17+
Ok(Value::Null)
18+
}
19+
}
20+
}
21+
22+
pub fn print(values: Vec<Value>, context: &Context) -> Result<Value, String> {
23+
match format(values, context) {
24+
error @ Err(_) => error,
25+
Ok(value) => {
26+
print!("{value}");
27+
Ok(Value::Null)
28+
}
1029
}
1130
}
1231
}
1332

14-
pub fn print(values: Vec<Value>, context: &Context) -> Result<Value, String> {
15-
match format(values, context) {
16-
error @ Err(_) => error,
17-
Ok(value) => {
18-
print!("{value}");
19-
Ok(Value::Null)
33+
#[cfg(feature = "wasm")]
34+
pub use wasm::*;
35+
#[cfg(feature = "wasm")]
36+
mod wasm {
37+
use super::format;
38+
use crate::Context;
39+
use crate::Value;
40+
use wasm_bindgen::prelude::wasm_bindgen;
41+
42+
#[wasm_bindgen]
43+
extern "C" {
44+
#[wasm_bindgen(js_name = appendOutput)]
45+
fn append_output(output: &str);
46+
}
47+
48+
pub fn println(values: Vec<Value>, context: &Context) -> Result<Value, String> {
49+
match format(values, context) {
50+
error @ Err(_) => error,
51+
Ok(value) => {
52+
append_output(format!("{value}\r\n").as_str());
53+
Ok(Value::Null)
54+
}
55+
}
56+
}
57+
58+
pub fn print(values: Vec<Value>, context: &Context) -> Result<Value, String> {
59+
match format(values, context) {
60+
error @ Err(_) => error,
61+
Ok(value) => {
62+
append_output(format!("{value}").as_str());
63+
Ok(Value::Null)
64+
}
2065
}
2166
}
2267
}

0 commit comments

Comments
 (0)