Skip to content

Commit

Permalink
Merge pull request #74 from unyt-org/refactor-com-interfaces
Browse files Browse the repository at this point in the history
Refactor com interfaces
  • Loading branch information
benStre authored Feb 10, 2024
2 parents 9fae977 + b945f57 commit 8a47455
Show file tree
Hide file tree
Showing 51 changed files with 3,700 additions and 1,907 deletions.
5 changes: 0 additions & 5 deletions .vscode/settings.json

This file was deleted.

59 changes: 37 additions & 22 deletions compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export class Compiler {

static SIGN_DEFAULT = true; // can be changed

static BIG_BANG_TIME = new Date(2022, 0, 22, 0, 0, 0, 0).getTime() // 1642806000000
static BIG_BANG_TIME = Date.UTC(2024, 0, 0, 0, 0, 0, 0)
static MAX_INT_32 = 2_147_483_647;
static MIN_INT_32 = -2_147_483_648;

Expand Down Expand Up @@ -416,12 +416,6 @@ export class Compiler {
return parseInt(binary, 2);
}

/** Set TTL of header of existing block */
public static setHeaderTTL(dx_block:ArrayBuffer, ttl:number):ArrayBuffer {
const uint8 = new Uint8Array(dx_block);
uint8[4] = ttl;
return uint8.buffer;
}

// get sender from header
public static extractHeaderSender(dx_block: ArrayBuffer, last_byte?:[number], _appspace_byte = true, _start = 8): Endpoint|undefined {
Expand Down Expand Up @@ -606,7 +600,7 @@ export class Compiler {
}

/** Add a header to a Datex block */
public static DEFAULT_TTL = 64;
public static DEFAULT_TTL = 10;

private static device_types = {
"default": 0,
Expand Down Expand Up @@ -783,8 +777,8 @@ export class Compiler {
// ROUTING HEADER /////////////////////////////////////////////////
// ttl
pre_header_uint8[i++] = __routing_ttl;
// priority
pre_header_uint8[i++] = __routing_prio;
// initial ttl (originally: prio, currently unused)
pre_header_uint8[i++] = __routing_ttl; //__routing_prio;

// signed = 1, encrypted+signed = 2, encrypted = 3, others = 0
pre_header_uint8[i++] = sign && !encrypt ? 1: (sign && encrypt ? 2 : (!sign && encrypt ? 3 : 0));
Expand Down Expand Up @@ -2188,20 +2182,25 @@ export class Compiler {
const pointer_origin = (id_buffer[0]==BinaryCode.ENDPOINT || id_buffer[0]==BinaryCode.PERSON_ALIAS || id_buffer[0]==BinaryCode.INSTITUTION_ALIAS) ? <IdEndpoint> Target.get(id_buffer.slice(1,19), id_buffer.slice(19,21), id_buffer[0]) : null;

const singleReceiver =
SCOPE.options.to instanceof Endpoint ||
(
SCOPE.options.to instanceof Disjunction &&
SCOPE.options.to.size == 1 &&
[...SCOPE.options.to][0] instanceof Endpoint
)
SCOPE.options.to instanceof Endpoint ?
SCOPE.options.to :
(
(
SCOPE.options.to instanceof Disjunction &&
SCOPE.options.to.size == 1 &&
[...SCOPE.options.to][0] instanceof Endpoint
) ?
[...SCOPE.options.to][0] as Endpoint :
null
)

if (
pointer_origin &&
SCOPE.options.preemptive_pointer_init !== false && // preemptive_pointer_init enabled
action_type == ACTION_TYPE.GET && // is get
Runtime.endpoint.equals(pointer_origin) && // is own pointer
SCOPE.options.to != Runtime.endpoint && // not sending to self
!Pointer.get(id)?.subscribers?.has(singleReceiver) // receiver is subscribed to pointer - assume it already has the current pointer value
!(singleReceiver && Pointer.get(id)?.subscribers?.has(singleReceiver)) // receiver is subscribed to pointer - assume it already has the current pointer value
) {
return Compiler.builder.addPreemptivePointer(SCOPE, id)
}
Expand Down Expand Up @@ -2259,10 +2258,11 @@ export class Compiler {

// preemptive value already exists and was not yet initialized in scope
if (ptr?.value_initialized && !alreadyInitializing) {

parentScope.preemptive_pointers.set(normalized_id, SCOPE);
Compiler.builder.handleRequiredBufferSize(SCOPE.b_index+1, SCOPE);
SCOPE.uint8[SCOPE.b_index++] = BinaryCode.SUBSCOPE_START;
Compiler.builder.addPointerNormal(SCOPE, id, ACTION_TYPE.INIT, undefined, true, ptr.val, (ptr.force_local_transform && ptr.transform_scope) ? ptr.transform_scope : undefined); // sync
Compiler.builder.addPointerNormal(SCOPE, id, ACTION_TYPE.INIT, undefined, true, ptr, (ptr.force_local_transform && ptr.transform_scope) ? ptr.transform_scope : undefined); // sync
Compiler.builder.handleRequiredBufferSize(SCOPE.b_index+1, SCOPE);
SCOPE.uint8[SCOPE.b_index++] = BinaryCode.CLOSE_AND_STORE;
Compiler.builder.addPointerNormal(SCOPE, id, ACTION_TYPE.GET, undefined, undefined, ptr.val); // sync
Expand Down Expand Up @@ -2315,7 +2315,14 @@ export class Compiler {
if (Runtime.OPTIONS.PROTECT_POINTERS) {
const receiver = Compiler.builder.getScopeReceiver(SCOPE);
if (receiver !== Runtime.endpoint) {
p.grantAccessTo(receiver)
if (receiver instanceof Endpoint) p.grantAccessTo(receiver)
else if (receiver instanceof Disjunction) {
for (const endpoint of receiver) {
if (endpoint instanceof Endpoint) p.grantAccessTo(endpoint)
else logger.error("Can't grant access to receiver:" + endpoint);
}
}
else logger.error("Can't grant access to receivers:" + receiver);
}
}

Expand All @@ -2324,7 +2331,7 @@ export class Compiler {
Compiler.builder.insertExtractedVariable(<compiler_scope>SCOPE, BinaryCode.POINTER, buffer2hex(p.id_buffer));
}
// add normally
else return Compiler.builder.addPointerByID (SCOPE, p.id_buffer, action_type, action_specifier, undefined, NOT_EXISTING)
else return Compiler.builder.addPointerByID(SCOPE, p.id_buffer, action_type, action_specifier, undefined, NOT_EXISTING)
},

// add <Array>
Expand Down Expand Up @@ -2751,6 +2758,8 @@ export class Compiler {

if (value?.[DX_REPLACE]) value = value[DX_REPLACE];

const indirectReferencePtr = (value instanceof Pointer && value.indirectReference) ? true : false;

// make sure normal pointers are collapsed (ignore error if uninitialized pointer is passed in)
try {
value = Ref.collapseValue(value);
Expand Down Expand Up @@ -2808,7 +2817,7 @@ export class Compiler {
const start_index = Compiler.builder.getDynamicIndex(SCOPE.b_index, SCOPE);

// add original value to inserted values map (only if useful, exclude short values like boolean and null)
if (!(SCOPE.options.no_duplicate_value_optimization && (typeof value == "bigint" || typeof value == "number" || typeof value == "string")) && value!==VOID &&
if (!indirectReferencePtr && !(SCOPE.options.no_duplicate_value_optimization && (typeof value == "bigint" || typeof value == "number" || typeof value == "string")) && value!==VOID &&
value !==null &&
typeof value != "boolean" &&
!((typeof value == "bigint" || typeof value == "number") && value<=Compiler.MAX_INT_32 && value>=Compiler.MIN_INT_32)
Expand Down Expand Up @@ -2845,7 +2854,6 @@ export class Compiler {
const option_collapse = SCOPE.options.collapse_pointers && !(SCOPE.options.keep_external_pointers && value instanceof Pointer && !value.is_origin);
const no_proxify = value instanceof Ref && (((value instanceof Pointer && value.is_anonymous) || option_collapse) || skip_first_collapse);


// proxify pointer exceptions:
if (no_proxify) {

Expand All @@ -2860,6 +2868,13 @@ export class Compiler {
return;
}

// indirect reference pointer
if (indirectReferencePtr) {
SCOPE.options._first_insert_done = true;
Compiler.builder.insert(Ref.collapseValue(value, true), SCOPE, is_root, parents, unassigned_children);
return;
}


value = value.val; // don't proxify anonymous pointers or serialize ptr
// add $$ operator, not if no_create_pointers enabled or skip_first_collapse
Expand Down
5 changes: 2 additions & 3 deletions datex_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export * from "./compiler/unit_codes.ts";


// network
export * from "./network/client.ts";
export * from "./network/supranet.ts";
export * from "./network/network_utils.ts";
export * from "./network/unyt.ts";
Expand Down Expand Up @@ -61,8 +60,8 @@ export * from "./types/tuple.ts";
export * from "./types/type.ts";
export * from "./types/quantity.ts";
export * from "./types/time.ts";
export * from "./types/storage_map.ts";
export * from "./types/storage_set.ts";
export * from "./types/storage-map.ts";
export * from "./types/storage-set.ts";
export * from "./types/struct.ts";

// polyfills
Expand Down
30 changes: 28 additions & 2 deletions datex_short.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { eternals, getLazyEternal, waitingEternals, waitingLazyEternals } from "

import {instance} from "./js_adapter/js_class_adapter.ts";
import { client_type } from "./utils/constants.ts";
import { communicationHub } from "./network/communication-hub.ts";
import { MessageLogger } from "./utils/message_logger.ts";
export {instance} from "./js_adapter/js_class_adapter.ts";

declare global {
Expand Down Expand Up @@ -139,7 +141,7 @@ function _datex(dx:string|TemplateStringsArray|PrecompiledDXB, data?:unknown[],
// local execution
if (to === Runtime.endpoint) return Runtime.executeDatexLocally(dx, data, {plugins, sign, encrypt}, context_location ? new URL(context_location.toString()) : undefined);
// remote execution
else return Runtime.datexOut([dx, data, {plugins, sign, encrypt, context_location: context_location ? new URL(context_location.toString()) : undefined}], typeof to == "string" ? f(<endpoint_name>to) : to, undefined, undefined, undefined, undefined, undefined, undefined, timeout);
else return Runtime.datexOut([dx, data, {plugins, sign, encrypt, context_location: context_location ? new URL(context_location.toString()) : undefined}], typeof to == "string" ? f(<endpoint_name>to) : to, undefined, undefined, undefined, undefined, undefined, timeout);

}

Expand Down Expand Up @@ -525,11 +527,29 @@ export function printTrace(endpoint: string|Endpoint) {
endpoint = typeof endpoint == "string" ? Target.get(endpoint) as Endpoint : endpoint;
return endpoint.printTrace()
}

type printTraceT = typeof printTrace;

export function printComStatus() {
return communicationHub.printStatus()
}
type printComStatusT = typeof printComStatus;

export function enableMessageLogger(showRedirectMessages?: boolean) {
return MessageLogger.enable(showRedirectMessages)
}
type enableMessageLoggerT = typeof enableMessageLogger;

export function disableMessageLogger() {
return MessageLogger.disable()
}
type disableMessageLoggerT = typeof disableMessageLogger;

declare global {
const printTrace: printTraceT;
const printComStatus: printComStatusT;
const printSnapshot: typeof Storage.printSnapshot
const enableMessageLogger: enableMessageLoggerT;
const disableMessageLogger: disableMessageLoggerT;
}


Expand Down Expand Up @@ -658,6 +678,12 @@ globalThis.f = f;
// @ts-ignore
globalThis.printTrace = printTrace;
// @ts-ignore
globalThis.printComStatus = printComStatus;
// @ts-ignore
globalThis.printSnapshot = Storage.printSnapshot.bind(Storage);
// @ts-ignore
globalThis.enableMessageLogger = enableMessageLogger;
// @ts-ignore
globalThis.disableMessageLogger = disableMessageLogger;
// @ts-ignore
globalThis.props = props;
2 changes: 1 addition & 1 deletion docs/manual/01 Introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> [!WARNING]
> The current implementation of the DATEX JavaScript Library is still a beta version.
> We are activly working on a new stable [Rust implementation](https://github.com/unyt-org/datex-core) that will be much more performant.
> We are actively working on a new stable [Rust implementation](https://github.com/unyt-org/datex-core) that will be much more performant.
The DATEX JavaScript Library (*DATEX JS*) enables fine-grained reactivity with cross-device processing including data synchronisation.

Expand Down
2 changes: 1 addition & 1 deletion functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export function toggle<T>(value:RefLike<boolean>, if_true:T, if_false:T): Minima
always (
if (${Runtime.valueToDatexString(value)}) (${Runtime.valueToDatexString(if_true)})
else (${Runtime.valueToDatexString(if_false)})
)`*/).js_value;
)`*/);
}


Expand Down
100 changes: 0 additions & 100 deletions iframes/iframe-com-interface.ts

This file was deleted.

10 changes: 7 additions & 3 deletions iframes/iframe-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
*/

import { Datex } from "../datex.ts";
import "./iframe-com-interface.ts";
import { communicationHub } from "../network/communication-hub.ts";
import { WindowInterface } from "../network/communication-interfaces/window-interface.ts";

await Datex.Supranet.connect();
await Datex.InterfaceManager.connect("iframe", undefined, [parent])
await Datex.Supranet.init();
const windowInterface = WindowInterface.createParentInterface(window.parent)
// use as default interface only if no other default interface active
const useAsDefaultInterface = !communicationHub.defaultSocket
await communicationHub.addInterface(windowInterface, useAsDefaultInterface)
Loading

0 comments on commit 8a47455

Please sign in to comment.