Skip to content

Commit

Permalink
feat: start integrating mesc fns
Browse files Browse the repository at this point in the history
  • Loading branch information
o-az committed Feb 25, 2024
1 parent a35df7e commit 858ec0c
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 576 deletions.
1 change: 1 addition & 0 deletions wasm/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ dist-ssr
*.njsproj
*.sln
*.sw?
.vercel
Binary file modified wasm/bun.lockb
Binary file not shown.
12 changes: 9 additions & 3 deletions wasm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="https://cdn.jsdelivr.net/npm/@unocss/runtime"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@unocss/reset/normalize.min.css">
<title>mesc wasm</title>
<style>
</style>
<script type="module" src="./src/index.ts"></script>
</head>

<body>
<h1>mesc </h1>
<p>Can you see an alert window?</p>
<body class="font-mono">
<h1>mesc</h1>
<p>is mesc enabled?
<span id="mesc-enabled" class="text-red-500 font-bold"></span>
</p>
</body>

</html>
87 changes: 87 additions & 0 deletions wasm/mesc-wasm/Cargo.lock

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

5 changes: 4 additions & 1 deletion wasm/mesc-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
wasm-bindgen = "0.2.91"
mesc = { path = "../../rust/crates/mesc" }
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
Expand All @@ -21,6 +24,7 @@ console_error_panic_hook = { version = "0.1.7", optional = true }
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.5", optional = true }
serde-wasm-bindgen = "0.6.4"

[dev-dependencies]
wasm-bindgen-test = "0.3.41"
Expand All @@ -30,5 +34,4 @@ wasm-bindgen-test = "0.3.41"
opt-level = "s"

[features]
# default = ["console_error_panic_hook" ,"wee_alloc"]
default = ["console_error_panic_hook"]
17 changes: 14 additions & 3 deletions wasm/mesc-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod utils;

pub use mesc::*;
use serde_wasm_bindgen::to_value;
use wasm_bindgen::prelude::*;

#[cfg(feature = "wee_alloc")]
Expand All @@ -8,10 +10,19 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}

#[wasm_bindgen]
pub fn greet(name: &str) {
alert(&format!("Hello,{}!", name));
pub fn is_mesc_enabled_wrapper() -> bool {
mesc::is_mesc_enabled()
}

#[wasm_bindgen]
pub fn get_default_endpoint_wrapper(option: Option<String>) -> Result<JsValue, JsValue> {
let option = option.as_deref();
let result =
mesc::get_default_endpoint(option).map_err(|e| JsValue::from_str(&e.to_string()))?;
to_value(&result).map_err(|e| JsValue::from_str(&e.to_string()))
}
Loading

0 comments on commit 858ec0c

Please sign in to comment.