Skip to content

An inlined function reads an external callee before it evaluates the argument #9334

Open

Description

@mununki

A function whose body is the call of an external is inlined so that JavaScript reads the external function before the argument runs. ReScript evaluates the argument first.

%%raw(`globalThis.hook = x => x + 1`)

@val external hook: int => int = "hook"

let install: unit => int = %raw(`() => {
  globalThis.hook = x => x + 100
  return 1
}`)

let callHook = x => hook(x)

type obj

let o: obj = %raw(`{run: x => x + 1}`)

@send external run: (obj, int) => int = "run"

let replaceRun: unit => int = %raw(`() => {
  o.run = x => x + 100
  return 1
}`)

let callRun = (o, x) => o->run(x)

Console.log(callHook(install()))
Console.log(callRun(o, replaceRun()))

Output, on master and on 12.3.1:

console.log(hook(install()));
console.log(o.run(replaceRun()));

This prints 2 and 2. The wrappers called without inlining print 101 and 101, because install() and replaceRun() run before the wrapper reads hook and o.run.

The rewrite is the fast path of beta reduction, Lam_beta_reduce_util.simple_beta_reduce. It takes the callee of an Lapply as evaluated before the arguments, but not the function or method that a Pjs_call reads. #9296 fixes the same order for let x = e; f(x) and leaves this case out: binding the argument here changes the output of every inlined wrapper around an external whose argument has side effects.

Activity

  1. self-assigned this
    on Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions