Skip to content

Commit

Permalink
simplify vm constructor instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyasShabi committed Jan 9, 2025
1 parent 0a30759 commit 2bfb603
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions packages/datadog-instrumentations/src/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,27 @@ addHook({ name: names }, function (vm) {
vm.Script = class extends vm.Script {
constructor (code) {
super(...arguments)
this.code = code

if (runScriptStartChannel.hasSubscribers && code) {
runScriptStartChannel.publish({ code })
}
}
}

shimmer.wrap(vm.Script.prototype, 'runInContext', wrapVMMethod(1))
shimmer.wrap(vm.Script.prototype, 'runInNewContext', wrapVMMethod())
shimmer.wrap(vm.Script.prototype, 'runInThisContext', wrapVMMethod())

shimmer.wrap(vm, 'runInContext', wrapVMMethod())
shimmer.wrap(vm, 'runInNewContext', wrapVMMethod())
shimmer.wrap(vm, 'runInThisContext', wrapVMMethod())
shimmer.wrap(vm, 'compileFunction', wrapVMMethod())
shimmer.wrap(vm, 'runInContext', wrapVMMethod)
shimmer.wrap(vm, 'runInNewContext', wrapVMMethod)
shimmer.wrap(vm, 'runInThisContext', wrapVMMethod)
shimmer.wrap(vm, 'compileFunction', wrapVMMethod)

return vm
})

function wrapVMMethod (codeIndex = 0) {
return function wrap (original) {
return function wrapped () {
const code = arguments[codeIndex] ? arguments[codeIndex] : this.code

if (runScriptStartChannel.hasSubscribers && code) {
runScriptStartChannel.publish({ code })
}

return original.apply(this, arguments)
function wrapVMMethod (original) {
return function wrappedVMMethod (code) {
if (runScriptStartChannel.hasSubscribers && code) {
runScriptStartChannel.publish({ code })
}

return original.apply(this, arguments)
}
}

0 comments on commit 2bfb603

Please sign in to comment.