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
35 changes: 25 additions & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
with:
deno-version: v2.x
- name: Build
run: deno task debug
run: deno task debug-no-opt
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
Expand All @@ -24,25 +24,22 @@ jobs:
./src/datex-web/datex_web.internal.js
./src/datex-web/datex_web.wasm
retention-days: 5

test:
runs-on: ubuntu-latest
name: Test
lint:
name: Lint
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: "true"
- uses: denoland/setup-deno@v2
with:
deno-version: v2.x
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: build
path: ./src/datex-web

- uses: denoland/setup-deno@v2
with:
deno-version: v2.x

- name: Check formatting
run: deno fmt --check

Expand All @@ -52,6 +49,24 @@ jobs:
- name: Type Checking
run: deno check -I ./src ./test

test:
runs-on: ubuntu-latest
name: Test
needs: build
steps:
- uses: actions/checkout@v4
with:
submodules: "true"
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: build
path: ./src/datex-web

- uses: denoland/setup-deno@v2
with:
deno-version: v2.x

- name: Run Tests
run: deno task test-no-build

Expand Down
26 changes: 15 additions & 11 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion datex-browser-demo/package-lock.json

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

10 changes: 8 additions & 2 deletions datex-browser-demo/src/demo/demo-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Ref, Runtime } from "datex";
import { Endpoint, Range, Ref, Runtime } from "datex";

export const runtime = await Runtime.create(
{
interfaces: [
{
type: "websocket-client",
config: {
url: "ws://localhost:8043",
url: "wss://example.unyt.land",
},
},
],
Expand All @@ -25,3 +25,9 @@ runtime.comHub.printMetadata();
globalThis.Datex = runtime;
// @ts-ignore global variable for debugging
globalThis.Ref = Ref;

// @ts-ignore global variable for debugging
globalThis.Range = Range;

// @ts-ignore global variable for debugging
globalThis.Endpoint = Endpoint;
6 changes: 3 additions & 3 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
]
},
"tasks": {
"debug": "deno run -A scripts/build-wasm.ts --profile debug --features debug",
"debug": "deno run -A scripts/build-wasm.ts --profile debug",
"release": "deno run -A scripts/build-wasm.ts --profile release",
"debug-unsecure": "deno run -A scripts/build-wasm.ts --profile debug --features debug,allow_unsigned_blocks",
"debug-unsecure": "deno run -A scripts/build-wasm.ts --profile debug --features allow_unsigned_blocks",

"debug-no-opt": "deno task debug --no-opt",
"test": "deno task debug-no-opt && deno test -A",
Expand All @@ -31,7 +31,7 @@
"browser-demo-no-build": "deno task build-npm && cd datex-browser-demo && npm install && npm run dev",

"fmt": "deno fmt && cargo fmt",
"lint": "deno lint --fix && cargo clippy --fix --features debug"
"lint": "deno lint --fix && cargo clippy --fix"
},
"fmt": {
"indentWidth": 4,
Expand Down
7 changes: 2 additions & 5 deletions rs-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ panic = "abort"

[dependencies]
log = { version = "0.4", features = ["std", "serde"] }
datex-core = { version = "0.0.10", default-features = false, features = [
datex-core = { git = "https://github.com/unyt-org/datex", branch = "release/0.0.11", version = "0.0.11", default-features = false, features = [
"target_wasm",
"compiler",
"decompiler",
"syntax_highlighting_legacy",
"lsp_wasm",
] }
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" }
datex-crypto-facade = { git = "https://github.com/unyt-org/datex", branch = "release/0.0.11", version = "0.0.2" }

async-trait = "0.1.87"
url = { version = "2.5.7" }
Expand Down Expand Up @@ -111,7 +109,6 @@ default = [
"webrtc",
"lsp", # Make optional
]
debug = []
lsp = ["datex-core/lsp_wasm"]

allow_unsigned_blocks = ["datex-core/allow_unsigned_blocks"]
Expand Down
1 change: 1 addition & 0 deletions src/dif/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const CoreTypeAddress = {
map: "0c0000",
never: "0d0000",
unknown: "0e0000",
range: "100000",
decimal: "2c0100",
decimal_f32: "2d0100",
decimal_f64: "2e0100",
Expand Down
23 changes: 23 additions & 0 deletions src/dif/dif-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { JSRuntime, RuntimeDIFHandle } from "../datex.ts";
import { Ref } from "../refs/ref.ts";
import { Endpoint } from "../lib/special-core-types/endpoint.ts";
import { Range } from "../lib/special-core-types/range.ts";
import {
type DIFArray,
type DIFMap,
Expand Down Expand Up @@ -410,6 +411,20 @@ export class DIFHandler {
} // endpoint types are resolved to Endpoint instances
else if (type === CoreTypeAddress.endpoint) {
return Endpoint.get(value.value as string) as T;
} else if (type === CoreTypeAddress.range) {
const [start, end] = value.value as DIFArray;
const result = this.promiseAllOrSync<number>([
this.resolveDIFValueContainer(start),
this.resolveDIFValueContainer(end),
]);
if (result instanceof Promise) {
return result.then(([start, end]) => {
return new Range(start, end) as T;
});
} else {
const [start, end] = result as number[];
return new Range(start, end) as T;
}
} else if (type === CoreTypeAddress.list) {
return this.promiseAllOrSync(
(value.value as DIFArray).map((v) => this.resolveDIFValueContainer(v)),
Expand Down Expand Up @@ -1142,6 +1157,14 @@ export class DIFHandler {
type: CoreTypeAddress.endpoint,
value: value.toString(),
};
} else if (value instanceof Range) {
return {
type: CoreTypeAddress.range,
value: [
this.convertJSValueToDIFValueContainer(value.start),
this.convertJSValueToDIFValueContainer(value.end),
],
};
} else if (Array.isArray(value)) {
return {
value: value.map((v) => this.convertJSValueToDIFValueContainer(v)),
Expand Down
4 changes: 2 additions & 2 deletions src/lib/special-core-types/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ export class Endpoint {
return this.#endpoint;
}

get [Symbol.toStringTag]() {
get [Symbol.toStringTag](): string {
return this.#endpoint;
}

get [Symbol.toPrimitive]() {
get [Symbol.toPrimitive](): string {
return this.#endpoint;
}
}
37 changes: 37 additions & 0 deletions src/lib/special-core-types/range.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Represents a range of numbers with a start and end.
*/
export class Range {
constructor(public start: number, public end: number) {}

/**
* Gets the length of the range (end - start).
*/
get length(): number {
return this.end - this.start;
}

/**
* Checks if a given value is within the range (inclusive of start, exclusive of end).
* @param value The number to check.
* @returns True if the value is within the range, false otherwise.
*/
public contains(value: number): boolean {
return value >= this.start && value < this.end;
}

/**
* Turns a string representation of the range in the format
* @returns The string representation of the range.
*/
public toString(): string {
return `${this.start}..${this.end}`;
}

get [Symbol.toStringTag](): string {
return this.toString();
}
get [Symbol.toPrimitive](): string {
return this.toString();
}
}
2 changes: 2 additions & 0 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@
export * from "./runtime/runtime.ts";
export * as DIF from "./dif/mod.ts";
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";
import "./utils/devtools-formatter.ts";
21 changes: 21 additions & 0 deletions src/utils/devtools-formatter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Endpoint } from "../lib/special-core-types/endpoint.ts";
import { Range } from "../lib/special-core-types/range.ts";
import { Ref } from "../refs/ref.ts";

// @ts-ignore devtoolsFormatters
Expand Down Expand Up @@ -27,6 +28,26 @@ globalThis.devtoolsFormatters = [
],
],
];
} else if (obj instanceof Range) {
return [
"span",
{},
[
"span",
{},
["span", { style: "color: #d57258" }, `${obj.start}`],
],
[
"span",
{},
["span", {}, `..`],
],
[
"span",
{},
["span", { style: "color: #d57258" }, `${obj.end}`],
],
];
}
return null; // fall back to default
},
Expand Down
9 changes: 9 additions & 0 deletions test/runtime/execute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Runtime } from "../../src/runtime/runtime.ts";
import { assertEquals } from "@std/assert";
import { Endpoint } from "../../src/lib/special-core-types/endpoint.ts";
import { CoreTypeAddress } from "../../src/dif/core.ts";
import { Range } from "../../src/lib/special-core-types/range.ts";
Deno.test("execute sync with string result", async () => {
const runtime = await Runtime.create({ endpoint: "@jonas" });
const script = "1 + 2";
Expand Down Expand Up @@ -112,6 +113,14 @@ Deno.test("execute sync endpoint", async () => {
assertEquals(result, Endpoint.get("@jonas"));
});

Deno.test("execute sync range", async () => {
const runtime = await Runtime.create({ endpoint: "@jonas" });
const result = runtime.executeSync<Range>("1..2");
assertEquals(result.start, 1);
assertEquals(result.end, 2);
assertEquals(result, new Range(1, 2));
});

Deno.test("execute sync pass number from JS", async () => {
const runtime = await Runtime.create({ endpoint: "@jonas" });
const result = runtime.executeSync<number>("1 + ?", [41]);
Expand Down
Loading