Class GcMarshaller

java.lang.Object
ai.tegmentum.webassembly4j.runtime.gc.GcMarshaller

public final class GcMarshaller extends Object
Marshals Java objects to and from WebAssembly GC struct instances.

Works with classes annotated with GcMapped. The marshaller reflects the class fields, maps them to GC struct fields, and handles bidirectional conversion.

Example:


 GcExtension gc = instance.extension(GcExtension.class).orElseThrow();
 GcMarshaller marshaller = GcMarshaller.forExtension(gc);

 // Java → GC struct
 Point p = new Point(1.0, 2.0);
 GcStructInstance struct = marshaller.marshal(p);

 // GC struct → Java
 Point result = marshaller.unmarshal(struct, Point.class);
 
  • Method Details

    • forExtension

      public static GcMarshaller forExtension(ai.tegmentum.webassembly4j.api.gc.GcExtension gc)
      Creates a marshaller using the given GC extension and a new type mapper.
    • forExtension

      public static GcMarshaller forExtension(ai.tegmentum.webassembly4j.api.gc.GcExtension gc, GcTypeMapper typeMapper)
      Creates a marshaller using the given GC extension and type mapper.
    • typeMapper

      public GcTypeMapper typeMapper()
      Returns the type mapper used by this marshaller.
    • marshal

      public ai.tegmentum.webassembly4j.api.gc.GcStructInstance marshal(Object object)
      Marshals a Java object to a GC struct instance.
      Parameters:
      object - an instance of a GcMapped-annotated class
      Returns:
      a new GC struct instance with the object's field values
      Throws:
      IllegalArgumentException - if the object's class is not GC-mappable
    • unmarshal

      public <T> T unmarshal(ai.tegmentum.webassembly4j.api.gc.GcStructInstance struct, Class<T> type)
      Unmarshals a GC struct instance to a Java object.
      Type Parameters:
      T - the target type
      Parameters:
      struct - the GC struct instance
      type - the target Java class (must be GcMapped-annotated)
      Returns:
      a new Java object with fields populated from the struct
      Throws:
      IllegalArgumentException - if the class is not GC-mappable