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{
470bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch    GCController* controller = static_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
580bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch    GCController* controller = static_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{
660bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch    GCController* controller = static_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
78643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    JSClassRef classRef = getJSClass();
79643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    JSValueRef gcControllerObject = JSObjectMake(context, classRef, this);
80643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    JSClassRelease(classRef);
81563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
82643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    JSObjectSetProperty(context, windowObject, gcControllerStr.get(), gcControllerObject, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, exception);
83563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
84563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
85643ca7872b450ea4efacab6188849e5aac2ba161Steve BlockJSClassRef GCController::getJSClass()
86563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark{
87563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    static JSStaticFunction staticFunctions[] = {
88563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        { "collect", collectCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
89563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        { "collectOnAlternateThread", collectOnAlternateThreadCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
90563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        { "getJSObjectCount", getJSObjectCountCallback, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
91563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark        { 0, 0, 0 }
92563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark    };
93563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark
94643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    static JSClassDefinition classDefinition = {
95643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        0, kJSClassAttributeNone, "GCController", 0, 0, staticFunctions,
96643ca7872b450ea4efacab6188849e5aac2ba161Steve Block        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
97643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    };
98643ca7872b450ea4efacab6188849e5aac2ba161Steve Block
99643ca7872b450ea4efacab6188849e5aac2ba161Steve Block    return JSClassCreate(&classDefinition);
100563af33bc48281d19dce701398dbb88cb54fd7ecCary Clark}
101