Skip to content

Commit c74eff8

Browse files
Jack-Worksmydea
andauthored
fix(core): Avoid crash when Function.prototype is frozen (#7899)
Co-authored-by: Francesco Novy <[email protected]>
1 parent 8f5a036 commit c74eff8

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

packages/core/src/integrations/functiontostring.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@ export class FunctionToString implements Integration {
2222
// eslint-disable-next-line @typescript-eslint/unbound-method
2323
originalFunctionToString = Function.prototype.toString;
2424

25-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
26-
Function.prototype.toString = function (this: WrappedFunction, ...args: any[]): string {
27-
const context = getOriginalFunction(this) || this;
28-
return originalFunctionToString.apply(context, args);
29-
};
25+
// intrinsics (like Function.prototype) might be immutable in some environments
26+
// e.g. Node with --frozen-intrinsics, XS (an embedded JavaScript engine) or SES (a JavaScript proposal)
27+
try {
28+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
29+
Function.prototype.toString = function (this: WrappedFunction, ...args: any[]): string {
30+
const context = getOriginalFunction(this) || this;
31+
return originalFunctionToString.apply(context, args);
32+
};
33+
} catch {
34+
// ignore errors here, just don't patch this
35+
}
3036
}
3137
}

0 commit comments

Comments
 (0)