michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "js/StructuredClone.h" michael@0: michael@0: #include "jsapi-tests/tests.h" michael@0: michael@0: BEGIN_TEST(testStructuredClone_object) michael@0: { michael@0: JS::RootedObject g1(cx, createGlobal()); michael@0: JS::RootedObject g2(cx, createGlobal()); michael@0: CHECK(g1); michael@0: CHECK(g2); michael@0: michael@0: JS::RootedValue v1(cx); michael@0: michael@0: { michael@0: JSAutoCompartment ac(cx, g1); michael@0: JS::RootedValue prop(cx, JS::Int32Value(1337)); michael@0: michael@0: JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr())); michael@0: v1 = JS::ObjectOrNullValue(obj); michael@0: CHECK(v1.isObject()); michael@0: CHECK(JS_SetProperty(cx, obj, "prop", prop)); michael@0: } michael@0: michael@0: { michael@0: JSAutoCompartment ac(cx, g2); michael@0: JS::RootedValue v2(cx); michael@0: michael@0: CHECK(JS_StructuredClone(cx, v1, &v2, nullptr, nullptr)); michael@0: CHECK(v2.isObject()); michael@0: JS::RootedObject obj(cx, &v2.toObject()); michael@0: michael@0: JS::RootedValue prop(cx); michael@0: CHECK(JS_GetProperty(cx, obj, "prop", &prop)); michael@0: CHECK(prop.isInt32()); michael@0: CHECK(&v1.toObject() != obj); michael@0: CHECK_EQUAL(prop.toInt32(), 1337); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: END_TEST(testStructuredClone_object) michael@0: michael@0: BEGIN_TEST(testStructuredClone_string) michael@0: { michael@0: JS::RootedObject g1(cx, createGlobal()); michael@0: JS::RootedObject g2(cx, createGlobal()); michael@0: CHECK(g1); michael@0: CHECK(g2); michael@0: michael@0: JS::RootedValue v1(cx); michael@0: michael@0: { michael@0: JSAutoCompartment ac(cx, g1); michael@0: JS::RootedValue prop(cx, JS::Int32Value(1337)); michael@0: michael@0: v1 = JS::StringValue(JS_NewStringCopyZ(cx, "Hello World!")); michael@0: CHECK(v1.isString()); michael@0: CHECK(v1.toString()); michael@0: } michael@0: michael@0: { michael@0: JSAutoCompartment ac(cx, g2); michael@0: JS::RootedValue v2(cx); michael@0: michael@0: CHECK(JS_StructuredClone(cx, v1, &v2, nullptr, nullptr)); michael@0: CHECK(v2.isString()); michael@0: CHECK(v2.toString()); michael@0: michael@0: JS::RootedValue expected(cx, JS::StringValue( michael@0: JS_NewStringCopyZ(cx, "Hello World!"))); michael@0: CHECK_SAME(v2, expected); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: END_TEST(testStructuredClone_string)