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 357 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 // Return the script wrapper directly from the cache. | 368 // Return the script wrapper directly from the cache. |
369 return Handle<JSValue>( | 369 return Handle<JSValue>( |
370 reinterpret_cast<JSValue**>(script->wrapper()->foreign_address())); | 370 reinterpret_cast<JSValue**>(script->wrapper()->foreign_address())); |
371 } | 371 } |
372 Isolate* isolate = Isolate::Current(); | 372 Isolate* isolate = Isolate::Current(); |
373 // Construct a new script wrapper. | 373 // Construct a new script wrapper. |
374 isolate->counters()->script_wrappers()->Increment(); | 374 isolate->counters()->script_wrappers()->Increment(); |
375 Handle<JSFunction> constructor = isolate->script_function(); | 375 Handle<JSFunction> constructor = isolate->script_function(); |
376 Handle<JSValue> result = | 376 Handle<JSValue> result = |
377 Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor)); | 377 Handle<JSValue>::cast(isolate->factory()->NewJSObject(constructor)); |
| 378 |
| 379 // The allocation might have triggered a GC, which could have called this |
| 380 // function recursively, and a wrapper has already been created and cached. |
| 381 // In that case, simply return the cached wrapper. |
| 382 if (script->wrapper()->foreign_address() != NULL) { |
| 383 return Handle<JSValue>( |
| 384 reinterpret_cast<JSValue**>(script->wrapper()->foreign_address())); |
| 385 } |
| 386 |
378 result->set_value(*script); | 387 result->set_value(*script); |
379 | 388 |
380 // Create a new weak global handle and use it to cache the wrapper | 389 // Create a new weak global handle and use it to cache the wrapper |
381 // for future use. The cache will automatically be cleared by the | 390 // for future use. The cache will automatically be cleared by the |
382 // garbage collector when it is not used anymore. | 391 // garbage collector when it is not used anymore. |
383 Handle<Object> handle = isolate->global_handles()->Create(*result); | 392 Handle<Object> handle = isolate->global_handles()->Create(*result); |
384 isolate->global_handles()->MakeWeak(handle.location(), NULL, | 393 isolate->global_handles()->MakeWeak(handle.location(), NULL, |
385 &ClearWrapperCache); | 394 &ClearWrapperCache); |
386 script->wrapper()->set_foreign_address( | 395 script->wrapper()->set_foreign_address( |
387 reinterpret_cast<Address>(handle.location())); | 396 reinterpret_cast<Address>(handle.location())); |
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
919 data->next = prev_next_; | 928 data->next = prev_next_; |
920 data->limit = prev_limit_; | 929 data->limit = prev_limit_; |
921 #ifdef DEBUG | 930 #ifdef DEBUG |
922 handles_detached_ = true; | 931 handles_detached_ = true; |
923 #endif | 932 #endif |
924 return deferred; | 933 return deferred; |
925 } | 934 } |
926 | 935 |
927 | 936 |
928 } } // namespace v8::internal | 937 } } // namespace v8::internal |
OLD | NEW |