ChakraCore icon indicating copy to clipboard operation
ChakraCore copied to clipboard

Optional chaining

Open ShortDevelopment opened this issue 1 year ago • 3 comments

This PR aims to add support for the stage-4 proposal optional-chaining. It's inspired by the work of @rhuanjl but uses a more hacky approach to parsing.

Goals

  • Minimize amount of changes
  • (Hopefully) Performance

ToDo

  • [x] Add tests
    • [x] Unused expression result
    • [x] Return expression
    • [x] Root optional-call (e.g. eval?.('a'))
    • [x] Scoped method load (e.g. inside eval)
    • [x] delete
  • [x] implement optional-deletion
  • [x] optional call invoked on eval function should be indirect eval
  • [x] Simple function calls
  • [x] Preserve this (a.b)().c should be equivalent to a.b().c
  • [x] short circuit embedded expressions like a?.[++x] ++x should not be evaluated if a is null or undefined
  • [x] Don't tokenize a?.3:0 (ternary) as tkOptChain (?.)
  • [x] Tagged templates are not allowed in optional chain
  • [x] fix tmp-renaming for eval result eval("foo?.()") or eval("foo?.property")
  • [ ] Works with optimizations
    • [ ] What about the apply call optimization?
    • [x] Loop inversion (AST cloning) Only triggered for 2 nested for loops with assignment https://github.com/chakra-core/ChakraCore/blob/e26c81f6eabf3b4c0f45c3963be807d3ea90c63e/test/loop/loopinversion.js#L60-L66

Out of scope

  • Remove EmitInvoke (seems unused)
  • Better error message for a call on a non-function

Spec: Optional Chains Fixes #6349

ShortDevelopment avatar Apr 15 '24 07:04 ShortDevelopment

I'm currently working on function calls. At the moment it would just be handled as if no optional-chaining would be used ⇾ crash in js land

Edit: Function call should work now but this is not always propagated correctly (yet) Edit: Function calls should now work

ShortDevelopment avatar Apr 18 '24 22:04 ShortDevelopment

The jitted code currently crashes at this assertion (causing the test-failures) https://github.com/chakra-core/ChakraCore/blob/1f6e17c4c55231846596223eab8ab36360349fa3/lib/Backend/GlobOpt.cpp#L11384

ShortDevelopment avatar Apr 25 '24 18:04 ShortDevelopment