Skip to content

Commit

Permalink
Merge pull request #90 from unyt-org/feat-sql-db
Browse files Browse the repository at this point in the history
Implement SQL storage location
  • Loading branch information
benStre authored Mar 2, 2024
2 parents d989e05 + 43d6b6b commit 1a2e507
Show file tree
Hide file tree
Showing 51 changed files with 2,846 additions and 1,422 deletions.
36 changes: 28 additions & 8 deletions compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import { Logger } from "../utils/logger.ts";
const logger = new Logger("datex compiler");

import { ReadableStream, Runtime, StaticScope} from "../runtime/runtime.ts";
import { ReadableStream, Runtime } from "../runtime/runtime.ts";
import { Endpoint, IdEndpoint, Target, WildcardTarget, Institution, Person, BROADCAST, target_clause, endpoints, LOCAL_ENDPOINT } from "../types/addressing.ts";
import { Pointer, PointerProperty, Ref } from "../runtime/pointers.ts";
import { CompilerError, RuntimeError, Error as DatexError, ValueError } from "../types/errors.ts";
Expand Down Expand Up @@ -45,8 +45,8 @@ import { client_type } from "../utils/constants.ts";
import { normalizePath } from "../utils/normalize-path.ts";
import { VolatileMap } from "../utils/volatile-map.ts";

await wasm_init();
wasm_init_runtime();
// await wasm_init();
// wasm_init_runtime();

export const activePlugins:string[] = [];

Expand Down Expand Up @@ -2860,12 +2860,32 @@ export class Compiler {
// handle pointers with transform (always ...)

// only if not ignore_first_collapse or, if ignore_first_collapse and keep_first_transform is enabled
if (!SCOPE.options.no_create_pointers && value instanceof Pointer && value.transform_scope && (value.force_local_transform || !skip_first_collapse || SCOPE.options.keep_first_transform)) {
SCOPE.options._first_insert_done = true; // set to true before next insert
if (!SCOPE.options.no_create_pointers && value instanceof Pointer && (value.force_local_transform || !skip_first_collapse || SCOPE.options.keep_first_transform)) {

if (value.transform_scope) {
SCOPE.options._first_insert_done = true; // set to true before next insert
Compiler.builder.insert_transform_scope(SCOPE, value.transform_scope);
return;
}

Compiler.builder.insert_transform_scope(SCOPE, value.transform_scope);

return;
else if (value.smart_transform_method) {
console.warn("DATEX serialization of JS transforms is not yet supported: ", value.smart_transform_method.toString())
// TODO:
// const isTransferableFn = value.smart_transform_method instanceof JSTransferableFunction;
// const transferableFn = isTransferableFn ? value.smart_transform_method as unknown as JSTransferableFunction : JSTransferableFunction.create(value.smart_transform_method);
// if (!isTransferableFn) transferableFn.source = `() => always(${transferableFn.source})`;

// value.smart_transform_method = transferableFn;
// Compiler.builder.handleRequiredBufferSize(SCOPE.b_index+2, SCOPE);
// SCOPE.uint8[SCOPE.b_index++] = BinaryCode.SUBSCOPE_START;
// SCOPE.uint8[SCOPE.b_index++] = BinaryCode.CREATE_POINTER;
// Compiler.builder.insert(transferableFn, SCOPE, is_root, parents, unassigned_children);
// Compiler.builder.handleRequiredBufferSize(SCOPE.b_index+2, SCOPE);
// SCOPE.uint8[SCOPE.b_index++] = BinaryCode.SUBSCOPE_END;
// SCOPE.uint8[SCOPE.b_index++] = BinaryCode.VOID;
// return;
}

}

// indirect reference pointer
Expand Down
19 changes: 16 additions & 3 deletions compiler/unit_codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ export const UnitAliases = {
} as const;


export type UnitAliasUnits = {
[k in keyof typeof UnitAliases]: typeof UnitAliases[k][2]
}

// reverse mapping for all aliases of a unit e.g. Unit.SECOND -> "min" | "h" | "d" | "a" | "yr"
export type UnitAliasesMap = {
[key in UnitAliasUnits[keyof UnitAliasUnits]]: {
[k in keyof UnitAliasUnits]: UnitAliasUnits[k] extends key ? k : never
}[keyof UnitAliasUnits]
}


// prefixes -----------------------------------------------------------------------------------------

Expand Down Expand Up @@ -282,8 +293,10 @@ export type unit_symbol = unit_base_symbol | keyof typeof UnitCodeBySymbolShortF
export type unit_prefix = keyof typeof UnitPrefixCodeBySymbol;


export type code_to_symbol = Combine<typeof UnitSymbol, typeof UnitSymbolShortFormsByCode>;
export type symbol_to_code = Combine<typeof UnitCodeBySymbol, typeof UnitAliases>;
export type code_to_symbol<C extends Unit> = typeof UnitSymbol[C] | typeof UnitSymbolShortFormsByCode[C & keyof typeof UnitSymbolShortFormsByCode];
export type symbol_to_code = typeof UnitCodeBySymbol & UnitAliasUnits & typeof UnitCodeBySymbolShortForms;

export type symbol_prefix_combinations<S extends string> = S|`${unit_prefix}${S}`

export type code_to_extended_symbol<C extends Unit = Unit> = C extends null ? string : code_to_symbol[C]|`${unit_prefix}${code_to_symbol[C]}`
export type code_to_extended_symbol<C extends Unit = Unit> = C extends null ? string : symbol_prefix_combinations<code_to_symbol<C>|UnitAliasesMap[C & keyof UnitAliasesMap]>
export type symbol_with_prefix = unit_symbol|`${unit_prefix}${unit_symbol}`
6 changes: 3 additions & 3 deletions datex_all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export * from "./runtime/runtime.ts";

// js_adapter
export * from "./js_adapter/js_class_adapter.ts";
export * from "./js_adapter/legacy_decorators.ts";
export * from "./js_adapter/decorators.ts";

// utils
export * from "./utils/global_types.ts";
export type * from "./utils/global_types.ts";
export * from "./utils/global_values.ts";
export * from "./utils/logger.ts";
export * from "./utils/observers.ts";
Expand Down Expand Up @@ -42,7 +42,7 @@ export * from "./runtime/cache_path.ts";
export * from "./storage/storage.ts";

// types
export * from "./types/abstract_types.ts";
export type * from "./types/abstract_types.ts";
export * from "./types/addressing.ts";
export * from "./types/assertion.ts";
export * from "./types/logic.ts";
Expand Down
26 changes: 18 additions & 8 deletions datex_short.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

// shortcut functions
// import { Datex } from "./datex.ts";
import { baseURL, Runtime, PrecompiledDXB, Type, Pointer, Ref, PointerProperty, primitive, any_class, Target, IdEndpoint, TransformFunctionInputs, AsyncTransformFunction, TransformFunction, Markdown, MinimalJSRef, RefOrValue, PartialRefOrValueObject, datex_meta, ObjectWithDatexValues, Compiler, endpoint_by_endpoint_name, endpoint_name, Storage, compiler_scope, datex_scope, DatexResponse, target_clause, ValueError, logger, Class, getUnknownMeta, Endpoint, INSERT_MARK, CollapsedValueAdvanced, CollapsedValue, SmartTransformFunction, compiler_options, activePlugins, METADATA, handleDecoratorArgs, RefOrValueObject, PointerPropertyParent, InferredPointerProperty, RefLike } from "./datex_all.ts";
import { baseURL, Runtime, PrecompiledDXB, Type, Pointer, Ref, PointerProperty, primitive, Target, IdEndpoint, Markdown, MinimalJSRef, RefOrValue, PartialRefOrValueObject, datex_meta, ObjectWithDatexValues, Compiler, endpoint_by_endpoint_name, endpoint_name, Storage, compiler_scope, datex_scope, DatexResponse, target_clause, ValueError, logger, Class, getUnknownMeta, Endpoint, INSERT_MARK, CollapsedValueAdvanced, CollapsedValue, SmartTransformFunction, compiler_options, activePlugins, METADATA, handleDecoratorArgs, RefOrValueObject, PointerPropertyParent, InferredPointerProperty, RefLike, dc } from "./datex_all.ts";

/** make decorators global */
import { assert as _assert, property as _property, sync as _sync, endpoint as _endpoint, template as _template, jsdoc as _jsdoc} from "./datex_all.ts";
import { assert as _assert, timeout as _timeout, entrypoint as _entrypoint, entrypointProperty as _entrypointProperty, property as _property, struct as _struct, endpoint as _endpoint, sync as _sync} from "./datex_all.ts";
import { effect as _effect, always as _always, reactiveFn as _reactiveFn, asyncAlways as _asyncAlways, toggle as _toggle, map as _map, equals as _equals, selectProperty as _selectProperty, not as _not } from "./functions.ts";
export * from "./functions.ts";
import { NOT_EXISTING, DX_SLOTS, SLOT_GET, SLOT_SET } from "./runtime/constants.ts";
Expand All @@ -22,10 +22,16 @@ declare global {
const property: typeof _property;
const assert: typeof _assert;

const jsdoc: typeof _jsdoc;
const sync: typeof _sync;
const struct: typeof _struct;
const endpoint: typeof _endpoint;
const entrypoint: typeof _entrypoint;
const entrypointProperty: typeof _entrypointProperty;
const timeout: typeof _timeout;
const always: typeof _always;
/**
* @deprecated Use struct(class {...}) instead;
*/
const sync: typeof _sync;
const asyncAlways: typeof _asyncAlways;
const reactiveFn: typeof _reactiveFn;
const toggle: typeof _toggle;
Expand Down Expand Up @@ -53,13 +59,17 @@ globalThis.property = _property;
globalThis.assert = _assert;

// @ts-ignore global
globalThis.sync = _sync;
globalThis.struct = _struct;
// @ts-ignore global
globalThis.endpoint = _endpoint;
// // @ts-ignore global
// globalThis.template = _template;
// @ts-ignore global
globalThis.jsdoc = _jsdoc;
globalThis.entrypoint = _entrypoint;
// @ts-ignore global
globalThis.entrypointProperty = _entrypointProperty;
// @ts-ignore global
globalThis.timeout = _timeout;
// @ts-ignore global
globalThis.sync = _sync;

// can be used instead of import(), calls a DATEX get instruction, works for urls, endpoint, ...
export async function get<T=unknown>(dx:string|URL|Endpoint, assert_type?:Type<T> | Class<T> | string, context_location?:URL|string, plugins?:string[]):Promise<T> {
Expand Down
41 changes: 39 additions & 2 deletions docs/manual/11 Types.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The DATEX Runtime comes with its own type system which can be mapped to JavaScri
DATEX types can be access via `Datex.Type`.

## Std types
The `Datex.Type.std` namespace contains all the builtin (*std*) DATEX types, e.g.:
The `Datex.Type.std` namespace contains all the builtin (*std*) DATEX types that can be accessed as runtime values, e.g.:
```ts
// primitive types
Datex.Type.std.text
Expand All @@ -27,6 +27,43 @@ Datex.Type.std.boolean === boolean
Datex.Type.std.Any === any
```

## Supported built-in JS and Web types
| **JS Type** | **Support** | **DATEX Type** | **Synchronizable** | **Limitations** |
|:-------------------------------|:----------------------|:---------------|:-------------------|:------------------------------------------------------------------------------------------|
| **string** | Full | std:text | Yes <sup>1)</sup> | <sup>3)</sup> |
| **number** | Full | std:decimal | Yes <sup>1)</sup> | <sup>3)</sup> |
| **bigint** | Full | std:integer | Yes <sup>1)</sup> | <sup>3)</sup> |
| **boolean** | Full | std:boolean | Yes <sup>1)</sup> | <sup>3)</sup> |
| **null** | Full | std:null | Yes <sup>1)</sup> | - |
| **undefined** | Full | std:void | Yes <sup>1)</sup> | - |
| **symbol** | Partial | js:Symbol | Yes <sup>1)</sup> | Registered and well-known symbols are not yet supported |
| **Object (without prototype)** | Full | std:Object | Yes | Objects with prototypes other than `Object.prototype` or `null` are mapped to `js:Object` |
| **Object** | Sufficient | js:Object | Yes | No synchronisation for nested objects per default |
| **Array** | Full | std:Array | Yes | - |
| **Set** | Full | std:Set | Yes | - |
| **Map** | Full | std:Map | Yes | - |
| **WeakSet** | None | - | - | Cannot be implemented because `WeakSet` internals are not accessible. Alternative: `StorageWeakSet` |
| **WeakMap** | None | - | - | Cannot be implemented because `WeakMap` internals are not accessible. Alternative: `StorageWeakMap` |
| **Function** | Sufficient | std:Function | No (Immutable) | Functions always return a Promise, even if they are synchronous |
| **AsyncFunction** | Sufficient | std:Function | No (Immutable) | - |
| **GeneratorFunction** | None | - | - | - |
| **ArrayBuffer** | Partial | std:buffer | No | ArrayBuffer mutations are currently not synchronized |
| **URL** | Partial | std:url | No | URL mutations are currently not synchronized |
| **Date** | Partial | std:time | No | `Date` objects are currently asymetrically mapped to DATEX `Time` objects |
| **RegExp** | Partial | js:RegExp | No (Immutable) | RegExp values wrapped in a Ref are currently not synchronized |
| **WeakRef** | Full | std:WeakRef | No (Immutable) | - |
| **Error** | Partial | std:Error | No | Error subclasses are not correctly mapped |
| **HTMLElement** | Partial <sup>2)</sup> | std:html | No | HTML element mutations are currently not synchronized |
| **SVGElement** | Partial <sup>2)</sup> | std:svg | No | SVG element mutations are currently not synchronized |
| **MathMLElement** | Partial <sup>2)</sup> | std:mathml | No | MathML element mutations are currently not synchronized |
| **Document** | Partial <sup>2)</sup> | std:htmldocument | No | Document mutations are currently not synchronized |
| **DocumentFragment** | Partial <sup>2)</sup> | std:htmlfragment | No | DocumentFragment mutations are currently not synchronized |


<sup>1)</sup> Primitive JS values are immutable and cannot be synchronized on their own, but when wrapped in a Ref.<br>
<sup>2)</sup> [UIX-DOM](https://github.com/unyt-org/uix-dom) required<br>
<sup>3)</sup> The corresponding object values of primitive values (e.g. `new Number()` for `number`) are not supported<br>

## Special JS types

Most builtin JavaScript types, like Map, Set or Array have equivalent types in the DATEX std library.
Expand Down Expand Up @@ -141,6 +178,6 @@ A struct definition accepts strings a keys and `Datex.Type`s,
JavaScript classes or other struct definitions as values.


## Mapping JS classes to DATEX types
## Mapping your own JS classes to DATEX types

Check out the chapter [11 Classes](./11%20Classes.md) for more information.
Loading

0 comments on commit 1a2e507

Please sign in to comment.