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

Feature/ternary expression js compiler #438

Merged
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
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
Loading