Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support hoisted functions and function declarations #22

Open
lxsmnsyc opened this issue May 15, 2023 · 0 comments
Open

Support hoisted functions and function declarations #22

lxsmnsyc opened this issue May 15, 2023 · 0 comments

Comments

@lxsmnsyc
Copy link
Owner

Hoisted functions are a partially supported, except for the following:

  • Function declarations (i.e. a function declared as a statement). Due to the quirky behavior of functions, it isn't currently optimized by Forgetti. Function declarations can be declared regardless of the function's flow (e.g. return, branches, etc.)
  • Functions with hoisted references.
function Example(props) {
  const hello = () => {
    console.log(a, b);
  }
  const a = 'hello';
  const b = 'world';
  hello();
  hello();
}

If hello is only accessed once, it works however if it is accessed more than once, the compiler doesn't know what to do:

import { useMemo as _useMemo } from "react";
import { $$cache as _$$cache } from "forgetti/runtime";
import { $$equals as _$$equals } from "forgetti/runtime";
function Example(props) {
  let _c = _$$cache(_useMemo, 5),
    _eq = _$$equals(_c, 0, a),
    _v = _eq ? _c[0] : _c[0] = a,
    _eq2 = _$$equals(_c, 1, b),
    _v2 = _eq2 ? _c[1] : _c[1] = b;
  const hello = _eq && _eq2 ? _c[2] : _c[2] = () => {
    console.log(a, b);
  };
  const a = 'hello';
  const b = 'world';
  3 in _c ? _c[3] : _c[3] = hello();
  4 in _c ? _c[4] : _c[4] = hello();
}
@lxsmnsyc lxsmnsyc changed the title Support hoisted functions Support hoisted functions and function declarations May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant