Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / js1_8_5 / extensions / clone-errors.js
1 // -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // Any copyright is dedicated to the Public Domain.
3 // http://creativecommons.org/licenses/publicdomain/
4
5 function check(v) {
6     try {
7         serialize(v);
8     } catch (exc) {
9         return;
10     }
11     throw new Error("serializing " + uneval(v) + " should have failed with an exception");
12 }
13
14 // Unsupported object types.
15 check(new Error("oops"));
16 check(this);
17 check(Math);
18 check(function () {});
19 check(Proxy.create({enumerate: function () { return []; }}));
20 check(<element/>);
21 check(new Namespace("x"));
22 check(new QName("x", "y"));
23
24 // A failing getter.
25 check({get x() { throw new Error("fail"); }});
26
27 // Various recursive objects, i.e. those which the structured cloning
28 // algorithm wants us to reject due to "memory".
29 //
30 // Recursive array.
31 var a = [];
32 a[0] = a;
33 check(a);
34
35 // Recursive Object.
36 var b = {};
37 b.next = b;
38 check(b);
39
40 // Mutually recursive objects.
41 a[0] = b;
42 b.next = a;
43 check(a);
44 check(b);
45
46 // A recursive object that doesn't fail until 'memory' contains lots of objects.
47 a = [];
48 b = a;
49 for (var i = 0; i < 10000; i++) {
50     b[0] = {};
51     b[1] = [];
52     b = b[1];
53 }
54 b[0] = {owner: a};
55 b[1] = [];
56 check(a);
57
58 reportCompare(0, 0, "ok");