Skip to content

Commit

Permalink
Feature/ternary expression js compiler (#438)
Browse files Browse the repository at this point in the history
* Makes conditional expressions compilable from JS to FuzzIL
* Implements unit test for tertiary expression compilation
  • Loading branch information
TobiasWienand authored and Samuel Groß committed Jul 26, 2024
1 parent d5785d4 commit 72fd77c
Show file tree
Hide file tree
Showing 5 changed files with 485 additions and 600 deletions.
7 changes: 7 additions & 0 deletions Sources/Fuzzilli/Compiler/Compiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,13 @@ public class JavaScriptCompiler {

switch expr {

case .ternaryExpression(let ternaryExpression):
let condition = try compileExpression(ternaryExpression.condition)
let consequent = try compileExpression(ternaryExpression.consequent)
let alternate = try compileExpression(ternaryExpression.alternate)
return emit(TernaryOperation(), withInputs: [condition, consequent, alternate]).output


case .identifier(let identifier):
// Identifiers can generally turn into one of three things:
// 1. A FuzzIL variable that has previously been associated with the identifier
Expand Down
12 changes: 9 additions & 3 deletions Sources/Fuzzilli/Compiler/Parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ function tryReadFile(path) {

// Parse the given JavaScript script and return an AST compatible with Fuzzilli's protobuf-based AST format.
function parse(script, proto) {
let ast = Parser.parse(script, { plugins: ["v8intrinsic"] });

let ast = Parser.parse(script, { plugins: ["v8intrinsic"] });
function assertNoError(err) {
if (err) throw err;
}
Expand Down Expand Up @@ -519,6 +519,12 @@ function parse(script, proto) {
let argument = visitExpression(node.argument);
return makeExpression('UnaryExpression', { operator, argument });
}
case 'ConditionalExpression': {
let condition = visitExpression(node.test);
let consequent = visitExpression(node.consequent);
let alternate = visitExpression(node.alternate);
return makeExpression('TernaryExpression', { condition, consequent, alternate });
}
case 'BinaryExpression':
case 'LogicalExpression': {
let operator = node.operator;
Expand Down Expand Up @@ -571,7 +577,7 @@ protobuf.load(astProtobufDefinitionPath, function(err, root) {

// Uncomment this to print the AST to stdout (will be very verbose).
//console.log(JSON.stringify(ast, null, 2));

const AST = root.lookupType('compiler.protobuf.AST');
let buffer = AST.encode(ast).finish();

Expand Down
Loading

0 comments on commit 72fd77c

Please sign in to comment.