OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 // Defined when linking against shared lib on Windows. | 6 // Defined when linking against shared lib on Windows. |
7 #if defined(USING_V8_SHARED) && !defined(V8_SHARED) | 7 #if defined(USING_V8_SHARED) && !defined(V8_SHARED) |
8 #define V8_SHARED | 8 #define V8_SHARED |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 | 244 |
245 | 245 |
246 #ifndef V8_SHARED | 246 #ifndef V8_SHARED |
247 CounterMap* Shell::counter_map_; | 247 CounterMap* Shell::counter_map_; |
248 base::OS::MemoryMappedFile* Shell::counters_file_ = NULL; | 248 base::OS::MemoryMappedFile* Shell::counters_file_ = NULL; |
249 CounterCollection Shell::local_counters_; | 249 CounterCollection Shell::local_counters_; |
250 CounterCollection* Shell::counters_ = &local_counters_; | 250 CounterCollection* Shell::counters_ = &local_counters_; |
251 base::LazyMutex Shell::context_mutex_; | 251 base::LazyMutex Shell::context_mutex_; |
252 const base::TimeTicks Shell::kInitialTicks = | 252 const base::TimeTicks Shell::kInitialTicks = |
253 base::TimeTicks::HighResolutionNow(); | 253 base::TimeTicks::HighResolutionNow(); |
254 Global<Context> Shell::utility_context_; | 254 Global<Function> Shell::stringify_function_; |
255 base::LazyMutex Shell::workers_mutex_; | 255 base::LazyMutex Shell::workers_mutex_; |
256 bool Shell::allow_new_workers_ = true; | 256 bool Shell::allow_new_workers_ = true; |
257 i::List<Worker*> Shell::workers_; | 257 i::List<Worker*> Shell::workers_; |
258 i::List<SharedArrayBuffer::Contents> Shell::externalized_shared_contents_; | 258 i::List<SharedArrayBuffer::Contents> Shell::externalized_shared_contents_; |
259 #endif // !V8_SHARED | 259 #endif // !V8_SHARED |
260 | 260 |
261 Global<Context> Shell::evaluation_context_; | 261 Global<Context> Shell::evaluation_context_; |
262 ArrayBuffer::Allocator* Shell::array_buffer_allocator; | 262 ArrayBuffer::Allocator* Shell::array_buffer_allocator; |
263 ShellOptions Shell::options; | 263 ShellOptions Shell::options; |
264 base::OnceType Shell::quit_once_ = V8_ONCE_INIT; | 264 base::OnceType Shell::quit_once_ = V8_ONCE_INIT; |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 #endif | 405 #endif |
406 if (!result->IsUndefined()) { | 406 if (!result->IsUndefined()) { |
407 // If all went well and the result wasn't undefined then print | 407 // If all went well and the result wasn't undefined then print |
408 // the returned value. | 408 // the returned value. |
409 v8::String::Utf8Value str(result); | 409 v8::String::Utf8Value str(result); |
410 fwrite(*str, sizeof(**str), str.length(), stdout); | 410 fwrite(*str, sizeof(**str), str.length(), stdout); |
411 printf("\n"); | 411 printf("\n"); |
412 } | 412 } |
413 #if !defined(V8_SHARED) | 413 #if !defined(V8_SHARED) |
414 } else { | 414 } else { |
415 v8::TryCatch try_catch(isolate); | 415 v8::String::Utf8Value str(Stringify(isolate, result)); |
416 v8::Local<v8::Context> context = | |
417 v8::Local<v8::Context>::New(isolate, utility_context_); | |
418 v8::Context::Scope context_scope(context); | |
419 Local<Object> global = context->Global(); | |
420 Local<Value> fun = | |
421 global->Get(context, String::NewFromUtf8(isolate, "Stringify", | |
422 v8::NewStringType::kNormal) | |
423 .ToLocalChecked()).ToLocalChecked(); | |
424 Local<Value> argv[1] = {result}; | |
425 Local<Value> s; | |
426 if (!Local<Function>::Cast(fun) | |
427 ->Call(context, global, 1, argv) | |
428 .ToLocal(&s)) { | |
429 return true; | |
430 } | |
431 DCHECK(!try_catch.HasCaught()); | |
432 v8::String::Utf8Value str(s); | |
433 fwrite(*str, sizeof(**str), str.length(), stdout); | 416 fwrite(*str, sizeof(**str), str.length(), stdout); |
434 printf("\n"); | 417 printf("\n"); |
435 } | 418 } |
436 #endif | 419 #endif |
437 } | 420 } |
438 return true; | 421 return true; |
439 } | 422 } |
440 | 423 |
441 | 424 |
442 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) { | 425 PerIsolateData::RealmScope::RealmScope(PerIsolateData* data) : data_(data) { |
(...skipping 456 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) { | 882 void Shell::Version(const v8::FunctionCallbackInfo<v8::Value>& args) { |
900 args.GetReturnValue().Set( | 883 args.GetReturnValue().Set( |
901 String::NewFromUtf8(args.GetIsolate(), V8::GetVersion(), | 884 String::NewFromUtf8(args.GetIsolate(), V8::GetVersion(), |
902 NewStringType::kNormal).ToLocalChecked()); | 885 NewStringType::kNormal).ToLocalChecked()); |
903 } | 886 } |
904 | 887 |
905 | 888 |
906 void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { | 889 void Shell::ReportException(Isolate* isolate, v8::TryCatch* try_catch) { |
907 HandleScope handle_scope(isolate); | 890 HandleScope handle_scope(isolate); |
908 #ifndef V8_SHARED | 891 #ifndef V8_SHARED |
909 Local<Context> utility_context; | 892 Local<Context> context; |
910 bool enter_context = !isolate->InContext(); | 893 bool enter_context = !isolate->InContext(); |
911 if (enter_context) { | 894 if (enter_context) { |
912 utility_context = Local<Context>::New(isolate, utility_context_); | 895 context = Local<Context>::New(isolate, evaluation_context_); |
913 utility_context->Enter(); | 896 context->Enter(); |
914 } | 897 } |
915 #endif // !V8_SHARED | 898 #endif // !V8_SHARED |
916 v8::String::Utf8Value exception(try_catch->Exception()); | 899 v8::String::Utf8Value exception(try_catch->Exception()); |
917 const char* exception_string = ToCString(exception); | 900 const char* exception_string = ToCString(exception); |
918 Local<Message> message = try_catch->Message(); | 901 Local<Message> message = try_catch->Message(); |
919 if (message.IsEmpty()) { | 902 if (message.IsEmpty()) { |
920 // V8 didn't provide any extra information about this error; just | 903 // V8 didn't provide any extra information about this error; just |
921 // print the exception. | 904 // print the exception. |
922 printf("%s\n", exception_string); | 905 printf("%s\n", exception_string); |
923 } else { | 906 } else { |
(...skipping 23 matching lines...) Expand all Loading... |
947 if (try_catch->StackTrace(isolate->GetCurrentContext()) | 930 if (try_catch->StackTrace(isolate->GetCurrentContext()) |
948 .ToLocal(&stack_trace_string) && | 931 .ToLocal(&stack_trace_string) && |
949 stack_trace_string->IsString()) { | 932 stack_trace_string->IsString()) { |
950 v8::String::Utf8Value stack_trace( | 933 v8::String::Utf8Value stack_trace( |
951 Local<String>::Cast(stack_trace_string)); | 934 Local<String>::Cast(stack_trace_string)); |
952 printf("%s\n", ToCString(stack_trace)); | 935 printf("%s\n", ToCString(stack_trace)); |
953 } | 936 } |
954 } | 937 } |
955 printf("\n"); | 938 printf("\n"); |
956 #ifndef V8_SHARED | 939 #ifndef V8_SHARED |
957 if (enter_context) utility_context->Exit(); | 940 if (enter_context) context->Exit(); |
958 #endif // !V8_SHARED | 941 #endif // !V8_SHARED |
959 } | 942 } |
960 | 943 |
961 | 944 |
962 #ifndef V8_SHARED | 945 #ifndef V8_SHARED |
963 int32_t* Counter::Bind(const char* name, bool is_histogram) { | 946 int32_t* Counter::Bind(const char* name, bool is_histogram) { |
964 int i; | 947 int i; |
965 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) | 948 for (i = 0; i < kMaxNameSize - 1 && name[i]; i++) |
966 name_[i] = static_cast<char>(name[i]); | 949 name_[i] = static_cast<char>(name[i]); |
967 name_[i] = '\0'; | 950 name_[i] = '\0'; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1050 size_t buckets) { | 1033 size_t buckets) { |
1051 return GetCounter(name, true); | 1034 return GetCounter(name, true); |
1052 } | 1035 } |
1053 | 1036 |
1054 | 1037 |
1055 void Shell::AddHistogramSample(void* histogram, int sample) { | 1038 void Shell::AddHistogramSample(void* histogram, int sample) { |
1056 Counter* counter = reinterpret_cast<Counter*>(histogram); | 1039 Counter* counter = reinterpret_cast<Counter*>(histogram); |
1057 counter->AddSample(sample); | 1040 counter->AddSample(sample); |
1058 } | 1041 } |
1059 | 1042 |
1060 | 1043 // Turn a value into a human-readable string. |
1061 void Shell::InstallUtilityScript(Isolate* isolate) { | 1044 Local<String> Shell::Stringify(Isolate* isolate, Local<Value> value) { |
1062 HandleScope scope(isolate); | 1045 v8::Local<v8::Context> context = |
1063 // If we use the utility context, we have to set the security tokens so that | |
1064 // utility, evaluation and debug context can all access each other. | |
1065 Local<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); | |
1066 utility_context_.Reset(isolate, Context::New(isolate, NULL, global_template)); | |
1067 v8::Local<v8::Context> utility_context = | |
1068 v8::Local<v8::Context>::New(isolate, utility_context_); | |
1069 v8::Local<v8::Context> evaluation_context = | |
1070 v8::Local<v8::Context>::New(isolate, evaluation_context_); | 1046 v8::Local<v8::Context>::New(isolate, evaluation_context_); |
1071 utility_context->SetSecurityToken(Undefined(isolate)); | 1047 if (stringify_function_.IsEmpty()) { |
1072 evaluation_context->SetSecurityToken(Undefined(isolate)); | 1048 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); |
1073 v8::Context::Scope context_scope(utility_context); | 1049 i::Vector<const char> source_string = |
1074 | 1050 i::NativesCollection<i::D8>::GetScriptSource(source_index); |
1075 // Run the d8 shell utility script in the utility context | 1051 i::Vector<const char> source_name = |
1076 int source_index = i::NativesCollection<i::D8>::GetIndex("d8"); | 1052 i::NativesCollection<i::D8>::GetScriptName(source_index); |
1077 i::Vector<const char> shell_source = | 1053 Local<String> source = |
1078 i::NativesCollection<i::D8>::GetScriptSource(source_index); | 1054 String::NewFromUtf8(isolate, source_string.start(), |
1079 i::Vector<const char> shell_source_name = | 1055 NewStringType::kNormal, source_string.length()) |
1080 i::NativesCollection<i::D8>::GetScriptName(source_index); | 1056 .ToLocalChecked(); |
1081 Local<String> source = | 1057 Local<String> name = |
1082 String::NewFromUtf8(isolate, shell_source.start(), NewStringType::kNormal, | 1058 String::NewFromUtf8(isolate, source_name.start(), |
1083 shell_source.length()).ToLocalChecked(); | 1059 NewStringType::kNormal, source_name.length()) |
1084 Local<String> name = | 1060 .ToLocalChecked(); |
1085 String::NewFromUtf8(isolate, shell_source_name.start(), | 1061 ScriptOrigin origin(name); |
1086 NewStringType::kNormal, | 1062 Local<Script> script = |
1087 shell_source_name.length()).ToLocalChecked(); | 1063 Script::Compile(context, source, &origin).ToLocalChecked(); |
1088 ScriptOrigin origin(name); | 1064 stringify_function_.Reset( |
1089 Local<Script> script = | 1065 isolate, script->Run(context).ToLocalChecked().As<Function>()); |
1090 Script::Compile(utility_context, source, &origin).ToLocalChecked(); | 1066 } |
1091 script->Run(utility_context).ToLocalChecked(); | 1067 Local<Function> fun = Local<Function>::New(isolate, stringify_function_); |
1092 // Mark the d8 shell script as native to avoid it showing up as normal source | 1068 Local<Value> argv[1] = {value}; |
1093 // in the debugger. | 1069 v8::TryCatch try_catch(isolate); |
1094 i::Handle<i::Object> compiled_script = Utils::OpenHandle(*script); | 1070 MaybeLocal<Value> result = |
1095 i::Handle<i::Script> script_object = compiled_script->IsJSFunction() | 1071 fun->Call(context, Undefined(isolate), 1, argv).ToLocalChecked(); |
1096 ? i::Handle<i::Script>(i::Script::cast( | 1072 if (result.IsEmpty()) return String::Empty(isolate); |
1097 i::JSFunction::cast(*compiled_script)->shared()->script())) | 1073 return result.ToLocalChecked().As<String>(); |
1098 : i::Handle<i::Script>(i::Script::cast( | |
1099 i::SharedFunctionInfo::cast(*compiled_script)->script())); | |
1100 script_object->set_type(i::Script::TYPE_EXTENSION); | |
1101 } | 1074 } |
1102 #endif // !V8_SHARED | 1075 #endif // !V8_SHARED |
1103 | 1076 |
1104 | 1077 |
1105 Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { | 1078 Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { |
1106 Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate); | 1079 Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate); |
1107 global_template->Set( | 1080 global_template->Set( |
1108 String::NewFromUtf8(isolate, "print", NewStringType::kNormal) | 1081 String::NewFromUtf8(isolate, "print", NewStringType::kNormal) |
1109 .ToLocalChecked(), | 1082 .ToLocalChecked(), |
1110 FunctionTemplate::New(isolate, Print)); | 1083 FunctionTemplate::New(isolate, Print)); |
(...skipping 1102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2213 | 2186 |
2214 return true; | 2187 return true; |
2215 } | 2188 } |
2216 | 2189 |
2217 | 2190 |
2218 MaybeLocal<Value> Shell::DeserializeValue(Isolate* isolate, | 2191 MaybeLocal<Value> Shell::DeserializeValue(Isolate* isolate, |
2219 const SerializationData& data, | 2192 const SerializationData& data, |
2220 int* offset) { | 2193 int* offset) { |
2221 DCHECK(offset); | 2194 DCHECK(offset); |
2222 EscapableHandleScope scope(isolate); | 2195 EscapableHandleScope scope(isolate); |
2223 // This function should not use utility_context_ because it is running on a | |
2224 // different thread. | |
2225 Local<Value> result; | 2196 Local<Value> result; |
2226 SerializationTag tag = data.ReadTag(offset); | 2197 SerializationTag tag = data.ReadTag(offset); |
2227 | 2198 |
2228 switch (tag) { | 2199 switch (tag) { |
2229 case kSerializationTagUndefined: | 2200 case kSerializationTagUndefined: |
2230 result = Undefined(isolate); | 2201 result = Undefined(isolate); |
2231 break; | 2202 break; |
2232 case kSerializationTagNull: | 2203 case kSerializationTagNull: |
2233 result = Null(isolate); | 2204 result = Null(isolate); |
2234 break; | 2205 break; |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2495 } | 2466 } |
2496 #endif | 2467 #endif |
2497 } else { | 2468 } else { |
2498 bool last_run = true; | 2469 bool last_run = true; |
2499 result = RunMain(isolate, argc, argv, last_run); | 2470 result = RunMain(isolate, argc, argv, last_run); |
2500 } | 2471 } |
2501 | 2472 |
2502 // Run interactive shell if explicitly requested or if no script has been | 2473 // Run interactive shell if explicitly requested or if no script has been |
2503 // executed, but never on --test | 2474 // executed, but never on --test |
2504 if (options.use_interactive_shell()) { | 2475 if (options.use_interactive_shell()) { |
2505 #ifndef V8_SHARED | |
2506 InstallUtilityScript(isolate); | |
2507 #endif // !V8_SHARED | |
2508 RunShell(isolate); | 2476 RunShell(isolate); |
2509 } | 2477 } |
2510 | 2478 |
2511 // Shut down contexts and collect garbage. | 2479 // Shut down contexts and collect garbage. |
2512 evaluation_context_.Reset(); | 2480 evaluation_context_.Reset(); |
2513 #ifndef V8_SHARED | |
2514 utility_context_.Reset(); | |
2515 #endif // !V8_SHARED | |
2516 CollectGarbage(isolate); | 2481 CollectGarbage(isolate); |
2517 } | 2482 } |
2518 OnExit(isolate); | 2483 OnExit(isolate); |
2519 #ifndef V8_SHARED | 2484 #ifndef V8_SHARED |
2520 // Dump basic block profiling data. | 2485 // Dump basic block profiling data. |
2521 if (i::BasicBlockProfiler* profiler = | 2486 if (i::BasicBlockProfiler* profiler = |
2522 reinterpret_cast<i::Isolate*>(isolate)->basic_block_profiler()) { | 2487 reinterpret_cast<i::Isolate*>(isolate)->basic_block_profiler()) { |
2523 i::OFStream os(stdout); | 2488 i::OFStream os(stdout); |
2524 os << *profiler; | 2489 os << *profiler; |
2525 } | 2490 } |
2526 #endif // !V8_SHARED | 2491 #endif // !V8_SHARED |
2527 isolate->Dispose(); | 2492 isolate->Dispose(); |
2528 V8::Dispose(); | 2493 V8::Dispose(); |
2529 V8::ShutdownPlatform(); | 2494 V8::ShutdownPlatform(); |
2530 delete g_platform; | 2495 delete g_platform; |
2531 | 2496 |
2532 return result; | 2497 return result; |
2533 } | 2498 } |
2534 | 2499 |
2535 } // namespace v8 | 2500 } // namespace v8 |
2536 | 2501 |
2537 | 2502 |
2538 #ifndef GOOGLE3 | 2503 #ifndef GOOGLE3 |
2539 int main(int argc, char* argv[]) { | 2504 int main(int argc, char* argv[]) { |
2540 return v8::Shell::Main(argc, argv); | 2505 return v8::Shell::Main(argc, argv); |
2541 } | 2506 } |
2542 #endif | 2507 #endif |
OLD | NEW |