/src/serenity/Userland/Libraries/LibJS/Runtime/ConsoleObject.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> |
3 | | * Copyright (c) 2020-2023, Linus Groh <linusg@serenityos.org> |
4 | | * Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com> |
5 | | * |
6 | | * SPDX-License-Identifier: BSD-2-Clause |
7 | | */ |
8 | | |
9 | | #include <LibJS/Console.h> |
10 | | #include <LibJS/Runtime/ConsoleObject.h> |
11 | | #include <LibJS/Runtime/ConsoleObjectPrototype.h> |
12 | | #include <LibJS/Runtime/GlobalObject.h> |
13 | | |
14 | | namespace JS { |
15 | | |
16 | | JS_DEFINE_ALLOCATOR(ConsoleObject); |
17 | | |
18 | | static NonnullGCPtr<ConsoleObjectPrototype> create_console_prototype(Realm& realm) |
19 | 0 | { |
20 | 0 | return realm.heap().allocate<ConsoleObjectPrototype>(realm, realm); |
21 | 0 | } |
22 | | |
23 | | ConsoleObject::ConsoleObject(Realm& realm) |
24 | 0 | : Object(ConstructWithPrototypeTag::Tag, create_console_prototype(realm)) |
25 | 0 | { |
26 | 0 | } |
27 | | |
28 | 0 | ConsoleObject::~ConsoleObject() = default; |
29 | | |
30 | | void ConsoleObject::visit_edges(Visitor& visitor) |
31 | 0 | { |
32 | 0 | Base::visit_edges(visitor); |
33 | 0 | visitor.visit(m_console); |
34 | 0 | } |
35 | | |
36 | | void ConsoleObject::initialize(Realm& realm) |
37 | 0 | { |
38 | 0 | auto& vm = this->vm(); |
39 | 0 | Base::initialize(realm); |
40 | 0 | m_console = vm.heap().allocate<Console>(realm, realm); |
41 | 0 | u8 attr = Attribute::Writable | Attribute::Enumerable | Attribute::Configurable; |
42 | 0 | define_native_function(realm, vm.names.assert, assert_, 0, attr); |
43 | 0 | define_native_function(realm, vm.names.clear, clear, 0, attr); |
44 | 0 | define_native_function(realm, vm.names.debug, debug, 0, attr); |
45 | 0 | define_native_function(realm, vm.names.error, error, 0, attr); |
46 | 0 | define_native_function(realm, vm.names.info, info, 0, attr); |
47 | 0 | define_native_function(realm, vm.names.log, log, 0, attr); |
48 | 0 | define_native_function(realm, vm.names.table, table, 0, attr); |
49 | 0 | define_native_function(realm, vm.names.trace, trace, 0, attr); |
50 | 0 | define_native_function(realm, vm.names.warn, warn, 0, attr); |
51 | 0 | define_native_function(realm, vm.names.dir, dir, 0, attr); |
52 | 0 | define_native_function(realm, vm.names.count, count, 0, attr); |
53 | 0 | define_native_function(realm, vm.names.countReset, count_reset, 0, attr); |
54 | 0 | define_native_function(realm, vm.names.group, group, 0, attr); |
55 | 0 | define_native_function(realm, vm.names.groupCollapsed, group_collapsed, 0, attr); |
56 | 0 | define_native_function(realm, vm.names.groupEnd, group_end, 0, attr); |
57 | 0 | define_native_function(realm, vm.names.time, time, 0, attr); |
58 | 0 | define_native_function(realm, vm.names.timeLog, time_log, 0, attr); |
59 | 0 | define_native_function(realm, vm.names.timeEnd, time_end, 0, attr); |
60 | |
|
61 | 0 | define_direct_property(vm.well_known_symbol_to_string_tag(), PrimitiveString::create(vm, "console"_string), Attribute::Configurable); |
62 | 0 | } |
63 | | |
64 | | // 1.1.1. assert(condition, ...data), https://console.spec.whatwg.org/#assert |
65 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::assert_) |
66 | 0 | { |
67 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
68 | 0 | return console_object.console().assert_(); |
69 | 0 | } |
70 | | |
71 | | // 1.1.2. clear(), https://console.spec.whatwg.org/#clear |
72 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::clear) |
73 | 0 | { |
74 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
75 | 0 | return console_object.console().clear(); |
76 | 0 | } |
77 | | |
78 | | // 1.1.3. debug(...data), https://console.spec.whatwg.org/#debug |
79 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::debug) |
80 | 0 | { |
81 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
82 | 0 | return console_object.console().debug(); |
83 | 0 | } |
84 | | |
85 | | // 1.1.4. error(...data), https://console.spec.whatwg.org/#error |
86 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::error) |
87 | 0 | { |
88 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
89 | 0 | return console_object.console().error(); |
90 | 0 | } |
91 | | |
92 | | // 1.1.5. info(...data), https://console.spec.whatwg.org/#info |
93 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::info) |
94 | 0 | { |
95 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
96 | 0 | return console_object.console().info(); |
97 | 0 | } |
98 | | |
99 | | // 1.1.6. log(...data), https://console.spec.whatwg.org/#log |
100 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::log) |
101 | 0 | { |
102 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
103 | 0 | return console_object.console().log(); |
104 | 0 | } |
105 | | |
106 | | // 1.1.7. table(tabularData, properties), https://console.spec.whatwg.org/#table |
107 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::table) |
108 | 0 | { |
109 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
110 | 0 | return console_object.console().table(); |
111 | 0 | } |
112 | | |
113 | | // 1.1.8. trace(...data), https://console.spec.whatwg.org/#trace |
114 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::trace) |
115 | 0 | { |
116 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
117 | 0 | return console_object.console().trace(); |
118 | 0 | } |
119 | | |
120 | | // 1.1.9. warn(...data), https://console.spec.whatwg.org/#warn |
121 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::warn) |
122 | 0 | { |
123 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
124 | 0 | return console_object.console().warn(); |
125 | 0 | } |
126 | | |
127 | | // 1.1.10. dir(item, options), https://console.spec.whatwg.org/#warn |
128 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::dir) |
129 | 0 | { |
130 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
131 | 0 | return console_object.console().dir(); |
132 | 0 | } |
133 | | |
134 | | // 1.2.1. count(label), https://console.spec.whatwg.org/#count |
135 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::count) |
136 | 0 | { |
137 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
138 | 0 | return console_object.console().count(); |
139 | 0 | } |
140 | | |
141 | | // 1.2.2. countReset(label), https://console.spec.whatwg.org/#countreset |
142 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::count_reset) |
143 | 0 | { |
144 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
145 | 0 | return console_object.console().count_reset(); |
146 | 0 | } |
147 | | |
148 | | // 1.3.1. group(...data), https://console.spec.whatwg.org/#group |
149 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::group) |
150 | 0 | { |
151 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
152 | 0 | return console_object.console().group(); |
153 | 0 | } |
154 | | |
155 | | // 1.3.2. groupCollapsed(...data), https://console.spec.whatwg.org/#groupcollapsed |
156 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::group_collapsed) |
157 | 0 | { |
158 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
159 | 0 | return console_object.console().group_collapsed(); |
160 | 0 | } |
161 | | |
162 | | // 1.3.3. groupEnd(), https://console.spec.whatwg.org/#groupend |
163 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::group_end) |
164 | 0 | { |
165 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
166 | 0 | return console_object.console().group_end(); |
167 | 0 | } |
168 | | |
169 | | // 1.4.1. time(label), https://console.spec.whatwg.org/#time |
170 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::time) |
171 | 0 | { |
172 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
173 | 0 | return console_object.console().time(); |
174 | 0 | } |
175 | | |
176 | | // 1.4.2. timeLog(label, ...data), https://console.spec.whatwg.org/#timelog |
177 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::time_log) |
178 | 0 | { |
179 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
180 | 0 | return console_object.console().time_log(); |
181 | 0 | } |
182 | | |
183 | | // 1.4.3. timeEnd(label), https://console.spec.whatwg.org/#timeend |
184 | | JS_DEFINE_NATIVE_FUNCTION(ConsoleObject::time_end) |
185 | 0 | { |
186 | 0 | auto& console_object = *vm.current_realm()->intrinsics().console_object(); |
187 | 0 | return console_object.console().time_end(); |
188 | 0 | } |
189 | | |
190 | | } |