Skip to content

Commit

Permalink
Serialize boxed PIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
bartblast committed Oct 18, 2024
1 parent 48b97d4 commit 9e5c410
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
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 === "pid") {
return $.#serializeBoxedPid(value, isFullScope);
}

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

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

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

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

static #serializeBoxedPort(term, isFullScope) {
if (isFullScope) {
return term;
Expand Down
100 changes: 99 additions & 1 deletion test/javascript/serializer_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,104 @@ describe("Serializer", () => {
});
});

describe("pid", () => {
describe("top-level", () => {
describe("originating in client", () => {
it("full scope", () => {
const term = Type.pid('my_node@my_"host', [0, 11, 222], "client");

const expected =
'[1,{"type":"pid","node":"my_node@my_\\"host","origin":"client","segments":[0,11,222]}]';

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

it("not full scope", () => {
const term = Type.pid("my_node@my_host", [0, 11, 222], "client");

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

describe("originating in server", () => {
it("full scope", () => {
const term = Type.pid('my_node@my_"host', [0, 11, 222], "server");

const expected =
'[1,{"type":"pid","node":"my_node@my_\\"host","origin":"server","segments":[0,11,222]}]';

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

it("not full scope", () => {
const term = Type.pid("my_node@my_host", [0, 11, 222], "server");
const expected = '[1,{"type":"pid","segments":[0,11,222]}]';

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

describe("nested", () => {
describe("originating in client", () => {
it("full scope", () => {
const term = {
a: Type.pid('my_node@my_"host', [0, 11, 222], "client"),
b: 2,
};

const expected =
'[1,{"a":{"type":"pid","node":"my_node@my_\\"host","origin":"client","segments":[0,11,222]},"b":2}]';

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

it("not full scope", () => {
const term = {
a: Type.pid("my_node@my_host", [0, 11, 222], "client"),
b: 2,
};

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

describe("originating in server", () => {
it("full scope", () => {
const term = {
a: Type.pid('my_node@my_"host', [0, 11, 222], "server"),
b: 2,
};

const expected =
'[1,{"a":{"type":"pid","node":"my_node@my_\\"host","origin":"server","segments":[0,11,222]},"b":2}]';

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

it("not full scope", () => {
const term = {
a: Type.pid("my_node@my_host", [0, 11, 222], "server"),
b: 2,
};

const expected =
'[1,{"a":{"type":"pid","segments":[0,11,222]},"b":2}]';

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

describe("port", () => {
describe("top-level", () => {
describe("originating in client", () => {
Expand Down Expand Up @@ -193,7 +291,7 @@ describe("Serializer", () => {
});
});

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

0 comments on commit 9e5c410

Please sign in to comment.