Skip to content

Commit 54271c2

Browse files
alexkozyCommit bot
authored andcommitted
[inspector] move console to builtins
What will we get: - console would be included into snapshot and allow us to reduce time that we spent in contextCreated function (~5 times faster), - it allows us to make further small improvement of console methods, e.g. we can implement super quick return from console.assert if first argument is true, - console calls are ~ 15% faster. CQ_INCLUDE_TRYBOTS=master.tryserver.blink:linux_trusty_blink_rel;master.tryserver.chromium.linux:linux_chromium_rel_ng BUG=v8:6175 R=dgozman@chromium.org Review-Url: https://codereview.chromium.org/2785293002 Cr-Original-Original-Original-Commit-Position: refs/heads/master@{#44353} Committed: https://chromium.googlesource.com/v8/v8/+/55905f85d63d75aaa9313e51eb7bede754a8e41c Review-Url: https://codereview.chromium.org/2785293002 Cr-Original-Original-Commit-Position: refs/heads/master@{#44355} Committed: https://chromium.googlesource.com/v8/v8/+/cc74ea0bc4fe4a71fa53d08b62cc18d15e01fbb3 Review-Url: https://codereview.chromium.org/2785293002 Cr-Original-Commit-Position: refs/heads/master@{#44416} Committed: https://chromium.googlesource.com/v8/v8/+/f5dc738cda0336929dcb8eb73b87fdcd01b7c685 Review-Url: https://codereview.chromium.org/2785293002 Cr-Commit-Position: refs/heads/master@{#44702}
1 parent 5971023 commit 54271c2

File tree

17 files changed

+387
-161
lines changed

17 files changed

+387
-161
lines changed

BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,7 @@ v8_source_set("v8_base") {
11151115
"src/builtins/builtins-boolean.cc",
11161116
"src/builtins/builtins-call.cc",
11171117
"src/builtins/builtins-callsite.cc",
1118+
"src/builtins/builtins-console.cc",
11181119
"src/builtins/builtins-constructor.h",
11191120
"src/builtins/builtins-dataview.cc",
11201121
"src/builtins/builtins-date.cc",

include/v8.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ class FunctionCallbackArguments;
150150
class GlobalHandles;
151151
} // namespace internal
152152

153+
namespace debug {
154+
class ConsoleCallArguments;
155+
} // namespace debug
153156

154157
// --- Handles ---
155158

@@ -3602,6 +3605,7 @@ class FunctionCallbackInfo {
36023605
protected:
36033606
friend class internal::FunctionCallbackArguments;
36043607
friend class internal::CustomArguments<FunctionCallbackInfo>;
3608+
friend class debug::ConsoleCallArguments;
36053609
static const int kHolderIndex = 0;
36063610
static const int kIsolateIndex = 1;
36073611
static const int kReturnValueDefaultValueIndex = 2;

src/api.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "src/base/safe_conversions.h"
2626
#include "src/base/utils/random-number-generator.h"
2727
#include "src/bootstrapper.h"
28+
#include "src/builtins/builtins-utils.h"
2829
#include "src/char-predicates-inl.h"
2930
#include "src/code-stubs.h"
3031
#include "src/compiler-dispatcher/compiler-dispatcher.h"
@@ -9671,6 +9672,22 @@ Local<Function> debug::GetBuiltin(Isolate* v8_isolate, Builtin builtin) {
96719672
return Utils::ToLocal(handle_scope.CloseAndEscape(fun));
96729673
}
96739674

9675+
void debug::SetConsoleDelegate(Isolate* v8_isolate, ConsoleDelegate* delegate) {
9676+
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
9677+
ENTER_V8(isolate);
9678+
isolate->set_console_delegate(delegate);
9679+
}
9680+
9681+
debug::ConsoleCallArguments::ConsoleCallArguments(
9682+
const v8::FunctionCallbackInfo<v8::Value>& info)
9683+
: v8::FunctionCallbackInfo<v8::Value>(nullptr, info.values_, info.length_) {
9684+
}
9685+
9686+
debug::ConsoleCallArguments::ConsoleCallArguments(
9687+
internal::BuiltinArguments& args)
9688+
: v8::FunctionCallbackInfo<v8::Value>(nullptr, &args[0] - 1,
9689+
args.length() - 1) {}
9690+
96749691
MaybeLocal<debug::Script> debug::GeneratorObject::Script() {
96759692
i::Handle<i::JSGeneratorObject> obj = Utils::OpenHandle(this);
96769693
i::Object* maybe_script = obj->function()->shared()->script();

src/bootstrapper.cc

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,6 +2558,54 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
25582558
static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
25592559
}
25602560

2561+
{ // -- C o n s o l e
2562+
Handle<String> name = factory->InternalizeUtf8String("console");
2563+
Handle<JSFunction> cons = factory->NewFunction(name);
2564+
Handle<JSObject> empty = factory->NewJSObject(isolate->object_function());
2565+
JSFunction::SetInstancePrototype(cons, empty);
2566+
Handle<JSObject> console = factory->NewJSObject(cons, TENURED);
2567+
DCHECK(console->IsJSObject());
2568+
JSObject::AddProperty(global, name, console, DONT_ENUM);
2569+
SimpleInstallFunction(console, "debug", Builtins::kConsoleDebug, 1, false);
2570+
SimpleInstallFunction(console, "error", Builtins::kConsoleError, 1, false);
2571+
SimpleInstallFunction(console, "info", Builtins::kConsoleInfo, 1, false);
2572+
SimpleInstallFunction(console, "log", Builtins::kConsoleLog, 1, false);
2573+
SimpleInstallFunction(console, "warn", Builtins::kConsoleWarn, 1, false);
2574+
SimpleInstallFunction(console, "dir", Builtins::kConsoleDir, 1, false);
2575+
SimpleInstallFunction(console, "dirxml", Builtins::kConsoleDirXml, 1,
2576+
false);
2577+
SimpleInstallFunction(console, "table", Builtins::kConsoleTable, 1, false);
2578+
SimpleInstallFunction(console, "trace", Builtins::kConsoleTrace, 1, false);
2579+
SimpleInstallFunction(console, "group", Builtins::kConsoleGroup, 1, false);
2580+
SimpleInstallFunction(console, "groupCollapsed",
2581+
Builtins::kConsoleGroupCollapsed, 1, false);
2582+
SimpleInstallFunction(console, "groupEnd", Builtins::kConsoleGroupEnd, 1,
2583+
false);
2584+
SimpleInstallFunction(console, "clear", Builtins::kConsoleClear, 1, false);
2585+
SimpleInstallFunction(console, "count", Builtins::kConsoleCount, 1, false);
2586+
SimpleInstallFunction(console, "assert", Builtins::kConsoleAssert, 1,
2587+
false);
2588+
SimpleInstallFunction(console, "markTimeline",
2589+
Builtins::kConsoleMarkTimeline, 1, false);
2590+
SimpleInstallFunction(console, "profile", Builtins::kConsoleProfile, 1,
2591+
false);
2592+
SimpleInstallFunction(console, "profileEnd", Builtins::kConsoleProfileEnd,
2593+
1, false);
2594+
SimpleInstallFunction(console, "timeline", Builtins::kConsoleTimeline, 1,
2595+
false);
2596+
SimpleInstallFunction(console, "timelineEnd", Builtins::kConsoleTimelineEnd,
2597+
1, false);
2598+
SimpleInstallFunction(console, "time", Builtins::kConsoleTime, 1, false);
2599+
SimpleInstallFunction(console, "timeEnd", Builtins::kConsoleTimeEnd, 1,
2600+
false);
2601+
SimpleInstallFunction(console, "timeStamp", Builtins::kConsoleTimeStamp, 1,
2602+
false);
2603+
JSObject::AddProperty(
2604+
console, factory->to_string_tag_symbol(),
2605+
factory->NewStringFromAsciiChecked("Object"),
2606+
static_cast<PropertyAttributes>(DONT_ENUM | READ_ONLY));
2607+
}
2608+
25612609
#ifdef V8_I18N_SUPPORT
25622610
{ // -- I n t l
25632611
Handle<String> name = factory->InternalizeUtf8String("Intl");

src/builtins/builtins-console.cc

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2017 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "src/builtins/builtins-utils.h"
6+
#include "src/builtins/builtins.h"
7+
8+
#include "src/debug/interface-types.h"
9+
#include "src/objects-inl.h"
10+
11+
namespace v8 {
12+
namespace internal {
13+
14+
// -----------------------------------------------------------------------------
15+
// Console
16+
17+
#define CONSOLE_METHOD_LIST(V) \
18+
V(Debug) \
19+
V(Error) \
20+
V(Info) \
21+
V(Log) \
22+
V(Warn) \
23+
V(Dir) \
24+
V(DirXml) \
25+
V(Table) \
26+
V(Trace) \
27+
V(Group) \
28+
V(GroupCollapsed) \
29+
V(GroupEnd) \
30+
V(Clear) \
31+
V(Count) \
32+
V(Assert) \
33+
V(MarkTimeline) \
34+
V(Profile) \
35+
V(ProfileEnd) \
36+
V(Timeline) \
37+
V(TimelineEnd) \
38+
V(Time) \
39+
V(TimeEnd) \
40+
V(TimeStamp)
41+
42+
#define CONSOLE_BUILTIN_IMPLEMENTATION(name) \
43+
BUILTIN(Console##name) { \
44+
HandleScope scope(isolate); \
45+
if (isolate->console_delegate()) { \
46+
debug::ConsoleCallArguments wrapper(args); \
47+
isolate->console_delegate()->name(wrapper); \
48+
} \
49+
return isolate->heap()->undefined_value(); \
50+
}
51+
CONSOLE_METHOD_LIST(CONSOLE_BUILTIN_IMPLEMENTATION)
52+
#undef CONSOLE_BUILTIN_IMPLEMENTATION
53+
54+
#undef CONSOLE_METHOD_LIST
55+
56+
} // namespace internal
57+
} // namespace v8

src/builtins/builtins-definitions.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,31 @@ namespace internal {
345345
CPP(CallSitePrototypeIsToplevel) \
346346
CPP(CallSitePrototypeToString) \
347347
\
348+
/* Console */ \
349+
CPP(ConsoleDebug) \
350+
CPP(ConsoleError) \
351+
CPP(ConsoleInfo) \
352+
CPP(ConsoleLog) \
353+
CPP(ConsoleWarn) \
354+
CPP(ConsoleDir) \
355+
CPP(ConsoleDirXml) \
356+
CPP(ConsoleTable) \
357+
CPP(ConsoleTrace) \
358+
CPP(ConsoleGroup) \
359+
CPP(ConsoleGroupCollapsed) \
360+
CPP(ConsoleGroupEnd) \
361+
CPP(ConsoleClear) \
362+
CPP(ConsoleCount) \
363+
CPP(ConsoleAssert) \
364+
CPP(ConsoleMarkTimeline) \
365+
CPP(ConsoleProfile) \
366+
CPP(ConsoleProfileEnd) \
367+
CPP(ConsoleTimeline) \
368+
CPP(ConsoleTimelineEnd) \
369+
CPP(ConsoleTime) \
370+
CPP(ConsoleTimeEnd) \
371+
CPP(ConsoleTimeStamp) \
372+
\
348373
/* DataView */ \
349374
CPP(DataViewConstructor) \
350375
CPP(DataViewConstructor_ConstructStub) \

src/debug/debug-interface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ enum Builtin {
209209

210210
Local<Function> GetBuiltin(Isolate* isolate, Builtin builtin);
211211

212+
void SetConsoleDelegate(Isolate* isolate, ConsoleDelegate* delegate);
213+
212214
/**
213215
* Native wrapper around v8::internal::JSGeneratorObject object.
214216
*/

src/debug/interface-types.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@
99
#include <string>
1010
#include <vector>
1111

12+
#include "include/v8.h"
1213
#include "src/globals.h"
1314

1415
namespace v8 {
16+
17+
namespace internal {
18+
class BuiltinArguments;
19+
} // internal
20+
1521
namespace debug {
1622

1723
/**
@@ -90,6 +96,47 @@ class V8_EXPORT_PRIVATE BreakLocation : public Location {
9096
BreakLocationType type_;
9197
};
9298

99+
class ConsoleCallArguments : private v8::FunctionCallbackInfo<v8::Value> {
100+
public:
101+
int Length() const { return v8::FunctionCallbackInfo<v8::Value>::Length(); }
102+
V8_INLINE Local<Value> operator[](int i) const {
103+
return v8::FunctionCallbackInfo<v8::Value>::operator[](i);
104+
}
105+
106+
explicit ConsoleCallArguments(const v8::FunctionCallbackInfo<v8::Value>&);
107+
explicit ConsoleCallArguments(internal::BuiltinArguments&);
108+
};
109+
110+
// v8::FunctionCallbackInfo could be used for getting arguments only. Calling
111+
// of any other getter will produce a crash.
112+
class ConsoleDelegate {
113+
public:
114+
virtual void Debug(const ConsoleCallArguments& args) {}
115+
virtual void Error(const ConsoleCallArguments& args) {}
116+
virtual void Info(const ConsoleCallArguments& args) {}
117+
virtual void Log(const ConsoleCallArguments& args) {}
118+
virtual void Warn(const ConsoleCallArguments& args) {}
119+
virtual void Dir(const ConsoleCallArguments& args) {}
120+
virtual void DirXml(const ConsoleCallArguments& args) {}
121+
virtual void Table(const ConsoleCallArguments& args) {}
122+
virtual void Trace(const ConsoleCallArguments& args) {}
123+
virtual void Group(const ConsoleCallArguments& args) {}
124+
virtual void GroupCollapsed(const ConsoleCallArguments& args) {}
125+
virtual void GroupEnd(const ConsoleCallArguments& args) {}
126+
virtual void Clear(const ConsoleCallArguments& args) {}
127+
virtual void Count(const ConsoleCallArguments& args) {}
128+
virtual void Assert(const ConsoleCallArguments& args) {}
129+
virtual void MarkTimeline(const ConsoleCallArguments& args) {}
130+
virtual void Profile(const ConsoleCallArguments& args) {}
131+
virtual void ProfileEnd(const ConsoleCallArguments& args) {}
132+
virtual void Timeline(const ConsoleCallArguments& args) {}
133+
virtual void TimelineEnd(const ConsoleCallArguments& args) {}
134+
virtual void Time(const ConsoleCallArguments& args) {}
135+
virtual void TimeEnd(const ConsoleCallArguments& args) {}
136+
virtual void TimeStamp(const ConsoleCallArguments& args) {}
137+
virtual ~ConsoleDelegate() = default;
138+
};
139+
93140
} // namespace debug
94141
} // namespace v8
95142

src/inspector/inspected-context.cc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,16 @@ InspectedContext::InspectedContext(V8InspectorImpl* inspector,
2525
m_humanReadableName(toString16(info.humanReadableName)),
2626
m_auxData(toString16(info.auxData)),
2727
m_reported(false) {
28-
v8::Isolate* isolate = m_inspector->isolate();
2928
v8::debug::SetContextId(info.context, contextId);
29+
if (!info.hasMemoryOnConsole) return;
30+
v8::Context::Scope contextScope(info.context);
3031
v8::Local<v8::Object> global = info.context->Global();
31-
v8::Local<v8::Object> console =
32-
m_inspector->console()->createConsole(info.context);
33-
if (info.hasMemoryOnConsole) {
34-
m_inspector->console()->installMemoryGetter(info.context, console);
35-
}
36-
if (!global
37-
->Set(info.context, toV8StringInternalized(isolate, "console"),
38-
console)
39-
.FromMaybe(false)) {
40-
return;
32+
v8::Local<v8::Value> console;
33+
if (global->Get(info.context, toV8String(m_inspector->isolate(), "console"))
34+
.ToLocal(&console) &&
35+
console->IsObject()) {
36+
m_inspector->console()->installMemoryGetter(
37+
info.context, v8::Local<v8::Object>::Cast(console));
4138
}
4239
}
4340

0 commit comments

Comments
 (0)