1/*
2 * Copyright (C) 2014-2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#include "config.h"
27#include "ConsoleObject.h"
28
29#include "ConsoleClient.h"
30#include "Error.h"
31#include "JSCInlines.h"
32#include "ScriptArguments.h"
33#include "ScriptCallStackFactory.h"
34
35namespace JSC {
36
37static String valueOrDefaultLabelString(ExecState* exec)
38{
39 if (exec->argumentCount() < 1)
40 return "default"_s;
41
42 auto value = exec->argument(0);
43 if (value.isUndefined())
44 return "default"_s;
45
46 return value.toWTFString(exec);
47}
48
49STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(ConsoleObject);
50
51static EncodedJSValue JSC_HOST_CALL consoleProtoFuncDebug(ExecState*);
52static EncodedJSValue JSC_HOST_CALL consoleProtoFuncError(ExecState*);
53static EncodedJSValue JSC_HOST_CALL consoleProtoFuncLog(ExecState*);
54static EncodedJSValue JSC_HOST_CALL consoleProtoFuncInfo(ExecState*);
55static EncodedJSValue JSC_HOST_CALL consoleProtoFuncWarn(ExecState*);
56static EncodedJSValue JSC_HOST_CALL consoleProtoFuncClear(ExecState*);
57static EncodedJSValue JSC_HOST_CALL consoleProtoFuncDir(ExecState*);
58static EncodedJSValue JSC_HOST_CALL consoleProtoFuncDirXML(ExecState*);
59static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTable(ExecState*);
60static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTrace(ExecState*);
61static EncodedJSValue JSC_HOST_CALL consoleProtoFuncAssert(ExecState*);
62static EncodedJSValue JSC_HOST_CALL consoleProtoFuncCount(ExecState*);
63static EncodedJSValue JSC_HOST_CALL consoleProtoFuncCountReset(ExecState*);
64static EncodedJSValue JSC_HOST_CALL consoleProtoFuncProfile(ExecState*);
65static EncodedJSValue JSC_HOST_CALL consoleProtoFuncProfileEnd(ExecState*);
66static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTakeHeapSnapshot(ExecState*);
67static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTime(ExecState*);
68static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTimeLog(ExecState*);
69static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTimeEnd(ExecState*);
70static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTimeStamp(ExecState*);
71static EncodedJSValue JSC_HOST_CALL consoleProtoFuncGroup(ExecState*);
72static EncodedJSValue JSC_HOST_CALL consoleProtoFuncGroupCollapsed(ExecState*);
73static EncodedJSValue JSC_HOST_CALL consoleProtoFuncGroupEnd(ExecState*);
74static EncodedJSValue JSC_HOST_CALL consoleProtoFuncRecord(ExecState*);
75static EncodedJSValue JSC_HOST_CALL consoleProtoFuncRecordEnd(ExecState*);
76static EncodedJSValue JSC_HOST_CALL consoleProtoFuncScreenshot(ExecState*);
77
78const ClassInfo ConsoleObject::s_info = { "Console", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(ConsoleObject) };
79
80ConsoleObject::ConsoleObject(VM& vm, Structure* structure)
81 : JSNonFinalObject(vm, structure)
82{
83}
84
85void ConsoleObject::finishCreation(VM& vm, JSGlobalObject* globalObject)
86{
87 Base::finishCreation(vm);
88 ASSERT(inherits(vm, info()));
89
90 // For legacy reasons, console properties are enumerable, writable, deleteable,
91 // and all have a length of 0. This may change if Console is standardized.
92
93 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("debug", consoleProtoFuncDebug, static_cast<unsigned>(PropertyAttribute::None), 0);
94 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("error", consoleProtoFuncError, static_cast<unsigned>(PropertyAttribute::None), 0);
95 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("log", consoleProtoFuncLog, static_cast<unsigned>(PropertyAttribute::None), 0);
96 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("info", consoleProtoFuncInfo, static_cast<unsigned>(PropertyAttribute::None), 0);
97 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("warn", consoleProtoFuncWarn, static_cast<unsigned>(PropertyAttribute::None), 0);
98
99 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->clear, consoleProtoFuncClear, static_cast<unsigned>(PropertyAttribute::None), 0);
100 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("dir", consoleProtoFuncDir, static_cast<unsigned>(PropertyAttribute::None), 0);
101 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("dirxml", consoleProtoFuncDirXML, static_cast<unsigned>(PropertyAttribute::None), 0);
102 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("table", consoleProtoFuncTable, static_cast<unsigned>(PropertyAttribute::None), 0);
103 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("trace", consoleProtoFuncTrace, static_cast<unsigned>(PropertyAttribute::None), 0);
104 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("assert", consoleProtoFuncAssert, static_cast<unsigned>(PropertyAttribute::None), 0);
105 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->count, consoleProtoFuncCount, static_cast<unsigned>(PropertyAttribute::None), 0);
106 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("countReset", consoleProtoFuncCountReset, static_cast<unsigned>(PropertyAttribute::None), 0);
107 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("profile", consoleProtoFuncProfile, static_cast<unsigned>(PropertyAttribute::None), 0);
108 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("profileEnd", consoleProtoFuncProfileEnd, static_cast<unsigned>(PropertyAttribute::None), 0);
109 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("time", consoleProtoFuncTime, static_cast<unsigned>(PropertyAttribute::None), 0);
110 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("timeLog", consoleProtoFuncTimeLog, static_cast<unsigned>(PropertyAttribute::None), 0);
111 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("timeEnd", consoleProtoFuncTimeEnd, static_cast<unsigned>(PropertyAttribute::None), 0);
112 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("timeStamp", consoleProtoFuncTimeStamp, static_cast<unsigned>(PropertyAttribute::None), 0);
113 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("takeHeapSnapshot", consoleProtoFuncTakeHeapSnapshot, static_cast<unsigned>(PropertyAttribute::None), 0);
114 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("group", consoleProtoFuncGroup, static_cast<unsigned>(PropertyAttribute::None), 0);
115 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("groupCollapsed", consoleProtoFuncGroupCollapsed, static_cast<unsigned>(PropertyAttribute::None), 0);
116 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("groupEnd", consoleProtoFuncGroupEnd, static_cast<unsigned>(PropertyAttribute::None), 0);
117 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("record", consoleProtoFuncRecord, static_cast<unsigned>(PropertyAttribute::None), 0);
118 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("recordEnd", consoleProtoFuncRecordEnd, static_cast<unsigned>(PropertyAttribute::None), 0);
119 JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("screenshot", consoleProtoFuncScreenshot, static_cast<unsigned>(PropertyAttribute::None), 0);
120}
121
122static String valueToStringWithUndefinedOrNullCheck(ExecState* exec, JSValue value)
123{
124 if (value.isUndefinedOrNull())
125 return String();
126 return value.toWTFString(exec);
127}
128
129static EncodedJSValue consoleLogWithLevel(ExecState* exec, MessageLevel level)
130{
131 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
132 if (!client)
133 return JSValue::encode(jsUndefined());
134
135 client->logWithLevel(exec, Inspector::createScriptArguments(exec, 0), level);
136 return JSValue::encode(jsUndefined());
137}
138
139static EncodedJSValue JSC_HOST_CALL consoleProtoFuncDebug(ExecState* exec)
140{
141 return consoleLogWithLevel(exec, MessageLevel::Debug);
142}
143
144static EncodedJSValue JSC_HOST_CALL consoleProtoFuncError(ExecState* exec)
145{
146 return consoleLogWithLevel(exec, MessageLevel::Error);
147}
148
149static EncodedJSValue JSC_HOST_CALL consoleProtoFuncLog(ExecState* exec)
150{
151 return consoleLogWithLevel(exec, MessageLevel::Log);
152}
153
154static EncodedJSValue JSC_HOST_CALL consoleProtoFuncInfo(ExecState* exec)
155{
156 return consoleLogWithLevel(exec, MessageLevel::Info);
157}
158
159static EncodedJSValue JSC_HOST_CALL consoleProtoFuncWarn(ExecState* exec)
160{
161 return consoleLogWithLevel(exec, MessageLevel::Warning);
162}
163
164static EncodedJSValue JSC_HOST_CALL consoleProtoFuncClear(ExecState* exec)
165{
166 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
167 if (!client)
168 return JSValue::encode(jsUndefined());
169
170 client->clear(exec);
171 return JSValue::encode(jsUndefined());
172}
173
174static EncodedJSValue JSC_HOST_CALL consoleProtoFuncDir(ExecState* exec)
175{
176 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
177 if (!client)
178 return JSValue::encode(jsUndefined());
179
180 client->dir(exec, Inspector::createScriptArguments(exec, 0));
181 return JSValue::encode(jsUndefined());
182}
183
184static EncodedJSValue JSC_HOST_CALL consoleProtoFuncDirXML(ExecState* exec)
185{
186 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
187 if (!client)
188 return JSValue::encode(jsUndefined());
189
190 client->dirXML(exec, Inspector::createScriptArguments(exec, 0));
191 return JSValue::encode(jsUndefined());
192}
193
194static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTable(ExecState* exec)
195{
196 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
197 if (!client)
198 return JSValue::encode(jsUndefined());
199
200 client->table(exec, Inspector::createScriptArguments(exec, 0));
201 return JSValue::encode(jsUndefined());
202}
203
204static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTrace(ExecState* exec)
205{
206 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
207 if (!client)
208 return JSValue::encode(jsUndefined());
209
210 client->trace(exec, Inspector::createScriptArguments(exec, 0));
211 return JSValue::encode(jsUndefined());
212}
213
214static EncodedJSValue JSC_HOST_CALL consoleProtoFuncAssert(ExecState* exec)
215{
216 VM& vm = exec->vm();
217 auto scope = DECLARE_THROW_SCOPE(vm);
218 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
219 if (!client)
220 return JSValue::encode(jsUndefined());
221
222 bool condition = exec->argument(0).toBoolean(exec);
223 RETURN_IF_EXCEPTION(scope, encodedJSValue());
224
225 if (condition)
226 return JSValue::encode(jsUndefined());
227
228 client->assertion(exec, Inspector::createScriptArguments(exec, 1));
229 return JSValue::encode(jsUndefined());
230}
231
232static EncodedJSValue JSC_HOST_CALL consoleProtoFuncCount(ExecState* exec)
233{
234 auto scope = DECLARE_THROW_SCOPE(exec->vm());
235 auto* client = exec->lexicalGlobalObject()->consoleClient();
236 if (!client)
237 return JSValue::encode(jsUndefined());
238
239 auto label = valueOrDefaultLabelString(exec);
240 RETURN_IF_EXCEPTION(scope, encodedJSValue());
241
242 client->count(exec, label);
243 return JSValue::encode(jsUndefined());
244}
245
246static EncodedJSValue JSC_HOST_CALL consoleProtoFuncCountReset(ExecState* exec)
247{
248 auto scope = DECLARE_THROW_SCOPE(exec->vm());
249 auto* client = exec->lexicalGlobalObject()->consoleClient();
250 if (!client)
251 return JSValue::encode(jsUndefined());
252
253 auto label = valueOrDefaultLabelString(exec);
254 RETURN_IF_EXCEPTION(scope, encodedJSValue());
255
256 client->countReset(exec, label);
257 return JSValue::encode(jsUndefined());
258}
259
260static EncodedJSValue JSC_HOST_CALL consoleProtoFuncProfile(ExecState* exec)
261{
262 VM& vm = exec->vm();
263 auto scope = DECLARE_THROW_SCOPE(vm);
264 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
265 if (!client)
266 return JSValue::encode(jsUndefined());
267
268 size_t argsCount = exec->argumentCount();
269 if (!argsCount) {
270 client->profile(exec, String());
271 return JSValue::encode(jsUndefined());
272 }
273
274 const String& title(valueToStringWithUndefinedOrNullCheck(exec, exec->argument(0)));
275 RETURN_IF_EXCEPTION(scope, encodedJSValue());
276
277 client->profile(exec, title);
278 return JSValue::encode(jsUndefined());
279}
280
281static EncodedJSValue JSC_HOST_CALL consoleProtoFuncProfileEnd(ExecState* exec)
282{
283 VM& vm = exec->vm();
284 auto scope = DECLARE_THROW_SCOPE(vm);
285 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
286 if (!client)
287 return JSValue::encode(jsUndefined());
288
289 size_t argsCount = exec->argumentCount();
290 if (!argsCount) {
291 client->profileEnd(exec, String());
292 return JSValue::encode(jsUndefined());
293 }
294
295 const String& title(valueToStringWithUndefinedOrNullCheck(exec, exec->argument(0)));
296 RETURN_IF_EXCEPTION(scope, encodedJSValue());
297
298 client->profileEnd(exec, title);
299 return JSValue::encode(jsUndefined());
300}
301
302static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTakeHeapSnapshot(ExecState* exec)
303{
304 VM& vm = exec->vm();
305 auto scope = DECLARE_THROW_SCOPE(vm);
306 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
307 if (!client)
308 return JSValue::encode(jsUndefined());
309
310 size_t argsCount = exec->argumentCount();
311 if (!argsCount) {
312 client->takeHeapSnapshot(exec, String());
313 return JSValue::encode(jsUndefined());
314 }
315
316 const String& title(valueToStringWithUndefinedOrNullCheck(exec, exec->argument(0)));
317 RETURN_IF_EXCEPTION(scope, encodedJSValue());
318
319 client->takeHeapSnapshot(exec, title);
320 return JSValue::encode(jsUndefined());
321}
322
323static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTime(ExecState* exec)
324{
325 auto scope = DECLARE_THROW_SCOPE(exec->vm());
326 auto* client = exec->lexicalGlobalObject()->consoleClient();
327 if (!client)
328 return JSValue::encode(jsUndefined());
329
330 auto label = valueOrDefaultLabelString(exec);
331 RETURN_IF_EXCEPTION(scope, encodedJSValue());
332
333 client->time(exec, label);
334 return JSValue::encode(jsUndefined());
335}
336
337static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTimeLog(ExecState* exec)
338{
339 auto scope = DECLARE_THROW_SCOPE(exec->vm());
340 auto* client = exec->lexicalGlobalObject()->consoleClient();
341 if (!client)
342 return JSValue::encode(jsUndefined());
343
344 auto label = valueOrDefaultLabelString(exec);
345 RETURN_IF_EXCEPTION(scope, encodedJSValue());
346
347 client->timeLog(exec, label, Inspector::createScriptArguments(exec, 1));
348 return JSValue::encode(jsUndefined());
349}
350
351static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTimeEnd(ExecState* exec)
352{
353 auto scope = DECLARE_THROW_SCOPE(exec->vm());
354 auto* client = exec->lexicalGlobalObject()->consoleClient();
355 if (!client)
356 return JSValue::encode(jsUndefined());
357
358 auto label = valueOrDefaultLabelString(exec);
359 RETURN_IF_EXCEPTION(scope, encodedJSValue());
360
361 client->timeEnd(exec, label);
362 return JSValue::encode(jsUndefined());
363}
364
365static EncodedJSValue JSC_HOST_CALL consoleProtoFuncTimeStamp(ExecState* exec)
366{
367 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
368 if (!client)
369 return JSValue::encode(jsUndefined());
370
371 client->timeStamp(exec, Inspector::createScriptArguments(exec, 0));
372 return JSValue::encode(jsUndefined());
373}
374
375static EncodedJSValue JSC_HOST_CALL consoleProtoFuncGroup(ExecState* exec)
376{
377 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
378 if (!client)
379 return JSValue::encode(jsUndefined());
380
381 client->group(exec, Inspector::createScriptArguments(exec, 0));
382 return JSValue::encode(jsUndefined());
383}
384
385static EncodedJSValue JSC_HOST_CALL consoleProtoFuncGroupCollapsed(ExecState* exec)
386{
387 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
388 if (!client)
389 return JSValue::encode(jsUndefined());
390
391 client->groupCollapsed(exec, Inspector::createScriptArguments(exec, 0));
392 return JSValue::encode(jsUndefined());
393}
394
395static EncodedJSValue JSC_HOST_CALL consoleProtoFuncGroupEnd(ExecState* exec)
396{
397 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
398 if (!client)
399 return JSValue::encode(jsUndefined());
400
401 client->groupEnd(exec, Inspector::createScriptArguments(exec, 0));
402 return JSValue::encode(jsUndefined());
403}
404
405static EncodedJSValue JSC_HOST_CALL consoleProtoFuncRecord(ExecState* exec)
406{
407 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
408 if (!client)
409 return JSValue::encode(jsUndefined());
410
411 client->record(exec, Inspector::createScriptArguments(exec, 0));
412 return JSValue::encode(jsUndefined());
413}
414
415static EncodedJSValue JSC_HOST_CALL consoleProtoFuncRecordEnd(ExecState* exec)
416{
417 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
418 if (!client)
419 return JSValue::encode(jsUndefined());
420
421 client->recordEnd(exec, Inspector::createScriptArguments(exec, 0));
422 return JSValue::encode(jsUndefined());
423}
424
425static EncodedJSValue JSC_HOST_CALL consoleProtoFuncScreenshot(ExecState* exec)
426{
427 ConsoleClient* client = exec->lexicalGlobalObject()->consoleClient();
428 if (!client)
429 return JSValue::encode(jsUndefined());
430
431 client->screenshot(exec, Inspector::createScriptArguments(exec, 0));
432 return JSValue::encode(jsUndefined());
433}
434
435} // namespace JSC
436