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 2 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
9 changes: 8 additions & 1 deletion Sources/Fuzzilli/Compiler/Compiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,16 @@ public class JavaScriptCompiler {
guard let expr = node.expression else {
throw CompilerError.invalidASTError("missing concrete expression in expression node")
}

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
17 changes: 13 additions & 4 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,15 @@ 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,8 +580,8 @@ 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');
const AST = root.lookupType('compiler.protobuf.AST');
let buffer = AST.encode(ast).finish();

fs.writeFileSync(outputFilePath, buffer);
Expand Down
Loading
Loading