Skip to content

Commit

Permalink
[Wasm GC] wasm-ctor-eval: Handle cycles of data (#5685)
Browse files Browse the repository at this point in the history
A cycle of data is something we can't just naively emit as wasm globals. If
at runtime we end up, for example, with an object A that refers to itself,
then we can't just emit

(global $A
  (struct.new $A
    (global.get $A)))

The struct.get is of this very global, and such a self-reference is invalid. So
we need to break such cycles as we emit them. The simple idea used here
is to find paths in the cycle that are nullable and mutable, and replace the
initial value with a null that is fixed up later in the start function:

(global $A
  (struct.new $A
    (ref.null $A)))

(func $start
  (struct.set
    (global.get $A)
    (global.get $A)))
)

This is not optimal in terms of breaking cycles, but it is fast (linear time)
and simple, and does well in practice on j2wasm (where cycles in fact
occur).
  • Loading branch information
kripken authored May 5, 2023
1 parent 5e7dceb commit 6086df0
Show file tree
Hide file tree
Showing 7 changed files with 1,867 additions and 67 deletions.
Loading

0 comments on commit 6086df0

Please sign in to comment.