Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

代码含二元表达式存在问题 #1

Open
duyinghua opened this issue Feb 9, 2023 · 1 comment
Open

代码含二元表达式存在问题 #1

duyinghua opened this issue Feb 9, 2023 · 1 comment

Comments

@duyinghua
Copy link

duyinghua commented Feb 9, 2023

当测试代码片段含有二元表达式时存在死循环问题,如:

var test = function (a, b){
   return a > 20 ? a : b
}
console.log(test(10, 1))
@duyinghua
Copy link
Author

尝试效仿IfStatement的解析,改动compiler.ts后可行,望作者CR并修复问题

case 'ConditionalExpression': {
         const endLabel = this.createLabelName('cond_end');
         const altLabel = this.createLabelName('cond_alt');
         const { test, consequent, alternate } = expr;
         this.compileExpression(test);
-        this.writeLabel(altLabel);
+        this.writeReference(alternate ? altLabel : endLabel);
         this.writeOp(OpCode.JUMPNOT);
         this.compileExpression(consequent);
-        this.writeReference(endLabel);
-        this.writeOp(OpCode.JUMP);
-        this.writeLabel(altLabel);
-        this.compileExpression(alternate);
+        if (alternate) {
+          this.writeReference(endLabel);
+          this.writeOp(OpCode.JUMP);
+          this.writeLabel(altLabel);
+          this.compileExpression(alternate);
+        }
         this.writeLabel(endLabel);
         break;
       }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant