diff --git a/Sources/Fuzzilli/Compiler/Compiler.swift b/Sources/Fuzzilli/Compiler/Compiler.swift index 1a47ce67a..41d348fbc 100644 --- a/Sources/Fuzzilli/Compiler/Compiler.swift +++ b/Sources/Fuzzilli/Compiler/Compiler.swift @@ -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 {