Skip to content

Commit 1aa7647

Browse files
committed
||/&& now doesn't use booleans (fix espruino#251)
1 parent 103a60e commit 1aa7647

2 files changed

Lines changed: 21 additions & 27 deletions

File tree

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Fix E.getAnalogVRef() regression
33
Add Math.tan()
44
Ensure Double/Integer have Number as a prototype (fixes: Number.prototype.n=function();(5.0).n() )
5+
||/&& now doesn't use booleans (fix #251)
56

67
1v54 : Add 4x6 font (instead of 8x8)
78
Fix occasional instability with Waveform read/write

src/jsparse.c

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,42 +1405,35 @@ NO_INLINE JsVar *jspeRelationalExpression() {
14051405
}
14061406

14071407
NO_INLINE JsVar *__jspeLogicalExpression(JsVar *a) {
1408-
JsVar *b = 0;
14091408
while (execInfo.lex->tk=='&' || execInfo.lex->tk=='|' || execInfo.lex->tk=='^' || execInfo.lex->tk==LEX_ANDAND || execInfo.lex->tk==LEX_OROR) {
1410-
bool shortCircuit = false;
1411-
bool boolean = false;
14121409
int op = execInfo.lex->tk;
14131410
JSP_MATCH(execInfo.lex->tk);
14141411

14151412
// if we have short-circuit ops, then if we know the outcome
14161413
// we don't bother to execute the other op. Even if not
14171414
// we need to tell mathsOp it's an & or |
1418-
if (op==LEX_ANDAND) {
1419-
op = '&';
1420-
shortCircuit = !jsvGetBoolAndUnLock(jsvSkipName(a));
1421-
boolean = true;
1422-
} else if (op==LEX_OROR) {
1423-
op = '|';
1424-
shortCircuit = jsvGetBoolAndUnLock(jsvSkipName(a));
1425-
boolean = true;
1426-
}
1427-
1428-
JSP_SAVE_EXECUTE();
1429-
if (shortCircuit) jspSetNoExecute();
1430-
b = jspeRelationalExpression();
1431-
if (shortCircuit) JSP_RESTORE_EXECUTE();
1432-
if (JSP_SHOULD_EXECUTE && !shortCircuit) {
1433-
JsVar *res;
1434-
if (boolean) {
1435-
JsVar *newa = jsvNewFromBool(jsvGetBoolAndUnLock(jsvSkipName(a)));
1436-
JsVar *newb = jsvNewFromBool(jsvGetBoolAndUnLock(jsvSkipName(b)));
1437-
jsvUnLock(a); a = newa;
1438-
jsvUnLock(b); b = newb;
1415+
if (op==LEX_ANDAND || op==LEX_OROR) {
1416+
bool aValue = jsvGetBoolAndUnLock(jsvSkipName(a));
1417+
if ((!aValue && op==LEX_ANDAND) ||
1418+
(aValue && op==LEX_OROR)) {
1419+
// use first argument (A)
1420+
JSP_SAVE_EXECUTE();
1421+
jspSetNoExecute();
1422+
jsvUnLock(jspeRelationalExpression());
1423+
JSP_RESTORE_EXECUTE();
1424+
} else {
1425+
// use second argument (B)
1426+
jsvUnLock(a);
1427+
a = jspeRelationalExpression();
14391428
}
1440-
res = jsvMathsOpSkipNames(a, b, op);
1441-
jsvUnLock(a); a = res;
1429+
} else { // else it's a more 'normal' logical expression - just use Maths
1430+
JsVar *b = jspeRelationalExpression();
1431+
if (JSP_SHOULD_EXECUTE) {
1432+
JsVar *res = jsvMathsOpSkipNames(a, b, op);
1433+
jsvUnLock(a); a = res;
1434+
}
1435+
jsvUnLock(b);
14421436
}
1443-
jsvUnLock(b);
14441437
}
14451438
return a;
14461439
}

0 commit comments

Comments
 (0)