Skip to content

[12.x] Read JS globals through globalThis wherever a same-named binding is in scope - #9464

Open
illusionalsagacity wants to merge 1 commit into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-external-shadow-scope-12.x
Open

illusionalsagacity wants to merge 1 commit into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-external-shadow-scope-12.x

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Backport of #8785 to 12.x. It is a prerequisite for the backport of #9417 (branch fix-for-loop-closure-12.x), whose later commits change which bindings Js_pass_external_shadow counts.

Problem

A reference to a @val global compiled to its bare name, which reads a binding with that name if one is in scope there. Js_pass_external_shadow only rewrote the global to globalThis.x inside the initializer of the binding itself, and a @scope access through a toplevel binding's name.

module G = {
  @val external shadowA: int = "shadowA"
  @val external shadowB: int = "shadowB"
}

let shadowA = (n: int) => n * n + G.shadowA
let readAAfter = () => G.shadowA

let paramB = (shadowB: int) => shadowB * shadowB + G.shadowB

Before (readAAfter() returns the function shadowA; paramB reads its parameter):

function shadowA(n) {
  return (n * n | 0) + globalThis.shadowA | 0;
}

function readAAfter() {
  return shadowA;
}

function paramB(shadowB$1) {
  return (shadowB$1 * shadowB$1 | 0) + shadowB | 0;
}

After:

function shadowA(n) {
  return (n * n | 0) + globalThis.shadowA | 0;
}

function readAAfter() {
  return globalThis.shadowA;
}

function paramB(shadowB) {
  return (shadowB * shadowB | 0) + globalThis.shadowB | 0;
}

Fix

As on master, Js_pass_external_shadow tracks the names of the bindings in scope (declarations of a block, which are in scope in the whole block, function parameters, loop variables, the catch parameter in its handler, imported modules, and the declarations of all cases of a switch, which share one scope), and reads a global with such a name through globalThis. A switch's discriminant is read outside the scope of its cases.

Differences from master:

Tests

  • tests/tests/src/external_shadow_test.res is master's: globals shadowed by a toplevel binding before and after their reads, a parameter, a local binding after a read in the same block, a binding in one case of a switch, a loop variable and an imported module. Its .mjs is the same as on master. Built with 12.x before this change, its test fails.
  • tests/ounit_tests/ounit_external_shadow_tests.ml (master's) checks that a switch's discriminant reads the global directly and a read in a case goes through globalThis. It fails without this change.
  • ExternalShadow.mjs and ppx_apply_test.mjs change as on master. In ppx_apply_test.mjs 12.x printed the toplevel xx without a suffix already, so h read the local xx = 6 instead of the global; now it reads globalThis.xx. No other module in tests/tests changes output.

Later changes on master to the pass don't fix regressions from #8785. #9284 also collects the declarations in a nested Block, which it needs because it moves the pass after Js_pass_tailcall_inline and Js_pass_flatten_and_mark_dead; on 12.x the pass still runs before them, right after Js_pass_flatten, so that isn't needed here. The later commits of #9417 change the pass, and are backported in fix-for-loop-closure-12.x.

Investigated by Claude.

…n scope

Backport of rescript-lang#8785 to 12.x.

A reference to a @Val global compiled to its bare name, which reads a
binding with that name if one is in scope. Previously this was only handled
inside the binding's own initializer. The cases of a switch share one
scope, and the discriminant is read outside of it.

Differences from master:
- 12.x has no rescript-lang#8772, which replaced the per-binding rewrite of an
  initializer with a single walk; rescript-lang#8785 replaces that code as a whole, so
  the pass here is master's rescript-lang#8785 version without rescript-lang#8772 in between, and
  rescript-lang#8772's ExternalShadow tests are not included.
- 12.x's J.ForRange has five fields and there is no ForOf or ForAwaitOf.
- Js_call_info.na_full_call takes the transformed-JSX flag on 12.x.
- ppx_apply_test.mjs: 12.x already printed the toplevel `xx` unsuffixed,
  so only the read of the global changes to `globalThis.xx` (on 12.x it
  read the local `xx = 6`).

Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

1 participant