2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-02-16 08:20:21 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
function assert(cond) {
|
|
|
|
if (!cond) {
|
|
|
|
throw Error("assert");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertArrayEquals(a1, a2) {
|
|
|
|
if (a1.length !== a2.length) throw Error("assert");
|
|
|
|
|
|
|
|
for (const index in a1) {
|
|
|
|
if (a1[index] !== a2[index]) {
|
2022-02-02 12:51:37 -05:00
|
|
|
throw Error(`assert: (index ${index}) ${a1[index]} !== ${a2[index]}`);
|
2021-02-16 08:20:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
const emptyString = "";
|
2022-03-11 09:29:01 -05:00
|
|
|
const emptyStringSerialized = [255, 15, 34, 0];
|
2022-06-07 05:25:10 -04:00
|
|
|
assertArrayEquals(
|
|
|
|
Deno.core.opSync("op_serialize", emptyString),
|
|
|
|
emptyStringSerialized,
|
|
|
|
);
|
2021-02-16 08:20:21 -05:00
|
|
|
assert(
|
2022-06-07 05:25:10 -04:00
|
|
|
Deno.core.opSync(
|
|
|
|
"op_deserialize",
|
|
|
|
new Uint8Array(emptyStringSerialized),
|
|
|
|
) ===
|
2021-02-16 08:20:21 -05:00
|
|
|
emptyString,
|
|
|
|
);
|
|
|
|
|
|
|
|
const primitiveValueArray = ["test", "a", null, undefined];
|
|
|
|
// deno-fmt-ignore
|
|
|
|
const primitiveValueArraySerialized = [
|
2022-03-11 09:29:01 -05:00
|
|
|
255, 15, 65, 4, 34, 4, 116, 101, 115, 116,
|
2021-02-16 08:20:21 -05:00
|
|
|
34, 1, 97, 48, 95, 36, 0, 4,
|
|
|
|
];
|
|
|
|
assertArrayEquals(
|
2022-06-07 05:25:10 -04:00
|
|
|
Deno.core.opSync("op_serialize", primitiveValueArray),
|
2021-02-16 08:20:21 -05:00
|
|
|
primitiveValueArraySerialized,
|
|
|
|
);
|
|
|
|
|
|
|
|
assertArrayEquals(
|
2022-06-07 05:25:10 -04:00
|
|
|
Deno.core.opSync(
|
|
|
|
"op_deserialize",
|
2021-02-16 08:20:21 -05:00
|
|
|
new Uint8Array(primitiveValueArraySerialized),
|
|
|
|
),
|
|
|
|
primitiveValueArray,
|
|
|
|
);
|
|
|
|
|
|
|
|
const circularObject = { test: null, test2: "dd", test3: "aa" };
|
|
|
|
circularObject.test = circularObject;
|
|
|
|
// deno-fmt-ignore
|
|
|
|
const circularObjectSerialized = [
|
2022-03-11 09:29:01 -05:00
|
|
|
255, 15, 111, 34, 4, 116, 101, 115,
|
2021-08-09 04:39:00 -04:00
|
|
|
116, 94, 0, 34, 5, 116, 101, 115,
|
|
|
|
116, 50, 34, 2, 100, 100, 34, 5,
|
|
|
|
116, 101, 115, 116, 51, 34, 2, 97,
|
|
|
|
97, 123, 3,
|
2021-02-16 08:20:21 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
assertArrayEquals(
|
2022-06-07 05:25:10 -04:00
|
|
|
Deno.core.opSync("op_serialize", circularObject),
|
2021-02-16 08:20:21 -05:00
|
|
|
circularObjectSerialized,
|
|
|
|
);
|
|
|
|
|
2022-06-07 05:25:10 -04:00
|
|
|
const deserializedCircularObject = Deno.core.opSync(
|
|
|
|
"op_deserialize",
|
2021-02-16 08:20:21 -05:00
|
|
|
new Uint8Array(circularObjectSerialized),
|
|
|
|
);
|
|
|
|
assert(deserializedCircularObject.test == deserializedCircularObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|