2
* Copyright (C) 2008 Apple Inc. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
#include "JavaScriptProfileNode.h"
29
#include "JSDOMBinding.h"
30
#include <profiler/ProfileNode.h>
31
#include <JavaScriptCore/APICast.h>
32
#include <JavaScriptCore/JSObjectRef.h>
33
#include <JavaScriptCore/JSContextRef.h>
34
#include <JavaScriptCore/JSRetainPtr.h>
35
#include <JavaScriptCore/JSStringRef.h>
36
#include <kjs/value.h>
44
typedef HashMap<ProfileNode*, JSValue*> ProfileNodeMap;
46
static ProfileNodeMap& ProfileNodeCache()
48
static ProfileNodeMap staticProfileNodes;
49
return staticProfileNodes;
52
static JSValueRef getFunctionName(JSContextRef ctx, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
54
if (!JSValueIsObjectOfClass(ctx, thisObject, ProfileNodeClass()))
55
return JSValueMakeUndefined(ctx);
57
ProfileNode* profileNode = static_cast<ProfileNode*>(JSObjectGetPrivate(thisObject));
58
JSRetainPtr<JSStringRef> functionNameString(Adopt, JSStringCreateWithCharacters(profileNode->functionName().data(), profileNode->functionName().size()));
59
return JSValueMakeString(ctx, functionNameString.get());
62
static JSValueRef getURL(JSContextRef ctx, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
64
if (!JSValueIsObjectOfClass(ctx, thisObject, ProfileNodeClass()))
65
return JSValueMakeUndefined(ctx);
67
ProfileNode* profileNode = static_cast<ProfileNode*>(JSObjectGetPrivate(thisObject));
68
JSRetainPtr<JSStringRef> urlString(Adopt, JSStringCreateWithCharacters(profileNode->url().data(), profileNode->url().size()));
69
return JSValueMakeString(ctx, urlString.get());
72
static JSValueRef getLineNumber(JSContextRef ctx, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
74
if (!JSValueIsObjectOfClass(ctx, thisObject, ProfileNodeClass()))
75
return JSValueMakeUndefined(ctx);
77
ProfileNode* profileNode = static_cast<ProfileNode*>(JSObjectGetPrivate(thisObject));
78
return JSValueMakeNumber(ctx, profileNode->lineNumber());
81
static JSValueRef getTotalTime(JSContextRef ctx, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
85
if (!JSValueIsObjectOfClass(ctx, thisObject, ProfileNodeClass()))
86
return JSValueMakeUndefined(ctx);
88
ProfileNode* profileNode = static_cast<ProfileNode*>(JSObjectGetPrivate(thisObject));
89
return JSValueMakeNumber(ctx, profileNode->totalTime());
92
static JSValueRef getSelfTime(JSContextRef ctx, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
96
if (!JSValueIsObjectOfClass(ctx, thisObject, ProfileNodeClass()))
97
return JSValueMakeUndefined(ctx);
99
ProfileNode* profileNode = static_cast<ProfileNode*>(JSObjectGetPrivate(thisObject));
100
return JSValueMakeNumber(ctx, profileNode->selfTime());
103
static JSValueRef getTotalPercent(JSContextRef ctx, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
107
if (!JSValueIsObjectOfClass(ctx, thisObject, ProfileNodeClass()))
108
return JSValueMakeUndefined(ctx);
110
ProfileNode* profileNode = static_cast<ProfileNode*>(JSObjectGetPrivate(thisObject));
111
return JSValueMakeNumber(ctx, profileNode->totalPercent());
114
static JSValueRef getSelfPercent(JSContextRef ctx, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
118
if (!JSValueIsObjectOfClass(ctx, thisObject, ProfileNodeClass()))
119
return JSValueMakeUndefined(ctx);
121
ProfileNode* profileNode = static_cast<ProfileNode*>(JSObjectGetPrivate(thisObject));
122
return JSValueMakeNumber(ctx, profileNode->selfPercent());
125
static JSValueRef getNumberOfCalls(JSContextRef ctx, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
129
if (!JSValueIsObjectOfClass(ctx, thisObject, ProfileNodeClass()))
130
return JSValueMakeUndefined(ctx);
132
ProfileNode* profileNode = static_cast<ProfileNode*>(JSObjectGetPrivate(thisObject));
133
return JSValueMakeNumber(ctx, profileNode->numberOfCalls());
136
static JSValueRef getChildren(JSContextRef ctx, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
140
if (!JSValueIsObjectOfClass(ctx, thisObject, ProfileNodeClass()))
141
return JSValueMakeUndefined(ctx);
143
ProfileNode* profileNode = static_cast<ProfileNode*>(JSObjectGetPrivate(thisObject));
144
const Vector<RefPtr<ProfileNode> >& children = profileNode->children();
146
JSObjectRef global = JSContextGetGlobalObject(ctx);
148
JSRetainPtr<JSStringRef> arrayString(Adopt, JSStringCreateWithUTF8CString("Array"));
150
JSValueRef arrayProperty = JSObjectGetProperty(ctx, global, arrayString.get(), exception);
151
if (exception && *exception)
152
return JSValueMakeUndefined(ctx);
154
JSObjectRef arrayConstructor = JSValueToObject(ctx, arrayProperty, exception);
155
if (exception && *exception)
156
return JSValueMakeUndefined(ctx);
158
JSObjectRef result = JSObjectCallAsConstructor(ctx, arrayConstructor, 0, 0, exception);
159
if (exception && *exception)
160
return JSValueMakeUndefined(ctx);
162
JSRetainPtr<JSStringRef> pushString(Adopt, JSStringCreateWithUTF8CString("push"));
164
JSValueRef pushProperty = JSObjectGetProperty(ctx, result, pushString.get(), exception);
165
if (exception && *exception)
166
return JSValueMakeUndefined(ctx);
168
JSObjectRef pushFunction = JSValueToObject(ctx, pushProperty, exception);
169
if (exception && *exception)
170
return JSValueMakeUndefined(ctx);
172
for (Vector<RefPtr<ProfileNode> >::const_iterator it = children.begin(); it != children.end(); ++it) {
173
JSValueRef arg0 = toRef(toJS(toJS(ctx), (*it).get() ));
174
JSObjectCallAsFunction(ctx, pushFunction, result, 1, &arg0, exception);
175
if (exception && *exception)
176
return JSValueMakeUndefined(ctx);
182
static JSValueRef getVisible(JSContextRef ctx, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
186
if (!JSValueIsObjectOfClass(ctx, thisObject, ProfileNodeClass()))
187
return JSValueMakeUndefined(ctx);
189
ProfileNode* profileNode = static_cast<ProfileNode*>(JSObjectGetPrivate(thisObject));
190
return JSValueMakeBoolean(ctx, profileNode->visible());
193
static void finalize(JSObjectRef object)
195
ProfileNode* profileNode = static_cast<ProfileNode*>(JSObjectGetPrivate(object));
196
ProfileNodeCache().remove(profileNode);
197
profileNode->deref();
200
JSClassRef ProfileNodeClass()
202
static JSStaticValue staticValues[] = {
203
{ "functionName", getFunctionName, 0, kJSPropertyAttributeNone },
204
{ "url", getURL, 0, kJSPropertyAttributeNone },
205
{ "lineNumber", getLineNumber, 0, kJSPropertyAttributeNone },
206
{ "totalTime", getTotalTime, 0, kJSPropertyAttributeNone },
207
{ "selfTime", getSelfTime, 0, kJSPropertyAttributeNone },
208
{ "totalPercent", getTotalPercent, 0, kJSPropertyAttributeNone },
209
{ "selfPercent", getSelfPercent, 0, kJSPropertyAttributeNone },
210
{ "numberOfCalls", getNumberOfCalls, 0, kJSPropertyAttributeNone },
211
{ "children", getChildren, 0, kJSPropertyAttributeNone },
212
{ "visible", getVisible, 0, kJSPropertyAttributeNone },
216
static JSClassDefinition classDefinition = {
217
0, kJSClassAttributeNone, "ProfileNode", 0, staticValues, 0,
218
0, finalize, 0, 0, 0, 0, 0, 0, 0, 0, 0
221
static JSClassRef ProfileNodeClass = JSClassCreate(&classDefinition);
222
return ProfileNodeClass;
225
JSValue* toJS(ExecState* exec, ProfileNode* ProfileNode)
230
JSValue* ProfileNodeWrapper = ProfileNodeCache().get(ProfileNode);
231
if (ProfileNodeWrapper)
232
return ProfileNodeWrapper;
236
ProfileNodeWrapper = toJS(JSObjectMake(toRef(exec), ProfileNodeClass(), static_cast<void*>(ProfileNode)));
237
ProfileNodeCache().set(ProfileNode, ProfileNodeWrapper);
238
return ProfileNodeWrapper;
242
} // namespace WebCore