OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 void InitializeExperimentalGlobal(); | 204 void InitializeExperimentalGlobal(); |
205 // Installs the contents of the native .js files on the global objects. | 205 // Installs the contents of the native .js files on the global objects. |
206 // Used for creating a context from scratch. | 206 // Used for creating a context from scratch. |
207 void InstallNativeFunctions(); | 207 void InstallNativeFunctions(); |
208 void InstallExperimentalNativeFunctions(); | 208 void InstallExperimentalNativeFunctions(); |
209 bool InstallNatives(); | 209 bool InstallNatives(); |
210 bool InstallExperimentalNatives(); | 210 bool InstallExperimentalNatives(); |
211 void InstallBuiltinFunctionIds(); | 211 void InstallBuiltinFunctionIds(); |
212 void InstallJSFunctionResultCaches(); | 212 void InstallJSFunctionResultCaches(); |
213 void InitializeNormalizedMapCaches(); | 213 void InitializeNormalizedMapCaches(); |
| 214 |
| 215 enum ExtensionTraversalState { |
| 216 UNVISITED, VISITED, INSTALLED |
| 217 }; |
| 218 |
| 219 class ExtensionStates { |
| 220 DISALLOW_COPY_AND_ASSIGN(ExtensionStates); |
| 221 public: |
| 222 ExtensionStates(); |
| 223 ExtensionTraversalState get_state(RegisteredExtension* extension); |
| 224 void set_state(RegisteredExtension* extension, |
| 225 ExtensionTraversalState state); |
| 226 private: |
| 227 Allocator allocator_; |
| 228 HashMap map_; |
| 229 }; |
| 230 |
214 // Used both for deserialized and from-scratch contexts to add the extensions | 231 // Used both for deserialized and from-scratch contexts to add the extensions |
215 // provided. | 232 // provided. |
216 static bool InstallExtensions(Handle<Context> global_context, | 233 static bool InstallExtensions(Handle<Context> global_context, |
217 v8::ExtensionConfiguration* extensions); | 234 v8::ExtensionConfiguration* extensions); |
218 static bool InstallExtension(const char* name); | 235 static bool InstallExtension(const char* name, |
219 static bool InstallExtension(v8::RegisteredExtension* current); | 236 ExtensionStates* extension_states); |
| 237 static bool InstallExtension(v8::RegisteredExtension* current, |
| 238 ExtensionStates* extension_states); |
220 static void InstallSpecialObjects(Handle<Context> global_context); | 239 static void InstallSpecialObjects(Handle<Context> global_context); |
221 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins); | 240 bool InstallJSBuiltins(Handle<JSBuiltinsObject> builtins); |
222 bool ConfigureApiObject(Handle<JSObject> object, | 241 bool ConfigureApiObject(Handle<JSObject> object, |
223 Handle<ObjectTemplateInfo> object_template); | 242 Handle<ObjectTemplateInfo> object_template); |
224 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template); | 243 bool ConfigureGlobalObjects(v8::Handle<v8::ObjectTemplate> global_template); |
225 | 244 |
226 // Migrates all properties from the 'from' object to the 'to' | 245 // Migrates all properties from the 'from' object to the 'to' |
227 // object and overrides the prototype in 'to' with the one from | 246 // object and overrides the prototype in 'to' with the one from |
228 // 'from'. | 247 // 'from'. |
229 void TransferObject(Handle<JSObject> from, Handle<JSObject> to); | 248 void TransferObject(Handle<JSObject> from, Handle<JSObject> to); |
(...skipping 1698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1928 global_context->security_token()); | 1947 global_context->security_token()); |
1929 | 1948 |
1930 Handle<String> debug_string = | 1949 Handle<String> debug_string = |
1931 factory->LookupAsciiSymbol(FLAG_expose_debug_as); | 1950 factory->LookupAsciiSymbol(FLAG_expose_debug_as); |
1932 Handle<Object> global_proxy(debug->debug_context()->global_proxy()); | 1951 Handle<Object> global_proxy(debug->debug_context()->global_proxy()); |
1933 SetLocalPropertyNoThrow(js_global, debug_string, global_proxy, DONT_ENUM); | 1952 SetLocalPropertyNoThrow(js_global, debug_string, global_proxy, DONT_ENUM); |
1934 } | 1953 } |
1935 #endif | 1954 #endif |
1936 } | 1955 } |
1937 | 1956 |
| 1957 static uint32_t Hash(RegisteredExtension* extension) { |
| 1958 return v8::internal::ComputePointerHash(extension); |
| 1959 } |
| 1960 |
| 1961 static bool MatchRegisteredExtensions(void* key1, void* key2) { |
| 1962 return key1 == key2; |
| 1963 } |
| 1964 |
| 1965 Genesis::ExtensionStates::ExtensionStates() |
| 1966 : allocator_(), |
| 1967 map_(MatchRegisteredExtensions, &allocator_, 8) |
| 1968 {} |
| 1969 |
| 1970 Genesis::ExtensionTraversalState Genesis::ExtensionStates::get_state( |
| 1971 RegisteredExtension* extension) { |
| 1972 i::HashMap::Entry* entry = map_.Lookup(extension, Hash(extension), false); |
| 1973 if (entry == NULL) { |
| 1974 return UNVISITED; |
| 1975 } |
| 1976 return static_cast<ExtensionTraversalState>( |
| 1977 reinterpret_cast<intptr_t>(entry->value)); |
| 1978 } |
| 1979 |
| 1980 void Genesis::ExtensionStates::set_state(RegisteredExtension* extension, |
| 1981 ExtensionTraversalState state) { |
| 1982 map_.Lookup(extension, Hash(extension), true)->value = |
| 1983 reinterpret_cast<void*>(static_cast<intptr_t>(state)); |
| 1984 } |
1938 | 1985 |
1939 bool Genesis::InstallExtensions(Handle<Context> global_context, | 1986 bool Genesis::InstallExtensions(Handle<Context> global_context, |
1940 v8::ExtensionConfiguration* extensions) { | 1987 v8::ExtensionConfiguration* extensions) { |
1941 // TODO(isolates): Extensions on multiple isolates may take a little more | 1988 // TODO(isolates): Extensions on multiple isolates may take a little more |
1942 // effort. (The external API reads 'ignore'-- does that mean | 1989 // effort. (The external API reads 'ignore'-- does that mean |
1943 // we can break the interface?) | 1990 // we can break the interface?) |
1944 | 1991 |
1945 // Clear coloring of extension list | 1992 ExtensionStates extension_states; // All extensions have state UNVISITED. |
| 1993 // Install auto extensions. |
1946 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension(); | 1994 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension(); |
1947 while (current != NULL) { | 1995 while (current != NULL) { |
1948 current->set_state(v8::UNVISITED); | |
1949 current = current->next(); | |
1950 } | |
1951 // Install auto extensions. | |
1952 current = v8::RegisteredExtension::first_extension(); | |
1953 while (current != NULL) { | |
1954 if (current->extension()->auto_enable()) | 1996 if (current->extension()->auto_enable()) |
1955 InstallExtension(current); | 1997 InstallExtension(current, &extension_states); |
1956 current = current->next(); | 1998 current = current->next(); |
1957 } | 1999 } |
1958 | 2000 |
1959 if (FLAG_expose_gc) InstallExtension("v8/gc"); | 2001 if (FLAG_expose_gc) InstallExtension("v8/gc", &extension_states); |
1960 if (FLAG_expose_externalize_string) InstallExtension("v8/externalize"); | 2002 if (FLAG_expose_externalize_string) { |
| 2003 InstallExtension("v8/externalize", &extension_states); |
| 2004 } |
1961 | 2005 |
1962 if (extensions == NULL) return true; | 2006 if (extensions == NULL) return true; |
1963 // Install required extensions | 2007 // Install required extensions |
1964 int count = v8::ImplementationUtilities::GetNameCount(extensions); | 2008 int count = v8::ImplementationUtilities::GetNameCount(extensions); |
1965 const char** names = v8::ImplementationUtilities::GetNames(extensions); | 2009 const char** names = v8::ImplementationUtilities::GetNames(extensions); |
1966 for (int i = 0; i < count; i++) { | 2010 for (int i = 0; i < count; i++) { |
1967 if (!InstallExtension(names[i])) | 2011 if (!InstallExtension(names[i], &extension_states)) |
1968 return false; | 2012 return false; |
1969 } | 2013 } |
1970 | 2014 |
1971 return true; | 2015 return true; |
1972 } | 2016 } |
1973 | 2017 |
1974 | 2018 |
1975 // Installs a named extension. This methods is unoptimized and does | 2019 // Installs a named extension. This methods is unoptimized and does |
1976 // not scale well if we want to support a large number of extensions. | 2020 // not scale well if we want to support a large number of extensions. |
1977 bool Genesis::InstallExtension(const char* name) { | 2021 bool Genesis::InstallExtension(const char* name, |
| 2022 ExtensionStates* extension_states) { |
1978 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension(); | 2023 v8::RegisteredExtension* current = v8::RegisteredExtension::first_extension(); |
1979 // Loop until we find the relevant extension | 2024 // Loop until we find the relevant extension |
1980 while (current != NULL) { | 2025 while (current != NULL) { |
1981 if (strcmp(name, current->extension()->name()) == 0) break; | 2026 if (strcmp(name, current->extension()->name()) == 0) break; |
1982 current = current->next(); | 2027 current = current->next(); |
1983 } | 2028 } |
1984 // Didn't find the extension; fail. | 2029 // Didn't find the extension; fail. |
1985 if (current == NULL) { | 2030 if (current == NULL) { |
1986 v8::Utils::ReportApiFailure( | 2031 v8::Utils::ReportApiFailure( |
1987 "v8::Context::New()", "Cannot find required extension"); | 2032 "v8::Context::New()", "Cannot find required extension"); |
1988 return false; | 2033 return false; |
1989 } | 2034 } |
1990 return InstallExtension(current); | 2035 return InstallExtension(current, extension_states); |
1991 } | 2036 } |
1992 | 2037 |
1993 | 2038 |
1994 bool Genesis::InstallExtension(v8::RegisteredExtension* current) { | 2039 bool Genesis::InstallExtension(v8::RegisteredExtension* current, |
| 2040 ExtensionStates* extension_states) { |
1995 HandleScope scope; | 2041 HandleScope scope; |
1996 | 2042 |
1997 if (current->state() == v8::INSTALLED) return true; | 2043 if (extension_states->get_state(current) == INSTALLED) return true; |
1998 // The current node has already been visited so there must be a | 2044 // The current node has already been visited so there must be a |
1999 // cycle in the dependency graph; fail. | 2045 // cycle in the dependency graph; fail. |
2000 if (current->state() == v8::VISITED) { | 2046 if (extension_states->get_state(current) == VISITED) { |
2001 v8::Utils::ReportApiFailure( | 2047 v8::Utils::ReportApiFailure( |
2002 "v8::Context::New()", "Circular extension dependency"); | 2048 "v8::Context::New()", "Circular extension dependency"); |
2003 return false; | 2049 return false; |
2004 } | 2050 } |
2005 ASSERT(current->state() == v8::UNVISITED); | 2051 ASSERT(extension_states->get_state(current) == UNVISITED); |
2006 current->set_state(v8::VISITED); | 2052 extension_states->set_state(current, VISITED); |
2007 v8::Extension* extension = current->extension(); | 2053 v8::Extension* extension = current->extension(); |
2008 // Install the extension's dependencies | 2054 // Install the extension's dependencies |
2009 for (int i = 0; i < extension->dependency_count(); i++) { | 2055 for (int i = 0; i < extension->dependency_count(); i++) { |
2010 if (!InstallExtension(extension->dependencies()[i])) return false; | 2056 if (!InstallExtension(extension->dependencies()[i], extension_states)) |
| 2057 return false; |
2011 } | 2058 } |
2012 Isolate* isolate = Isolate::Current(); | 2059 Isolate* isolate = Isolate::Current(); |
2013 Handle<String> source_code = | 2060 Handle<String> source_code = |
2014 isolate->factory()->NewExternalStringFromAscii(extension->source()); | 2061 isolate->factory()->NewExternalStringFromAscii(extension->source()); |
2015 bool result = CompileScriptCached( | 2062 bool result = CompileScriptCached( |
2016 CStrVector(extension->name()), | 2063 CStrVector(extension->name()), |
2017 source_code, | 2064 source_code, |
2018 isolate->bootstrapper()->extensions_cache(), | 2065 isolate->bootstrapper()->extensions_cache(), |
2019 extension, | 2066 extension, |
2020 Handle<Context>(isolate->context()), | 2067 Handle<Context>(isolate->context()), |
2021 false); | 2068 false); |
2022 ASSERT(isolate->has_pending_exception() != result); | 2069 ASSERT(isolate->has_pending_exception() != result); |
2023 if (!result) { | 2070 if (!result) { |
2024 // We print out the name of the extension that fail to install. | 2071 // We print out the name of the extension that fail to install. |
2025 // When an error is thrown during bootstrapping we automatically print | 2072 // When an error is thrown during bootstrapping we automatically print |
2026 // the line number at which this happened to the console in the isolate | 2073 // the line number at which this happened to the console in the isolate |
2027 // error throwing functionality. | 2074 // error throwing functionality. |
2028 OS::PrintError("Error installing extension '%s'.\n", | 2075 OS::PrintError("Error installing extension '%s'.\n", |
2029 current->extension()->name()); | 2076 current->extension()->name()); |
2030 isolate->clear_pending_exception(); | 2077 isolate->clear_pending_exception(); |
2031 } | 2078 } |
2032 current->set_state(v8::INSTALLED); | 2079 extension_states->set_state(current, INSTALLED); |
| 2080 isolate->NotifyExtensionInstalled(); |
2033 return result; | 2081 return result; |
2034 } | 2082 } |
2035 | 2083 |
2036 | 2084 |
2037 bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) { | 2085 bool Genesis::InstallJSBuiltins(Handle<JSBuiltinsObject> builtins) { |
2038 HandleScope scope; | 2086 HandleScope scope; |
2039 Factory* factory = builtins->GetIsolate()->factory(); | 2087 Factory* factory = builtins->GetIsolate()->factory(); |
2040 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) { | 2088 for (int i = 0; i < Builtins::NumberOfJavaScriptBuiltins(); i++) { |
2041 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i); | 2089 Builtins::JavaScript id = static_cast<Builtins::JavaScript>(i); |
2042 Handle<String> name = factory->LookupAsciiSymbol(Builtins::GetName(id)); | 2090 Handle<String> name = factory->LookupAsciiSymbol(Builtins::GetName(id)); |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2306 return from + sizeof(NestingCounterType); | 2354 return from + sizeof(NestingCounterType); |
2307 } | 2355 } |
2308 | 2356 |
2309 | 2357 |
2310 // Called when the top-level V8 mutex is destroyed. | 2358 // Called when the top-level V8 mutex is destroyed. |
2311 void Bootstrapper::FreeThreadResources() { | 2359 void Bootstrapper::FreeThreadResources() { |
2312 ASSERT(!IsActive()); | 2360 ASSERT(!IsActive()); |
2313 } | 2361 } |
2314 | 2362 |
2315 } } // namespace v8::internal | 2363 } } // namespace v8::internal |
OLD | NEW |