In an unconventional demonstration of JavaScript's internal mechanics, developer Bartosz has shared a method to invoke arbitrary functions without utilizing standard parentheses. The technique relies on manipulating the way the engine handles error stack traces. Specifically, it involves overwriting the `Error.prepareStackTrace` function—an optional API typically used by engines like V8 to format stack traces—and replacing it with the `Function` constructor. This allows the system to treat certain data structures as executable code.
The mechanism requires two additional steps to bypass typical syntax restrictions. First, because a raw stack trace is not valid JavaScript code, the author uses prototype pollution (specifically targeting `Object.prototype.message`) combined with an override of `Array.prototype.toString`. By pointing the array's string representation to the `Error.prototype.toString` method, the payload can successfully convert an array of positions into the desired script. Finally, the resulting object is executed using the 'valueOf trick,' where overriding an object's default `valueOf` method allows it to be invoked as a primitive through addition operators.
While technically impressive, this approach faces practical limitations regarding cross-browser compatibility and readability. Commenters noted that since `Error.prepareStackTrace` is an optional API primarily implemented in V8, the hack may fail in other environments like Firefox. Furthermore, while some suggested its use for obfuscation or as a litmus test for peer review, others pointed out that standard features like `Symbol.iterator` already provide legitimate ways to trigger logic without parentheses. The complexity of the final payload highlights the extreme flexibility—and potential for obscurity—inherent in the language's underlying object model.
