Skip to content

Commit

Permalink
Implements compilation of the delete operator
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasWienand committed Nov 13, 2024
1 parent f6b1917 commit 6378490
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
50 changes: 46 additions & 4 deletions Sources/Fuzzilli/Compiler/Compiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -987,11 +987,53 @@ public class JavaScriptCompiler {

if unaryExpression.operator == "typeof" {
return emit(TypeOf(), withInputs: [argument]).output
} else if unaryExpression.operator == "delete" {
let argumentExpression = unaryExpression.argument.expression!

if case .memberExpression(let memberExpression) = argumentExpression {
let obj = try compileExpression(memberExpression.object)
let isGuarded = memberExpression.isOptional

if memberExpression.name != "" {
// Deleting a non-computed property (e.g., delete obj.prop)
let propertyName = memberExpression.name
let instr = emit(
DeleteProperty(propertyName: propertyName, isGuarded: isGuarded),
withInputs: [obj]
)
return instr.output
} else {
// Deleting a computed property (e.g., delete obj[expr])
let propertyExpression = memberExpression.expression
let propertyExpr = propertyExpression.expression
let property = try compileExpression(propertyExpression)

if case .numberLiteral(let numberLiteral) = propertyExpr {
// Delete an element (e.g., delete arr[42])
let index = Int64(numberLiteral.value)
let instr = emit(
DeleteElement(index: index, isGuarded: isGuarded),
withInputs: [obj]
)
return instr.output
} else {
// Use DeleteComputedProperty for other computed properties (e.g., delete obj["key"])
let instr = emit(
DeleteComputedProperty(isGuarded: isGuarded),
withInputs: [obj, property]
)
return instr.output
}
}
} else {
throw CompilerError.invalidNodeError("Invalid argument for delete operator; expected a member expression")
}
} else {
guard let op = UnaryOperator(rawValue: unaryExpression.operator) else {
throw CompilerError.invalidNodeError("invalid unary operator: \(unaryExpression.operator)")
}
return emit(UnaryOperation(op), withInputs: [argument]).output
}
guard let op = UnaryOperator(rawValue: unaryExpression.operator) else {
throw CompilerError.invalidNodeError("invalid unary operator: \(unaryExpression.operator)")
}
return emit(UnaryOperation(op), withInputs: [argument]).output

case .binaryExpression(let binaryExpression):
let lhs = try compileExpression(binaryExpression.lhs)
Expand Down
22 changes: 22 additions & 0 deletions Tests/FuzzilliTests/CompilerTests/basic_delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
if (typeof output === 'undefined') output = console.log;

const obj = { a: 1 };
console.log(delete obj.a);
console.log(obj);

const propName = 'b';
obj[propName] = 2;
console.log(delete obj[propName]);
console.log(obj);

const arr = [1, 2, 3];
console.log(delete arr[1]);
console.log(arr);

const index = 0;
console.log(delete arr[index]);
console.log(arr);

const nestedObj = { a: { b: 2 } };
console.log(delete nestedObj?.a?.b);
console.log(nestedObj);

0 comments on commit 6378490

Please sign in to comment.