Skip to content

Commit

Permalink
update data path && wasm ctx can now be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Feb 12, 2024
1 parent 8af1619 commit 81093af
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 63 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ serde-wasm-bindgen = "0.6.3"
wee_alloc = "0.4.5"
wasm-bindgen-test = "0.3.41"
web-sys = "0.3.68"
js-sys = "0.3.68"

# workspace specific libs
adana-script-core = { version = "0.17.3", path = "./adana-script-core" }
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ My favorite dish 😋
1. Docker
- From the docker hub:
- `docker run -it nbittich/adana # latest from master`
- `docker run -it nbittich/adana:0.17.3 # latest release`
- `docker run -it nbittich/adana:0.17.4 # latest release`
- Manually:
- clone the repo
- build the docker image: `docker build -t adana .`
Expand All @@ -71,6 +71,8 @@ My favorite dish 😋
- Manually:
- `cargo build --release`
- `./target/release/adana`
3. WASM Playground
- Try it out at https://nbittich.github.io/adana

<hr>

Expand Down Expand Up @@ -333,7 +335,7 @@ The rust version is specified when running the repl.

To load a library dynamically, you can either specify a relative path, or an
absolute path. In case of a relative path, it should be relative to the
shared lib path (by default: `$HOME/.local/share/.libs_adana`).
shared lib path (by default: `$HOME/.local/share/adana/db`).

You can override this by providing a path when starting the repl (e.g: `adana -slp /tmp`).

Expand Down Expand Up @@ -379,12 +381,12 @@ If it is not installed yet, you will see instructions on how to install it, e.g:

```
[rust~/toyprograms/adana(master)] fs = require("@std/fs")
std lib doesn't exist: "/home/nbittich/.local/share/.libs_adana/adana-std/fs.so".
std lib doesn't exist: "/home/nbittich/.local/share/adana/lib/adana-std/fs.so".
Try to install it like so:
- wget -P /tmp https://github.com/nbittich/adana-std/releases/download/0.17.0/adana-std.tar.gz
- mkdir /home/nbittich/.local/share/.libs_adana/adana-std && tar xvzf /tmp/adana-std.tar.gz \
-C /home/nbittich/.local/share/.libs_adana/adana-std
- mkdir /home/nbittich/.local/share/adana/lib/adana-std && tar xvzf /tmp/adana-std.tar.gz \
-C /home/nbittich/.local/share/adana/lib/adana-std
```

### Loops
Expand Down
4 changes: 3 additions & 1 deletion adana-db/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ use anyhow::Context;
use log::debug;
use serde::de::DeserializeOwned;

const ADANA_DB_DIR: &str = "adana/db";

use super::{
file_lock, FileDb, FileLock, FileLockError, InMemoryDb, Key, Value,
};

fn get_default_db_path() -> Option<Box<Path>> {
let mut db_dir = dirs::data_dir().or_else(dirs::home_dir)?;
db_dir.push(ADANA_DB_DIR);
debug!("db dir: {}", db_dir.as_path().to_string_lossy());
db_dir.push(".adanadb");
if !db_dir.exists() {
std::fs::create_dir_all(&db_dir).ok()?;
}
Expand Down
27 changes: 15 additions & 12 deletions adana-playground/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { EXAMPLES } from "./examples.js";
async function loadWasmContext() {
const module = await import("./pkg/adana_script_wasm.js");
await module.default();
return module.compute_as_string;
// return module.compute_as_string; use that one instead to create the heap from javascript
return module.make_ctx_and_compute_as_string;
}

function toggleForm(form, toggle) {
Expand All @@ -20,13 +21,13 @@ async function run() {

let compute = await loadWasmContext();

const memory = new WebAssembly.Memory({
initial: 32, // 2mb
maximum: 512, // 32mb
shared: true,
});

const ctx = new Uint8Array(memory.buffer);
// uncomment this to create the heap from javascript
// const memory = new WebAssembly.Memory({
// initial: 32, // 2mb
// maximum: 512, // 32mb
// shared: true,
// });
// const ctx = new Uint8Array(memory.buffer);

form.classList.remove("d-none");

Expand Down Expand Up @@ -68,11 +69,13 @@ async function run() {
out.classList.remove("text-danger");

const data = new FormData(e.target);
for (let i = 0; i < ctx.length; i++) {
ctx[i] = undefined;
}
// uncomment this if you create memory from javascript
// for (let i = 0; i < ctx.length; i++) {
// ctx[i] = undefined;
// }
try {
let res = compute(data.get("code") || "", ctx);
// let res = compute(data.get("code") || "", ctx); // use this instead if you create memory from javascript
let res = compute(data.get("code") || "");
// console.log(res);
out.value = logs.join("");
toggleForm(form, false);
Expand Down
100 changes: 56 additions & 44 deletions adana-script-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,71 +1,83 @@
#![cfg(target_arch = "wasm32")]

mod utils;
use adana_script_core::primitive::{Primitive, RefPrimitive};
use std::collections::BTreeMap;
use wasm_bindgen::prelude::*;

#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

fn compute(
script: &str,
mem: &mut [u8],
) -> Result<(BTreeMap<String, RefPrimitive>, Primitive), JsError> {
utils::set_panic_hook();
let mut ctx: BTreeMap<String, RefPrimitive> = if !mem.is_empty() {
bincode::deserialize(mem)?
} else {
BTreeMap::new()
};
mod internal {
use crate::utils;
use adana_script_core::primitive::{Primitive, RefPrimitive};
use std::collections::BTreeMap;
use wasm_bindgen::prelude::{JsError, JsValue};
fn compute(
script: &str,
mem: &mut [u8],
) -> Result<(BTreeMap<String, RefPrimitive>, Primitive), JsError> {
utils::set_panic_hook();
let mut ctx: BTreeMap<String, RefPrimitive> = if !mem.is_empty() {
bincode::deserialize(mem)?
} else {
BTreeMap::new()
};

let result = adana_script::compute(script, &mut ctx, "N/A")
.map_err(|e| e.to_string())
.map_err(|e| JsError::new(&e))?;

let result = adana_script::compute(script, &mut ctx, "N/A")
.map_err(|e| e.to_string())
.map_err(|e| JsError::new(&e))?;
Ok((ctx, result))
}
pub(super) fn compute_as_js_value(
script: &str,
mem: &mut [u8],
) -> Result<(BTreeMap<String, RefPrimitive>, JsValue), JsError> {
let (ctx, result) = compute(script, mem)?;
let value = serde_wasm_bindgen::to_value(&result)?;

Ok((ctx, result))
Ok((ctx, value))
}
pub(super) fn compute_as_string(
script: &str,
mem: &mut [u8],
) -> Result<(BTreeMap<String, RefPrimitive>, String), JsError> {
let (ctx, result) = compute(script, mem)?;
Ok((ctx, result.to_string()))
}
}

#[wasm_bindgen]
pub fn compute_as_js_value(
script: &str,
mem: &mut [u8],
) -> Result<JsValue, JsError> {
let (ctx, result) = compute(script, mem)?;
let (ctx, res) = internal::compute_as_js_value(script, mem)?;
bincode::serialize_into(mem, &ctx)?;

Ok(serde_wasm_bindgen::to_value(&result)?)
Ok(res)
}

#[wasm_bindgen]
pub fn compute_as_string(
script: &str,
mem: &mut [u8],
) -> Result<String, JsError> {
let (ctx, result) = compute(script, mem)?;

let result = {
if let Some(out) = ctx.get("") {
let mut out_rl = out.write()?;

match &mut *out_rl {
Primitive::Array(ref mut a) if !a.is_empty() => {
let mut s = String::new();
for p in a.drain(0..) {
s.push_str(&p.to_string());
}
if !matches!(result, Primitive::Unit) {
s.push_str(&result.to_string());
}

Ok(s)
}
_ => Ok(result.to_string()),
}
} else {
Ok(result.to_string())
}
};
let (ctx, res) = internal::compute_as_string(script, mem)?;
bincode::serialize_into(mem, &ctx)?;
Ok(res)
}

result
#[wasm_bindgen]
pub fn make_ctx_and_compute_as_string(
script: &str,
heap_size_in_mb: Option<usize>,
) -> Result<String, JsError> {
let heap_size =
if let Some(heap_size) = heap_size_in_mb.filter(|h| h <= &32) {
heap_size
} else {
1
};
let mut mem = Vec::with_capacity(heap_size * 1024 * 1024); // 1mb by default
let (_, res) = internal::compute_as_string(script, &mut mem)?;
Ok(res)
}
2 changes: 1 addition & 1 deletion adana-shell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
const RUST_VERSION: &str = std::env!("CARGO_PKG_RUST_VERSION");
const PKG_NAME: &str = env!("CARGO_PKG_NAME");

const SHARED_LIB_DIR: &str = ".libs_adana";
const SHARED_LIB_DIR: &str = "adana/lib";

fn get_path_to_shared_libraries() -> Option<PathBuf> {
dirs::data_dir().or_else(dirs::home_dir).map(|mut pb| {
Expand Down

0 comments on commit 81093af

Please sign in to comment.