diff --git a/Cargo.lock b/Cargo.lock index 51122cc2..2912e98f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,7 +44,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "36f5e3dca4e09a6f340a61a0e9c7b61e030c69fc27bf29d73218f7e5e3b7638f" dependencies = [ - "unicode-width", + "unicode-width 0.1.14", "yansi", ] @@ -54,6 +54,12 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d62b7694a562cdf5a74227903507c56ab2cc8bdd1f781ed5cb4cf9c9f810bfc" +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + [[package]] name = "async-stream" version = "0.3.6" @@ -319,9 +325,7 @@ dependencies = [ [[package]] name = "datex-core" -version = "0.0.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43b45f844946d7722ef155e91be17f357c1384e2b001cb5820c8c63354da43dd" +version = "0.0.7" dependencies = [ "ariadne", "async-stream", @@ -335,7 +339,7 @@ dependencies = [ "chumsky", "console_error_panic_hook", "console_log", - "datex_macros", + "datex_macros 0.1.1", "default-args", "futures", "futures-core", @@ -344,6 +348,7 @@ dependencies = [ "gloo-timers 0.3.0", "hex", "indexmap 2.11.4", + "indoc 2.0.7", "internment", "itertools 0.14.0", "lazy_static", @@ -359,6 +364,7 @@ dependencies = [ "num_enum", "ordered-float", "pad", + "pretty", "ringmap", "serde", "serde-big-array", @@ -380,7 +386,7 @@ version = "0.0.8" dependencies = [ "async-trait", "datex-core", - "datex_macros", + "datex_macros 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "futures", "futures-channel", "futures-timer", @@ -399,6 +405,15 @@ dependencies = [ "web-sys", ] +[[package]] +name = "datex_macros" +version = "0.1.1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.106", +] + [[package]] name = "datex_macros" version = "0.1.1" @@ -854,6 +869,15 @@ version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfa799dd5ed20a7e349f3b4639aa80d74549c81716d9ec4f994c9b5815598306" +[[package]] +name = "indoc" +version = "2.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706" +dependencies = [ + "rustversion", +] + [[package]] name = "internment" version = "0.8.6" @@ -1169,7 +1193,7 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2ad9b889f1b12e0b9ee24db044b5129150d5eada288edc800f789928dc8c0e3" dependencies = [ - "unicode-width", + "unicode-width 0.1.14", ] [[package]] @@ -1224,6 +1248,17 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" +[[package]] +name = "pretty" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d22152487193190344590e4f30e219cf3fe140d9e7a3fdb683d82aa2c5f4156" +dependencies = [ + "arrayvec", + "typed-arena", + "unicode-width 0.2.2", +] + [[package]] name = "proc-macro-crate" version = "3.4.0" @@ -1526,7 +1561,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2240c3aa020aa61d2c569087d213baafbb212f4ceb9de9dd162376ea6aa0fe3" dependencies = [ "document-features", - "indoc", + "indoc 1.0.9", "once_cell", "paste", "serde", @@ -1817,6 +1852,12 @@ dependencies = [ "syn 2.0.106", ] +[[package]] +name = "typed-arena" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6af6ae20167a9ece4bcb41af5b80f8a1f1df981f6391189ce00fd257af04126a" + [[package]] name = "unicode-ident" version = "1.0.19" @@ -1835,6 +1876,12 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" +[[package]] +name = "unicode-width" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" + [[package]] name = "url" version = "2.5.7" diff --git a/rs-lib/Cargo.toml b/rs-lib/Cargo.toml index c3251a43..0980f9a2 100644 --- a/rs-lib/Cargo.toml +++ b/rs-lib/Cargo.toml @@ -20,7 +20,7 @@ panic = "abort" [dependencies] log = { version = "0.4", features = ["std", "serde"] } -datex-core = { version = "0.0.6", default-features = false, features = [ +datex-core = { version = "0.0.7", default-features = false, features = [ "std", "serde", "wasm_logger", diff --git a/rs-lib/src/runtime.rs b/rs-lib/src/runtime.rs index 948ecacf..c2f13495 100644 --- a/rs-lib/src/runtime.rs +++ b/rs-lib/src/runtime.rs @@ -3,7 +3,7 @@ use crate::js_utils::{js_array, js_error}; use crate::network::com_hub::JSComHub; use crate::utils::time::TimeJS; use datex_core::crypto::crypto::CryptoTrait; -use datex_core::decompiler::{DecompileOptions, decompile_value}; +use datex_core::decompiler::{DecompileOptions, decompile_value, FormattingMode}; use datex_core::dif::interface::{ DIFApplyError, DIFCreatePointerError, DIFInterface, DIFObserveError, DIFResolveReferenceError, DIFUpdateError, @@ -411,6 +411,7 @@ impl JSRuntime { from_value(decompile_options).unwrap_or_default(); DecompileOptions { formatting: Default::default(), + formatting_mode: FormattingMode::Pretty, colorized: js_decompile_options.colorized.unwrap_or(false), resolve_slots: js_decompile_options .resolve_slots diff --git a/src/datex-core/datex_core_js.d.ts b/src/datex-core/datex_core_js.d.ts index b6f9f426..61897543 100644 --- a/src/datex-core/datex_core_js.d.ts +++ b/src/datex-core/datex_core_js.d.ts @@ -2,21 +2,47 @@ // deno-lint-ignore-file // deno-fmt-ignore-file -/** - * Executes a Datex script and returns the result as a string. - */ -export function execute(datex_script: string, formatted: boolean): 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; +/** + * Executes a Datex script and returns the result as a string. + */ +export function execute(datex_script: string, formatted: boolean): string; export function create_runtime(config: string, debug_flags: any): JSRuntime; +export interface WebSocketServerInterfaceSetupData { + port: number; + /** + * if true, the server will use wss (secure WebSocket). Defaults to true. + */ + secure: boolean | null; +} + +export interface WebSocketClientInterfaceSetupData { + address: string; +} + +export interface TCPServerInterfaceSetupData { + port: number; +} + +export interface TCPClientInterfaceSetupData { + address: string; +} + +export interface RTCIceServer { + urls: string[]; + username: string | null; + credential: string | null; +} + export type BaseInterfaceSetupData = InterfaceProperties; -export interface WebRTCInterfaceSetupData { - peer_endpoint: string; - ice_servers: RTCIceServer[] | null; +export interface SerialInterfaceSetupData { + port_name: string | null; + baud_rate: number; } export type ReconnectionConfig = "NoReconnect" | "InstantReconnect" | { @@ -28,6 +54,8 @@ export type ReconnectionConfig = "NoReconnect" | "InstantReconnect" | { }; }; +export type InterfaceDirection = "In" | "Out" | "InOut"; + export interface InterfaceProperties { /** * the type of the interface, by which it is identified @@ -90,37 +118,9 @@ export interface InterfaceProperties { reconnect_attempts: number | null; } -export type InterfaceDirection = "In" | "Out" | "InOut"; - -export interface WebSocketServerInterfaceSetupData { - port: number; - /** - * if true, the server will use wss (secure WebSocket). Defaults to true. - */ - secure: boolean | null; -} - -export interface WebSocketClientInterfaceSetupData { - address: string; -} - -export interface RTCIceServer { - urls: string[]; - username: string | null; - credential: string | null; -} - -export interface TCPClientInterfaceSetupData { - address: string; -} - -export interface TCPServerInterfaceSetupData { - port: number; -} - -export interface SerialInterfaceSetupData { - port_name: string | null; - baud_rate: number; +export interface WebRTCInterfaceSetupData { + peer_endpoint: string; + ice_servers: RTCIceServer[] | null; } export class BaseJSInterface { @@ -130,12 +130,24 @@ export class BaseJSInterface { export class JSComHub { private constructor(); free(): void; - get_metadata_string(): string; - register_default_interface_factories(): void; - close_interface(interface_uuid: string): Promise; - create_interface(interface_type: string, properties: string): Promise; - update(): Promise; - get_trace_string(endpoint: string): Promise; + webrtc_interface_set_answer( + interface_uuid: string, + answer: Uint8Array, + ): Promise; + webrtc_interface_create_offer(interface_uuid: string): Promise; + webrtc_interface_create_answer( + interface_uuid: string, + offer: Uint8Array, + ): Promise; + webrtc_interface_add_ice_candidate( + interface_uuid: string, + candidate: Uint8Array, + ): Promise; + webrtc_interface_wait_for_connection(interface_uuid: string): Promise; + webrtc_interface_set_on_ice_candidate( + interface_uuid: string, + on_ice_candidate: Function, + ): void; /** * Send a block to the given interface and socket * This does not involve the routing on the ComHub level. @@ -147,42 +159,30 @@ export class JSComHub { interface_uuid: string, socket_uuid: string, ): Promise; + close_interface(interface_uuid: string): Promise; + create_interface(interface_type: string, properties: string): Promise; + get_trace_string(endpoint: string): Promise; + get_metadata_string(): string; _drain_incoming_blocks(): Uint8Array[]; - websocket_server_interface_add_socket( - interface_uuid: string, - websocket: WebSocket, - ): string; - base_interface_test_send_block( - uuid: string, - socket_uuid: string, - data: Uint8Array, - ): Promise; + register_default_interface_factories(): void; + update(): Promise; + base_interface_on_send(uuid: string, func: Function): void; base_interface_receive( uuid: string, socket_uuid: string, data: Uint8Array, ): void; - base_interface_register_socket(uuid: string, direction: string): string; base_interface_destroy_socket(uuid: string, socket_uuid: string): void; - base_interface_on_send(uuid: string, func: Function): void; - webrtc_interface_create_answer( - interface_uuid: string, - offer: Uint8Array, - ): Promise; - webrtc_interface_add_ice_candidate( - interface_uuid: string, - candidate: Uint8Array, - ): Promise; - webrtc_interface_wait_for_connection(interface_uuid: string): Promise; - webrtc_interface_set_on_ice_candidate( - interface_uuid: string, - on_ice_candidate: Function, - ): void; - webrtc_interface_set_answer( + base_interface_register_socket(uuid: string, direction: string): string; + base_interface_test_send_block( + uuid: string, + socket_uuid: string, + data: Uint8Array, + ): Promise; + websocket_server_interface_add_socket( interface_uuid: string, - answer: Uint8Array, - ): Promise; - webrtc_interface_create_offer(interface_uuid: string): Promise; + websocket: WebSocket, + ): string; } export class JSPointer { private constructor(); @@ -191,61 +191,61 @@ export class JSPointer { export class JSRuntime { private constructor(); free(): void; - /** - * Get a handle to the DIF interface of the runtime - */ - dif(): RuntimeDIFHandle; - _stop(): Promise; - crypto_test_tmp(): Promise>; + execute_sync(script: string, dif_values?: any[] | null): any; _create_block( body: Uint8Array | null | undefined, receivers: string[], ): Uint8Array; + crypto_test_tmp(): Promise>; value_to_string(dif_value: any, decompile_options: any): string; - execute(script: string, dif_values?: any[] | null): Promise; - start(): Promise; - execute_sync_with_string_result( + execute_with_string_result( script: string, dif_values: any[] | null | undefined, decompile_options: any, - ): string; - execute_sync(script: string, dif_values?: any[] | null): any; - execute_with_string_result( + ): Promise; + execute_sync_with_string_result( script: string, dif_values: any[] | null | undefined, decompile_options: any, - ): Promise; - readonly version: string; + ): string; + /** + * Get a handle to the DIF interface of the runtime + */ + dif(): RuntimeDIFHandle; + _stop(): Promise; + start(): Promise; + execute(script: string, dif_values?: any[] | null): Promise; com_hub: JSComHub; + readonly version: string; readonly endpoint: string; } export class RuntimeDIFHandle { private constructor(); free(): void; - /** - * Resolve a pointer address synchronously if it's in memory, otherwise return an error - */ - resolve_pointer_address_sync(address: string): any; - unobserve_pointer(address: string, observer_id: number): void; - apply(callee: any, value: any): any; - update_observer_options( + create_pointer(value: any, allowed_type: any, mutability: number): string; + observe_pointer( + transceiver_id: number, address: string, - observer_id: number, observe_options: any, - ): void; - create_pointer(value: any, allowed_type: any, mutability: number): string; + callback: Function, + ): number; + unobserve_pointer(address: string, observer_id: number): void; /** * Resolve a pointer address, returning a Promise * If the pointer is in memory, the promise resolves immediately * If the pointer is not in memory, it will be loaded first */ resolve_pointer_address(address: string): any; - observe_pointer( - transceiver_id: number, + update_observer_options( address: string, + observer_id: number, observe_options: any, - callback: Function, - ): number; + ): void; + /** + * Resolve a pointer address synchronously if it's in memory, otherwise return an error + */ + resolve_pointer_address_sync(address: string): any; + apply(callee: any, value: any): any; update(transceiver_id: number, address: string, update: any): void; } export class WebSocketServerRegistry { diff --git a/src/dif/definitions.ts b/src/dif/definitions.ts index b54bc7cc..cd4e4e82 100644 --- a/src/dif/definitions.ts +++ b/src/dif/definitions.ts @@ -73,7 +73,6 @@ export type DIFTypeKind = typeof DIFTypeKinds[keyof typeof DIFTypeKinds]; export const DIFReferenceMutability = { Mutable: 0, Immutable: 1, - Final: 2, } as const; export type DIFReferenceMutability = typeof DIFReferenceMutability[keyof typeof DIFReferenceMutability]; diff --git a/src/dif/dif-handler.ts b/src/dif/dif-handler.ts index 35492921..b49f5591 100644 --- a/src/dif/dif-handler.ts +++ b/src/dif/dif-handler.ts @@ -592,9 +592,9 @@ export class DIFHandler { allowedType, ); - // if not final, observe to keep the pointer 'live' and receive updates + // if not immutable, observe to keep the pointer 'live' and receive updates let observerId: number | null = null; - if (mutability !== DIFReferenceMutability.Final) { + if (mutability !== DIFReferenceMutability.Immutable) { observerId = this.observePointerBindDirect( ptrAddress, (update) => { diff --git a/src/dif/display.ts b/src/dif/display.ts index 77cd14e6..43ed59ca 100644 --- a/src/dif/display.ts +++ b/src/dif/display.ts @@ -10,8 +10,6 @@ import { export function mutabilityToDisplayString(mut: DIFReferenceMutability): string { if (mut === DIFReferenceMutability.Mutable) { return "&mut "; - } else if (mut === DIFReferenceMutability.Final) { - return "&final "; } else if (mut === DIFReferenceMutability.Immutable) { return "&"; } diff --git a/src/runtime/runtime.ts b/src/runtime/runtime.ts index bedb9383..e118961c 100644 --- a/src/runtime/runtime.ts +++ b/src/runtime/runtime.ts @@ -317,10 +317,10 @@ type IsPlainObject = T extends Builtins ? false type ObjectFieldOut = T extends Ref - ? M extends typeof DIFReferenceMutability.Final ? Ref : AssignableRef + ? M extends typeof DIFReferenceMutability.Immutable ? Ref : AssignableRef : IsPlainObject extends true ? ( ContainsRef extends true - ? M extends typeof DIFReferenceMutability.Final + ? M extends typeof DIFReferenceMutability.Immutable ? { readonly [K in keyof T]: ObjectFieldOut } : { [K in keyof T]: ObjectFieldOut } : { readonly [K in keyof T]: ObjectFieldOut } @@ -328,16 +328,16 @@ type ObjectFieldOut = T extends : T; type PointerOut = V extends Ref - ? M extends typeof DIFReferenceMutability.Final ? Ref : AssignableRef + ? M extends typeof DIFReferenceMutability.Immutable ? Ref : AssignableRef : IsPlainObject extends true ? ( ContainsRef extends true - ? M extends typeof DIFReferenceMutability.Final + ? M extends typeof DIFReferenceMutability.Immutable ? { readonly [K in keyof V]: ObjectFieldOut } : { [K in keyof V]: ObjectFieldOut } : { readonly [K in keyof V]: ObjectFieldOut } ) : V extends Builtins ? Pointer - : M extends typeof DIFReferenceMutability.Final ? Ref + : M extends typeof DIFReferenceMutability.Immutable ? Ref : Ref>; type CollectionProps = { diff --git a/test/runtime/dif.test.ts b/test/runtime/dif.test.ts index 81e537a8..86b3b1bf 100644 --- a/test/runtime/dif.test.ts +++ b/test/runtime/dif.test.ts @@ -82,7 +82,7 @@ Deno.test("pointer create primitive", () => { runtime.createPointer( 42, undefined, - DIFReferenceMutability.Final, + DIFReferenceMutability.Immutable, ) satisfies Ref<42>; runtime.createPointer( @@ -94,7 +94,7 @@ Deno.test("pointer create primitive", () => { runtime.createPointer( "hello world", undefined, - DIFReferenceMutability.Final, + DIFReferenceMutability.Immutable, ) satisfies Ref<"hello world">; runtime.createPointer( @@ -106,18 +106,18 @@ Deno.test("pointer create primitive", () => { runtime.createPointer( true, undefined, - DIFReferenceMutability.Final, + DIFReferenceMutability.Immutable, ) satisfies Ref; runtime.createPointer( { x: true } as const, undefined, - DIFReferenceMutability.Final, + DIFReferenceMutability.Immutable, ) satisfies { readonly x: true; }; - const a = runtime.createPointer(5, undefined, DIFReferenceMutability.Final); + const a = runtime.createPointer(5, undefined, DIFReferenceMutability.Immutable); const b = runtime.createPointer( { x: a }, undefined, @@ -141,7 +141,7 @@ Deno.test("pointer create struct", () => { const ptrObjFinal = runtime.createPointer( struct, undefined, - DIFReferenceMutability.Final, + DIFReferenceMutability.Immutable, ); ptrObjFinal.e satisfies { readonly f: Ref | number }; } @@ -371,12 +371,12 @@ Deno.test("immutable pointer primitive ref update", () => { ); }); -Deno.test("final pointer primitive ref update", () => { +Deno.test("immutable pointer primitive ref update", () => { const val = 123; const ptrObj = runtime.createPointer( val as number, undefined, - DIFReferenceMutability.Final, + DIFReferenceMutability.Immutable, ); if (!(ptrObj instanceof Ref)) { throw new Error("Pointer object is not a Ref"); @@ -387,7 +387,7 @@ Deno.test("final pointer primitive ref update", () => { const result = runtime.executeSyncWithStringResult( "$" + ptrObj.pointerAddress, ); - assertEquals(result, "&final 123f64"); + assertEquals(result, "&123f64"); // update the ref value assertThrows( @@ -559,18 +559,18 @@ Deno.test("pointer primitive ref remote update and observe local", () => { assertEquals(ptrObj.value, 456); }); -Deno.test("observer final", () => { +Deno.test("observer immutable", () => { const ref = runtime.dif.createPointer( - { value: "Final" }, + { value: "Immutable" }, undefined, - DIFReferenceMutability.Final, + DIFReferenceMutability.Immutable, ); assertThrows( () => { runtime.dif.observePointerBindDirect(ref, (_) => {}); }, Error, - `final reference`, + `immutable reference`, ); });