GCController.cpp revision 563af33bc48281d19dce701398dbb88cb54fd7ec
1563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark/* 2563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * Copyright (C) 2007 Apple Inc. All rights reserved. 3563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * 4563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * Redistribution and use in source and binary forms, with or without 5563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * modification, are permitted provided that the following conditions 6563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * are met: 7563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * 8563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * 1. Redistributions of source code must retain the above copyright 9563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * notice, this list of conditions and the following disclaimer. 10563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * 2. Redistributions in binary form must reproduce the above copyright 11563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * notice, this list of conditions and the following disclaimer in the 12563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * documentation and/or other materials provided with the distribution. 13563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 14563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * its contributors may be used to endorse or promote products derived 15563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * from this software without specific prior written permission. 16563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * 17563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 18563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 21563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark */ 28563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 29563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark#include "config.h" 30563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark#include "GCController.h" 31563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 32563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark#include <JavaScriptCore/JSObjectRef.h> 33563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark#include <JavaScriptCore/JSRetainPtr.h> 34563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 35563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkGCController::GCController() 36563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{ 37563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark} 38563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 39563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkGCController::~GCController() 40563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{ 41563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark} 42563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 43563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark// Static Functions 44563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 45563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkstatic JSValueRef collectCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) 46563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{ 47563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark GCController* controller = reinterpret_cast<GCController*>(JSObjectGetPrivate(thisObject)); 48563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark controller->collect(); 49563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark return JSValueMakeUndefined(context); 50563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark} 51563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 52563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkstatic JSValueRef collectOnAlternateThreadCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) 53563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{ 54563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark bool waitUntilDone = false; 55563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark if (argumentCount > 0) 56563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark waitUntilDone = JSValueToBoolean(context, arguments[0]); 57563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 58563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark GCController* controller = reinterpret_cast<GCController*>(JSObjectGetPrivate(thisObject)); 59563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark controller->collectOnAlternateThread(waitUntilDone); 60563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 61563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark return JSValueMakeUndefined(context); 62563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark} 63563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 64563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkstatic JSValueRef getJSObjectCountCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) 65563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{ 66563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark GCController* controller = reinterpret_cast<GCController*>(JSObjectGetPrivate(thisObject)); 67563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark size_t jsObjectCount = controller->getJSObjectCount(); 68563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 69563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark return JSValueMakeNumber(context, jsObjectCount); 70563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark} 71563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 72563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark// Object Creation 73563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 74563af33bc48281d19dce701398dbb88cb54fd7ecCary Clarkvoid GCController::makeWindowObject(JSContextRef context, JSObjectRef windowObject, JSValueRef* exception) 75563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{ 76563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark JSRetainPtr<JSStringRef> gcControllerStr(Adopt, JSStringCreateWithUTF8CString("GCController")); 77563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark JSValueRef gcControllerObject = JSObjectMake(context, getJSClass(), this); 78563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark JSObjectSetProperty(context, windowObject, gcControllerStr.get(), gcControllerObject, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception); 79563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark} 80563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 81563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkJSClassRef GCController::getJSClass() 82563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{ 83563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark static JSClassRef gcControllerClass = 0; 84563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 85563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark if (!gcControllerClass) { 86563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark JSStaticFunction* staticFunctions = GCController::staticFunctions(); 87563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark JSClassDefinition classDefinition = { 88563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 0, kJSClassAttributeNone, "GCController", 0, 0, staticFunctions, 89563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 90563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark }; 91563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 92563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark gcControllerClass = JSClassCreate(&classDefinition); 93563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark } 94563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 95563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark return gcControllerClass; 96563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark} 97563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 98563af33bc48281d19dce701398dbb88cb54fd7ecCary ClarkJSStaticFunction* GCController::staticFunctions() 99563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{ 100563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark static JSStaticFunction staticFunctions[] = { 101563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark { "collect", collectCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete }, 102563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark { "collectOnAlternateThread", collectOnAlternateThreadCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete }, 103563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark { "getJSObjectCount", getJSObjectCountCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete }, 104563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark { 0, 0, 0 } 105563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark }; 106563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark 107563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark return staticFunctions; 108563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark} 109