Skip to content

A bound method given as a function type with more parameters is called through an adapter (wasm: 00object_func2) - #812

Merged
ASDAlexander77 merged 1 commit into
mainfrom
wasm-object-func
Nov 11, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
wasm-object-func

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

A bound method given as a function type with more parameters than the method takes is now called through an adapter. Before, 00object_func2 trapped on wasm.

What went wrong

call_func(user.sayHi, user), where call_func takes (o: object) => void:

  • When function types are matched, a leading object parameter is taken as a this, so the cast went through unchanged.
  • The call then passed the method its this plus every argument of the type: sayHi(user, user).
  • Natively, a method that takes no parameters ignored the extra argument. A method that takes some read them in the wrong place: c.add given as (o: object, x: number) => number got o as its x.
  • On wasm, call_indirect checks the signature and traps.

The fix (MLIRGenCast.cpp, boundMethodWiderTypeAdapter)

  • The method and its this are known only at run time, because they are read from a field. So the bound method goes into a box, the way a closure keeps its captures. The adapter is bound to that box, so the value is still two words, like a bound method.
  • The adapter calls the method with its this and the last arguments, as many as the method takes.
  • The box holds the bound method itself rather than its this and function separately, so rc keeps the receiver through the usual bound-method retain and release. A box holding an object literal's this directly crashed under rc.
  • -mm=own doesn't accept a bound method passed as a function value yet. It keeps its existing error, and no adapter is made.

Tests

  • New test 00bound_method_wider_callback:
    • a class method called in a loop: the method's own this is used, not the argument;
    • a method with a parameter: it gets the last argument;
    • an object-literal method.
  • 00object_func2 is removed from wasm_skips.txt.
  • Native ctest: 6970/6970. During that run the adapter was made only in 00object_func2 and the new test.
  • wasm32 and wasm64, release and debug, 0 failures:
Suite none / rc own
tester 571 (wasm32), 572 (wasm64) pass 419 pass
defaultlib 181 pass 180 pass

🤖 Generated with Claude Code

…d through an adapter

`call_func(user.sayHi, user)`, where `call_func` takes `(o: object) => void`: an object parameter
first is taken as a `this` when function types are matched, so the cast went through as it was, and
the call passed the method its `this` and every argument of the type - `sayHi(user, user)`. Natively
the extra argument was ignored when the method took none, and read in the wrong place when it took
some (`(o: object, x: number) => number` given `c.add`: `x` was `o`). wasm's call_indirect checks the
signature and traps.

The method and its `this` are known only at run time (read from a field), so the adapter keeps the
bound method in a box, as a closure keeps its captures: the box and the adapter bound to it are two
words, as a bound method is. The adapter calls the method with its `this` and the last arguments, as
many as it takes. -mm=own does not take a bound method given as a function value yet, and keeps its
own error. 00object_func2 now runs on wasm32 and wasm64.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 6ab6641 into main Nov 11, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the wasm-object-func branch November 11, 2026 20:26
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