Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions src/jsxer/nodes/LogicalExpression.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#include "LogicalExpression.h"

string get_expr(AstNode *node, const string& literal){
return '(' + (node == nullptr ? literal : node->to_string()) + ')';
string LogicalExpression::parenthesis(AstNode *node){
switch (node->type()) {
case NodeType::LocalAssignmentExpression:
case NodeType::AssignmentExpression:
return "(" + node->to_string() + ")";
default:
return node->to_string();
}
}


void LogicalExpression::parse() {
// opName is either "&&" or "||"
opName = decoders::d_sid(reader);
Expand All @@ -14,5 +21,50 @@ void LogicalExpression::parse() {
}

string LogicalExpression::to_string() {
return get_expr(leftExpr, leftLiteral) + ' ' + opName + ' ' + get_expr(rightExpr, rightLiteral);
string result = " " + opName + " ";

// Explanation of the below logic:
// ExtendScript does not honor traditional boolean order of operations with logical expressions.
// The decompiler now produces output that forces order of operations equivalent to ES with modern interpreters like V8,
// and it manages to do it such that it does not affect the order of operations if evaluated in the ES runtime.
// Awesome! :)

if (leftExpr != nullptr) {
if (leftExpr->type() == NodeType::LogicalExpression) {
LogicalExpression* left = (LogicalExpression *) leftExpr;
if (left->opName == "||" && opName == "&&") {
result = "(" + left->to_string() + ")" + result;
if (rightExpr != nullptr && rightExpr->type() == NodeType::LogicalExpression) {
LogicalExpression* right = (LogicalExpression *) rightExpr;
if (right->opName == "||"){
result += "(" + right->to_string() + ")";
goto done;
}
}
}
else
result = left->to_string() + result;
} else {
result = parenthesis(leftExpr) + result;
}
} else {
result = leftLiteral + result;
}

if (rightExpr != nullptr) {
if (rightExpr->type() == NodeType::LogicalExpression) {
LogicalExpression* right = (LogicalExpression *) rightExpr;
if (right->opName == "||")
result += "(" + right->to_string() + ")";
else
result += right->to_string();
} else {
result += parenthesis(rightExpr);
}
} else {
result += rightLiteral;
}

done:
return result;
}
6 changes: 6 additions & 0 deletions src/jsxer/nodes/LogicalExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ namespace jsxer { namespace nodes {
AstNode *rightExpr;
string leftLiteral;
string rightLiteral;

string get_expr(AstNode *node, const string &literal);

bool contains_or();

string parenthesis(AstNode *node);
};
} }
2 changes: 1 addition & 1 deletion src/jsxer/nodes/TernaryExpression.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "TernaryExpression.h"

bool parenthesis(AstNode* node) {
bool TernaryExpression::parenthesis(AstNode* node) {
return (node->type() == NodeType::TernaryExpression) && (node->type() == NodeType::ListExpression);
}

Expand Down
1 change: 1 addition & 0 deletions src/jsxer/nodes/TernaryExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ namespace jsxer { namespace nodes {
AstNode* node_true;
AstNode* node_false;

bool parenthesis(AstNode *node);
};
} }