Commit 310ed9eb authored by yangguo@chromium.org's avatar yangguo@chromium.org

removed some unnecessary stuff from d8's initialization

and got rid of the utility context unless interactive shell is used

Review URL: http://codereview.chromium.org/7219003

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@8424 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent ab2c0d45
...@@ -272,6 +272,7 @@ RemoteDebuggerEvent* RemoteDebugger::GetEvent() { ...@@ -272,6 +272,7 @@ RemoteDebuggerEvent* RemoteDebugger::GetEvent() {
void RemoteDebugger::HandleMessageReceived(char* message) { void RemoteDebugger::HandleMessageReceived(char* message) {
Locker lock;
HandleScope scope; HandleScope scope;
// Print the event details. // Print the event details.
...@@ -300,6 +301,7 @@ void RemoteDebugger::HandleMessageReceived(char* message) { ...@@ -300,6 +301,7 @@ void RemoteDebugger::HandleMessageReceived(char* message) {
void RemoteDebugger::HandleKeyboardCommand(char* command) { void RemoteDebugger::HandleKeyboardCommand(char* command) {
Locker lock;
HandleScope scope; HandleScope scope;
// Convert the debugger command to a JSON debugger request. // Convert the debugger command to a JSON debugger request.
......
...@@ -311,10 +311,6 @@ static Handle<Value> GetStdout(int child_fd, ...@@ -311,10 +311,6 @@ static Handle<Value> GetStdout(int child_fd,
int read_timeout, int read_timeout,
int total_timeout) { int total_timeout) {
Handle<String> accumulator = String::Empty(); Handle<String> accumulator = String::Empty();
const char* source = "(function(a, b) { return a + b; })";
Handle<Value> cons_as_obj(Script::Compile(String::New(source))->Run());
Handle<Function> cons_function(Function::Cast(*cons_as_obj));
Handle<Value> cons_args[2];
int fullness = 0; int fullness = 0;
static const int kStdoutReadBufferSize = 4096; static const int kStdoutReadBufferSize = 4096;
...@@ -350,12 +346,7 @@ static Handle<Value> GetStdout(int child_fd, ...@@ -350,12 +346,7 @@ static Handle<Value> GetStdout(int child_fd,
bytes_read + fullness : bytes_read + fullness :
LengthWithoutIncompleteUtf8(buffer, bytes_read + fullness); LengthWithoutIncompleteUtf8(buffer, bytes_read + fullness);
Handle<String> addition = String::New(buffer, length); Handle<String> addition = String::New(buffer, length);
cons_args[0] = accumulator; accumulator = String::Concat(accumulator, addition);
cons_args[1] = addition;
accumulator = Handle<String>::Cast(cons_function->Call(
Shell::utility_context()->Global(),
2,
cons_args));
fullness = bytes_read + fullness - length; fullness = bytes_read + fullness - length;
memcpy(buffer, buffer + length, fullness); memcpy(buffer, buffer + length, fullness);
} }
......
...@@ -489,7 +489,23 @@ void Shell::AddHistogramSample(void* histogram, int sample) { ...@@ -489,7 +489,23 @@ void Shell::AddHistogramSample(void* histogram, int sample) {
void Shell::InstallUtilityScript() { void Shell::InstallUtilityScript() {
Locker lock; Locker lock;
HandleScope scope; HandleScope scope;
// 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.
utility_context_->SetSecurityToken(Undefined());
evaluation_context_->SetSecurityToken(Undefined());
Context::Scope utility_scope(utility_context_); Context::Scope utility_scope(utility_context_);
#ifdef ENABLE_DEBUGGER_SUPPORT
// Install the debugger object in the utility scope
i::Debug* debug = i::Isolate::Current()->debug();
debug->Load();
i::Handle<i::JSObject> js_debug
= i::Handle<i::JSObject>(debug->debug_context()->global());
utility_context_->Global()->Set(String::New("$debug"),
Utils::ToLocal(js_debug));
debug->debug_context()->set_security_token(HEAP->undefined_value());
#endif
// Run the d8 shell utility script in the 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> shell_source =
...@@ -514,6 +530,7 @@ void Shell::InstallUtilityScript() { ...@@ -514,6 +530,7 @@ void Shell::InstallUtilityScript() {
script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE)); script_object->set_type(i::Smi::FromInt(i::Script::TYPE_NATIVE));
} }
#ifdef COMPRESS_STARTUP_DATA_BZ2 #ifdef COMPRESS_STARTUP_DATA_BZ2
class BZip2Decompressor : public v8::StartupDataDecompressor { class BZip2Decompressor : public v8::StartupDataDecompressor {
public: public:
...@@ -585,7 +602,8 @@ Handle<ObjectTemplate> Shell::CreateGlobalTemplate() { ...@@ -585,7 +602,8 @@ Handle<ObjectTemplate> Shell::CreateGlobalTemplate() {
return global_template; return global_template;
} }
void Shell::Initialize() {
void Shell::Initialize(bool test_shell) {
#ifdef COMPRESS_STARTUP_DATA_BZ2 #ifdef COMPRESS_STARTUP_DATA_BZ2
BZip2Decompressor startup_data_decompressor; BZip2Decompressor startup_data_decompressor;
int bz2_result = startup_data_decompressor.Decompress(); int bz2_result = startup_data_decompressor.Decompress();
...@@ -605,22 +623,23 @@ void Shell::Initialize() { ...@@ -605,22 +623,23 @@ void Shell::Initialize() {
V8::SetAddHistogramSampleFunction(AddHistogramSample); V8::SetAddHistogramSampleFunction(AddHistogramSample);
} }
// Initialize the global objects if (test_shell) return;
Locker lock;
HandleScope scope; HandleScope scope;
Handle<ObjectTemplate> global_template = CreateGlobalTemplate(); Handle<ObjectTemplate> global_template = CreateGlobalTemplate();
utility_context_ = Context::New(NULL, global_template); utility_context_ = Context::New(NULL, global_template);
utility_context_->SetSecurityToken(Undefined());
Context::Scope utility_scope(utility_context_);
#ifdef ENABLE_DEBUGGER_SUPPORT #ifdef ENABLE_DEBUGGER_SUPPORT
// Install the debugger object in the utility scope // Start the debugger agent if requested.
i::Debug* debug = i::Isolate::Current()->debug(); if (i::FLAG_debugger_agent) {
debug->Load(); v8::Debug::EnableAgent("d8 shell", i::FLAG_debugger_port, true);
i::Handle<i::JSObject> js_debug }
= i::Handle<i::JSObject>(debug->debug_context()->global());
utility_context_->Global()->Set(String::New("$debug"), // Start the in-process debugger if requested.
Utils::ToLocal(js_debug)); if (i::FLAG_debugger && !i::FLAG_debugger_agent) {
v8::Debug::SetDebugEventListener(HandleDebugEvent);
}
#endif #endif
} }
...@@ -635,9 +654,8 @@ void Shell::RenewEvaluationContext() { ...@@ -635,9 +654,8 @@ void Shell::RenewEvaluationContext() {
evaluation_context_.Dispose(); evaluation_context_.Dispose();
} }
evaluation_context_ = Context::New(NULL, global_template); evaluation_context_ = Context::New(NULL, global_template);
evaluation_context_->SetSecurityToken(Undefined()); Context::Scope utility_scope(evaluation_context_);
Context::Scope utility_scope(utility_context_);
i::JSArguments js_args = i::FLAG_js_arguments; i::JSArguments js_args = i::FLAG_js_arguments;
i::Handle<i::FixedArray> arguments_array = i::Handle<i::FixedArray> arguments_array =
FACTORY->NewFixedArray(js_args.argc()); FACTORY->NewFixedArray(js_args.argc());
...@@ -650,24 +668,6 @@ void Shell::RenewEvaluationContext() { ...@@ -650,24 +668,6 @@ void Shell::RenewEvaluationContext() {
FACTORY->NewJSArrayWithElements(arguments_array); FACTORY->NewJSArrayWithElements(arguments_array);
evaluation_context_->Global()->Set(String::New("arguments"), evaluation_context_->Global()->Set(String::New("arguments"),
Utils::ToLocal(arguments_jsarray)); Utils::ToLocal(arguments_jsarray));
#ifdef ENABLE_DEBUGGER_SUPPORT
i::Debug* debug = i::Isolate::Current()->debug();
debug->Load();
// Set the security token of the debug context to allow access.
debug->debug_context()->set_security_token(HEAP->undefined_value());
// Start the debugger agent if requested.
if (i::FLAG_debugger_agent) {
v8::Debug::EnableAgent("d8 shell", i::FLAG_debugger_port, true);
}
// Start the in-process debugger if requested.
if (i::FLAG_debugger && !i::FLAG_debugger_agent) {
v8::Debug::SetDebugEventListener(HandleDebugEvent);
}
#endif
} }
...@@ -753,6 +753,7 @@ void Shell::RunShell() { ...@@ -753,6 +753,7 @@ void Shell::RunShell() {
if (i::FLAG_debugger) { if (i::FLAG_debugger) {
printf("JavaScript debugger enabled\n"); printf("JavaScript debugger enabled\n");
} }
editor->Open(); editor->Open();
while (true) { while (true) {
Locker locker; Locker locker;
...@@ -800,7 +801,6 @@ void ShellThread::Run() { ...@@ -800,7 +801,6 @@ void ShellThread::Run() {
} }
Persistent<Context> thread_context = Context::New(NULL, global_template); Persistent<Context> thread_context = Context::New(NULL, global_template);
thread_context->SetSecurityToken(Undefined());
Context::Scope context_scope(thread_context); Context::Scope context_scope(thread_context);
while ((ptr != NULL) && (*ptr != '\0')) { while ((ptr != NULL) && (*ptr != '\0')) {
...@@ -826,7 +826,7 @@ void ShellThread::Run() { ...@@ -826,7 +826,7 @@ void ShellThread::Run() {
} }
} }
int Shell::RunMain(int argc, char* argv[]) { int Shell::RunMain(int argc, char* argv[], bool* executed) {
// Default use preemption if threads are created. // Default use preemption if threads are created.
bool use_preemption = true; bool use_preemption = true;
...@@ -871,6 +871,7 @@ int Shell::RunMain(int argc, char* argv[]) { ...@@ -871,6 +871,7 @@ int Shell::RunMain(int argc, char* argv[]) {
v8::HandleScope handle_scope; v8::HandleScope handle_scope;
v8::Handle<v8::String> file_name = v8::String::New("unnamed"); v8::Handle<v8::String> file_name = v8::String::New("unnamed");
v8::Handle<v8::String> source = v8::String::New(argv[++i]); v8::Handle<v8::String> source = v8::String::New(argv[++i]);
(*executed) = true;
if (!ExecuteString(source, file_name, false, true)) { if (!ExecuteString(source, file_name, false, true)) {
OnExit(); OnExit();
return 1; return 1;
...@@ -884,11 +885,13 @@ int Shell::RunMain(int argc, char* argv[]) { ...@@ -884,11 +885,13 @@ int Shell::RunMain(int argc, char* argv[]) {
i::Vector<const char>(files, size)); i::Vector<const char>(files, size));
thread->Start(); thread->Start();
threads.Add(thread); threads.Add(thread);
(*executed) = true;
} else { } else {
// Use all other arguments as names of files to load and run. // Use all other arguments as names of files to load and run.
HandleScope handle_scope; HandleScope handle_scope;
Handle<String> file_name = v8::String::New(str); Handle<String> file_name = v8::String::New(str);
Handle<String> source = ReadFile(str); Handle<String> source = ReadFile(str);
(*executed) = true;
if (source.IsEmpty()) { if (source.IsEmpty()) {
printf("Error reading '%s'\n", str); printf("Error reading '%s'\n", str);
return 1; return 1;
...@@ -922,7 +925,9 @@ int Shell::Main(int argc, char* argv[]) { ...@@ -922,7 +925,9 @@ int Shell::Main(int argc, char* argv[]) {
// optimization in the last run. // optimization in the last run.
bool FLAG_stress_opt = false; bool FLAG_stress_opt = false;
bool FLAG_stress_deopt = false; bool FLAG_stress_deopt = false;
bool FLAG_run_shell = false; bool FLAG_interactive_shell = false;
bool FLAG_test_shell = false;
bool script_executed = false;
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
if (strcmp(argv[i], "--stress-opt") == 0) { if (strcmp(argv[i], "--stress-opt") == 0) {
...@@ -936,18 +941,17 @@ int Shell::Main(int argc, char* argv[]) { ...@@ -936,18 +941,17 @@ int Shell::Main(int argc, char* argv[]) {
FLAG_stress_opt = false; FLAG_stress_opt = false;
FLAG_stress_deopt = false; FLAG_stress_deopt = false;
} else if (strcmp(argv[i], "--shell") == 0) { } else if (strcmp(argv[i], "--shell") == 0) {
FLAG_run_shell = true; FLAG_interactive_shell = true;
argv[i] = NULL;
} else if (strcmp(argv[i], "--test") == 0) {
FLAG_test_shell = true;
argv[i] = NULL; argv[i] = NULL;
} }
} }
v8::V8::SetFlagsFromCommandLine(&argc, argv, true); v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
// Allow SetFlagsFromCommandLine to decrement argc before deciding to Initialize(FLAG_test_shell);
// run the shell or not.
bool run_shell = FLAG_run_shell || (argc == 1);
Initialize();
int result = 0; int result = 0;
if (FLAG_stress_opt || FLAG_stress_deopt) { if (FLAG_stress_opt || FLAG_stress_deopt) {
...@@ -958,22 +962,26 @@ int Shell::Main(int argc, char* argv[]) { ...@@ -958,22 +962,26 @@ int Shell::Main(int argc, char* argv[]) {
for (int i = 0; i < stress_runs && result == 0; i++) { for (int i = 0; i < stress_runs && result == 0; i++) {
printf("============ Stress %d/%d ============\n", i + 1, stress_runs); printf("============ Stress %d/%d ============\n", i + 1, stress_runs);
v8::Testing::PrepareStressRun(i); v8::Testing::PrepareStressRun(i);
result = RunMain(argc, argv); result = RunMain(argc, argv, &script_executed);
} }
printf("======== Full Deoptimization =======\n"); printf("======== Full Deoptimization =======\n");
v8::Testing::DeoptimizeAll(); v8::Testing::DeoptimizeAll();
} else { } else {
result = RunMain(argc, argv); result = RunMain(argc, argv, &script_executed);
} }
#ifdef ENABLE_DEBUGGER_SUPPORT #ifdef ENABLE_DEBUGGER_SUPPORT
if (i::FLAG_remote_debugger) { // Run remote debugger if requested, but never on --test
if (i::FLAG_remote_debugger && !FLAG_test_shell) {
InstallUtilityScript();
RunRemoteDebugger(i::FLAG_debugger_port); RunRemoteDebugger(i::FLAG_debugger_port);
return 0; return 0;
} }
#endif #endif
if (run_shell) { // Run interactive shell if explicitly requested or if no script has been
// executed, but never on --test
if ((FLAG_interactive_shell || !script_executed) && !FLAG_test_shell) {
InstallUtilityScript(); InstallUtilityScript();
RunShell(); RunShell();
} }
......
...@@ -129,12 +129,12 @@ class Shell: public i::AllStatic { ...@@ -129,12 +129,12 @@ class Shell: public i::AllStatic {
static void AddHistogramSample(void* histogram, int sample); static void AddHistogramSample(void* histogram, int sample);
static void MapCounters(const char* name); static void MapCounters(const char* name);
static Handle<String> ReadFile(const char* name); static Handle<String> ReadFile(const char* name);
static void Initialize(); static void Initialize(bool test_shell);
static void RenewEvaluationContext(); static void RenewEvaluationContext();
static void InstallUtilityScript(); static void InstallUtilityScript();
static void RunShell(); static void RunShell();
static int RunScript(char* filename); static int RunScript(char* filename);
static int RunMain(int argc, char* argv[]); static int RunMain(int argc, char* argv[], bool* executed);
static int Main(int argc, char* argv[]); static int Main(int argc, char* argv[]);
static Handle<ObjectTemplate> CreateGlobalTemplate(); static Handle<ObjectTemplate> CreateGlobalTemplate();
static Handle<Array> GetCompletions(Handle<String> text, static Handle<Array> GetCompletions(Handle<String> text,
...@@ -202,8 +202,6 @@ class Shell: public i::AllStatic { ...@@ -202,8 +202,6 @@ class Shell: public i::AllStatic {
static void AddOSMethods(Handle<ObjectTemplate> os_template); static void AddOSMethods(Handle<ObjectTemplate> os_template);
static Handle<Context> utility_context() { return utility_context_; }
static const char* kHistoryFileName; static const char* kHistoryFileName;
static const char* kPrompt; static const char* kPrompt;
......
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