Commit 18c80bc8 authored by yangguo's avatar yangguo Committed by Commit bot

[d8] remove utility context.

We only use it to store the Stringify function to format
REPL output. This is overkill and introduces issues with
security tokens.

R=jochen@chromium.org
BUG=

Review URL: https://codereview.chromium.org/1845833002

Cr-Commit-Position: refs/heads/master@{#35158}
parent 35fa419a
...@@ -251,7 +251,7 @@ CounterCollection* Shell::counters_ = &local_counters_; ...@@ -251,7 +251,7 @@ CounterCollection* Shell::counters_ = &local_counters_;
base::LazyMutex Shell::context_mutex_; base::LazyMutex Shell::context_mutex_;
const base::TimeTicks Shell::kInitialTicks = const base::TimeTicks Shell::kInitialTicks =
base::TimeTicks::HighResolutionNow(); base::TimeTicks::HighResolutionNow();
Global<Context> Shell::utility_context_; Global<Function> Shell::stringify_function_;
base::LazyMutex Shell::workers_mutex_; base::LazyMutex Shell::workers_mutex_;
bool Shell::allow_new_workers_ = true; bool Shell::allow_new_workers_ = true;
i::List<Worker*> Shell::workers_; i::List<Worker*> Shell::workers_;
...@@ -412,24 +412,7 @@ bool Shell::ExecuteString(Isolate* isolate, Local<String> source, ...@@ -412,24 +412,7 @@ bool Shell::ExecuteString(Isolate* isolate, Local<String> source,
} }
#if !defined(V8_SHARED) #if !defined(V8_SHARED)
} else { } else {
v8::TryCatch try_catch(isolate); v8::String::Utf8Value str(Stringify(isolate, result));
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate, utility_context_);
v8::Context::Scope context_scope(context);
Local<Object> global = context->Global();
Local<Value> fun =
global->Get(context, String::NewFromUtf8(isolate, "Stringify",
v8::NewStringType::kNormal)
.ToLocalChecked()).ToLocalChecked();
Local<Value> argv[1] = {result};
Local<Value> s;
if (!Local<Function>::Cast(fun)
->Call(context, global, 1, argv)
.ToLocal(&s)) {
return true;
}
DCHECK(!try_catch.HasCaught());
v8::String::Utf8Value str(s);
fwrite(*str, sizeof(**str), str.length(), stdout); fwrite(*str, sizeof(**str), str.length(), stdout);
printf("\n"); printf("\n");
} }
...@@ -906,11 +889,11 @@ void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) { ...@@ -906,11 +889,11 @@ void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) {
void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) {
HandleScope handle_scope(isolate); HandleScope handle_scope(isolate);
#ifndef V8_SHARED #ifndef V8_SHARED
Local<Context> utility_context; Local<Context> context;
bool enter_context = !isolate->InContext(); bool enter_context = !isolate->InContext();
if (enter_context) { if (enter_context) {
utility_context = Local<Context>::New(isolate, utility_context_); context = Local<Context>::New(isolate, evaluation_context_);
utility_context->Enter(); context->Enter();
} }
#endif // !V8_SHARED #endif // !V8_SHARED
v8::String::Utf8Value exception(try_catch->Exception()); v8::String::Utf8Value exception(try_catch->Exception());
...@@ -954,7 +937,7 @@ void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { ...@@ -954,7 +937,7 @@ void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) {
} }
printf("\n"); printf("\n");
#ifndef V8_SHARED #ifndef V8_SHARED
if (enter_context) utility_context->Exit(); if (enter_context) context->Exit();
#endif // !V8_SHARED #endif // !V8_SHARED
} }
...@@ -1057,47 +1040,37 @@ void Shell::AddHistogramSample(void* histogram, int sample) { ...@@ -1057,47 +1040,37 @@ void Shell::AddHistogramSample(void* histogram, int sample) {
counter->AddSample(sample); counter->AddSample(sample);
} }
// Turn a value into a human-readable string.
void Shell::InstallUtilityScript(Isolate* isolate) { Local<String> Shell::Stringify(Isolate* isolate, Local<Value> value) {
HandleScope scope(isolate); v8::Local<v8::Context> context =
// If we use the utility context, we have to set the security tokens so that
// utility, evaluation and debug context can all access each other.
Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate);
utility_context_.Reset(isolate, Context::New(isolate, NULL, global_template));
v8::Local<v8::Context> utility_context =
v8::Local<v8::Context>::New(isolate, utility_context_);
v8::Local<v8::Context> evaluation_context =
v8::Local<v8::Context>::New(isolate, evaluation_context_); v8::Local<v8::Context>::New(isolate, evaluation_context_);
utility_context->SetSecurityToken(Undefined(isolate)); if (stringify_function_.IsEmpty()) {
evaluation_context->SetSecurityToken(Undefined(isolate));
v8::Context::Scope context_scope(utility_context);
// Run the d8 shell utility script in the utility context
int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); int source_index = i::NativesCollection<i::D8>::GetIndex("d8");
i::Vector<const char> shell_source = i::Vector<const char> source_string =
i::NativesCollection<i::D8>::GetScriptSource(source_index); i::NativesCollection<i::D8>::GetScriptSource(source_index);
i::Vector<const char> shell_source_name = i::Vector<const char> source_name =
i::NativesCollection<i::D8>::GetScriptName(source_index); i::NativesCollection<i::D8>::GetScriptName(source_index);
Local<String> source = Local<String> source =
String::NewFromUtf8(isolate, shell_source.start(), NewStringType::kNormal, String::NewFromUtf8(isolate, source_string.start(),
shell_source.length()).ToLocalChecked(); NewStringType::kNormal, source_string.length())
.ToLocalChecked();
Local<String> name = Local<String> name =
String::NewFromUtf8(isolate, shell_source_name.start(), String::NewFromUtf8(isolate, source_name.start(),
NewStringType::kNormal, NewStringType::kNormal, source_name.length())
shell_source_name.length()).ToLocalChecked(); .ToLocalChecked();
ScriptOrigin origin(name); ScriptOrigin origin(name);
Local<Script> script = Local<Script> script =
Script::Compile(utility_context, source, &origin).ToLocalChecked(); Script::Compile(context, source, &origin).ToLocalChecked();
script->Run(utility_context).ToLocalChecked(); stringify_function_.Reset(
// Mark the d8 shell script as native to avoid it showing up as normal source isolate, script->Run(context).ToLocalChecked().As<Function>());
// in the debugger. }
i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); Local<Function> fun = Local<Function>::New(isolate, stringify_function_);
i::Handle<i::Script> script_object = compiled_script->IsJSFunction() Local<Value> argv[1] = {value};
? i::Handle<i::Script>(i::Script::cast( v8::TryCatch try_catch(isolate);
i::JSFunction::cast(*compiled_script)->shared()->script())) MaybeLocal<Value> result =
: i::Handle<i::Script>(i::Script::cast( fun->Call(context, Undefined(isolate), 1, argv).ToLocalChecked();
i::SharedFunctionInfo::cast(*compiled_script)->script())); if (result.IsEmpty()) return String::Empty(isolate);
script_object->set_type(i::Script::TYPE_EXTENSION); return result.ToLocalChecked().As<String>();
} }
#endif // !V8_SHARED #endif // !V8_SHARED
...@@ -2220,8 +2193,6 @@ MaybeLocal<Value> Shell::DeserializeValue(Isolate* isolate, ...@@ -2220,8 +2193,6 @@ MaybeLocal<Value> Shell::DeserializeValue(Isolate* isolate,
int* offset) { int* offset) {
DCHECK(offset); DCHECK(offset);
EscapableHandleScope scope(isolate); EscapableHandleScope scope(isolate);
// This function should not use utility_context_ because it is running on a
// different thread.
Local<Value> result; Local<Value> result;
SerializationTag tag = data.ReadTag(offset); SerializationTag tag = data.ReadTag(offset);
...@@ -2502,17 +2473,11 @@ int Shell::Main(int argc, char* argv[]) { ...@@ -2502,17 +2473,11 @@ int Shell::Main(int argc, char* argv[]) {
// Run interactive shell if explicitly requested or if no script has been // Run interactive shell if explicitly requested or if no script has been
// executed, but never on --test // executed, but never on --test
if (options.use_interactive_shell()) { if (options.use_interactive_shell()) {
#ifndef V8_SHARED
InstallUtilityScript(isolate);
#endif // !V8_SHARED
RunShell(isolate); RunShell(isolate);
} }
// Shut down contexts and collect garbage. // Shut down contexts and collect garbage.
evaluation_context_.Reset(); evaluation_context_.Reset();
#ifndef V8_SHARED
utility_context_.Reset();
#endif // !V8_SHARED
CollectGarbage(isolate); CollectGarbage(isolate);
} }
OnExit(isolate); OnExit(isolate);
......
...@@ -446,7 +446,7 @@ class Shell : public i::AllStatic { ...@@ -446,7 +446,7 @@ class Shell : public i::AllStatic {
static Global<Context> evaluation_context_; static Global<Context> evaluation_context_;
static base::OnceType quit_once_; static base::OnceType quit_once_;
#ifndef V8_SHARED #ifndef V8_SHARED
static Global<Context> utility_context_; static Global<Function> stringify_function_;
static CounterMap* counter_map_; static CounterMap* counter_map_;
// We statically allocate a set of local counters to be used if we // We statically allocate a set of local counters to be used if we
// don't want to store the stats in a memory-mapped file // don't want to store the stats in a memory-mapped file
...@@ -462,7 +462,7 @@ class Shell : public i::AllStatic { ...@@ -462,7 +462,7 @@ class Shell : public i::AllStatic {
static i::List<SharedArrayBuffer::Contents> externalized_shared_contents_; static i::List<SharedArrayBuffer::Contents> externalized_shared_contents_;
static Counter* GetCounter(const char* name, bool is_histogram); static Counter* GetCounter(const char* name, bool is_histogram);
static void InstallUtilityScript(Isolate* isolate); static Local<String> Stringify(Isolate* isolate, Local<Value> value);
#endif // !V8_SHARED #endif // !V8_SHARED
static void Initialize(Isolate* isolate); static void Initialize(Isolate* isolate);
static void RunShell(Isolate* isolate); static void RunShell(Isolate* isolate);
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
(function() {
"use strict"; "use strict";
// A more universal stringify that supports more types than JSON. // A more universal stringify that supports more types than JSON.
...@@ -89,3 +90,6 @@ function StringifyProxy(proxy, depth) { ...@@ -89,3 +90,6 @@ function StringifyProxy(proxy, depth) {
} }
return '[' + proxy_type + ' Proxy ' + Stringify(info_object, depth-1) + ']'; return '[' + proxy_type + ' Proxy ' + Stringify(info_object, depth-1) + ']';
} }
return Stringify;
})();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment