Skip to content

Commit

Permalink
Fixes style issues in delete operator compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasWienand committed Nov 14, 2024
1 parent 08f0af5 commit f8ba804
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions Sources/Fuzzilli/Compiler/Compiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -988,45 +988,44 @@ 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
guard case .memberExpression(let memberExpression) = unaryExpression.argument.expression else {
throw CompilerError.invalidNodeError("delete operator must be applied to a member expression")
}

if memberExpression.name != "" {
// Deleting a non-computed property (e.g., delete obj.prop)
let propertyName = memberExpression.name
let obj = try compileExpression(memberExpression.object)
// isGuarded is true if the member expression is optional (e.g., obj?.prop)
let isGuarded = memberExpression.isOptional

if !memberExpression.name.isEmpty {
// 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(
DeleteProperty(propertyName: propertyName, isGuarded: isGuarded),
DeleteElement(index: index, 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
}
// 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 {
Expand Down

0 comments on commit f8ba804

Please sign in to comment.