Skip to content

Commit 032f503

Browse files
committed
Replace SimplePromise with native Promises
This PR replaces usage of SimplePromise with native Promises. The API shape is slightly different, requiring some logic changes. The biggest change is that `Stage.initialize()` is now async, which lets us remove `waitUntilRead()`, and `_runBenchmarkAndRecordResults()` is also async. modified: MotionMark/resources/debug-runner/debug-runner.js: Fix a bug that broke clicking some controls. modified: MotionMark/resources/extensions.js modified: MotionMark/resources/runner/benchmark-runner.js modified: MotionMark/resources/runner/motionmark.js modified: MotionMark/tests/3d/resources/webgl.js modified: MotionMark/tests/3d/resources/webgpu.js modified: MotionMark/tests/bouncing-particles/resources/bouncing-canvas-images.js modified: MotionMark/tests/bouncing-particles/resources/bouncing-canvas-particles.js modified: MotionMark/tests/bouncing-particles/resources/bouncing-canvas-shapes.js modified: MotionMark/tests/bouncing-particles/resources/bouncing-css-images.js modified: MotionMark/tests/bouncing-particles/resources/bouncing-css-shapes.js modified: MotionMark/tests/bouncing-particles/resources/bouncing-particles.js modified: MotionMark/tests/bouncing-particles/resources/bouncing-svg-images.js modified: MotionMark/tests/bouncing-particles/resources/bouncing-svg-shapes.js modified: MotionMark/tests/bouncing-particles/resources/bouncing-tagged-images.js modified: MotionMark/tests/core/resources/canvas-stage.js modified: MotionMark/tests/core/resources/canvas-tests.js modified: MotionMark/tests/core/resources/design.js modified: MotionMark/tests/core/resources/focus.js modified: MotionMark/tests/core/resources/image-data.js modified: MotionMark/tests/core/resources/leaves.js modified: MotionMark/tests/core/resources/multiply.js modified: MotionMark/tests/core/resources/suits.js modified: MotionMark/tests/dom/resources/compositing-transforms.js modified: MotionMark/tests/dom/resources/dom-particles.js modified: MotionMark/tests/dom/resources/focus.js modified: MotionMark/tests/resources/main.js modified: MotionMark/tests/simple/resources/simple-canvas-paths.js modified: MotionMark/tests/simple/resources/tiled-canvas-image.js modified: MotionMark/tests/template/resources/template-canvas.js modified: MotionMark/tests/template/resources/template-css.js modified: MotionMark/tests/template/resources/template-svg.js
1 parent 75efd9e commit 032f503

31 files changed

+256
-401
lines changed

MotionMark/resources/extensions.js

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -434,44 +434,6 @@ class UnitBezier {
434434
}
435435
}
436436

437-
class SimplePromise {
438-
constructor()
439-
{
440-
this._chainedPromise = null;
441-
this._callback = null;
442-
}
443-
444-
then (callback)
445-
{
446-
if (this._callback)
447-
throw "SimplePromise doesn't support multiple calls to then";
448-
449-
this._callback = callback;
450-
this._chainedPromise = new SimplePromise;
451-
452-
if (this._resolved)
453-
this.resolve(this._resolvedValue);
454-
455-
return this._chainedPromise;
456-
}
457-
458-
resolve (value)
459-
{
460-
if (!this._callback) {
461-
this._resolved = true;
462-
this._resolvedValue = value;
463-
return;
464-
}
465-
466-
var result = this._callback(value);
467-
if (result instanceof SimplePromise) {
468-
var chainedPromise = this._chainedPromise;
469-
result.then(function (result) { chainedPromise.resolve(result); });
470-
} else
471-
this._chainedPromise.resolve(result);
472-
}
473-
}
474-
475437
class Heap {
476438

477439
static createMinHeap(maxSize)

MotionMark/resources/runner/benchmark-runner.js

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ class BenchmarkRunnerState {
6464

6565
prepareCurrentTest(runner, frame)
6666
{
67-
var test = this.currentTest();
68-
var promise = new SimplePromise;
69-
70-
frame.onload = function() {
71-
promise.resolve();
72-
};
67+
const test = this.currentTest();
68+
const promise = new Promise(resolve => {
69+
frame.onload = function() {
70+
resolve();
71+
};
72+
});
7373

7474
frame.src = "tests/" + test.url;
7575
return promise;
@@ -102,86 +102,80 @@ class BenchmarkRunner {
102102
}
103103
}
104104

105-
_runBenchmarkAndRecordResults(state)
105+
async _runBenchmarkAndRecordResults(state)
106106
{
107-
var promise = new SimplePromise;
108-
var suite = state.currentSuite();
109-
var test = state.currentTest();
107+
const suite = state.currentSuite();
108+
const test = state.currentTest();
110109

111110
if (this._client && this._client.willRunTest)
112111
this._client.willRunTest(suite, test);
113112

114-
var contentWindow = this._frame.contentWindow;
115-
var self = this;
113+
const contentWindow = this._frame.contentWindow;
116114

117-
var options = { complexity: test.complexity };
115+
const options = { complexity: test.complexity };
118116
Utilities.extendObject(options, this._client.options);
119117
Utilities.extendObject(options, Utilities.parseParameters(contentWindow.location));
120118

121-
var benchmark = new contentWindow.benchmarkClass(options);
122-
document.body.style.backgroundColor = benchmark.backgroundColor();
123-
benchmark.run().then(function(testData) {
124-
var suiteResults = self._suitesResults[suite.name] || {};
125-
suiteResults[test.name] = testData;
126-
self._suitesResults[suite.name] = suiteResults;
119+
const benchmark = new contentWindow.benchmarkClass(options);
120+
document.body.style.backgroundColor = benchmark.backgroundColor(); // FIXME: Do this via a selector.
121+
122+
await benchmark.initialize(options);
123+
const testData = await benchmark.run();
124+
const suiteResults = this._suitesResults[suite.name] || {};
125+
suiteResults[test.name] = testData;
126+
this._suitesResults[suite.name] = suiteResults;
127127

128-
if (self._client && self._client.didRunTest)
129-
self._client.didRunTest(testData);
128+
if (this._client && this._client.didRunTest)
129+
this._client.didRunTest(testData);
130130

131-
state.next();
132-
if (state.currentSuite() != suite)
133-
self._removeFrame();
134-
promise.resolve(state);
135-
});
131+
state.next();
132+
if (state.currentSuite() != suite)
133+
this._removeFrame();
136134

137-
return promise;
135+
return state;
138136
}
139137

140-
step(state)
138+
async step(state)
141139
{
142140
if (!state) {
143141
state = new BenchmarkRunnerState(this._suites);
144142
this._suitesResults = {};
145143
}
146144

147-
var suite = state.currentSuite();
145+
const suite = state.currentSuite();
148146
if (!suite) {
149147
this._finalize();
150-
var promise = new SimplePromise;
151-
promise.resolve();
152-
return promise;
148+
return;
153149
}
154150

155-
if (state.isFirstTest()) {
151+
if (state.isFirstTest())
156152
this._appendFrame();
157-
}
158153

159-
return state.prepareCurrentTest(this, this._frame).then(function(prepareReturnValue) {
160-
return this._runBenchmarkAndRecordResults(state);
161-
}.bind(this));
154+
await state.prepareCurrentTest(this, this._frame);
155+
const nextState = await this._runBenchmarkAndRecordResults(state);
156+
return nextState;
162157
}
163158

164159
runAllSteps(startingState)
165160
{
166-
var nextCallee = this.runAllSteps.bind(this);
167-
this.step(startingState).then(function(nextState) {
168-
if (nextState)
169-
nextCallee(nextState);
161+
this.step(startingState).then(nextState => {
162+
if (!nextState)
163+
return;
164+
this.runAllSteps(nextState);
170165
});
171166
}
172167

173168
runMultipleIterations()
174169
{
175-
var self = this;
176-
var currentIteration = 0;
170+
let currentIteration = 0;
177171

178-
this._runNextIteration = function() {
179-
currentIteration++;
180-
if (currentIteration < self._client.iterationCount)
181-
self.runAllSteps();
172+
this._runNextIteration = () => {
173+
++currentIteration;
174+
if (currentIteration < this._client.iterationCount)
175+
this.runAllSteps();
182176
else if (this._client && this._client.didFinishLastIteration) {
183-
document.body.style.backgroundColor = "";
184-
self._client.didFinishLastIteration();
177+
document.body.style.backgroundColor = ""; // FIXME: Do this via a selector.
178+
this._client.didFinishLastIteration();
185179
}
186180
}
187181

MotionMark/tests/3d/resources/webgl.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
*/
2525

2626
class WebGLStage extends Stage {
27-
initialize(benchmark, options)
27+
async initialize(benchmark, options)
2828
{
29-
super.initialize(benchmark, options);
30-
29+
await super.initialize(benchmark, options);
3130
this._numTriangles = 0;
3231
this._bufferSize = 0;
3332

0 commit comments

Comments
 (0)