diff options
author | Inteon <42113979+inteon@users.noreply.github.com> | 2021-02-16 14:20:21 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-16 14:20:21 +0100 |
commit | e2a91190c38f494c2666d3362d247f698718d2f8 (patch) | |
tree | 418a399b922f064d43fcef91ec34e4acec9d9198 /core/serialize_deserialize_test.js | |
parent | c6b3982e782432af5d9e6f6359f522d34e663cf0 (diff) |
feat: add structured cloning to Deno.core (#9458)
This commit adds two new "Deno.core" APIs:
- "Deno.core.serialize"
- "Deno.core.deserialize"
These APIs are used to provide structured cloning of values
and will be used for further web worker implementation.
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Diffstat (limited to 'core/serialize_deserialize_test.js')
-rw-r--r-- | core/serialize_deserialize_test.js | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/core/serialize_deserialize_test.js b/core/serialize_deserialize_test.js new file mode 100644 index 000000000..6368d56db --- /dev/null +++ b/core/serialize_deserialize_test.js @@ -0,0 +1,69 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. +"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]) { + throw Error("assert"); + } + } +} + +function main() { + const emptyString = ""; + const emptyStringSerialized = [34, 0]; + assertArrayEquals(Deno.core.serialize(emptyString), emptyStringSerialized); + assert( + Deno.core.deserialize(new Uint8Array(emptyStringSerialized)) === + emptyString, + ); + + const primitiveValueArray = ["test", "a", null, undefined]; + // deno-fmt-ignore + const primitiveValueArraySerialized = [ + 65, 4, 34, 4, 116, 101, 115, 116, + 34, 1, 97, 48, 95, 36, 0, 4, + ]; + assertArrayEquals( + Deno.core.serialize(primitiveValueArray), + primitiveValueArraySerialized, + ); + + assertArrayEquals( + Deno.core.deserialize( + new Uint8Array(primitiveValueArraySerialized), + ), + primitiveValueArray, + ); + + const circularObject = { test: null, test2: "dd", test3: "aa" }; + circularObject.test = circularObject; + // deno-fmt-ignore + const circularObjectSerialized = [ + 111, 34, 4, 116, 101, 115, 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, + ]; + + assertArrayEquals( + Deno.core.serialize(circularObject), + circularObjectSerialized, + ); + + const deserializedCircularObject = Deno.core.deserialize( + new Uint8Array(circularObjectSerialized), + ); + assert(deserializedCircularObject.test == deserializedCircularObject); +} + +main(); |