diff --git a/Cargo.lock b/Cargo.lock index 55e0d196..bee6202c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -166,9 +166,9 @@ dependencies = [ [[package]] name = "binrw" -version = "0.15.0" +version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81419ff39e6ed10a92a7f125290859776ced35d9a08a665ae40b23e7ca702f30" +checksum = "d53195f985e88ab94d1cc87e80049dd2929fd39e4a772c5ae96a7e5c4aad3642" dependencies = [ "array-init", "binrw_derive", @@ -177,9 +177,9 @@ dependencies = [ [[package]] name = "binrw_derive" -version = "0.15.0" +version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "376404e55ec40d0d6f8b4b7df3f87b87954bd987f0cf9a7207ea3b6ea5c9add4" +checksum = "5910da05ee556b789032c8ff5a61fb99239580aa3fd0bfaa8f4d094b2aee00ad" dependencies = [ "either", "proc-macro2", @@ -219,6 +219,9 @@ name = "bs58" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf88ba1141d185c399bee5288d850d63b8369520c1eafc32a0430b5b6c287bf4" +dependencies = [ + "tinyvec", +] [[package]] name = "bumpalo" @@ -347,9 +350,9 @@ dependencies = [ [[package]] name = "datex-core" -version = "0.0.9" +version = "0.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6324622757bf96a6facaf4a9c1c5b42a90878afe431bf02d1a8710ea95bacced" +checksum = "828f8fd2544e3e7062641e6e1bc64cf2d05d749f8f0765d0f4fd4e588c2a080d" dependencies = [ "ariadne", "async-select", @@ -402,18 +405,18 @@ dependencies = [ [[package]] name = "datex-crypto-facade" -version = "0.0.1" +version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8871b51b760cdf5a7c3fd2f4244391eda4a4a43e6fadd65d9651bac731934a0" +checksum = "56ea2af04cab3b42b4ab122ed2e850a7837e6d402bb4b9a9ffe0b9cfca7ad26d" dependencies = [ "bs58", ] [[package]] name = "datex-crypto-web" -version = "0.0.1" +version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc433cd04b23788f3fddbe9997c27252361cabcfbae50710a4a71b2ca9b09489" +checksum = "07a56e4ff8e888b4609dd502a1f63e17fd669ae050eee06af97f2a195bdb77c0" dependencies = [ "datex-crypto-facade", "wasm-bindgen", @@ -434,7 +437,7 @@ dependencies = [ [[package]] name = "datex-web" -version = "0.0.12" +version = "0.0.13" dependencies = [ "async-trait", "cfg-if", @@ -1244,18 +1247,18 @@ checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" [[package]] name = "pin-project" -version = "1.1.10" +version = "1.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "677f1add503faace112b9f1373e43e9e054bfdd22ff1a63c1bc485eaec6a6a8a" +checksum = "f1749c7ed4bcaf4c3d0a3efc28538844fb29bcdd7d2b67b2be7e20ba861ff517" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "1.1.10" +version = "1.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e918e4ff8c4549eb882f14b3a4bc8c8bc93de829416eacf579f1207a8fbf861" +checksum = "d9b20ed30f105399776b9c883e68e536ef602a16ae6f596d2c473591d6ad64c6" dependencies = [ "proc-macro2", "quote", @@ -1264,9 +1267,9 @@ dependencies = [ [[package]] name = "pin-project-lite" -version = "0.2.16" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" [[package]] name = "plist" @@ -1309,9 +1312,9 @@ dependencies = [ [[package]] name = "proc-macro-crate" -version = "3.4.0" +version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "219cb19e96be00ab2e37d6e299658a0cfa83e52429179969b0f0121b4ac46983" +checksum = "e67ba7e9b2b56446f1d419b1d807906278ffa1a658a8a5d8a39dcb1f5a78614f" dependencies = [ "toml_edit", ] @@ -1336,9 +1339,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.44" +version = "1.0.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" dependencies = [ "proc-macro2", ] @@ -1881,20 +1884,35 @@ dependencies = [ "zerovec", ] +[[package]] +name = "tinyvec" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa5fdc3bce6191a1dbc8c02d5c8bffcf557bafa17c124c5264a458f1b0613fa" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "toml_datetime" -version = "0.7.5+spec-1.1.0" +version = "1.0.0+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92e1cfed4a3038bc5a127e35a2d360f145e1f4b971b551a2ba5fd7aedf7e1347" +checksum = "32c2555c699578a4f59f0cc68e5116c8d7cabbd45e1409b989d4be085b53f13e" dependencies = [ "serde_core", ] [[package]] name = "toml_edit" -version = "0.23.10+spec-1.0.0" +version = "0.25.4+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84c8b9f757e028cee9fa244aea147aab2a9ec09d5325a9b01e0a49730c2b5269" +checksum = "7193cbd0ce53dc966037f54351dbbcf0d5a642c7f0038c382ef9e677ce8c13f2" dependencies = [ "indexmap 2.13.0", "toml_datetime", diff --git a/README.md b/README.md index c2f0629a..6975041b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# DATEX Web +# DATEX > Check out our specification at [datex.unyt.org](https://datex.unyt.org). diff --git a/deno.json b/deno.json index d5b171ed..393ab24f 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@unyt/datex", - "version": "0.0.12", + "version": "0.0.13", "license": "MIT", "exports": { ".": "./src/mod.ts", diff --git a/rs-lib/Cargo.toml b/rs-lib/Cargo.toml index 3ad43a6d..72eabf64 100644 --- a/rs-lib/Cargo.toml +++ b/rs-lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "datex-web" -version = "0.0.12" +version = "0.0.13" authors = [ "Benedikt Strehle ", "Jonas Strehle ", @@ -20,14 +20,14 @@ panic = "abort" [dependencies] log = { version = "0.4", features = ["std", "serde"] } -datex-core = { version = "0.0.9", default-features = false, features = [ +datex-core = { version = "0.0.10", default-features = false, features = [ "target_wasm", "compiler", "decompiler", "syntax_highlighting_legacy", "lsp_wasm", ] } -datex-crypto-facade = { version = "0.0.1" } +datex-crypto-facade = { version = "0.0.2" } # datex_macros = { git = "https://github.com/unyt-org/datex", branch = "refactor/update-loop", version = "0.1.3", package = "datex_macros" } diff --git a/rs-lib/src/js_utils.rs b/rs-lib/src/js_utils.rs index fbdcd197..7e8f7704 100644 --- a/rs-lib/src/js_utils.rs +++ b/rs-lib/src/js_utils.rs @@ -108,10 +108,9 @@ pub fn dif_js_value_to_value_container( pub fn value_container_to_dif_js_value( value_container: &ValueContainer, - memory: &RefCell, ) -> JsValue { let dif_value_container = - DIFValueContainer::from_value_container(&value_container, memory); + DIFValueContainer::from_value_container(&value_container); to_js_value(&dif_value_container) .expect("Failed to serialize DIFValueContainer to JsValue") } diff --git a/rs-lib/src/lib.rs b/rs-lib/src/lib.rs index e8d1fc07..c19b4bc9 100644 --- a/rs-lib/src/lib.rs +++ b/rs-lib/src/lib.rs @@ -22,7 +22,6 @@ use runtime::JSRuntime; pub mod network; pub mod js_utils; -pub mod pointer; pub mod utils; #[cfg(feature = "lsp")] @@ -79,60 +78,4 @@ pub async fn create_runtime( }); JSRuntime::run(config).await -} - -/// Executes a Datex script and returns the result as a string. -#[wasm_bindgen] -pub fn execute(datex_script: &str, decompile_options: JsValue) -> String { - let result = compile_script(datex_script, CompileOptions::default()); - if let Ok((dxb, _)) = result { - let input = ExecutionInput::new( - &dxb, - ExecutionOptions { - verbose: true, - ..ExecutionOptions::default() - }, - None, - ); - let result = execute_dxb_sync(input).unwrap_or_else(|err| { - panic!("Failed to execute script: {err:?}"); - }); - let result = result.unwrap(); - let (result_dxb, _) = - compile_template("?", &[Some(result)], CompileOptions::default()) - .unwrap(); - - decompile_body( - &result_dxb, - from_value(decompile_options).unwrap_or_default(), - ) - .unwrap_or_else(|err| { - panic!("Failed to decompile result: {err:?}"); - }) - } else { - panic!("Failed to compile script: {:?}", result.err()); - } -} - -/// Executes a Datex script and returns true when execution was successful. -/// Does not return the result of the script, but only indicates success or failure. -#[wasm_bindgen] -pub fn execute_internal(datex_script: &str) -> bool { - let result = compile_script(datex_script, CompileOptions::default()); - if let Ok((dxb, _)) = result { - let input = ExecutionInput::new( - &dxb, - ExecutionOptions { - verbose: true, - ..ExecutionOptions::default() - }, - None, - ); - let result = execute_dxb_sync(input).unwrap_or_else(|err| { - panic!("Failed to execute script: {err:?}"); - }); - result.is_some() - } else { - panic!("Failed to compile script: {:?}", result.err()); - } -} +} \ No newline at end of file diff --git a/rs-lib/src/network/com_hub.rs b/rs-lib/src/network/com_hub.rs index 69ecdd67..b8b2a5c1 100644 --- a/rs-lib/src/network/com_hub.rs +++ b/rs-lib/src/network/com_hub.rs @@ -122,7 +122,6 @@ impl JSComHub { &JsValue::UNDEFINED, &value_container_to_dif_js_value( &setup_data, - runtime.memory(), ), ) .map_err(|e| { diff --git a/rs-lib/src/pointer.rs b/rs-lib/src/pointer.rs deleted file mode 100644 index 770583c1..00000000 --- a/rs-lib/src/pointer.rs +++ /dev/null @@ -1,22 +0,0 @@ -use datex_core::references::reference::Reference; -use wasm_bindgen::prelude::*; - -#[wasm_bindgen] -pub struct JSPointer { - reference: Reference, -} - -/** - * Internal impl of the JSRuntime, not exposed to JavaScript - */ -impl JSPointer { - pub fn new(reference: Reference) -> JSPointer { - JSPointer { reference } - } -} - -/** - * Exposed properties and methods for JavaScript - */ -#[wasm_bindgen] -impl JSPointer {} diff --git a/rs-lib/src/runtime.rs b/rs-lib/src/runtime.rs index 0f6c5c0e..67fe9e60 100644 --- a/rs-lib/src/runtime.rs +++ b/rs-lib/src/runtime.rs @@ -19,13 +19,12 @@ use datex_core::{ dxb_block::DXBBlock, protocol_structures::block_header::{BlockHeader, FlagsAndTimestamp}, }, - references::{ + shared_values::{ observers::{ObserveOptions, TransceiverId}, - reference::ReferenceMutability, }, serde::deserializer::DatexDeserializer, values::{ - core_values::endpoint::Endpoint, pointer::PointerAddress, + core_values::endpoint::Endpoint, value_container::ValueContainer, }, }; @@ -44,6 +43,8 @@ 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 wasm_bindgen::prelude::*; use wasm_bindgen_futures::{future_to_promise, spawn_local}; use web_sys::js_sys::Promise; @@ -112,12 +113,12 @@ impl JSRuntime { future_to_promise(async move { let something = b"yellow submarineyellow submarine".to_owned(); let some_check = - b"9At2nzU19GjL8F4WFRyB7RZSGLemMGUMVBZAMChfndF2".to_owned(); + "9At2nzU19GjL8F4WFRyB7RZSGLemMGUMVBZAMChfndF2".to_owned(); - let based = CryptoImpl::enc_b58(&something).unwrap(); - let unbased = CryptoImpl::dec_b58(&some_check).unwrap(); + let based = CryptoImpl::enc_b58(&something).as_bytes().to_vec(); + let unbased = CryptoImpl::dec_b58_32(&some_check).unwrap(); assert_eq!(something, unbased); - assert_eq!(some_check, based); + assert_eq!(some_check.as_bytes().to_vec(), based); // Hashes let mut ikm = Vec::from([0u8; 32]); @@ -224,12 +225,14 @@ impl JSRuntime { assert_ne!(msg, ctr_ciphered); // AES key wrapping - let wrapped = CryptoImpl::key_upwrap(&random_bytes, &random_bytes) - .await - .unwrap(); - let unwrapped = CryptoImpl::key_unwrap(&random_bytes, &wrapped) - .await - .unwrap(); + let wrapped = + CryptoImpl::key_wrap_rfc3394(&random_bytes, &random_bytes) + .await + .unwrap(); + let unwrapped = + CryptoImpl::key_unwrap_rfc3394(&random_bytes, &wrapped) + .await + .unwrap(); assert_eq!(random_bytes.to_vec(), unwrapped); // assert_ne!(wrapped, unwrapped); @@ -395,7 +398,6 @@ impl JSRuntime { let dif_value_container = DIFValueContainer::from_value_container( &value_container, - self.runtime.memory(), ); to_js_value(&dif_value_container).unwrap() } @@ -555,7 +557,7 @@ impl RuntimeDIFHandle { } else { Some(from_value(allowed_type).map_err(js_error)?) }; - let dif_mutability = ReferenceMutability::try_from(mutability) + let dif_mutability = SharedContainerMutability::try_from(mutability) .map_err(|_| js_error(ConversionError::InvalidValue))?; let address = DIFInterface::create_pointer( self, @@ -640,7 +642,7 @@ impl DIFInterface for RuntimeDIFHandle { &self, value: DIFValueContainer, allowed_type: Option, - mutability: ReferenceMutability, + mutability: SharedContainerMutability, ) -> Result { self.internal .create_pointer(value, allowed_type, mutability) diff --git a/scripts/build-wasm.ts b/scripts/build-wasm.ts index eb8f99dd..a1a4f99b 100644 --- a/scripts/build-wasm.ts +++ b/scripts/build-wasm.ts @@ -105,7 +105,7 @@ async function runWasmBindgen(args: { }) { // make sure wasm-bindgen cli is installed const wasmBindgenInstallProcess = new Deno.Command("cargo", { - args: ["install", "wasm-bindgen-cli"], + args: ["install", "wasm-bindgen-cli", "--version", "0.2.112"], }).spawn(); const installRes = await wasmBindgenInstallProcess.status; if (!installRes.success) { diff --git a/src/datex-web/datex_web.d.ts b/src/datex-web/datex_web.d.ts index 55e0a1c0..9cd277a0 100644 --- a/src/datex-web/datex_web.d.ts +++ b/src/datex-web/datex_web.d.ts @@ -275,12 +275,6 @@ export class JSComHub { remove_socket(socket_uuid: string): Promise; } -export class JSPointer { - private constructor(); - free(): void; - [Symbol.dispose](): void; -} - export class JSRuntime { private constructor(); free(): void; @@ -328,14 +322,3 @@ export class RuntimeDIFHandle { } export function create_runtime(config: any, debug_config: any): Promise; - -/** - * Executes a Datex script and returns the result as a string. - */ -export function execute(datex_script: string, decompile_options: any): string; - -/** - * Executes a Datex script and returns true when execution was successful. - * Does not return the result of the script, but only indicates success or failure. - */ -export function execute_internal(datex_script: string): boolean; diff --git a/src/dif/definitions.ts b/src/dif/definitions.ts index 18537155..e667ce1f 100644 --- a/src/dif/definitions.ts +++ b/src/dif/definitions.ts @@ -39,18 +39,18 @@ export type DIFTypeDefinitionKind = typeof DIFTypeDefinitionKind[keyof typeof DI /** * Representation of reference mutability (mutable or immutable) in DIF. */ -export const DIFReferenceMutability = { +export const DIFSharedValueMutability = { Mutable: 0, Immutable: 1, } as const; /** A DIF reference mutability. */ -export type DIFReferenceMutability = typeof DIFReferenceMutability[keyof typeof DIFReferenceMutability]; +export type DIFSharedValueMutability = typeof DIFSharedValueMutability[keyof typeof DIFSharedValueMutability]; export type DIFType = | DIFPointerAddress // shorthand for DIFType with DIFTypeDefinitionKind.Reference | { name?: string; - mut?: DIFReferenceMutability; + mut?: DIFSharedValueMutability; def: DIFTypeDefinition; }; @@ -79,11 +79,11 @@ type VerboseDIFTypeDefinition< def: DIFTypeDefinitionInner; }; -/** A representation of a reference in DIF. */ -export type DIFReference = { +/** A representation of a shared value in DIF. */ +export type DIFSharedValue = { value: DIFValueContainer; allowed_type: DIFTypeDefinition; - mut: DIFReferenceMutability; + mut: DIFSharedValueMutability; }; /** A representation of a value or pointer address in DIF. */ diff --git a/src/dif/dif-handler.ts b/src/dif/dif-handler.ts index 745f9600..a27bca2b 100644 --- a/src/dif/dif-handler.ts +++ b/src/dif/dif-handler.ts @@ -7,8 +7,8 @@ import { type DIFObject, type DIFPointerAddress, type DIFProperty, - type DIFReference, - DIFReferenceMutability, + type DIFSharedValue, + DIFSharedValueMutability, type DIFTypeDefinition, DIFTypeDefinitionKind, type DIFUpdate, @@ -156,16 +156,16 @@ export class DIFHandler { } /** - * Creates a new pointer for the specified DIF value. + * Creates a new shared value for the specified DIF value. * @param difValueContainer - The DIFValueContainer value to create a pointer for. * @param allowedType - The allowed type for the pointer. * @param mutability - The mutability of the pointer. * @returns The created pointer address. */ - public createReferenceFromDIFValue( + public createSharedValueFromDIFValue( difValueContainer: DIFValueContainer, allowedType: DIFTypeDefinition | null = null, - mutability: DIFReferenceMutability, + mutability: DIFSharedValueMutability, ): string { return this.#handle.create_pointer( difValueContainer, @@ -589,7 +589,7 @@ export class DIFHandler { return cached as T; } // if not in cache, resolve from runtime - const reference: DIFReference | Promise = this + const reference: DIFSharedValue | Promise = this .#handle.resolve_pointer_address(address); return this.mapPromise(reference, (reference) => { const value: T | Promise = this.resolveDIFValueContainer( @@ -625,7 +625,7 @@ export class DIFHandler { return cached as T; } // if not in cache, resolve from runtime - const entry: DIFReference = this.#handle + const entry: DIFSharedValue = this.#handle .resolve_pointer_address_sync(address); const value: T = this.resolveDIFValueContainerSync( entry.value, @@ -699,7 +699,7 @@ export class DIFHandler { protected initReference( pointerAddress: string, value: T, - mutability: DIFReferenceMutability, + mutability: DIFSharedValueMutability, allowedType: DIFTypeDefinition | null = null, ): T | Ref { let wrappedValue = this.wrapJSValue( @@ -712,7 +712,7 @@ export class DIFHandler { let metadata: CustomReferenceMetadata | undefined = undefined; // bind js value (if mutable, nominal type) - const bindJSValue = mutability !== DIFReferenceMutability.Immutable && + const bindJSValue = mutability !== DIFSharedValueMutability.Immutable && typeof allowedType == "string"; if (bindJSValue && !(wrappedValue instanceof Ref)) { typeBinding = this.type_registry.getTypeBinding(allowedType); @@ -729,7 +729,7 @@ export class DIFHandler { // if not immutable, observe to keep the pointer 'live' and receive updates let observerId: number | null = null; - if (mutability !== DIFReferenceMutability.Immutable) { + if (mutability !== DIFSharedValueMutability.Immutable) { observerId = this.observePointerBindDirect( pointerAddress, (update) => { @@ -911,11 +911,11 @@ export class DIFHandler { */ public createTransparentReference< V, - M extends DIFReferenceMutability = typeof DIFReferenceMutability.Mutable, + M extends DIFSharedValueMutability = typeof DIFSharedValueMutability.Mutable, >( value: V, allowedType: DIFTypeDefinition | null = null, - mutability: M = DIFReferenceMutability.Mutable as M, + mutability: M = DIFSharedValueMutability.Mutable as M, ): PointerOut { // if already bound to a reference, return the existing reference proxy (or the value itself) const pointerAddress = this.getPointerAddressForValue(value as WeakKey); @@ -926,7 +926,7 @@ export class DIFHandler { } const difValue = this.convertJSValueToDIFValueContainer(value); - const ptrAddress = this.createReferenceFromDIFValue( + const ptrAddress = this.createSharedValueFromDIFValue( difValue, allowedType, mutability, @@ -935,7 +935,7 @@ export class DIFHandler { if (!allowedType) { allowedType = (this.#handle.resolve_pointer_address_sync( ptrAddress, - ) as DIFReference).allowed_type; + ) as DIFSharedValue).allowed_type; } return this.initReference( ptrAddress, @@ -1338,23 +1338,23 @@ type IsPlainObject = T extends Builtins ? false : T extends object ? true : false; -type ObjectFieldOut = T extends Ref - ? M extends typeof DIFReferenceMutability.Immutable ? Ref +type ObjectFieldOut = T extends Ref + ? M extends typeof DIFSharedValueMutability.Immutable ? Ref : AssignableRef : IsPlainObject extends true ? ( ContainsRef extends true - ? M extends typeof DIFReferenceMutability.Immutable + ? M extends typeof DIFSharedValueMutability.Immutable ? { readonly [K in keyof T]: ObjectFieldOut } : { [K in keyof T]: ObjectFieldOut } : { readonly [K in keyof T]: ObjectFieldOut } ) : T; -export type PointerOut = V extends Ref - ? M extends typeof DIFReferenceMutability.Immutable ? Ref +export type PointerOut = V extends Ref + ? M extends typeof DIFSharedValueMutability.Immutable ? Ref : AssignableRef : IsPlainObject extends true ? ( - M extends typeof DIFReferenceMutability.Immutable ? { readonly [K in keyof V]: V[K] } + M extends typeof DIFSharedValueMutability.Immutable ? { readonly [K in keyof V]: V[K] } : { [K in keyof V]: V[K] } // ContainsRef extends true // ? M extends typeof DIFReferenceMutability.Immutable @@ -1363,7 +1363,7 @@ export type PointerOut = V extends Ref } ) : V extends PrimitiveValue ? Ref< - M extends typeof DIFReferenceMutability["Immutable"] ? V + M extends typeof DIFSharedValueMutability["Immutable"] ? V : WidenLiteral > : V; diff --git a/src/dif/display.ts b/src/dif/display.ts index 32e73f87..49af5891 100644 --- a/src/dif/display.ts +++ b/src/dif/display.ts @@ -4,9 +4,9 @@ * This module contains helper functions to convert DIF structures to display strings. */ import { - type DIFReference, - DIFReferenceMutability, type DIFRepresentationValue, + type DIFSharedValue, + DIFSharedValueMutability, type DIFTypeDefinition, type DIFValueContainer, } from "./definitions.ts"; @@ -15,10 +15,10 @@ import { CoreTypeAddress } from "./core.ts"; /** * Converts a DIF reference mutability to a display string. */ -export function mutabilityToDisplayString(mut: DIFReferenceMutability): string { - if (mut === DIFReferenceMutability.Mutable) { +export function mutabilityToDisplayString(mut: DIFSharedValueMutability): string { + if (mut === DIFSharedValueMutability.Mutable) { return "&mut "; - } else if (mut === DIFReferenceMutability.Immutable) { + } else if (mut === DIFSharedValueMutability.Immutable) { return "&"; } throw new Error("Unknown mutability: " + mut); @@ -28,7 +28,7 @@ export function mutabilityToDisplayString(mut: DIFReferenceMutability): string { * Converts a DIF reference to a display string. */ export function difReferenceToDisplayString( - reference: DIFReference, + reference: DIFSharedValue, ): string { const typeString = difTypeDefinitionToDisplayString(reference.allowed_type); const valueString = difValueContainerToDisplayString(reference.value); diff --git a/src/runtime/runtime.ts b/src/runtime/runtime.ts index 46598a4b..91ca8515 100644 --- a/src/runtime/runtime.ts +++ b/src/runtime/runtime.ts @@ -1,13 +1,13 @@ -import { create_runtime, type DecompileOptions, execute_internal, type JSRuntime } from "../datex.ts"; +import { create_runtime, type DecompileOptions, type JSRuntime } from "../datex.ts"; import { ComHub } from "../network/com-hub.ts"; import { DIFHandler, type PointerOut } from "../dif/dif-handler.ts"; -import type { DIFReferenceMutability, DIFTypeDefinition } from "../dif/definitions.ts"; +import type { DIFSharedValueMutability, DIFTypeDefinition } from "../dif/definitions.ts"; import type { Ref } from "../refs/ref.ts"; import { unimplemented } from "../utils/exceptions.ts"; // TODO: move to global.ts /** auto-generated version - do not edit: */ -const VERSION: string = "0.0.12"; +const VERSION: string = "0.0.13"; /** debug flags for the runtime */ interface DebugConfig { @@ -276,10 +276,6 @@ export class Runtime { return { datexScript, valuesArray }; } - public _execute_internal(datexScript: string): boolean { - return execute_internal(datexScript); - } - /** * Creates a new reference containg the given JS value. * For primitive values, a Ref wrapper is returned. @@ -292,7 +288,7 @@ export class Runtime { */ public createTransparentReference< V, - M extends DIFReferenceMutability = typeof DIFReferenceMutability.Mutable, + M extends DIFSharedValueMutability = typeof DIFSharedValueMutability.Mutable, >( // deno-lint-ignore ban-types value: V & {}, @@ -317,7 +313,7 @@ export class Runtime { */ public createOrGetWrappedReference< V, - M extends DIFReferenceMutability = typeof DIFReferenceMutability.Mutable, + M extends DIFSharedValueMutability = typeof DIFSharedValueMutability.Mutable, >( _value: V, _allowedType?: DIFTypeDefinition | null, diff --git a/test/dif/dif.test.ts b/test/dif/dif.test.ts index fac02042..8a81f619 100644 --- a/test/dif/dif.test.ts +++ b/test/dif/dif.test.ts @@ -2,9 +2,9 @@ import { Runtime } from "../../src/runtime/runtime.ts"; import { assert, assertEquals } from "@std/assert"; import { assertThrows } from "@std/assert/throws"; import { - type DIFReference, - DIFReferenceMutability, type DIFRepresentationValue, + type DIFSharedValue, + DIFSharedValueMutability, type DIFUpdate, type DIFUpdateData, DIFUpdateKind, @@ -19,18 +19,18 @@ const runtime = await Runtime.create({ endpoint: "@jonas" }); runtime.dif.type_registry.registerTypeBinding(arrayTypeBinding); Deno.test("pointer create with observe", () => { - const ref = runtime.dif.createReferenceFromDIFValue( + const ref = runtime.dif.createSharedValueFromDIFValue( { value: "Hello, DATEX!", }, undefined, - DIFReferenceMutability.Mutable, + DIFSharedValueMutability.Mutable, ); assertEquals(typeof ref, "string"); let observed: DIFUpdate | null = null; const observerId = runtime.dif.observePointerBindDirect(ref, (value) => { - runtime.executeSync("'xy'"); + runtime.executeSync('"xy"'); runtime.dif.unobserveReferenceBindDirect(ref, observerId); observed = value; // TODO: print error message somewhere (don't throw) @@ -53,12 +53,12 @@ Deno.test("pointer create with observe", () => { }); Deno.test("pointer create without observe", () => { - const ref = runtime.dif.createReferenceFromDIFValue( + const ref = runtime.dif.createSharedValueFromDIFValue( { value: "Hello, Datex!", }, undefined, - DIFReferenceMutability.Mutable, + DIFSharedValueMutability.Mutable, ); assertEquals(typeof ref, "string"); @@ -82,37 +82,37 @@ Deno.test("pointer create primitive", () => { runtime.createTransparentReference( 42, undefined, - DIFReferenceMutability.Immutable, + DIFSharedValueMutability.Immutable, ) satisfies Ref<42>; runtime.createTransparentReference( 42, undefined, - DIFReferenceMutability.Mutable, + DIFSharedValueMutability.Mutable, ) satisfies Ref; runtime.createTransparentReference( "hello world", undefined, - DIFReferenceMutability.Immutable, + DIFSharedValueMutability.Immutable, ) satisfies Ref<"hello world">; runtime.createTransparentReference( "hello world", undefined, - DIFReferenceMutability.Mutable, + DIFSharedValueMutability.Mutable, ) satisfies Ref; runtime.createTransparentReference( true, undefined, - DIFReferenceMutability.Immutable, + DIFSharedValueMutability.Immutable, ) satisfies Ref; runtime.createTransparentReference( { x: true } as const, undefined, - DIFReferenceMutability.Immutable, + DIFSharedValueMutability.Immutable, ) satisfies { readonly x: true; }; @@ -120,12 +120,12 @@ Deno.test("pointer create primitive", () => { const a = runtime.createTransparentReference( 5, undefined, - DIFReferenceMutability.Immutable, + DIFSharedValueMutability.Immutable, ); const b = runtime.createTransparentReference( { x: a }, undefined, - DIFReferenceMutability.Mutable, + DIFSharedValueMutability.Mutable, ) satisfies { x: Ref<5>; }; @@ -182,7 +182,7 @@ Deno.test("pointer create struct", () => { * -> User * -> readonly User */ - DIFReferenceMutability.Mutable, + DIFSharedValueMutability.Mutable, ); const struct = { a: 1.0, b: "text", c: { d: true }, e: { f: innerPtr } }; @@ -190,7 +190,7 @@ Deno.test("pointer create struct", () => { const ptrObjImmutable = runtime.createTransparentReference( struct, undefined, - DIFReferenceMutability.Immutable, + DIFSharedValueMutability.Immutable, ); ptrObjImmutable.e satisfies { readonly f: Ref }; @@ -200,7 +200,7 @@ Deno.test("pointer create struct", () => { runtime.createTransparentReference( ptrObjImmutable, undefined, - DIFReferenceMutability.Mutable, + DIFSharedValueMutability.Mutable, ); }, Error, @@ -213,7 +213,7 @@ Deno.test("pointer create struct", () => { runtime.createTransparentReference( struct, undefined, - DIFReferenceMutability.Mutable, + DIFSharedValueMutability.Mutable, ); }, Error, @@ -264,10 +264,10 @@ Deno.test("pointer create and resolve", () => { // `Invalid`, // ); - const ptr = runtime.dif.createReferenceFromDIFValue( + const ptr = runtime.dif.createSharedValueFromDIFValue( { value: "unyt.org" }, undefined, - DIFReferenceMutability.Mutable, + DIFSharedValueMutability.Mutable, ); const resolved = runtime.dif.resolveDIFValueContainerSync( ptr, @@ -280,12 +280,12 @@ Deno.test("pointer object create and resolve", () => { [{ value: "a" }, { value: 123 }], [{ value: "b" }, { value: 456 }], ]; - const ptr = runtime.dif.createReferenceFromDIFValue( + const ptr = runtime.dif.createSharedValueFromDIFValue( { value: initialDIFValue, }, undefined, - DIFReferenceMutability.Mutable, + DIFSharedValueMutability.Mutable, ); console.log("ptr address", ptr); const loadedDIFValue = runtime.dif._handle.resolve_pointer_address_sync( @@ -297,11 +297,11 @@ Deno.test("pointer object create and resolve", () => { loadedDIFValue, { allowed_type: "0c0000", - mut: DIFReferenceMutability.Mutable, + mut: DIFSharedValueMutability.Mutable, value: { value: initialDIFValue, }, - } satisfies DIFReference, + } satisfies DIFSharedValue, ); }); @@ -359,7 +359,7 @@ Deno.test("pointer map create and cache", () => { difReferenceToDisplayString( runtime.dif._handle.resolve_pointer_address( ptrId, - ) as DIFReference, + ) as DIFSharedValue, ), ); @@ -396,46 +396,18 @@ Deno.test("pointer primitive ref update", () => { // get value of ptrObj from DATEX execution let result = runtime.executeSyncWithStringResult( - "$" + ptrObj.pointerAddress, + "'$" + ptrObj.pointerAddress, ); - assertEquals(result, "&mut 123f64"); + assertEquals(result, "shared mut 123f64"); // update the ref value ptrObj.value = 456; // get value of ptrObj from DATEX execution result = runtime.executeSyncWithStringResult( - "$" + ptrObj.pointerAddress, - ); - assertEquals(result, "&mut 456f64"); -}); - -Deno.test("immutable pointer primitive ref update", () => { - const val = 123; - const ptrObj = runtime.createTransparentReference( - val as number, - undefined, - DIFReferenceMutability.Immutable, - ); - if (!(ptrObj instanceof Ref)) { - throw new Error("Pointer object is not a Ref"); - } - assertEquals(ptrObj.value, val); - - // get value of ptrObj from DATEX execution - const result = runtime.executeSyncWithStringResult( - "$" + ptrObj.pointerAddress, - ); - assertEquals(result, "&123f64"); - - // update the ref value - assertThrows( - () => { - ptrObj.value = 456; - }, - Error, - `immutable reference`, + "'$" + ptrObj.pointerAddress, ); + assertEquals(result, "shared mut 456f64"); }); Deno.test("immutable pointer primitive ref update", () => { @@ -443,7 +415,7 @@ Deno.test("immutable pointer primitive ref update", () => { const ptrObj = runtime.createTransparentReference( val as number, undefined, - DIFReferenceMutability.Immutable, + DIFSharedValueMutability.Immutable, ); if (!(ptrObj instanceof Ref)) { throw new Error("Pointer object is not a Ref"); @@ -452,9 +424,9 @@ Deno.test("immutable pointer primitive ref update", () => { // get value of ptrObj from DATEX execution const result = runtime.executeSyncWithStringResult( - "$" + ptrObj.pointerAddress, + "'$" + ptrObj.pointerAddress, ); - assertEquals(result, "&123f64"); + assertEquals(result, "shared 123f64"); // update the ref value assertThrows( @@ -643,10 +615,10 @@ Deno.test("pointer primitive ref remote update and observe local", () => { }); Deno.test("observer immutable", () => { - const ref = runtime.dif.createReferenceFromDIFValue( + const ref = runtime.dif.createSharedValueFromDIFValue( { value: "Immutable" }, undefined, - DIFReferenceMutability.Immutable, + DIFSharedValueMutability.Immutable, ); assertThrows( () => { @@ -658,10 +630,10 @@ Deno.test("observer immutable", () => { }); Deno.test("pointer observe unobserve", () => { - const ref = runtime.dif.createReferenceFromDIFValue( + const ref = runtime.dif.createSharedValueFromDIFValue( { value: "42" }, undefined, - DIFReferenceMutability.Mutable, + DIFSharedValueMutability.Mutable, ); assertThrows( () => { diff --git a/test/runtime/execute.test.ts b/test/runtime/execute.test.ts index a4d8bebc..56f1d210 100644 --- a/test/runtime/execute.test.ts +++ b/test/runtime/execute.test.ts @@ -67,7 +67,7 @@ Deno.test("execute sync bigint", async () => { Deno.test("execute sync string", async () => { const runtime = await Runtime.create({ endpoint: "@jonas" }); - const result = runtime.executeSync("'lol'"); + const result = runtime.executeSync(`"lol"`); assertEquals(result, "lol"); }); @@ -98,7 +98,7 @@ Deno.test("execute sync none", async () => { Deno.test("execute sync object", async () => { const runtime = await Runtime.create({ endpoint: "@jonas" }); const result = runtime.executeSync>( - "{ a: 1, b: 'test' }", + '{ a: 1, b: "test" }', ); assertEquals( result,