| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #ifndef ScriptWrappable_h | 31 #ifndef ScriptWrappable_h |
| 32 #define ScriptWrappable_h | 32 #define ScriptWrappable_h |
| 33 | 33 |
| 34 #include "WebCoreMemoryInstrumentation.h" | 34 #include "WebCoreMemoryInstrumentation.h" |
| 35 #include <v8.h> | 35 #include <v8.h> |
| 36 | 36 |
| 37 namespace WebCore { | 37 namespace WebCore { |
| 38 | 38 |
| 39 class ScriptWrappable { | 39 class ScriptWrappable { |
| 40 public: | 40 public: |
| 41 ScriptWrappable() | 41 ScriptWrappable() { } |
| 42 { | |
| 43 } | |
| 44 | 42 |
| 45 v8::Persistent<v8::Object> wrapper() const | 43 v8::Persistent<v8::Object> wrapper() const |
| 46 { | 44 { |
| 47 return m_wrapper; | 45 return v8::Persistent<v8::Object>(maskOrUnmaskPointer(*m_maskedWrapper))
; |
| 48 } | 46 } |
| 49 | 47 |
| 50 void setWrapper(v8::Persistent<v8::Object> wrapper) | 48 void setWrapper(v8::Persistent<v8::Object> wrapper) |
| 51 { | 49 { |
| 52 ASSERT(!wrapper.IsEmpty()); | 50 m_maskedWrapper = maskOrUnmaskPointer(*wrapper); |
| 53 m_wrapper = wrapper; | |
| 54 } | 51 } |
| 55 | 52 |
| 56 void clearWrapper() | 53 void clearWrapper() |
| 57 { | 54 { |
| 58 ASSERT(!m_wrapper.IsEmpty()); | 55 ASSERT(!m_maskedWrapper.IsEmpty()); |
| 59 m_wrapper.Clear(); | 56 m_maskedWrapper.Clear(); |
| 60 } | 57 } |
| 61 | 58 |
| 62 void disposeWrapper() | 59 void disposeWrapper() |
| 63 { | 60 { |
| 64 ASSERT(!m_wrapper.IsEmpty()); | 61 ASSERT(!m_maskedWrapper.IsEmpty()); |
| 65 m_wrapper.Dispose(); | 62 m_maskedWrapper = wrapper(); |
| 66 m_wrapper.Clear(); | 63 m_maskedWrapper.Dispose(); |
| 64 m_maskedWrapper.Clear(); |
| 67 } | 65 } |
| 68 | 66 |
| 69 void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const | 67 void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const |
| 70 { | 68 { |
| 71 MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::DOM); | 69 MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::DOM); |
| 72 info.addWeakPointer(const_cast<v8::Persistent<v8::Object>*>(&m_wrapper))
; | 70 info.addWeakPointer(const_cast<v8::Persistent<v8::Object>*>(&m_maskedWra
pper)); |
| 73 } | 71 } |
| 74 | 72 |
| 75 private: | 73 private: |
| 76 v8::Persistent<v8::Object> m_wrapper; | 74 v8::Persistent<v8::Object> m_maskedWrapper; |
| 75 |
| 76 static inline v8::Object* maskOrUnmaskPointer(const v8::Object* object) |
| 77 { |
| 78 const uintptr_t objectPointer = reinterpret_cast<uintptr_t>(object); |
| 79 const uintptr_t randomMask = ~(reinterpret_cast<uintptr_t>(&WebCoreMemor
yTypes::DOM) >> 13); // Entropy via ASLR. |
| 80 return reinterpret_cast<v8::Object*>((objectPointer ^ randomMask) & (!ob
jectPointer - 1)); // Preserve null without branching. |
| 81 } |
| 77 }; | 82 }; |
| 78 | 83 |
| 79 } // namespace WebCore | 84 } // namespace WebCore |
| 80 | 85 |
| 81 #endif // ScriptWrappable_h | 86 #endif // ScriptWrappable_h |
| OLD | NEW |