2008-03-11 11:47:54 -07:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Bookmarks Sync.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dan Mills <thunder@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
const EXPORTED_SYMBOLS = ['Async'];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://weave/log4moz.js");
|
|
|
|
Cu.import("resource://weave/util.js");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Asynchronous generator helpers
|
|
|
|
*/
|
|
|
|
|
2008-06-20 13:58:56 -07:00
|
|
|
// Simple class to help us keep track of "namable" objects: that is,
|
|
|
|
// any object with a string property called "name".
|
|
|
|
function NamableTracker() {
|
|
|
|
this.__length = 0;
|
|
|
|
this.__dict = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
NamableTracker.prototype = {
|
|
|
|
get length() { return this.__length; },
|
|
|
|
add: function GD_add(item) {
|
|
|
|
this.__dict[item.name] = item;
|
|
|
|
this.__length++;
|
|
|
|
},
|
|
|
|
remove: function GD_remove(item) {
|
|
|
|
delete this.__dict[item.name];
|
|
|
|
this.__length--;
|
|
|
|
},
|
|
|
|
__iterator__: function GD_iterator() {
|
|
|
|
for (name in this.__dict)
|
|
|
|
yield name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-18 16:11:15 -07:00
|
|
|
let gCurrentId = 0;
|
2008-06-19 19:03:10 -07:00
|
|
|
let gCurrentCbId = 0;
|
2008-06-20 13:58:56 -07:00
|
|
|
let gOutstandingGenerators = new NamableTracker();
|
2008-06-11 18:58:30 -07:00
|
|
|
|
2008-04-10 21:38:15 -07:00
|
|
|
function AsyncException(initFrame, message) {
|
2008-03-11 11:47:54 -07:00
|
|
|
this.message = message;
|
2008-04-10 21:38:15 -07:00
|
|
|
this._trace = initFrame;
|
2008-03-11 11:47:54 -07:00
|
|
|
}
|
|
|
|
AsyncException.prototype = {
|
|
|
|
get message() { return this._message; },
|
|
|
|
set message(value) { this._message = value; },
|
|
|
|
|
|
|
|
get trace() { return this._trace; },
|
|
|
|
set trace(value) { this._trace = value; },
|
2008-04-10 21:38:15 -07:00
|
|
|
|
|
|
|
addFrame: function AsyncException_addFrame(frame) {
|
|
|
|
this.trace += (this.trace? "\n" : "") + formatFrame(frame);
|
|
|
|
},
|
|
|
|
|
2008-03-11 11:47:54 -07:00
|
|
|
toString: function AsyncException_toString() {
|
|
|
|
return this.message;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-06-06 17:46:34 -07:00
|
|
|
function Generator(thisArg, method, onComplete, args) {
|
2008-06-11 19:19:16 -07:00
|
|
|
this._outstandingCbs = 0;
|
2008-03-11 11:47:54 -07:00
|
|
|
this._log = Log4Moz.Service.getLogger("Async.Generator");
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.level =
|
|
|
|
Log4Moz.Level[Utils.prefs.getCharPref("log.logger.async")];
|
2008-06-06 17:46:34 -07:00
|
|
|
this._thisArg = thisArg;
|
2008-03-11 11:47:54 -07:00
|
|
|
this._method = method;
|
2008-06-18 16:11:15 -07:00
|
|
|
this._id = gCurrentId++;
|
2008-03-11 11:47:54 -07:00
|
|
|
this.onComplete = onComplete;
|
|
|
|
this._args = args;
|
2008-06-20 13:58:56 -07:00
|
|
|
|
|
|
|
gOutstandingGenerators.add(this);
|
|
|
|
|
2008-04-10 21:38:15 -07:00
|
|
|
this._initFrame = Components.stack.caller;
|
|
|
|
// skip our frames
|
|
|
|
// FIXME: we should have a pref for this, for debugging async.js itself
|
|
|
|
while (this._initFrame.name.match(/^Async(Gen|)_/))
|
|
|
|
this._initFrame = this._initFrame.caller;
|
2008-03-11 11:47:54 -07:00
|
|
|
}
|
|
|
|
Generator.prototype = {
|
2008-06-11 18:58:30 -07:00
|
|
|
get name() { return this._method.name + "-" + this._id; },
|
2008-03-11 11:47:54 -07:00
|
|
|
get generator() { return this._generator; },
|
|
|
|
|
|
|
|
get cb() {
|
2008-06-11 18:58:30 -07:00
|
|
|
let caller = Components.stack.caller;
|
2008-06-19 19:03:10 -07:00
|
|
|
let cbId = gCurrentCbId++;
|
2008-06-11 19:19:16 -07:00
|
|
|
this._outstandingCbs++;
|
2008-06-19 19:03:10 -07:00
|
|
|
this._log.trace(this.name + ": cb-" + cbId + " generated at:\n" +
|
|
|
|
formatFrame(caller));
|
|
|
|
let self = this;
|
|
|
|
let cb = function(data) {
|
|
|
|
self._log.trace(self.name + ": cb-" + cbId + " called.");
|
|
|
|
self._cont(data);
|
|
|
|
};
|
2008-03-11 11:47:54 -07:00
|
|
|
cb.parentGenerator = this;
|
|
|
|
return cb;
|
|
|
|
},
|
|
|
|
get listener() { return new Utils.EventListener(this.cb); },
|
|
|
|
|
2008-06-06 17:46:34 -07:00
|
|
|
get _thisArg() { return this.__thisArg; },
|
|
|
|
set _thisArg(value) {
|
2008-03-11 11:47:54 -07:00
|
|
|
if (typeof value != "object")
|
2008-05-30 17:38:27 -07:00
|
|
|
throw "Generator: expected type 'object', got type '" + typeof(value) + "'";
|
2008-06-06 17:46:34 -07:00
|
|
|
this.__thisArg = value;
|
2008-03-11 11:47:54 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
get _method() { return this.__method; },
|
|
|
|
set _method(value) {
|
|
|
|
if (typeof value != "function")
|
2008-05-30 17:38:27 -07:00
|
|
|
throw "Generator: expected type 'function', got type '" + typeof(value) + "'";
|
2008-03-11 11:47:54 -07:00
|
|
|
this.__method = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
get onComplete() {
|
|
|
|
if (this._onComplete)
|
|
|
|
return this._onComplete;
|
2008-03-19 15:17:04 -07:00
|
|
|
return function() {
|
|
|
|
//this._log.trace("Generator " + this.name + " has no onComplete");
|
|
|
|
};
|
2008-03-11 11:47:54 -07:00
|
|
|
},
|
|
|
|
set onComplete(value) {
|
|
|
|
if (value && typeof value != "function")
|
2008-05-30 17:38:27 -07:00
|
|
|
throw "Generator: expected type 'function', got type '" + typeof(value) + "'";
|
2008-03-11 11:47:54 -07:00
|
|
|
this._onComplete = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
get trace() {
|
2008-04-10 21:38:15 -07:00
|
|
|
return "unknown (async) :: " + this.name + "\n" + trace(this._initFrame);
|
2008-03-11 11:47:54 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_handleException: function AsyncGen__handleException(e) {
|
|
|
|
if (e instanceof StopIteration) {
|
2008-06-12 23:47:41 -07:00
|
|
|
this._log.trace(this.name + ": End of coroutine reached.");
|
2008-04-10 21:38:15 -07:00
|
|
|
// skip to calling done()
|
2008-03-11 11:47:54 -07:00
|
|
|
|
|
|
|
} else if (this.onComplete.parentGenerator instanceof Generator) {
|
2008-03-26 00:59:34 -07:00
|
|
|
this._log.trace("[" + this.name + "] Saving exception and stack trace");
|
2008-04-10 21:38:15 -07:00
|
|
|
this._log.trace("Exception: " + Utils.exceptionStr(e));
|
2008-03-11 11:47:54 -07:00
|
|
|
|
2008-04-10 21:38:15 -07:00
|
|
|
if (e instanceof AsyncException) {
|
|
|
|
// FIXME: attempt to skip repeated frames, which can happen if the
|
|
|
|
// child generator never yielded. Would break for valid repeats (recursion)
|
|
|
|
if (e.trace.indexOf(formatFrame(this._initFrame)) == -1)
|
|
|
|
e.addFrame(this._initFrame);
|
|
|
|
} else {
|
|
|
|
e = new AsyncException(this.trace, e);
|
|
|
|
}
|
2008-03-11 11:47:54 -07:00
|
|
|
|
|
|
|
this._exception = e;
|
|
|
|
|
|
|
|
} else {
|
2008-04-10 21:38:15 -07:00
|
|
|
this._log.error("Exception: " + Utils.exceptionStr(e));
|
|
|
|
this._log.debug("Stack trace:\n" + (e.trace? e.trace : this.trace));
|
2008-03-11 11:47:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// continue execution of caller.
|
|
|
|
// in the case of StopIteration we could return an error right
|
|
|
|
// away, but instead it's easiest/best to let the caller handle
|
|
|
|
// the error after a yield / in a callback.
|
2008-03-26 00:59:34 -07:00
|
|
|
if (!this._timer) {
|
|
|
|
this._log.trace("[" + this.name + "] running done() from _handleException()");
|
|
|
|
this.done();
|
|
|
|
}
|
2008-03-11 11:47:54 -07:00
|
|
|
},
|
|
|
|
|
2008-06-12 23:47:41 -07:00
|
|
|
_detectDeadlock: function AsyncGen__detectDeadlock() {
|
2008-06-11 19:19:16 -07:00
|
|
|
if (this._outstandingCbs == 0)
|
|
|
|
this._log.warn("Async method '" + this.name +
|
2008-06-12 23:47:41 -07:00
|
|
|
"' may have yielded without an outstanding callback.");
|
2008-06-11 19:19:16 -07:00
|
|
|
},
|
|
|
|
|
2008-03-11 11:47:54 -07:00
|
|
|
run: function AsyncGen_run() {
|
2008-04-10 21:38:15 -07:00
|
|
|
this._continued = false;
|
2008-03-11 11:47:54 -07:00
|
|
|
try {
|
2008-06-06 17:46:34 -07:00
|
|
|
this._generator = this._method.apply(this._thisArg, this._args);
|
2008-03-11 11:47:54 -07:00
|
|
|
this.generator.next(); // must initialize before sending
|
|
|
|
this.generator.send(this);
|
2008-06-11 19:19:16 -07:00
|
|
|
this._detectDeadlock();
|
2008-03-11 11:47:54 -07:00
|
|
|
} catch (e) {
|
2008-03-27 19:12:53 -07:00
|
|
|
if (!(e instanceof StopIteration) || !this._timer)
|
|
|
|
this._handleException(e);
|
2008-03-11 11:47:54 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-06-19 19:03:10 -07:00
|
|
|
_cont: function AsyncGen__cont(data) {
|
2008-06-11 19:19:16 -07:00
|
|
|
this._outstandingCbs--;
|
2008-06-12 23:47:41 -07:00
|
|
|
this._log.trace(this.name + ": resuming coroutine.");
|
2008-04-10 21:38:15 -07:00
|
|
|
this._continued = true;
|
2008-06-11 19:19:16 -07:00
|
|
|
try {
|
|
|
|
this.generator.send(data);
|
|
|
|
this._detectDeadlock();
|
|
|
|
} catch (e) {
|
2008-03-27 19:12:53 -07:00
|
|
|
if (!(e instanceof StopIteration) || !this._timer)
|
|
|
|
this._handleException(e);
|
|
|
|
}
|
2008-03-11 11:47:54 -07:00
|
|
|
},
|
|
|
|
|
2008-06-19 19:03:10 -07:00
|
|
|
_throw: function AsyncGen__throw(exception) {
|
|
|
|
this._outstandingCbs--;
|
2008-03-11 11:47:54 -07:00
|
|
|
try { this.generator.throw(exception); }
|
2008-03-27 19:12:53 -07:00
|
|
|
catch (e) {
|
|
|
|
if (!(e instanceof StopIteration) || !this._timer)
|
|
|
|
this._handleException(e);
|
|
|
|
}
|
2008-03-11 11:47:54 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
// async generators can't simply call a callback with the return
|
|
|
|
// value, since that would cause the calling function to end up
|
|
|
|
// running (after the yield) from inside the generator. Instead,
|
|
|
|
// generators can call this method which sets up a timer to call the
|
|
|
|
// callback from a timer (and cleans up the timer to avoid leaks).
|
|
|
|
// It also closes generators after the timeout, to keep things
|
|
|
|
// clean.
|
|
|
|
done: function AsyncGen_done(retval) {
|
|
|
|
if (this._timer) // the generator/_handleException called self.done() twice
|
|
|
|
return;
|
|
|
|
let self = this;
|
|
|
|
let cb = function() { self._done(retval); };
|
2008-06-12 23:47:41 -07:00
|
|
|
this._log.trace(this.name + ": done() called.");
|
|
|
|
if (!this._exception && this._outstandingCbs > 0)
|
2008-06-11 19:19:16 -07:00
|
|
|
this._log.warn("Async method '" + this.name +
|
|
|
|
"' may have outstanding callbacks.");
|
2008-06-09 19:30:11 -07:00
|
|
|
this._timer = Utils.makeTimerForCall(cb);
|
2008-03-11 11:47:54 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_done: function AsyncGen__done(retval) {
|
2008-03-19 15:17:04 -07:00
|
|
|
if (!this._generator) {
|
2008-03-28 03:25:51 -07:00
|
|
|
this._log.error("Async method '" + this.name + "' is missing a 'yield' call " +
|
|
|
|
"(or called done() after being finalized)");
|
2008-03-19 15:17:04 -07:00
|
|
|
this._log.trace("Initial stack trace:\n" + this.trace);
|
2008-03-28 03:25:51 -07:00
|
|
|
} else {
|
|
|
|
this._generator.close();
|
2008-03-19 15:17:04 -07:00
|
|
|
}
|
2008-03-11 11:47:54 -07:00
|
|
|
this._generator = null;
|
|
|
|
this._timer = null;
|
|
|
|
|
|
|
|
if (this._exception) {
|
2008-03-26 00:59:34 -07:00
|
|
|
this._log.trace("[" + this.name + "] Propagating exception to parent generator");
|
2008-06-19 19:03:10 -07:00
|
|
|
this.onComplete.parentGenerator._throw(this._exception);
|
2008-03-11 11:47:54 -07:00
|
|
|
} else {
|
|
|
|
try {
|
2008-03-26 00:59:34 -07:00
|
|
|
this._log.trace("[" + this.name + "] Running onComplete()");
|
2008-03-11 11:47:54 -07:00
|
|
|
this.onComplete(retval);
|
|
|
|
} catch (e) {
|
|
|
|
this._log.error("Exception caught from onComplete handler of " +
|
|
|
|
this.name + " generator");
|
|
|
|
this._log.error("Exception: " + Utils.exceptionStr(e));
|
2008-04-10 21:38:15 -07:00
|
|
|
this._log.trace("Current stack trace:\n" + trace(Components.stack));
|
2008-03-11 11:47:54 -07:00
|
|
|
this._log.trace("Initial stack trace:\n" + this.trace);
|
|
|
|
}
|
|
|
|
}
|
2008-06-20 13:58:56 -07:00
|
|
|
gOutstandingGenerators.remove(this);
|
2008-03-11 11:47:54 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-04-10 21:38:15 -07:00
|
|
|
function formatFrame(frame) {
|
|
|
|
// FIXME: sort of hackish, might be confusing if there are multiple
|
|
|
|
// extensions with similar filenames
|
|
|
|
let tmp = frame.filename.replace(/^file:\/\/.*\/([^\/]+.js)$/, "module:$1");
|
|
|
|
tmp += ":" + frame.lineNumber + " :: " + frame.name;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
function trace(frame, str) {
|
|
|
|
if (!str)
|
|
|
|
str = "";
|
|
|
|
|
|
|
|
// skip our frames
|
|
|
|
// FIXME: we should have a pref for this, for debugging async.js itself
|
2008-06-09 18:40:30 -07:00
|
|
|
while (frame.name && frame.name.match(/^Async(Gen|)_/))
|
2008-04-10 21:38:15 -07:00
|
|
|
frame = frame.caller;
|
|
|
|
|
|
|
|
if (frame.caller)
|
|
|
|
str = trace(frame.caller, str);
|
|
|
|
str = formatFrame(frame) + (str? "\n" : "") + str;
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-11 11:47:54 -07:00
|
|
|
Async = {
|
2008-06-18 16:11:15 -07:00
|
|
|
get outstandingGenerators() { return gOutstandingGenerators; },
|
2008-03-11 11:47:54 -07:00
|
|
|
|
|
|
|
// Use:
|
|
|
|
// let gen = Async.run(this, this.fooGen, ...);
|
|
|
|
// let ret = yield;
|
|
|
|
//
|
|
|
|
// where fooGen is a generator function, and gen is a Generator instance
|
|
|
|
// ret is whatever the generator 'returns' via Generator.done().
|
|
|
|
|
2008-06-06 17:46:34 -07:00
|
|
|
run: function Async_run(thisArg, method, onComplete /* , arg1, arg2, ... */) {
|
2008-03-11 11:47:54 -07:00
|
|
|
let args = Array.prototype.slice.call(arguments, 3);
|
2008-06-06 17:46:34 -07:00
|
|
|
let gen = new Generator(thisArg, method, onComplete, args);
|
2008-03-11 11:47:54 -07:00
|
|
|
gen.run();
|
|
|
|
return gen;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Syntactic sugar for run(). Meant to be used like this in code
|
|
|
|
// that imports this file:
|
|
|
|
//
|
|
|
|
// Function.prototype.async = Async.sugar;
|
|
|
|
//
|
|
|
|
// So that you can do:
|
|
|
|
//
|
|
|
|
// let gen = fooGen.async(...);
|
|
|
|
// let ret = yield;
|
|
|
|
//
|
|
|
|
// Note that 'this' refers to the method being called, not the
|
|
|
|
// Async object.
|
|
|
|
|
2008-06-06 17:46:34 -07:00
|
|
|
sugar: function Async_sugar(thisArg, onComplete /* , arg1, arg2, ... */) {
|
2008-03-11 11:47:54 -07:00
|
|
|
let args = Array.prototype.slice.call(arguments, 1);
|
2008-06-06 17:46:34 -07:00
|
|
|
args.unshift(thisArg, this);
|
2008-03-11 11:47:54 -07:00
|
|
|
Async.run.apply(Async, args);
|
|
|
|
},
|
|
|
|
|
|
|
|
exceptionStr: function Async_exceptionStr(gen, ex) {
|
|
|
|
return "Exception caught in " + gen.name + ": " + Utils.exceptionStr(ex);
|
|
|
|
}
|
|
|
|
};
|