Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
20 changes: 8 additions & 12 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions datex-browser-demo/src/demo/demo-runtime.ts
Original file line number Diff line number Diff line change
@@ -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(
{
Expand All @@ -15,7 +15,7 @@ export const runtime = await Runtime.create(
},
},
{
log_level: "warn",
log_level: "info",
},
);

Expand All @@ -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;
8 changes: 6 additions & 2 deletions rs-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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"]

Expand Down
5 changes: 4 additions & 1 deletion rs-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -78,4 +81,4 @@ pub async fn create_runtime(
});

JSRuntime::run(config).await
}
}
45 changes: 45 additions & 0 deletions rs-lib/src/repl/mod.rs
Original file line number Diff line number Diff line change
@@ -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<JsValue, JsError> {
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))
}
}
50 changes: 23 additions & 27 deletions rs-lib/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -98,6 +96,20 @@ impl JSRuntime {
js_runtime
}

pub fn maybe_value_container_to_dif(
&self,
maybe_value_container: Option<ValueContainer>,
) -> 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 }
Expand Down Expand Up @@ -388,22 +400,6 @@ impl JSRuntime {
))
}

fn maybe_value_container_to_dif(
&self,
maybe_value_container: Option<ValueContainer>,
) -> 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<Vec<JsValue>>,
Expand Down
2 changes: 1 addition & 1 deletion rs-lib/tests/core_types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use datex_core::{
libs::core::{CoreLibPointerId, create_core_lib_types},
values::pointer::PointerAddress,
shared_values::pointer_address::PointerAddress,
};

#[test]
Expand Down
7 changes: 7 additions & 0 deletions src/datex-web/datex_web.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,13 @@ export class JSRuntime {
readonly version: string;
}

export class Repl {
free(): void;
[Symbol.dispose](): void;
execute(script: string): Promise<any>;
constructor(runtime: JSRuntime, verbose: boolean);
}

export class RuntimeDIFHandle {
private constructor();
free(): void;
Expand Down
1 change: 1 addition & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
14 changes: 14 additions & 0 deletions src/repl/mod.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
const result = await this.#repl.execute(script);
const jsValue = this.runtime.dif.resolveDIFValueContainerSync(result);
return this.runtime.valueToString(jsValue);
}
}