Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/JsLatch.cpp

Issue 29372702: Issue #4826 - Use latch to replace thread-sleeping in tests
Patch Set: Created Jan. 19, 2017, 5:56 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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 */
24 v8::Handle<v8::Value> ArriveCallback(const v8::Arguments& arguments)
25 {
26 auto self = static_cast<JsTestingLatch *>(
27 v8::Local<v8::External>::Cast(arguments.Holder()->GetInternalField(0))->Va lue());
28 if (self)
29 {
30 self->GetLatch().Arrive();
31 }
32 return v8::Undefined();
33 }
34 }
35
36 /**
37 * \par Implementation Notes
38 * Internal field 0 of the JS object contains a copy of \c this.
39 */
40 V8PersistentNG<v8::Object> JsTestingLatch::JsObjectInitializer(const std::string & propertyName)
41 {
42 V8ExecutionScope context(engine);
43 // Object template
44 auto jsObjectTemplate = v8::ObjectTemplate::New();
45 jsObjectTemplate->SetInternalFieldCount(1);
46 jsObjectTemplate->Set(engine->ToV8String("Arrive"), v8::FunctionTemplate::New( ArriveCallback));
47 // Object
48 auto localJsObject = jsObjectTemplate->NewInstance();
49 localJsObject->SetInternalField(0, v8::External::New(this));
50 // Property on global
51 engine->GetGlobalObject()->Set(engine->ToV8String(propertyName), localJsObject );
52 // Persistent
53 return V8PersistentNG<v8::Object>(engine->GetIsolate(), localJsObject);
54 }
55
56 JsTestingLatch::JsTestingLatch(JsEngineInternal* engine, const std::string& prop ertyName, int count)
57 : engine(engine), latch(count), jsObject(JsObjectInitializer(propertyName))
58 {}
59
60 JsTestingLatch::~JsTestingLatch()
61 {
62 V8ExecutionScope context(engine);
63 auto obj = jsObject.Get(engine->GetIsolate());
64 // ensure object still exists
65 if (obj->IsObject())
66 {
67 obj->SetInternalField(0, v8::External::New(nullptr));
68 }
69 // Rather than changing the "Arrive" property, we handle a null pointer in the callback for it.
70 }
OLDNEW

Powered by Google App Engine
This is Rietveld