1

Using Typescript 4.1.2 is it possible to compile code that references the functions one calls when the --allow-natives-syntax flag in v8 is set?

For example:

function foo(bar: ()=>void)): void {
    %OptimizeFunctionOnNextCall(bar); // --> TS1109; Expression expected
}

Neither //@ts-ignore nor //@ts-expect-error silence this error

Ryder Brooks
  • 2,049
  • 2
  • 21
  • 29
  • 1
    Please keep in mind that using `%OptimizeFunctionOnNextCall` in a way that does _not_ degrade performance is extremely difficult. This function is meant for testing, where triggering certain internal mechanisms is more important than achieving high performance. For regular operation, V8's default behavior is almost guaranteed to give you better outcomes. In particular, optimization only makes sense if sufficient type feedback has been collected beforehand. Optimization on the very first execution is *always* counter-productive. – jmrk Dec 08 '20 at 09:11
  • Thank you jmrk. I agree with everything you're saying. I'm just toying around and exploring, trying to get a feel for when v8 stops trying to untangle my spaghetti monsters... – Ryder Brooks Dec 08 '20 at 14:58

2 Answers2

1

I don't have boilerplate to test v8 natives, so I don't know if it works, but You can try next piece of code

function foo(bar: () => void): void {
  //@ts-ignore
  ((void 0) %OptimizeFunctionOnNextCall(bar));
}

Don't judge me) I'm just trying to help)

1

Feel free to use eval

function foo(bar: ()=>void)): void {
    eval('%OptimizeFunctionOnNextCall(bar)');
}
darky
  • 163
  • 1
  • 7