LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 #include "JsLatch.h" | 18 #include "JsLatch.h" |
19 | 19 |
20 namespace | 20 namespace |
21 { | 21 { |
22 /** | 22 /** |
| 23 * Implementation of JS function "Arrive()" on latch objects. |
23 */ | 24 */ |
24 v8::Handle<v8::Value> ArriveCallback(const v8::Arguments& arguments) | 25 v8::Handle<v8::Value> ArriveCallback(const v8::Arguments& arguments) |
25 { | 26 { |
26 auto self = static_cast<JsTestingLatch *>( | 27 auto self = static_cast<JsTestingLatch *>( |
27 v8::Local<v8::External>::Cast(arguments.Holder()->GetInternalField(0))->Va
lue()); | 28 v8::Local<v8::External>::Cast(arguments.Holder()->GetInternalField(0))->Va
lue()); |
28 if (self) | 29 if (self) |
29 { | 30 { |
30 self->GetLatch().Arrive(); | 31 self->GetLatch().Arrive(); |
31 } | 32 } |
32 return v8::Undefined(); | 33 return v8::Undefined(); |
33 } | 34 } |
34 } | 35 } |
35 | 36 |
36 /** | 37 /** |
37 * \par Implementation Notes | 38 * \par Implementation Notes |
38 * Internal field 0 of the JS object contains a copy of \c this. | 39 * Internal field 0 of the JS object contains a copy of \c this. |
39 */ | 40 */ |
40 V8PersistentNG<v8::Object> JsTestingLatch::JsObjectInitializer(const std::string
& propertyName) | 41 V8PersistentNG<v8::Object> JsTestingLatch::JsObjectInitializer(const std::string
& propertyName) |
41 { | 42 { |
| 43 /* |
| 44 * N.B. Member order ensures that \c engine has been initialized before this f
unction runs. |
| 45 */ |
42 V8ExecutionScope context(engine); | 46 V8ExecutionScope context(engine); |
43 // Object template | 47 // Object template |
44 auto jsObjectTemplate = v8::ObjectTemplate::New(); | 48 auto jsObjectTemplate = v8::ObjectTemplate::New(); |
45 jsObjectTemplate->SetInternalFieldCount(1); | 49 jsObjectTemplate->SetInternalFieldCount(1); |
46 jsObjectTemplate->Set(engine->ToV8String("Arrive"), v8::FunctionTemplate::New(
ArriveCallback)); | 50 jsObjectTemplate->Set(engine->ToV8String("Arrive"), v8::FunctionTemplate::New(
ArriveCallback)); |
47 // Object | 51 // Object |
48 auto localJsObject = jsObjectTemplate->NewInstance(); | 52 auto localJsObject = jsObjectTemplate->NewInstance(); |
49 localJsObject->SetInternalField(0, v8::External::New(this)); | 53 localJsObject->SetInternalField(0, v8::External::New(this)); |
50 // Property on global | 54 // Property on global |
51 engine->GetGlobalObject()->Set(engine->ToV8String(propertyName), localJsObject
); | 55 engine->GetGlobalObject()->Set(engine->ToV8String(propertyName), localJsObject
); |
52 // Persistent | 56 // Persistent |
53 return V8PersistentNG<v8::Object>(engine->GetIsolate(), localJsObject); | 57 return V8PersistentNG<v8::Object>(engine->GetIsolate(), localJsObject); |
54 } | 58 } |
55 | 59 |
56 JsTestingLatch::JsTestingLatch(JsEngineInternal* engine, const std::string& prop
ertyName, int count) | 60 JsTestingLatch::JsTestingLatch(JsEngineInternal* engine, const std::string& prop
ertyName, int count) |
57 : engine(engine), latch(count), jsObject(JsObjectInitializer(propertyName)) | 61 : engine(engine), latch(count), jsObject(JsObjectInitializer(propertyName)) |
58 {} | 62 {} |
59 | 63 |
60 JsTestingLatch::~JsTestingLatch() | 64 JsTestingLatch::~JsTestingLatch() |
61 { | 65 { |
62 V8ExecutionScope context(engine); | 66 V8ExecutionScope context(engine); |
63 auto obj = jsObject.Get(engine->GetIsolate()); | 67 auto obj = jsObject.Get(engine->GetIsolate()); |
64 // ensure object still exists | 68 // ensure object still exists |
65 if (obj->IsObject()) | 69 if (obj->IsObject()) |
66 { | 70 { |
67 obj->SetInternalField(0, v8::External::New(nullptr)); | 71 obj->SetInternalField(0, v8::External::New(nullptr)); |
68 } | 72 } |
69 // Rather than changing the "Arrive" property, we handle a null pointer in the
callback for it. | 73 // Rather than changing the "Arrive" property, we handle a null pointer in the
callback for it. |
70 } | 74 } |
LEFT | RIGHT |