.NET Notes #77
Description
I was asked to add some notes based on the needs of Microsoft .NET implementations wrt GC in WebAssembly.
- Object layout
.NET objects in memory usually consist of a header followed by object data. The header contains data
such as:
- the vtable/type pointer
- a sync word for locking on the object
- a length field for arrays/strings
- for multi-dimensional arrays, a pointer to a struct describing the dimensions.
The type system of the current proposal doesn't seem to be able to support this layout, i.e. a header followed by array data. Also, .NET supports arrays of structs, i.e. an array of {ref,non-ref} would
look like in memory: [ref, non-ref, ref, non-ref, etc.].
In general, it seems very difficult to model all possible object layouts used by GCd languages.
-
Interior pointers
In .NET, its quite common to have pointers into the middle of objects (arrays), and pointers to one past
the end of an array.anyref
doesn't seem to be able to support this. -
Interop with C/C++ code
The .net runtimes are written in C/C++ and assume that object references are normal C pointers which
point to linear memory, and objects can be accessed from C code as a pointers to C structs. The current
proposal places allocated objects outside linear memory and adds new accessors to read/write their
contents. To allow manipulation of these objects from C code would require extensions to the C compilers. -
Finalization
The .net runtime needs to be notified somehow when an object with a finalizer dies. -
Weak references
.net supports multiple kinds of weak references which might not be supported by the underlying JS GC. -
Non web runtimes
Non-web runtimes would need to add a GC implementation, since GC is such a core feature that it
probably cannot be treated as an optional feature like SIMD. -
LLVM support
The new types/type constructs don't exist in LLVM, not clear how they can be added.