michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: */ 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: #ifdef JSGC_GENERATIONAL michael@0: michael@0: #include "js/RootingAPI.h" michael@0: #include "jsapi-tests/tests.h" michael@0: michael@0: BEGIN_TEST(testGCHeapPostBarriers) michael@0: { michael@0: /* Sanity check - objects start in the nursery and then become tenured. */ michael@0: JS_GC(cx->runtime()); michael@0: JS::RootedObject obj(cx, NurseryObject()); michael@0: CHECK(js::gc::IsInsideNursery(obj.get())); michael@0: JS_GC(cx->runtime()); michael@0: CHECK(!js::gc::IsInsideNursery(obj.get())); michael@0: JS::RootedObject tenuredObject(cx, obj); michael@0: michael@0: /* Currently JSObject and JSFunction objects are nursery allocated. */ michael@0: CHECK(TestHeapPostBarriers(NurseryObject())); michael@0: CHECK(TestHeapPostBarriers(NurseryFunction())); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: template michael@0: bool michael@0: TestHeapPostBarriers(T initialObj) michael@0: { michael@0: CHECK(initialObj != nullptr); michael@0: CHECK(js::gc::IsInsideNursery(initialObj)); michael@0: michael@0: /* Construct Heap<> wrapper. */ michael@0: JS::Heap *heapData = new JS::Heap(); michael@0: CHECK(heapData); michael@0: CHECK(heapData->get() == nullptr); michael@0: heapData->set(initialObj); michael@0: michael@0: /* Store the pointer as an integer so that the hazard analysis will miss it. */ michael@0: uintptr_t initialObjAsInt = uintptr_t(initialObj); michael@0: michael@0: /* Perform minor GC and check heap wrapper is udated with new pointer. */ michael@0: js::MinorGC(cx, JS::gcreason::API); michael@0: CHECK(uintptr_t(heapData->get()) != initialObjAsInt); michael@0: CHECK(!js::gc::IsInsideNursery(heapData->get())); michael@0: michael@0: /* Check object is definitely still alive. */ michael@0: JS::Rooted obj(cx, heapData->get()); michael@0: JS::RootedValue value(cx); michael@0: CHECK(JS_GetProperty(cx, obj, "x", &value)); michael@0: CHECK(value.isInt32()); michael@0: CHECK(value.toInt32() == 42); michael@0: michael@0: delete heapData; michael@0: return true; michael@0: } michael@0: michael@0: JSObject *NurseryObject() michael@0: { michael@0: JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr())); michael@0: if (!obj) michael@0: return nullptr; michael@0: JS_DefineProperty(cx, obj, "x", 42, 0); michael@0: return obj; michael@0: } michael@0: michael@0: JSFunction *NurseryFunction() michael@0: { michael@0: /* michael@0: * We don't actually use the function as a function, so here we cheat and michael@0: * cast a JSObject. michael@0: */ michael@0: return static_cast(NurseryObject()); michael@0: } michael@0: michael@0: END_TEST(testGCHeapPostBarriers) michael@0: michael@0: #endif