Skip to content

Commit

Permalink
suspension aware __bool__
Browse files Browse the repository at this point in the history
  • Loading branch information
s-cork committed Dec 16, 2020
1 parent 2f3c0f6 commit ed34e38
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ Compiler.prototype.outputInterruptTest = function () { // Added by RNL
};

Compiler.prototype._jumpfalse = function (test, block) {
var cond = this._gr("jfalse", "(", test, "===false||!Sk.misceval.isTrue(", test, "))");
out("$ret = Sk.misceval.isTrue(", test, ", true);");
this._checkSuspension();
var cond = this._gr("jfalse", "!$ret");
out("if(", cond, "){/*test failed */$blk=", block, ";continue;}");
};

Expand Down
6 changes: 3 additions & 3 deletions src/misceval.js
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ Sk.exportSymbol("Sk.misceval.opAllowsEquality", Sk.misceval.opAllowsEquality);
* @returns {boolean}
* @param {*} x
*/
Sk.misceval.isTrue = function (x) {
Sk.misceval.isTrue = function (x, canSuspend) {
if (x === true || x === Sk.builtin.bool.true$) {
return true;
}
Expand All @@ -626,11 +626,11 @@ Sk.misceval.isTrue = function (x) {
return false;
}
if (x.nb$bool) {
return x.nb$bool(); // the slot wrapper takes care of converting to js Boolean
return x.nb$bool(canSuspend); // the slot wrapper takes care of converting to js Boolean
}
if (x.sq$length) {
// the slot wrapper takes care of the error message and converting to js int
return x.sq$length() !== 0;
return Sk.misceval.chain(x.sq$length(canSuspend), (r) => r !== 0);
}
return Boolean(x);
};
Expand Down
38 changes: 13 additions & 25 deletions src/slotdefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,19 @@ function slotFuncNoArgs(dunderFunc) {
*/
function slotFuncNoArgsWithCheck(dunderName, checkFunc, checkMsg, f) {
return function (dunderFunc) {
return function () {
return function slotfunc(canSuspend) {
const func = dunderFunc.tp$descr_get ? dunderFunc.tp$descr_get(this) : dunderFunc;
let res = Sk.misceval.callsimArray(func, []);
if (!checkFunc(res)) {
throw new Sk.builtin.TypeError(dunderName + " should return " + checkMsg + " (returned " + Sk.abstr.typeName(res) + ")");
}
// f is might be a function that changes the result to a js object like for nb$bool which returns a Boolean
if (f !== undefined) {
return f(res);
}
return res;
const ret = Sk.misceval.chain(Sk.misceval.callsimOrSuspendArray(func, []), (res) => {
if (checkFunc && !checkFunc(res)) {
throw new Sk.builtin.TypeError(dunderName + " should return " + checkMsg + " (returned " + Sk.abstr.typeName(res) + ")");
}
// f is might be a function that changes the result to a js object like for nb$bool which returns a Boolean
if (f !== undefined) {
return f(res);
}
return res;
});
return canSuspend ? ret : Sk.misceval.retryOptionalSuspensionOrThrow(ret);
};
};
}
Expand Down Expand Up @@ -846,21 +848,7 @@ slots.__next__ = {
slots.__len__ = {
$name: "__len__",
$slot_name: "sq$length",
$slot_func: function (dunderFunc) {
return function sq$length(canSuspend) {
let res;
const func = dunderFunc.tp$descr_get ? dunderFunc.tp$descr_get(this) : dunderFunc;
if (canSuspend) {
res = Sk.misceval.callsimOrSuspendArray(func, []);
return Sk.misceval.chain(res, (r) => {
return Sk.misceval.asIndexOrThrow(r);
});
} else {
res = Sk.misceval.callsimArray(func, []);
return Sk.misceval.asIndexOrThrow(res);
}
};
},
$slot_func: slotFuncNoArgsWithCheck(null, null, null, (r) => Sk.misceval.asIndexOrThrow(r)), // asIndexOrThrow checks
$wrapper: wrapperCallBack(wrapperCallNoArgs, (res) => new Sk.builtin.int_(res)),
$flags: { NoArgs: true },
$textsig: "($self, /)",
Expand Down

0 comments on commit ed34e38

Please sign in to comment.