From e89e51b6b150595ea3190d694d78d02063bb9da1 Mon Sep 17 00:00:00 2001 From: Jonas Strehle Date: Thu, 5 Mar 2026 22:35:18 +0100 Subject: [PATCH 1/2] Add REPL support: implement Repl class, update Cargo.toml dependencies, and adjust settings --- .vscode/settings.json | 2 +- Cargo.lock | 12 ----- datex-browser-demo/src/demo/demo-runtime.ts | 7 ++- rs-lib/Cargo.toml | 8 +++- rs-lib/src/lib.rs | 5 ++- rs-lib/src/repl/mod.rs | 45 +++++++++++++++++++ rs-lib/src/runtime.rs | 50 ++++++++++----------- rs-lib/tests/core_types.rs | 2 +- src/datex-web/datex_web.d.ts | 7 +++ src/mod.ts | 1 + src/repl/mod.ts | 14 ++++++ 11 files changed, 107 insertions(+), 46 deletions(-) create mode 100644 rs-lib/src/repl/mod.rs create mode 100644 src/repl/mod.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 58367c64..9391f542 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,5 +9,5 @@ "RUSTFLAGS": "--cfg=web_sys_unstable_apis" }, "rust-analyzer.cargo.target": "wasm32-unknown-unknown", - "rust-analyzer.cargo.features": ["debug"] + "rust-analyzer.cargo.features": [] } diff --git a/Cargo.lock b/Cargo.lock index 7b398b39..c267468c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -351,7 +351,6 @@ dependencies = [ [[package]] name = "datex-core" version = "0.0.11" -source = "git+https://github.com/unyt-org/datex?branch=release%2F0.0.11#fe3421a6e19ef07285ab914fd3436805e477f000" dependencies = [ "ariadne", "async-select", @@ -405,7 +404,6 @@ dependencies = [ [[package]] name = "datex-crypto-facade" version = "0.0.2" -source = "git+https://github.com/unyt-org/datex?branch=release%2F0.0.11#fe3421a6e19ef07285ab914fd3436805e477f000" dependencies = [ "bs58", ] @@ -413,7 +411,6 @@ dependencies = [ [[package]] name = "datex-crypto-web" version = "0.0.2" -source = "git+https://github.com/unyt-org/datex?branch=release%2F0.0.11#fe3421a6e19ef07285ab914fd3436805e477f000" dependencies = [ "datex-crypto-facade", "wasm-bindgen", @@ -424,7 +421,6 @@ dependencies = [ [[package]] name = "datex-macros-internal" version = "0.0.1" -source = "git+https://github.com/unyt-org/datex?branch=release%2F0.0.11#fe3421a6e19ef07285ab914fd3436805e477f000" dependencies = [ "proc-macro2", "quote", @@ -2389,11 +2385,3 @@ name = "zmij" version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa" - -[[patch.unused]] -name = "datex-core" -version = "0.0.11" - -[[patch.unused]] -name = "datex-crypto-facade" -version = "0.0.2" diff --git a/datex-browser-demo/src/demo/demo-runtime.ts b/datex-browser-demo/src/demo/demo-runtime.ts index 1efb1b01..e2a0c31b 100644 --- a/datex-browser-demo/src/demo/demo-runtime.ts +++ b/datex-browser-demo/src/demo/demo-runtime.ts @@ -1,4 +1,4 @@ -import { Endpoint, Range, Ref, Runtime } from "datex"; +import { Endpoint, Range, Ref, Repl, Runtime } from "datex"; export const runtime = await Runtime.create( { @@ -15,7 +15,7 @@ export const runtime = await Runtime.create( }, }, { - log_level: "warn", + log_level: "info", }, ); @@ -31,3 +31,6 @@ globalThis.Range = Range; // @ts-ignore global variable for debugging globalThis.Endpoint = Endpoint; + +// @ts-ignore global variable for debugging +globalThis.Repl = Repl; diff --git a/rs-lib/Cargo.toml b/rs-lib/Cargo.toml index 973df437..85fecdf8 100644 --- a/rs-lib/Cargo.toml +++ b/rs-lib/Cargo.toml @@ -20,14 +20,16 @@ panic = "abort" [dependencies] log = { version = "0.4", features = ["std", "serde"] } -datex-core = { git = "https://github.com/unyt-org/datex", branch = "release/0.0.11", version = "0.0.11", default-features = false, features = [ +# git = "https://github.com/unyt-org/datex", branch = "release/0.0.11", +datex-core = { version = "0.0.11", default-features = false, features = [ "target_wasm", "compiler", "decompiler", "syntax_highlighting_legacy", "lsp_wasm", ] } -datex-crypto-facade = { git = "https://github.com/unyt-org/datex", branch = "release/0.0.11", version = "0.0.2" } +# git = "https://github.com/unyt-org/datex", branch = "release/0.0.11", +datex-crypto-facade = { version = "0.0.2" } async-trait = "0.1.87" url = { version = "2.5.7" } @@ -107,9 +109,11 @@ default = [ "websocket-client", "serial-client", "webrtc", + "repl", "lsp", # Make optional ] lsp = ["datex-core/lsp_wasm"] +repl = [] allow_unsigned_blocks = ["datex-core/allow_unsigned_blocks"] diff --git a/rs-lib/src/lib.rs b/rs-lib/src/lib.rs index c19b4bc9..086d2701 100644 --- a/rs-lib/src/lib.rs +++ b/rs-lib/src/lib.rs @@ -24,6 +24,9 @@ pub mod network; pub mod js_utils; pub mod utils; +#[cfg(feature = "repl")] +pub mod repl; + #[cfg(feature = "lsp")] pub mod lsp; @@ -78,4 +81,4 @@ pub async fn create_runtime( }); JSRuntime::run(config).await -} \ No newline at end of file +} diff --git a/rs-lib/src/repl/mod.rs b/rs-lib/src/repl/mod.rs new file mode 100644 index 00000000..f3b73fff --- /dev/null +++ b/rs-lib/src/repl/mod.rs @@ -0,0 +1,45 @@ +use datex_core::runtime::execution::context::{ + ExecutionContext, ExecutionMode, +}; +use wasm_bindgen::{JsError, JsValue, prelude::wasm_bindgen}; + +use crate::{js_utils::js_error, runtime::JSRuntime}; + +#[wasm_bindgen] +pub struct Repl { + runtime: JSRuntime, + execution_context: ExecutionContext, +} + +#[wasm_bindgen] +impl Repl { + #[wasm_bindgen(constructor)] + pub fn new(runtime: &JSRuntime, verbose: bool) -> Self { + let execution_context = if verbose { + ExecutionContext::local_debug( + ExecutionMode::unbounded(), + runtime.runtime().internal(), + ) + } else { + ExecutionContext::local( + ExecutionMode::unbounded(), + runtime.runtime().internal(), + ) + }; + + Self { + runtime: runtime.clone(), + execution_context, + } + } + + pub async fn execute(&mut self, script: &str) -> Result { + let result = self + .runtime + .runtime() + .execute(script, &[], Some(&mut self.execution_context)) + .await + .map_err(js_error)?; + Ok(self.runtime.maybe_value_container_to_dif(result)) + } +} diff --git a/rs-lib/src/runtime.rs b/rs-lib/src/runtime.rs index 67fe9e60..f43d2b3b 100644 --- a/rs-lib/src/runtime.rs +++ b/rs-lib/src/runtime.rs @@ -19,13 +19,10 @@ use datex_core::{ dxb_block::DXBBlock, protocol_structures::block_header::{BlockHeader, FlagsAndTimestamp}, }, - shared_values::{ - observers::{ObserveOptions, TransceiverId}, - }, serde::deserializer::DatexDeserializer, + shared_values::observers::{ObserveOptions, TransceiverId}, values::{ - core_values::endpoint::Endpoint, - value_container::ValueContainer, + core_values::endpoint::Endpoint, value_container::ValueContainer, }, }; use datex_crypto_facade::crypto::Crypto; @@ -37,19 +34,20 @@ use datex_core::{ runtime::{ Runtime, RuntimeConfig, RuntimeInternal, RuntimeRunner, memory::Memory, }, + shared_values::{ + pointer_address::PointerAddress, + shared_container::SharedContainerMutability, + }, }; use js_sys::Function; -use log::info; -use serde::{Deserialize, Serialize}; -use serde_wasm_bindgen::{Error, from_value}; -use std::{cell::RefCell, fmt::Display, rc::Rc, str::FromStr, sync::Arc}; -use datex_core::shared_values::pointer_address::PointerAddress; -use datex_core::shared_values::shared_container::SharedContainerMutability; +use serde_wasm_bindgen::from_value; +use std::{cell::RefCell, fmt::Display, rc::Rc}; use wasm_bindgen::prelude::*; use wasm_bindgen_futures::{future_to_promise, spawn_local}; use web_sys::js_sys::Promise; #[wasm_bindgen(getter_with_clone)] +#[derive(Clone)] pub struct JSRuntime { runtime: Runtime, pub com_hub: JSComHub, @@ -98,6 +96,20 @@ impl JSRuntime { js_runtime } + pub fn maybe_value_container_to_dif( + &self, + maybe_value_container: Option, + ) -> JsValue { + match maybe_value_container { + None => JsValue::NULL, + Some(value_container) => { + let dif_value_container = + DIFValueContainer::from_value_container(&value_container); + to_js_value(&dif_value_container).unwrap() + } + } + } + fn new(runtime: Runtime) -> JSRuntime { let com_hub = JSComHub::new(runtime.clone()); JSRuntime { runtime, com_hub } @@ -388,22 +400,6 @@ impl JSRuntime { )) } - fn maybe_value_container_to_dif( - &self, - maybe_value_container: Option, - ) -> JsValue { - match maybe_value_container { - None => JsValue::NULL, - Some(value_container) => { - let dif_value_container = - DIFValueContainer::from_value_container( - &value_container, - ); - to_js_value(&dif_value_container).unwrap() - } - } - } - fn js_values_to_value_containers( &self, js_values: Option>, diff --git a/rs-lib/tests/core_types.rs b/rs-lib/tests/core_types.rs index eee4279b..0323ff8b 100644 --- a/rs-lib/tests/core_types.rs +++ b/rs-lib/tests/core_types.rs @@ -1,6 +1,6 @@ use datex_core::{ libs::core::{CoreLibPointerId, create_core_lib_types}, - values::pointer::PointerAddress, + shared_values::pointer_address::PointerAddress, }; #[test] diff --git a/src/datex-web/datex_web.d.ts b/src/datex-web/datex_web.d.ts index 9cd277a0..ac787f37 100644 --- a/src/datex-web/datex_web.d.ts +++ b/src/datex-web/datex_web.d.ts @@ -299,6 +299,13 @@ export class JSRuntime { readonly version: string; } +export class Repl { + free(): void; + [Symbol.dispose](): void; + execute(script: string): Promise; + constructor(runtime: JSRuntime, verbose: boolean); +} + export class RuntimeDIFHandle { private constructor(); free(): void; diff --git a/src/mod.ts b/src/mod.ts index c643a755..a60c2bc7 100644 --- a/src/mod.ts +++ b/src/mod.ts @@ -26,4 +26,5 @@ export * as Network from "./network/mod.ts"; export * from "./lib/special-core-types/endpoint.ts"; export * from "./lib/special-core-types/range.ts"; export * from "./refs/ref.ts"; +export * from "./repl/mod.ts"; import "./utils/devtools-formatter.ts"; diff --git a/src/repl/mod.ts b/src/repl/mod.ts new file mode 100644 index 00000000..1cd42e07 --- /dev/null +++ b/src/repl/mod.ts @@ -0,0 +1,14 @@ +import type { Runtime } from "../runtime/runtime.ts"; +import { Repl as ReplInternal } from "../datex.ts"; +export class Repl { + #repl: ReplInternal; + constructor(private runtime: Runtime, verbose = false) { + this.#repl = new ReplInternal(runtime._runtime, verbose); + } + + public async execute(script: string): Promise { + const result = await this.#repl.execute(script); + const jsValue = this.runtime.dif.resolveDIFValueContainerSync(result); + return this.runtime.valueToString(jsValue); + } +} From 779a2b9cb04210d9868b0ae648bb61811bbacb55 Mon Sep 17 00:00:00 2001 From: Jonas Strehle Date: Thu, 5 Mar 2026 22:51:07 +0100 Subject: [PATCH 2/2] fix cargo lock --- Cargo.lock | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index c267468c..2a0f3f49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -351,6 +351,8 @@ dependencies = [ [[package]] name = "datex-core" version = "0.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1471cd0f342832e2a44603ce2c910359d2fd3325f15476359ca99493df421f05" dependencies = [ "ariadne", "async-select", @@ -404,6 +406,8 @@ dependencies = [ [[package]] name = "datex-crypto-facade" version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56ea2af04cab3b42b4ab122ed2e850a7837e6d402bb4b9a9ffe0b9cfca7ad26d" dependencies = [ "bs58", ] @@ -411,6 +415,8 @@ dependencies = [ [[package]] name = "datex-crypto-web" version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07a56e4ff8e888b4609dd502a1f63e17fd669ae050eee06af97f2a195bdb77c0" dependencies = [ "datex-crypto-facade", "wasm-bindgen", @@ -421,6 +427,8 @@ dependencies = [ [[package]] name = "datex-macros-internal" version = "0.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6717768303603f37c8d6fd393ae7da7b78ae33b1ad0d6980253fa19005d0b84" dependencies = [ "proc-macro2", "quote",