Skip to content

Commit c2a5c3a

Browse files
Feature/ternary expression js compiler (#438)
* Makes conditional expressions compilable from JS to FuzzIL * Implements unit test for tertiary expression compilation
1 parent 139d074 commit c2a5c3a

File tree

5 files changed

+485
-600
lines changed

5 files changed

+485
-600
lines changed

Sources/Fuzzilli/Compiler/Compiler.swift

+7
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,13 @@ public class JavaScriptCompiler {
505505

506506
switch expr {
507507

508+
case .ternaryExpression(let ternaryExpression):
509+
let condition = try compileExpression(ternaryExpression.condition)
510+
let consequent = try compileExpression(ternaryExpression.consequent)
511+
let alternate = try compileExpression(ternaryExpression.alternate)
512+
return emit(TernaryOperation(), withInputs: [condition, consequent, alternate]).output
513+
514+
508515
case .identifier(let identifier):
509516
// Identifiers can generally turn into one of three things:
510517
// 1. A FuzzIL variable that has previously been associated with the identifier

Sources/Fuzzilli/Compiler/Parser/parser.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ function tryReadFile(path) {
3434

3535
// Parse the given JavaScript script and return an AST compatible with Fuzzilli's protobuf-based AST format.
3636
function parse(script, proto) {
37-
let ast = Parser.parse(script, { plugins: ["v8intrinsic"] });
38-
37+
let ast = Parser.parse(script, { plugins: ["v8intrinsic"] });
38+
3939
function assertNoError(err) {
4040
if (err) throw err;
4141
}
@@ -519,6 +519,12 @@ function parse(script, proto) {
519519
let argument = visitExpression(node.argument);
520520
return makeExpression('UnaryExpression', { operator, argument });
521521
}
522+
case 'ConditionalExpression': {
523+
let condition = visitExpression(node.test);
524+
let consequent = visitExpression(node.consequent);
525+
let alternate = visitExpression(node.alternate);
526+
return makeExpression('TernaryExpression', { condition, consequent, alternate });
527+
}
522528
case 'BinaryExpression':
523529
case 'LogicalExpression': {
524530
let operator = node.operator;
@@ -571,7 +577,7 @@ protobuf.load(astProtobufDefinitionPath, function(err, root) {
571577

572578
// Uncomment this to print the AST to stdout (will be very verbose).
573579
//console.log(JSON.stringify(ast, null, 2));
574-
580+
575581
const AST = root.lookupType('compiler.protobuf.AST');
576582
let buffer = AST.encode(ast).finish();
577583

0 commit comments

Comments
 (0)