diff --git a/packages/datadog-instrumentations/src/vm.js b/packages/datadog-instrumentations/src/vm.js index d91f89cd48..0c6fdfa5c8 100644 --- a/packages/datadog-instrumentations/src/vm.js +++ b/packages/datadog-instrumentations/src/vm.js @@ -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) } }