| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 #ifndef ADBLOCK_PLUS_V8_VALUE_HOLDER_H | 18 #ifndef ADBLOCK_PLUS_V8_VALUE_HOLDER_H |
| 19 #define ADBLOCK_PLUS_V8_VALUE_HOLDER_H | 19 #define ADBLOCK_PLUS_V8_VALUE_HOLDER_H |
| 20 | 20 |
| 21 #include <memory> | 21 #include <memory> |
| 22 | 22 |
| 23 namespace v8 | 23 namespace v8 |
| 24 { | 24 { |
| 25 class Isolate; | 25 class Isolate; |
| 26 template<class T> class Handle; | 26 template<class T> class Handle; |
| 27 template<class T> class Persistent; | 27 template<class T> class Persistent; |
| 28 class Value; | |
| 28 } | 29 } |
| 29 | 30 |
| 30 namespace AdblockPlus | 31 namespace AdblockPlus |
| 31 { | 32 { |
| 33 | |
|
Felix Dahlke
2014/07/11 09:22:20
Superfluous whitespace.
| |
| 32 template<class T> | 34 template<class T> |
| 33 class V8ValueHolder | 35 class V8ValueHolder |
| 34 { | 36 { |
| 35 public: | 37 public: |
| 36 V8ValueHolder() | 38 V8ValueHolder() |
| 37 { | 39 { |
| 40 value = NULL; | |
|
Wladimir Palant
2014/07/11 06:19:42
Note that we generally use 0 rather than NULL.
Felix Dahlke
2014/07/11 09:22:20
Also, if we actually want to move away from auto_p
sergei
2014/07/22 16:25:38
All members should be initialized in the ctr (in a
Wladimir Palant
2014/07/23 11:21:29
The reset() call below will set the isolate member
| |
| 38 reset(0, v8::Persistent<T>()); | 41 reset(0, v8::Persistent<T>()); |
| 39 } | 42 } |
| 40 V8ValueHolder(V8ValueHolder& value) | 43 V8ValueHolder(V8ValueHolder& value) |
|
sergei
2014/07/22 16:25:38
Why is not const?
| |
| 41 { | 44 { |
| 45 this->value = NULL; | |
| 42 reset(value.isolate, static_cast<v8::Handle<T> >(value)); | 46 reset(value.isolate, static_cast<v8::Handle<T> >(value)); |
| 43 } | 47 } |
| 44 V8ValueHolder(v8::Isolate* isolate, v8::Persistent<T> value) | 48 V8ValueHolder(v8::Isolate* isolate, v8::Persistent<T> value) |
| 45 { | 49 { |
| 50 this->value = NULL; | |
| 46 reset(isolate, value); | 51 reset(isolate, value); |
| 47 } | 52 } |
| 48 V8ValueHolder(v8::Isolate* isolate, v8::Handle<T> value) | 53 V8ValueHolder(v8::Isolate* isolate, v8::Handle<T> value) |
| 49 { | 54 { |
| 55 this->value = NULL; | |
| 50 reset(isolate, value); | 56 reset(isolate, value); |
| 51 } | 57 } |
| 52 ~V8ValueHolder() | 58 ~V8ValueHolder() |
| 53 { | 59 { |
| 54 reset(isolate, v8::Persistent<T>()); | 60 reset(isolate, v8::Persistent<T>()); |
|
sergei
2014/07/22 16:25:38
Superfluous space at the end of the line.
| |
| 55 } | 61 } |
| 62 | |
| 63 static void ABPDisposeCallback(v8::Persistent<v8::Value> object, void* param eter) | |
| 64 { | |
| 65 v8::Persistent<v8::Value>* objectPointer = (v8::Persistent<v8::Value>*)par ameter; | |
|
sergei
2014/07/22 16:25:38
static cast
| |
| 66 delete objectPointer; | |
| 67 | |
| 68 object.Dispose(); | |
|
sergei
2014/07/22 16:25:38
In the docs it's said we should use ``Dispose(isol
| |
| 69 object.Clear(); | |
| 70 } | |
| 71 | |
| 56 void reset(v8::Isolate* isolate, v8::Persistent<T> value) | 72 void reset(v8::Isolate* isolate, v8::Persistent<T> value) |
| 57 { | 73 { |
| 58 if (this->value.get()) | 74 if (this->value != NULL) |
| 59 { | 75 { |
| 60 this->value->Dispose(this->isolate); | 76 this->value->MakeWeak(this->value, (v8::WeakReferenceCallback)&AdblockPl us::V8ValueHolder<T>::ABPDisposeCallback); |
|
Wladimir Palant
2014/07/11 06:19:42
So, why is calling Dispose() an issue here? This V
Felix Dahlke
2014/07/11 09:22:20
I agree, the actual change you made (turning this
Felix Dahlke
2014/07/11 09:24:16
Oh, and we should definitely also ensure to declar
sergei
2014/07/22 16:25:38
The recommended version is with `isolate`.
Oleksandr
2014/10/23 10:48:24
I think ideally we should let V8 dispose the handl
| |
| 61 this->value.reset(0); | |
| 62 } | 77 } |
| 63 | 78 |
| 64 if (!value.IsEmpty()) | 79 if (!value.IsEmpty()) |
| 65 { | 80 { |
| 66 this->isolate = isolate; | 81 this->isolate = isolate; |
| 67 this->value.reset(new v8::Persistent<T>(value)); | 82 this->value = new v8::Persistent<T>(value); |
| 68 } | 83 } |
| 69 } | 84 } |
| 70 | 85 |
| 71 void reset(v8::Isolate* isolate, v8::Handle<T> value) | 86 void reset(v8::Isolate* isolate, v8::Handle<T> value) |
| 72 { | 87 { |
| 73 reset(isolate, v8::Persistent<T>::New(isolate, value)); | 88 reset(isolate, v8::Persistent<T>::New(isolate, value)); |
| 74 } | 89 } |
| 75 | 90 |
| 76 T* operator->() const | 91 T* operator->() const |
| 77 { | 92 { |
| 78 return **value; | 93 return **value; |
|
sergei
2014/07/22 16:25:38
I would like to have at least assert before derefe
Wladimir Palant
2014/07/23 11:21:29
Note that in the original version (which we want t
| |
| 79 } | 94 } |
| 80 operator v8::Handle<T>() const | 95 operator v8::Handle<T>() const |
|
sergei
2014/07/22 16:25:38
I'm not sure whether we want to return the copy or
| |
| 81 { | 96 { |
| 82 return *value; | 97 return *value; |
| 83 } | 98 } |
| 84 operator v8::Persistent<T>() const | 99 operator v8::Persistent<T>() const |
| 85 { | 100 { |
| 86 return *value; | 101 return *value; |
| 87 } | 102 } |
| 88 private: | 103 private: |
| 89 v8::Isolate* isolate; | 104 v8::Isolate* isolate; |
| 90 std::auto_ptr<v8::Persistent<T> > value; | 105 v8::Persistent<T>* value; |
|
Wladimir Palant
2014/07/11 06:19:42
I don't consider using raw pointers a good idea -
Felix Dahlke
2014/07/11 09:22:20
Agreed, auto_ptr is evidently what we want here: D
Oleksandr
2014/10/23 10:48:24
The reason of a raw pointer here is that we want t
| |
| 91 }; | 106 }; |
| 92 } | 107 } |
| 93 | 108 |
| 94 #endif | 109 #endif |
| OLD | NEW |