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