“Then I said, “Behold, I have come; in the scroll of the book it is written of me: I delight to do your will, O my God; your law is within my heart.”” -Psalm 40:7–8
The New Testament writers have a high view of the verse before us, as a prophecy concerning Christ, and therefore ascribing it to Jesus.
“Then he said to them, “These are my words that I spoke to you while I was still with you, that everything written about me in the Law of Moses and the Prophets and the Psalms must be fulfilled.”” -Luke 24:44 Cf. Luke 24:47 & John 5:46).
This is not wrong as many of the prophetic passages and Psalms pointed to Christ and could only be fully understood in this way with Jesus’s commentary, and in retrospect.
But so far, the Psalm has unfolded logically and verses 7-8 participate in the logic of the poetic narrative. Behold, I have come is the equivalent of Here am I, Lord! similarly spoken by Samuel (1 Samuel 3) and later by Isaiah (Isaiah 6).
Next he says, in the scroll of the book it is written of me. The word translated written is the Hebrew word, כָּתַב kâthab, and can mean describe, inscribe, prescribe, or subscribe depending on the context. The Psalmist asserts that the Law (the scroll of the book) prescribes for him obedience to the will of the Lord.
This is exactly what he was saying in verse 6, that the Lord had given him ears to hear. It was his obedient ear the Lord was really after and not the sacrifices, per se.
Further, he delights to do God’s will as it is not only written on the scroll, but written on his heart. This is not only true for the Psalmist, but it is true for all humanity as well (Romans 2:15).