diff --git a/Cargo.lock b/Cargo.lock index 731dc2c..e6c7e12 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -143,7 +143,7 @@ checksum = "c6fa2087f2753a7da8cc1c0dbfcf89579dd57458e36769de5ac750b4671737ca" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn", ] [[package]] @@ -233,7 +233,7 @@ dependencies = [ "heck 0.4.1", "proc-macro2", "quote", - "syn 2.0.77", + "syn", ] [[package]] @@ -820,21 +820,18 @@ dependencies = [ "http", ] -[[package]] -name = "heck" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] - [[package]] name = "heck" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.3.9" @@ -1010,10 +1007,11 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "js-sys" -version = "0.3.70" +version = "0.3.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" +checksum = "6717b6b5b077764fb5966237269cb3c64edddde4b14ce42647430a78ced9e7b7" dependencies = [ + "once_cell", "wasm-bindgen", ] @@ -1168,6 +1166,7 @@ dependencies = [ "uuid", "wasm-bindgen", "wasm-bindgen-cli", + "wasm-bindgen-futures", "wasm-bindgen-test", "web-sys", ] @@ -1318,7 +1317,7 @@ checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn", ] [[package]] @@ -1368,7 +1367,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn", ] [[package]] @@ -1403,9 +1402,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" -version = "1.0.84" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec96c6a92621310b51366f1e28d05ef11489516e93be030060e5fc12024a49d6" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] @@ -1418,9 +1417,9 @@ checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" [[package]] name = "quote" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] @@ -1711,7 +1710,7 @@ checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn", ] [[package]] @@ -1857,17 +1856,6 @@ version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" -[[package]] -name = "syn" -version = "1.0.109" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - [[package]] name = "syn" version = "2.0.77" @@ -1940,7 +1928,7 @@ checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn", ] [[package]] @@ -2157,7 +2145,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn", ] [[package]] @@ -2269,12 +2257,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-segmentation" -version = "1.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4c87d22b6e3f4a18d4d40ef354e97c90fcb14dd91d7dc0aa9d8a1172ebf7202" - [[package]] name = "untrusted" version = "0.9.0" @@ -2348,9 +2330,9 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "walrus" -version = "0.21.2" +version = "0.23.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "160c3708e3ad718ab4d84bec8de8c3d3450cd2902bd6c3ee3bbf50ad7529c2ad" +checksum = "6481311b98508f4bc2d0abbfa5d42172e7a54b4b24d8f15e28b0dc650be0c59f" dependencies = [ "anyhow", "gimli 0.26.2", @@ -2360,19 +2342,19 @@ dependencies = [ "rayon", "walrus-macro", "wasm-encoder", - "wasmparser", + "wasmparser 0.214.0", ] [[package]] name = "walrus-macro" -version = "0.19.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a6e5bd22c71e77d60140b0bd5be56155a37e5bd14e24f5f87298040d0cc40d7" +checksum = "439ad39ff894c43c9649fa724cdde9a6fc50b855d517ef071a93e5df82fe51d3" dependencies = [ - "heck 0.3.3", + "heck 0.5.0", "proc-macro2", "quote", - "syn 1.0.109", + "syn", ] [[package]] @@ -2392,9 +2374,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.93" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" +checksum = "a474f6281d1d70c17ae7aa6a613c87fce69a127e2624002df63dcb39d6cf6396" dependencies = [ "cfg-if", "once_cell", @@ -2403,24 +2385,23 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.93" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" +checksum = "5f89bb38646b4f81674e8f5c3fb81b562be1fd936d84320f3264486418519c79" dependencies = [ "bumpalo", "log", - "once_cell", "proc-macro2", "quote", - "syn 2.0.77", + "syn", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-cli" -version = "0.2.93" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb05f9e183365f5cf3ac04d60b54b0e33f494e93730267b5af82633443ee8035" +checksum = "f97e9a1cec561aca323967702b8c50680a0d17a197b93fda433ba7a76cf57a0c" dependencies = [ "anyhow", "docopt", @@ -2438,14 +2419,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-cli-support" -version = "0.2.93" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a7f49ca6e7da74d53d6d716b868828a47d1c3808a8f676e2104fadd3b9874bb" +checksum = "533e5315f66ba7a1c0b9731b1f6606a6fb7fc6d800eff4d5e43d48567f137203" dependencies = [ "anyhow", "base64 0.22.1", "log", "rustc-demangle", + "serde", "serde_json", "tempfile", "unicode-ident", @@ -2460,9 +2442,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-externref-xform" -version = "0.2.93" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c1f37f2705f4177cc87e5b2763115d078d39e4843e351438b34b085d53a8930" +checksum = "e71a35edea8d70380465025fcd6fb5376c9f2b5dacb2bc515bce5575e0ff0a2c" dependencies = [ "anyhow", "walrus", @@ -2471,21 +2453,22 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.42" +version = "0.4.49" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" +checksum = "38176d9b44ea84e9184eff0bc34cc167ed044f816accfe5922e54d84cf48eca2" dependencies = [ "cfg-if", "js-sys", + "once_cell", "wasm-bindgen", "web-sys", ] [[package]] name = "wasm-bindgen-macro" -version = "0.2.93" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" +checksum = "2cc6181fd9a7492eef6fef1f33961e3695e4579b9872a6f7c83aee556666d4fe" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -2493,22 +2476,22 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.93" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" +checksum = "30d7a95b763d3c45903ed6c81f156801839e5ee968bb07e534c44df0fcd330c2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-multi-value-xform" -version = "0.2.93" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2864e3f221fef3869992b541b238e55f53f99b43c4ce220422a6e1ec9458dc21" +checksum = "58d05670395e140f246c9fd6f59ca75da67030121e1679031089aa71309d0ee5" dependencies = [ "anyhow", "walrus", @@ -2517,9 +2500,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.93" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" +checksum = "943aab3fdaaa029a6e0271b35ea10b72b943135afe9bffca82384098ad0e06a6" [[package]] name = "wasm-bindgen-test" @@ -2543,14 +2526,14 @@ checksum = "b7f89739351a2e03cb94beb799d47fb2cac01759b40ec441f7de39b00cbf7ef0" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn", ] [[package]] name = "wasm-bindgen-threads-xform" -version = "0.2.93" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e9ca60ee029d64cf6f63a630050935360c3877844f6de38e8287afb8f1d2715" +checksum = "578e66a90d904cc4ff44f48e8ac3f606a42ecfda5cbe336e89b315127fe02952" dependencies = [ "anyhow", "walrus", @@ -2559,22 +2542,22 @@ dependencies = [ [[package]] name = "wasm-bindgen-wasm-conventions" -version = "0.2.93" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e429e00149de60ffc768e6e1f0563778a237e7c11cc01801edf9734bff8161f" +checksum = "3523d0794bca5434b5e05d8cd7498ff7c6f2c86a4e5d790e35a3bd8e29c1351b" dependencies = [ "anyhow", "leb128", "log", "walrus", - "wasmparser", + "wasmparser 0.212.0", ] [[package]] name = "wasm-bindgen-wasm-interpreter" -version = "0.2.93" +version = "0.2.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "771c49db324f195f221796bf3868247dd91cca4a85604dcb3729213e439abe59" +checksum = "0d94f86fcc9c75aac4823f9cbf156ff37e3e8f4ebc1ccff2785e045efd9aeee1" dependencies = [ "anyhow", "log", @@ -2584,9 +2567,9 @@ dependencies = [ [[package]] name = "wasm-encoder" -version = "0.212.0" +version = "0.214.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "501940df4418b8929eb6d52f1aade1fdd15a5b86c92453cb696e3c906bd3fc33" +checksum = "ff694f02a8d7a50b6922b197ae03883fbf18cdb2ae9fbee7b6148456f5f44041" dependencies = [ "leb128", ] @@ -2606,10 +2589,24 @@ dependencies = [ ] [[package]] -name = "web-sys" -version = "0.3.70" +name = "wasmparser" +version = "0.214.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0" +checksum = "5309c1090e3e84dad0d382f42064e9933fdaedb87e468cc239f0eabea73ddcb6" +dependencies = [ + "ahash", + "bitflags 2.5.0", + "hashbrown 0.14.5", + "indexmap 2.5.0", + "semver", + "serde", +] + +[[package]] +name = "web-sys" +version = "0.3.76" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04dd7223427d52553d3702c004d3b2fe07c148165faa56313cb00211e31c12bc" dependencies = [ "js-sys", "wasm-bindgen", @@ -2841,7 +2838,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.77", + "syn", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 7d6b1e3..c82702c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ required-features = [ "os-target" ] we_alloc = [] default = [ "os-target" ] os-target = [ "clap", "home", "nix", "sysinfo", "ciborium/default", "tracing-subscriber", "async", "tracing-subscriber/env-filter", "libloading", "ciborium-io", "tracing-core", "tar", "flate2", "http_req" ] -wasm-target = [ "wasm-bindgen", "console_error_panic_hook", "wasm-bindgen-cli", "web-sys", "web-sys/Worker", "web-sys/Window", "web-sys/WorkerOptions", "web-sys/WorkerType", "serde-wasm-bindgen" ] +wasm-target = [ "wasm-bindgen", "console_error_panic_hook", "wasm-bindgen-cli", "web-sys", "web-sys/Worker", "web-sys/Window", "web-sys/WorkerOptions", "web-sys/WorkerType", "serde-wasm-bindgen", "wasm-bindgen-futures" ] async = ["tokio/net", "tokio", "tokio/rt-multi-thread", "tokio/io-util"] @@ -50,6 +50,7 @@ console_error_panic_hook = { version = "0.1.7", optional = true } wasm-bindgen-cli = { version = "0.2.92", optional = true } web-sys = { version = "0.3.70", optional = true } serde-wasm-bindgen = { version = "0.6.5", optional = true } +wasm-bindgen-futures = { version = "0.4.49", optional = true } # os dependencies clap = { version = "4.4.11", features = ["cargo"], optional = true } diff --git a/README.md b/README.md index ae83cae..d0b12a8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # MiZe -- An implementation of how I think data should be handled. -- Slogan: _Elevate the Unix file philosophy into the age of connectivity._ +With Mize I am building the software platform, that I would want to use. + +_Elevate the Unix file philosophy into the age of connectivity._ ## Why am I doing this? - What annoys me about software these days is, that things are done in so many different ways. @@ -22,16 +23,3 @@ - That's why the slogan is going to be: _Elevate the unix file philosophy into the age of connectivity._ -## How am I going to do this? -Is not yet completely clear, but here's what I know so far: -- Using what I call "Items" instead of files - - With "Items" being a key-value store of strings/bytestreams - - Those "Items" have types, that give meaning to the key-value pairs. -- A server written in Rust that stores those "Items". -- A way to mount "Items" (or parts of them) into a regular filesystem. -- Web Components to show/render the contents of items in a browser (or Electron-style apps). -- Server middleware to connect external APIs (e.g. emails, YouTube, Google accounts) to this ecosystem. -- The server is one binary without any external requirements (that should run on any POSIX system) - - This makes it very simple to deploy - - I've previously experimented with using MongoDB to store items, but that requires a MongoDB instance to always be running that the server can connect to. And the connection can fail ... and lots of other things can go wrong. - - I want a binary that you can simply start, which then provides a working server. diff --git a/flake.lock b/flake.lock index 058edf8..b91a484 100644 --- a/flake.lock +++ b/flake.lock @@ -23,11 +23,11 @@ "rust-analyzer-src": "rust-analyzer-src" }, "locked": { - "lastModified": 1715063087, - "narHash": "sha256-cktPkcCmJ2sR0V/FaWEuCWmKuGPbwoMltih/EfF0mXg=", + "lastModified": 1734935689, + "narHash": "sha256-yl/iko/0pvRN3PF6Z4FjQeb6AuGiavMENEisQWJ78h0=", "owner": "nix-community", "repo": "fenix", - "rev": "f8f16c1f2c83bea4e51e6522d988ec8bfcc8420e", + "rev": "30616281e9bfe0883acb3369f2b89aad6850706f", "type": "github" }, "original": { @@ -99,11 +99,11 @@ "rust-analyzer-src": { "flake": false, "locked": { - "lastModified": 1714936835, - "narHash": "sha256-M+PpgfRMBfHo8Jb2ou/s3maAZbps0XnuHXQU9Hv9vL0=", + "lastModified": 1734874959, + "narHash": "sha256-NlsVD/fI32wsHFua9Xvc7IFHCUpQIOs6D6RS/3AhMT8=", "owner": "rust-lang", "repo": "rust-analyzer", - "rev": "c4618fe14d39992fbbb85c2d6cad028a232c13d2", + "rev": "fa4a40bbe867ed54f5a7c905b591fd7d60ba35eb", "type": "github" }, "original": { diff --git a/lib.nix b/lib.nix index 272b23b..1c44302 100644 --- a/lib.nix +++ b/lib.nix @@ -68,7 +68,7 @@ rec { findModules = path: let - mize_module_no_externals = builtins.getEnv "MIZE_MODULE_NO_Externals"; + mize_module_no_externals = builtins.getEnv "MIZE_MODULE_NO_EXTERNALS"; module_list_string = builtins.readFile "${module_list_drv path}/modules_to_build"; module_list = pkgs.lib.lists.remove "" (pkgs.lib.strings.splitString "\n" module_list_string); getExternals = path: map findModules ((pkgs.callPackage ((import path).externals or (args: [])) {})); @@ -145,6 +145,12 @@ rec { ########## build Rust Module mkMizeRustModule = attrs: craneLib.buildPackage (attrs // { MIZE_BUILD_CONFIG = mizeBuildConfigStr; + mizeInstallPhase = '' + mkdir -p $out/lib/ + cp ./target/$debugOrRelease/libmize_module_${attrs.modName}.so $out/lib/ + ''; + mizeBuildPhase = '' + ''; selector_string = mkSelString (attrs.select or {} // { inherit (attrs) modName; }); @@ -326,6 +332,8 @@ rec { postInstall = '' rm -rf $out/target.tar.zst + + cat $src/src/platform/wasm/init.js >> $out/pkg/mize.js ''; buildInputs = with pkgsNative; [ wasm-bindgen-cli binaryen wasm-pack ]; diff --git a/src/core/instance/mod.rs b/src/core/instance/mod.rs index 25ed8df..6b3b1be 100644 --- a/src/core/instance/mod.rs +++ b/src/core/instance/mod.rs @@ -14,7 +14,7 @@ use interner::shared::{VecStringPool, StringPool}; use crate::error::{MizeError, MizeResult, IntoMizeResult, MizeResultTrait}; use crate::instance::store::Store; -use crate::instance::updater::updater_thread; +use crate::instance::updater::{ updater_thread, updater_thread_async }; use crate::id::{IntoMizeId, MizeId, Namespace}; use crate::instance::subscription::Subscription; use crate::item::{Item, ItemData}; @@ -59,7 +59,7 @@ pub struct Instance { connections: Arc>>, next_con_id: Arc>, subs: Arc>>>, - pub modules: Arc>>>, + pub modules: Arc>>>, pub id_pool: Arc>, pub namespace_pool: Arc>, @@ -74,7 +74,7 @@ pub struct Instance { next_thread_id: Arc>, give_msg_wait: Arc>>>>, create_msg_wait: Arc>>>, - update_thread_busy: Arc>, + #[cfg(feature = "async")] pub runtime: Arc>, } @@ -113,7 +113,6 @@ impl Instance { next_thread_id: Arc::new(Mutex::new(0)), next_con_id: Arc::new(Mutex::new(1)), give_msg_wait, create_msg_wait, - update_thread_busy: Arc::new(Mutex::new(false)), #[cfg(feature = "async")] runtime: Arc::new(Mutex::new(Runtime::new().mize_result_msg("Could not create async runtime")?)), @@ -121,14 +120,29 @@ impl Instance { - let instance_clone = instance.clone(); - let op_rx_clone = op_rx.clone(); - let closure = move || updater_thread(op_rx_clone, &instance_clone); - instance.spawn("updater_thread", closure)?; + #[cfg(feature = "os-target")] + { + let instance_clone = instance.clone(); + let op_rx_clone = op_rx.clone(); + let closure = move || updater_thread(op_rx_clone, &instance_clone); + instance.spawn("updater_thread", closure)?; - let instance_clone_two = instance.clone(); - let closure_two = move || updater_thread(op_rx, &instance_clone_two); - instance.spawn("updater_thread", closure_two)?; + let instance_clone_two = instance.clone(); + let closure_two = move || updater_thread(op_rx, &instance_clone_two); + instance.spawn("updater_thread", closure_two)?; + } + + + // set up async update "threads" when using wasm + #[cfg(feature = "wasm-target")] + { + let instance_clone = instance.clone(); + let op_rx_clone = op_rx.clone(); + wasm_bindgen_futures::spawn_local(updater_thread_async(op_rx_clone, instance_clone)); + + let instance_clone_two = instance.clone(); + wasm_bindgen_futures::spawn_local(updater_thread_async(op_rx, instance_clone_two)); + } // will move the msg stuff into it's own thread // like this it can deadlock.... if a msg waits on an operation to complete @@ -161,7 +175,7 @@ impl Instance { return Ok(instance); } - fn init(&mut self) -> MizeResult<()> { + pub fn init(&mut self) -> MizeResult<()> { // platform specific init code crate::platform::any::instance_init(self)?; @@ -169,7 +183,18 @@ impl Instance { // end of platform specific init code - self.load_module("String")?; + // load the modules, ad specified in the load_modules config + match self.get("0/config/load_modules")?.value_string() { + Ok(modules_to_load) => { + for module in modules_to_load.split(" ") { + self.load_module(module)?; + } + }, + Err(err) => { + // no load_modules option is set + } + } + debug!("INSTANCE INIT DONE"); @@ -318,6 +343,14 @@ impl Instance { crate::platform::any::load_module(self, name, Some(path)) } + pub fn get_module(&mut self, name: &str) -> MizeResult> { + let inner = self.modules.lock()?; + + let module = inner.get(name).ok_or(mize_err!("Couldn't get_module('{name}')"))?.clone_module(); + + Ok(module) + } + pub fn namespace_from_string(&self, ns_str: String) -> MizeResult { let namespace_pool_inner = self.namespace_pool.lock()?; let namespace = Namespace ( namespace_pool_inner.get(ns_str) ); @@ -539,16 +572,6 @@ impl Instance { } } - pub fn wait_for_updaater_thread(&self) -> MizeResult<()> { - thread::sleep_ms(10000000); - return Ok(()); - while !self.op_tx.is_empty() { - thread::sleep_ms(50); - } - self.update_thread_busy.lock()?; - trace!("wait_for_updaater_thread Thread Idle"); - Ok(()) - } pub fn report_error(err: MizeError) { err.log(); diff --git a/src/core/instance/module.rs b/src/core/instance/module.rs index e96e86a..5772126 100644 --- a/src/core/instance/module.rs +++ b/src/core/instance/module.rs @@ -2,19 +2,32 @@ // https://users.rust-lang.org/t/casting-between-trait-object-types/97220/2 +use std::ffi::OsString; + use crate::instance::Instance; use crate::error::MizeResult; -pub trait Module { +pub trait Module: { fn init(&mut self, instance: &Instance) -> MizeResult<()>; fn exit(&mut self, instance: &Instance) -> MizeResult<()>; + + fn clone_module(&self) -> Box; + + + // extra traits, that can be implemented + // return None if not implemented + + fn run_cli(&mut self, instance: &Instance, cmd_line: Vec) -> Option> { + None + } } pub struct EmptyModule { } + impl Module for EmptyModule { fn init(&mut self, instance: &Instance) -> MizeResult<()> { println!("empty module fn init"); @@ -25,5 +38,9 @@ impl Module for EmptyModule { Ok(()) } + + fn clone_module(&self) -> Box { + Box::new(EmptyModule { }) + } } diff --git a/src/core/instance/updater.rs b/src/core/instance/updater.rs index c59b985..77209a0 100644 --- a/src/core/instance/updater.rs +++ b/src/core/instance/updater.rs @@ -19,6 +19,75 @@ pub enum Operation { Msg(MizeMessage), } + + + + +// console_log macro +// that can be copied into other files for debugging purposes +#[cfg(feature = "wasm-target")] +use wasm_bindgen::prelude::*; + +#[cfg(feature = "wasm-target")] +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_namespace = console)] + fn log(s: &str); +} + +#[cfg(feature = "wasm-target")] +macro_rules! console_log { + // Note that this is using the `log` function imported above during + // `bare_bones` + ($($t:tt)*) => (unsafe { log(&format_args!($($t)*).to_string())}) +} + +#[cfg(not(feature = "wasm-target"))] +macro_rules! console_log { + // Note that this is using the `log` function imported above during + // `bare_bones` + ($($t:tt)*) => () +} +//end of console_log macro + + + + + +pub async fn updater_thread_async(operation_rx : Receiver, instance: Instance) -> () { + let mut count = 0; + console_log!("inside an updater thread"); + + loop { + let mut operation = match operation_rx.recv_async().await { + Ok(val) => val, + Err(e) => { + instance.report_err(e.into()); + continue; + } + }; + + let op_str = match operation { + Operation::Set(_, _, _) => "SET", + Operation::Msg(_) => "MSG", + }; + + trace!("OPERATION {} - {}", count, op_str); + console_log!("OPERATION {} - {}", count, op_str); + + let result = handle_operation(&mut operation, &instance); + + trace!("OPERATION {} DONE", count); + count += 1; + + if let Err(err) = result { + error!("OPERATION {} FAILED: {:?}", count, operation); + err.log(); + } + + } +} + pub fn updater_thread(operation_rx : Receiver, instance: &Instance) -> MizeResult<()> { let mut count = 0; @@ -31,8 +100,6 @@ pub fn updater_thread(operation_rx : Receiver, instance: &Instance) - trace!("OPERATION {} - {}", count, op_str); - //let mut busy = instance.update_thread_busy.lock()?; - let result = handle_operation(&mut operation, instance); trace!("OPERATION {} DONE", count); @@ -43,7 +110,6 @@ pub fn updater_thread(operation_rx : Receiver, instance: &Instance) - err.log(); } - //drop(busy); } Ok(()) } diff --git a/src/core/item.rs b/src/core/item.rs index 4508268..7dd46f3 100644 --- a/src/core/item.rs +++ b/src/core/item.rs @@ -30,7 +30,7 @@ pub struct Item<'a> { // without an Instance it is not an item, but only the "data of an item" // and this type for now is just an alias to CborValue -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct ItemData ( pub CborValue ); impl Item<'_> { @@ -162,6 +162,13 @@ impl ItemData { return Ok(value.into_item_data()); } + pub fn value_string(self) -> MizeResult { + match self.cbor() { + CborValue::Text(string) => Ok(string.to_owned()), + _ => Err(mize_err!("this ItemData has no string value")), + } + } + pub fn from_string>(into_string: S) -> ItemData { let string = into_string.into(); let data = CborValue::Text(string); @@ -177,6 +184,15 @@ impl ItemData { Ok(String::from_utf8(result)?) } + pub fn from_json(json_str: String) -> MizeResult { + let mut json_deserializer = serde_json::Deserializer::from_str(json_str.as_str()); + + let value = CborValue::deserialize(&mut json_deserializer) + .mize_result_msg(format!("could not deserialize the json string: {}", json_str))?; + + return Ok(value.into_item_data()); + } + pub fn merge(&mut self, other: ItemData) { item_data_merge(&mut self.0, &other.0); } @@ -226,6 +242,41 @@ impl ItemData { } + +impl Default for ItemData { + fn default() -> Self { + ItemData::new() + } +} + +pub fn data_from_string(data_string: String) -> MizeResult { + + let mut config = ItemData::new(); + + // in case it's just a string + if !data_string.contains("=") { + let cbor = CborValue::Text(data_string); + return Ok(ItemData::from_cbor(cbor)); + } + + for option in data_string.split(":") { + if option == "".to_owned() { + continue; + } + + let path = option.split("=").nth(0) + .ok_or(MizeError::new().msg(format!("Failed to parse Option: option '{}' has an empty path (thing beforee =)", option)))?; + let value = option.split("=").nth(1) + .ok_or(MizeError::new().msg(format!("Failed to parse Option: option '{}' has an empty value (thing after =)", option)))?; + let mut path_vec = vec!["config"]; + path_vec.extend(path.split(".")); + + config.set_path(path_vec, ItemData::parse(value))?; + } + + return Ok(config); +} + pub fn item_data_get_path(data: &CborValue, path: Vec) -> MizeResult<&CborValue> { let mut path_iter = path.clone().into_iter(); //println!("data: {:?}", data); diff --git a/src/platform/os/cli.rs b/src/platform/os/cli.rs index f0eb4b6..a571f77 100644 --- a/src/platform/os/cli.rs +++ b/src/platform/os/cli.rs @@ -1,8 +1,12 @@ +use std::ffi::OsString; use std::path::PathBuf; +use std::str::FromStr; use clap::ArgMatches; use home::home_dir; use flume::bounded; +use mize::mize_err; use std::io::Read; +use tracing::debug; use mize::error::{IntoMizeResult, MizeError, MizeResult, MizeResultTrait}; use mize::instance::Instance; @@ -158,4 +162,19 @@ pub fn format_cbor(sub_matches: &ArgMatches) -> MizeResult<()> { } +pub fn run_from_module(cmd: &str, sub_matches: &ArgMatches) -> MizeResult<()> { + let default = OsString::default(); + let mut sub_cmd_line: Vec = sub_matches.get_many::("").unwrap().map(|e| e.to_owned()).collect(); + sub_cmd_line.insert(0, OsString::from_str(cmd)?); + + let mut instance = Instance::with_config(config_from_cli_args(sub_matches)?)?; + + debug!("loading module '{}'", cmd); + instance.load_module(cmd)?; + + instance.get_module(cmd)?.run_cli(&instance, sub_cmd_line) + .ok_or(mize_err!("The Module '{cmd}' does not implement the run_cli() functionality"))??; + + Ok(()) +} diff --git a/src/platform/os/main.rs b/src/platform/os/main.rs index 2da4ad3..49113b7 100644 --- a/src/platform/os/main.rs +++ b/src/platform/os/main.rs @@ -1,11 +1,14 @@ #![ allow( warnings ) ] +use std::ffi::OsString; use std::path::PathBuf; +use clap::builder::OsStr; use clap::{ArgAction, ArgMatches}; use clap::{Arg, crate_version, Command}; use mize::error::{MizeError, MizeResult}; +use mize::{instance, Instance}; use mize::item::ItemData; use tokio::sync::{Mutex, mpsc}; use std::sync::Arc; @@ -70,7 +73,7 @@ fn main() { Some(("format-cbor", sub_matches)) => cli::format_cbor(sub_matches), // some unknown command passed - Some((cmd, sub_matches)) => Err(MizeError::new().msg(format!("The subcommand: {} is not known. use --help to list availavle commands", cmd))), + Some((cmd, sub_matches)) => cli::run_from_module(cmd, sub_matches), None => Err(MizeError::new().msg("No subcommand was passed. use --help to list availavle comamnds.")), }; @@ -81,6 +84,8 @@ fn main() { } + + fn cli_matches() -> clap::ArgMatches { @@ -88,6 +93,7 @@ fn cli_matches() -> clap::ArgMatches { .version(crate_version!()) .author("Sebastian Moser") .about("The MiZe Command line tool") + .allow_external_subcommands(true) .arg(Arg::new("verbose") .long("verbose") .short('v') diff --git a/src/platform/os/mod.rs b/src/platform/os/mod.rs index 3a07742..31e113a 100644 --- a/src/platform/os/mod.rs +++ b/src/platform/os/mod.rs @@ -16,7 +16,7 @@ use crate::id::MizeId; use crate::instance::store::Store; use crate::instance::Instance; use crate::error::{IntoMizeResult, MizeError, MizeResult}; -use crate::item::{ItemData, IntoItemData}; +use crate::item::{data_from_string, IntoItemData, ItemData}; use crate::memstore::MemStore; use crate::{mize_err, Module}; use crate::instance::module::EmptyModule; @@ -57,7 +57,7 @@ pub fn os_instance_init(instance: &mut Instance) -> MizeResult<()> { ////// load config from MIZE_CONFIG env var match std::env::var("MIZE_CONFIG") { Ok(config_string) => { - let config = config_from_string(config_string)?; + let config = data_from_string(config_string)?; debug!("env var MIZE_CONFIG present"); instance.set_blocking("0", config)?; trace!("config after MIZE_CONFIG env var: {}", instance.get("0/config")?); @@ -282,7 +282,7 @@ pub fn config_from_cli_args(matches: &ArgMatches) -> MizeResult { } if let Some(config_string) = matches.get_one::("config") { - config.merge(config_from_string(config_string.to_string())?); + config.merge(data_from_string(config_string.to_string())?); trace!("config after --config arg: {}", config); } @@ -300,27 +300,6 @@ pub fn config_from_file(file_path: String) -> MizeResult { return Ok(item_data); } -fn config_from_string(config_string: String) -> MizeResult { - - let mut config = ItemData::new(); - - for option in config_string.split(":") { - if option == "".to_owned() { - continue; - } - - let path = option.split("=").nth(0) - .ok_or(MizeError::new().msg(format!("Failed to parse Option: option '{}' has an empty path (thing beforee =)", option)))?; - let value = option.split("=").nth(1) - .ok_or(MizeError::new().msg(format!("Failed to parse Option: option '{}' has an empty value (thing after =)", option)))?; - let mut path_vec = vec!["config"]; - path_vec.extend(path.split(".")); - - config.set_path(path_vec, ItemData::parse(value))?; - } - - return Ok(config); -} diff --git a/src/platform/wasm/init.js b/src/platform/wasm/init.js index 51f9fbc..805a68e 100644 --- a/src/platform/wasm/init.js +++ b/src/platform/wasm/init.js @@ -7,12 +7,24 @@ async function init_mize(config) { return window.mize } + let config_str = ""; + if typeof config == "string" { + config_str = config - const { JsInstance } = wasm_bindgen; + } else if typeof config == "object" { + config_str = JSON.stringify(config) - await wasm_bindgen() + } else { + throw "Error: config passed to init_mize() is not of type 'object' or 'string'" + } - window.mize = await new JsInstance(config) + const { new_js_instance } = wasm_bindgen; - return window.mize + await wasm_bindgen() + + window.mize = await new_js_instance(config) + window.mize.mod = {} + window.mize.init() + + return window.mize } diff --git a/src/platform/wasm/mod.rs b/src/platform/wasm/mod.rs index 46fd8d7..e92d9a2 100644 --- a/src/platform/wasm/mod.rs +++ b/src/platform/wasm/mod.rs @@ -2,7 +2,7 @@ use std::ptr::NonNull; use std::panic; use web_sys::js_sys::{self, eval}; use crate::id::MizeId; -use crate::item::Item; +use crate::item::{Item, ItemData}; use crate::platform::wasm::js_sys::Function; use web_sys::{WorkerOptions, WorkerType}; use web_sys::Worker; @@ -55,13 +55,16 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; pub fn wasm_instance_init(instance: &mut Instance) -> MizeResult<()> { console_log!("Hello world from wasm_instance_init!!!!!!!!!!"); - eval("window.mize.mod = {}").map_err(|_| mize_err!("failed to set window.mize.mod to an empty object"))?; - Ok(()) } pub fn load_module(instance: &mut Instance, name: &str, path: Option) -> MizeResult<()> { console_log!("loading module: {}", name); + + let hi = 1; + let hi = 1; + let hi = 1; + Ok(()) } @@ -77,33 +80,35 @@ pub struct JsItemHandle { id: MizeId, } +#[wasm_bindgen] +pub async fn new_js_instance(config_json_str: String) -> JsInstance { + + panic::set_hook(Box::new(console_error_panic_hook::hook)); + + let config = ItemData::from_json(config_json_str).expect("parsing of json config failed"); + + let mut instance = Instance::empty().expect("Instance::empty() failed"); + + instance.set_blocking("0/config", config).expect("Failed to set the config at item 0"); + + let mut js_instance = JsInstance { inner: NonNull::from(Box::leak(Box::new(instance))) }; + + return js_instance; +} #[wasm_bindgen] impl JsInstance { - #[wasm_bindgen(constructor)] - pub fn new() -> JsInstance { - - panic::set_hook(Box::new(console_error_panic_hook::hook)); - - let instance = match Instance::new() { - Ok(val) => val, - Err(e) => { - console_log!("Instance::new() failed with: {:?}", e); - panic!() - }, - }; - - - let mut js_instance = JsInstance { inner: NonNull::from(Box::leak(Box::new(instance))) }; - return js_instance; + #[wasm_bindgen] + pub unsafe fn init(&mut self) -> MizeResult<()> { + self.inner.as_mut().init() } #[wasm_bindgen] pub unsafe fn set(&mut self, id: String, value: String) -> () { let data = value.into_item_data(); console_log!("data in set: {}", data); - self.inner.as_mut().set_blocking(id, data); + self.inner.as_mut().set(id, data); } #[wasm_bindgen]