Skip to content

Commit

Permalink
Serialize boxed ports
Browse files Browse the repository at this point in the history
  • Loading branch information
bartblast committed Oct 18, 2024
1 parent f61c8df commit 48b97d4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
19 changes: 19 additions & 0 deletions assets/js/serializer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export default class Serializer {
return {...value, data: Object.values(value.data)};
}

if (value?.type === "port") {
return $.#serializeBoxedPort(value, isFullScope);
}

if (value?.type === "reference") {
return $.#serializeBoxedReference(value, isFullScope);
}
Expand Down Expand Up @@ -63,6 +67,21 @@ export default class Serializer {
return {...term, bits: Array.from(term.bits)};
}

static #serializeBoxedPort(term, isFullScope) {
if (isFullScope) {
return term;
}

if (term.origin === "client") {
throw new HologramRuntimeError(
"can't encode client terms that are ports originating in client",
);
}

const {origin, ...rest} = term;
return rest;
}

static #serializeBoxedReference(term, isFullScope) {
if (isFullScope) {
return term;
Expand Down
85 changes: 85 additions & 0 deletions test/javascript/serializer_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,91 @@ describe("Serializer", () => {
});
});

describe("port", () => {
describe("top-level", () => {
describe("originating in client", () => {
it("full scope", () => {
const term = Type.port("0.11", "client");

const expected =
'[1,{"type":"port","origin":"client","value":"0.11"}]';

assert.equal(serialize(term, true), expected);
});

it("not full scope", () => {
const term = Type.port("0.11", "client");

assert.throw(
() => serialize(term, false),
HologramRuntimeError,
"can't encode client terms that are ports originating in client",
);
});
});

describe("originating in server", () => {
it("full scope", () => {
const term = Type.port("0.11", "server");

const expected =
'[1,{"type":"port","origin":"server","value":"0.11"}]';

assert.equal(serialize(term, true), expected);
});

it("not full scope", () => {
const term = Type.port("0.11", "server");
const expected = '[1,{"type":"port","value":"0.11"}]';

assert.equal(serialize(term, false), expected);
});
});
});

describe.only("nested", () => {
describe("originating in client", () => {
it("full scope", () => {
const term = {a: Type.port("0.11", "client"), b: 2};

const expected =
'[1,{"a":{"type":"port","origin":"client","value":"0.11"},"b":2}]';

assert.equal(serialize(term, true), expected);
});

it("not full scope", () => {
const term = {a: Type.port("0.11", "client"), b: 2};

assert.throw(
() => serialize(term, false),
HologramRuntimeError,
"can't encode client terms that are ports originating in client",
);
});
});

describe("originating in server", () => {
it("full scope", () => {
const term = {a: Type.port("0.11", "server"), b: 2};

const expected =
'[1,{"a":{"type":"port","origin":"server","value":"0.11"},"b":2}]';

assert.equal(serialize(term, true), expected);
});

it("not full scope", () => {
const term = {a: Type.port("0.11", "server"), b: 2};

const expected = '[1,{"a":{"type":"port","value":"0.11"},"b":2}]';

assert.equal(serialize(term, false), expected);
});
});
});
});

describe("reference", () => {
describe("top-level", () => {
describe("originating in client", () => {
Expand Down

0 comments on commit 48b97d4

Please sign in to comment.