| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 i::Mutex Shell::context_mutex_; | 161 i::Mutex Shell::context_mutex_; |
| 162 const i::TimeTicks Shell::kInitialTicks = i::TimeTicks::HighResolutionNow(); | 162 const i::TimeTicks Shell::kInitialTicks = i::TimeTicks::HighResolutionNow(); |
| 163 Persistent<Context> Shell::utility_context_; | 163 Persistent<Context> Shell::utility_context_; |
| 164 #endif // V8_SHARED | 164 #endif // V8_SHARED |
| 165 | 165 |
| 166 Persistent<Context> Shell::evaluation_context_; | 166 Persistent<Context> Shell::evaluation_context_; |
| 167 ShellOptions Shell::options; | 167 ShellOptions Shell::options; |
| 168 const char* Shell::kPrompt = "d8> "; | 168 const char* Shell::kPrompt = "d8> "; |
| 169 | 169 |
| 170 | 170 |
| 171 #ifndef V8_SHARED |
| 171 const int MB = 1024 * 1024; | 172 const int MB = 1024 * 1024; |
| 172 | 173 |
| 173 | |
| 174 #ifndef V8_SHARED | |
| 175 bool CounterMap::Match(void* key1, void* key2) { | 174 bool CounterMap::Match(void* key1, void* key2) { |
| 176 const char* name1 = reinterpret_cast<const char*>(key1); | 175 const char* name1 = reinterpret_cast<const char*>(key1); |
| 177 const char* name2 = reinterpret_cast<const char*>(key2); | 176 const char* name2 = reinterpret_cast<const char*>(key2); |
| 178 return strcmp(name1, name2) == 0; | 177 return strcmp(name1, name2) == 0; |
| 179 } | 178 } |
| 180 #endif // V8_SHARED | 179 #endif // V8_SHARED |
| 181 | 180 |
| 182 | 181 |
| 183 // Converts a V8 value to a C string. | 182 // Converts a V8 value to a C string. |
| 184 const char* Shell::ToCString(const v8::String::Utf8Value& value) { | 183 const char* Shell::ToCString(const v8::String::Utf8Value& value) { |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) | 602 #if !defined(V8_SHARED) && defined(ENABLE_DEBUGGER_SUPPORT) |
| 604 if (enter_context) utility_context->Exit(); | 603 if (enter_context) utility_context->Exit(); |
| 605 #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT | 604 #endif // !V8_SHARED && ENABLE_DEBUGGER_SUPPORT |
| 606 } | 605 } |
| 607 | 606 |
| 608 | 607 |
| 609 #ifndef V8_SHARED | 608 #ifndef V8_SHARED |
| 610 Handle<Array> Shell::GetCompletions(Isolate* isolate, | 609 Handle<Array> Shell::GetCompletions(Isolate* isolate, |
| 611 Handle<String> text, | 610 Handle<String> text, |
| 612 Handle<String> full) { | 611 Handle<String> full) { |
| 613 HandleScope handle_scope(isolate); | 612 EscapableHandleScope handle_scope(isolate); |
| 614 v8::Local<v8::Context> utility_context = | 613 v8::Local<v8::Context> utility_context = |
| 615 v8::Local<v8::Context>::New(isolate, utility_context_); | 614 v8::Local<v8::Context>::New(isolate, utility_context_); |
| 616 v8::Context::Scope context_scope(utility_context); | 615 v8::Context::Scope context_scope(utility_context); |
| 617 Handle<Object> global = utility_context->Global(); | 616 Handle<Object> global = utility_context->Global(); |
| 618 Handle<Value> fun = | 617 Local<Value> fun = |
| 619 global->Get(String::NewFromUtf8(isolate, "GetCompletions")); | 618 global->Get(String::NewFromUtf8(isolate, "GetCompletions")); |
| 620 static const int kArgc = 3; | 619 static const int kArgc = 3; |
| 621 v8::Local<v8::Context> evaluation_context = | 620 v8::Local<v8::Context> evaluation_context = |
| 622 v8::Local<v8::Context>::New(isolate, evaluation_context_); | 621 v8::Local<v8::Context>::New(isolate, evaluation_context_); |
| 623 Handle<Value> argv[kArgc] = { evaluation_context->Global(), text, full }; | 622 Handle<Value> argv[kArgc] = { evaluation_context->Global(), text, full }; |
| 624 Handle<Value> val = Handle<Function>::Cast(fun)->Call(global, kArgc, argv); | 623 Local<Value> val = Local<Function>::Cast(fun)->Call(global, kArgc, argv); |
| 625 return handle_scope.Close(Handle<Array>::Cast(val)); | 624 return handle_scope.Escape(Local<Array>::Cast(val)); |
| 626 } | 625 } |
| 627 | 626 |
| 628 | 627 |
| 629 #ifdef ENABLE_DEBUGGER_SUPPORT | 628 #ifdef ENABLE_DEBUGGER_SUPPORT |
| 630 Handle<Object> Shell::DebugMessageDetails(Isolate* isolate, | 629 Handle<Object> Shell::DebugMessageDetails(Isolate* isolate, |
| 631 Handle<String> message) { | 630 Handle<String> message) { |
| 632 HandleScope handle_scope(isolate); | 631 HandleScope handle_scope(isolate); |
| 633 v8::Local<v8::Context> context = | 632 v8::Local<v8::Context> context = |
| 634 v8::Local<v8::Context>::New(isolate, utility_context_); | 633 v8::Local<v8::Context>::New(isolate, utility_context_); |
| 635 v8::Context::Scope context_scope(context); | 634 v8::Context::Scope context_scope(context); |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 959 } | 958 } |
| 960 | 959 |
| 961 | 960 |
| 962 Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) { | 961 Local<Context> Shell::CreateEvaluationContext(Isolate* isolate) { |
| 963 #ifndef V8_SHARED | 962 #ifndef V8_SHARED |
| 964 // This needs to be a critical section since this is not thread-safe | 963 // This needs to be a critical section since this is not thread-safe |
| 965 i::LockGuard<i::Mutex> lock_guard(&context_mutex_); | 964 i::LockGuard<i::Mutex> lock_guard(&context_mutex_); |
| 966 #endif // V8_SHARED | 965 #endif // V8_SHARED |
| 967 // Initialize the global objects | 966 // Initialize the global objects |
| 968 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); | 967 Handle<ObjectTemplate> global_template = CreateGlobalTemplate(isolate); |
| 969 HandleScope handle_scope(isolate); | 968 EscapableHandleScope handle_scope(isolate); |
| 970 Local<Context> context = Context::New(isolate, NULL, global_template); | 969 Local<Context> context = Context::New(isolate, NULL, global_template); |
| 971 ASSERT(!context.IsEmpty()); | 970 ASSERT(!context.IsEmpty()); |
| 972 Context::Scope scope(context); | 971 Context::Scope scope(context); |
| 973 | 972 |
| 974 #ifndef V8_SHARED | 973 #ifndef V8_SHARED |
| 975 i::Factory* factory = reinterpret_cast<i::Isolate*>(isolate)->factory(); | 974 i::Factory* factory = reinterpret_cast<i::Isolate*>(isolate)->factory(); |
| 976 i::JSArguments js_args = i::FLAG_js_arguments; | 975 i::JSArguments js_args = i::FLAG_js_arguments; |
| 977 i::Handle<i::FixedArray> arguments_array = | 976 i::Handle<i::FixedArray> arguments_array = |
| 978 factory->NewFixedArray(js_args.argc); | 977 factory->NewFixedArray(js_args.argc); |
| 979 for (int j = 0; j < js_args.argc; j++) { | 978 for (int j = 0; j < js_args.argc; j++) { |
| 980 i::Handle<i::String> arg = | 979 i::Handle<i::String> arg = |
| 981 factory->NewStringFromUtf8(i::CStrVector(js_args[j])); | 980 factory->NewStringFromUtf8(i::CStrVector(js_args[j])); |
| 982 arguments_array->set(j, *arg); | 981 arguments_array->set(j, *arg); |
| 983 } | 982 } |
| 984 i::Handle<i::JSArray> arguments_jsarray = | 983 i::Handle<i::JSArray> arguments_jsarray = |
| 985 factory->NewJSArrayWithElements(arguments_array); | 984 factory->NewJSArrayWithElements(arguments_array); |
| 986 context->Global()->Set(String::NewFromUtf8(isolate, "arguments"), | 985 context->Global()->Set(String::NewFromUtf8(isolate, "arguments"), |
| 987 Utils::ToLocal(arguments_jsarray)); | 986 Utils::ToLocal(arguments_jsarray)); |
| 988 #endif // V8_SHARED | 987 #endif // V8_SHARED |
| 989 return handle_scope.Close(context); | 988 return handle_scope.Escape(context); |
| 990 } | 989 } |
| 991 | 990 |
| 992 | 991 |
| 993 void Shell::Exit(int exit_code) { | 992 void Shell::Exit(int exit_code) { |
| 994 // Use _exit instead of exit to avoid races between isolate | 993 // Use _exit instead of exit to avoid races between isolate |
| 995 // threads and static destructors. | 994 // threads and static destructors. |
| 996 fflush(stdout); | 995 fflush(stdout); |
| 997 fflush(stderr); | 996 fflush(stderr); |
| 998 _exit(exit_code); | 997 _exit(exit_code); |
| 999 } | 998 } |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1090 chars[size] = '\0'; | 1089 chars[size] = '\0'; |
| 1091 for (int i = 0; i < size;) { | 1090 for (int i = 0; i < size;) { |
| 1092 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); | 1091 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); |
| 1093 i += read; | 1092 i += read; |
| 1094 } | 1093 } |
| 1095 fclose(file); | 1094 fclose(file); |
| 1096 *size_out = size; | 1095 *size_out = size; |
| 1097 return chars; | 1096 return chars; |
| 1098 } | 1097 } |
| 1099 | 1098 |
| 1100 static void ReadBufferWeakCallback(v8::Isolate* isolate, | 1099 |
| 1101 Persistent<ArrayBuffer>* array_buffer, | 1100 struct DataAndPersistent { |
| 1102 uint8_t* data) { | 1101 uint8_t* data; |
| 1103 size_t byte_length = | 1102 Persistent<ArrayBuffer> handle; |
| 1104 Local<ArrayBuffer>::New(isolate, *array_buffer)->ByteLength(); | 1103 }; |
| 1105 isolate->AdjustAmountOfExternalAllocatedMemory( | 1104 |
| 1105 |
| 1106 static void ReadBufferWeakCallback( |
| 1107 const v8::WeakCallbackData<ArrayBuffer, DataAndPersistent>& data) { |
| 1108 size_t byte_length = data.GetValue()->ByteLength(); |
| 1109 data.GetIsolate()->AdjustAmountOfExternalAllocatedMemory( |
| 1106 -static_cast<intptr_t>(byte_length)); | 1110 -static_cast<intptr_t>(byte_length)); |
| 1107 | 1111 |
| 1108 delete[] data; | 1112 delete[] data.GetParameter()->data; |
| 1109 array_buffer->Reset(); | 1113 data.GetParameter()->handle.Reset(); |
| 1114 delete data.GetParameter(); |
| 1110 } | 1115 } |
| 1111 | 1116 |
| 1112 | 1117 |
| 1113 void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { | 1118 void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 1114 ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT | 1119 ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT |
| 1115 String::Utf8Value filename(args[0]); | 1120 String::Utf8Value filename(args[0]); |
| 1116 int length; | 1121 int length; |
| 1117 if (*filename == NULL) { | 1122 if (*filename == NULL) { |
| 1118 Throw(args.GetIsolate(), "Error loading file"); | 1123 Throw(args.GetIsolate(), "Error loading file"); |
| 1119 return; | 1124 return; |
| 1120 } | 1125 } |
| 1121 | 1126 |
| 1122 Isolate* isolate = args.GetIsolate(); | 1127 Isolate* isolate = args.GetIsolate(); |
| 1123 uint8_t* data = reinterpret_cast<uint8_t*>( | 1128 DataAndPersistent* data = new DataAndPersistent; |
| 1129 data->data = reinterpret_cast<uint8_t*>( |
| 1124 ReadChars(args.GetIsolate(), *filename, &length)); | 1130 ReadChars(args.GetIsolate(), *filename, &length)); |
| 1125 if (data == NULL) { | 1131 if (data->data == NULL) { |
| 1132 delete data; |
| 1126 Throw(args.GetIsolate(), "Error reading file"); | 1133 Throw(args.GetIsolate(), "Error reading file"); |
| 1127 return; | 1134 return; |
| 1128 } | 1135 } |
| 1129 Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(data, length); | 1136 Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(isolate, data, length); |
| 1130 v8::Persistent<v8::ArrayBuffer> weak_handle(isolate, buffer); | 1137 data->handle.Reset(isolate, buffer); |
| 1131 weak_handle.MakeWeak(data, ReadBufferWeakCallback); | 1138 data->handle.SetWeak(data, ReadBufferWeakCallback); |
| 1132 weak_handle.MarkIndependent(); | 1139 data->handle.MarkIndependent(); |
| 1133 isolate->AdjustAmountOfExternalAllocatedMemory(length); | 1140 isolate->AdjustAmountOfExternalAllocatedMemory(length); |
| 1134 | 1141 |
| 1135 args.GetReturnValue().Set(buffer); | 1142 args.GetReturnValue().Set(buffer); |
| 1136 } | 1143 } |
| 1137 | 1144 |
| 1138 | 1145 |
| 1139 #ifndef V8_SHARED | 1146 #ifndef V8_SHARED |
| 1140 static char* ReadToken(char* data, char token) { | 1147 static char* ReadToken(char* data, char token) { |
| 1141 char* next = i::OS::StrChr(data, token); | 1148 char* next = i::OS::StrChr(data, token); |
| 1142 if (next != NULL) { | 1149 if (next != NULL) { |
| (...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1755 } | 1762 } |
| 1756 | 1763 |
| 1757 } // namespace v8 | 1764 } // namespace v8 |
| 1758 | 1765 |
| 1759 | 1766 |
| 1760 #ifndef GOOGLE3 | 1767 #ifndef GOOGLE3 |
| 1761 int main(int argc, char* argv[]) { | 1768 int main(int argc, char* argv[]) { |
| 1762 return v8::Shell::Main(argc, argv); | 1769 return v8::Shell::Main(argc, argv); |
| 1763 } | 1770 } |
| 1764 #endif | 1771 #endif |
| OLD | NEW |