Skip to content

Support Recursive Values #430

@jdegoes

Description

@jdegoes

There are quite a few cases that require defining recursive values:

package wit:json@0.1.0;

interface types {
  variant json {
    null,
    boolean(bool),
    number(float64),
    string(string),
    array(list<json>),
    object(list<tuple<string, json>>)
  }
}

Currently, these are illegal due to the difficulty of supporting them, including, presumably, the challanges of defining an ABI for recursive values. However, there exists a simple translation that could form the basis of the ABI without adding significant complexity.

This transformation involves:

  1. Encoding the recursive references as an unsigned integer, so the type is no longer recursive, and which represents a simple and flat node.
  2. Wrapping the data type in a higher level data type, which has a list of nodes, where the first entry in the list is guaranteed to exist and which represents the top-level structure.

For the JSON example, we might call the node structure json-node and the top-level type json:

package wit:json@0.1.0;

interface types {
  variant json-node {
    null,
    boolean(bool),
    number(float64),
    string(string),
    array(list<u32>),
    object(list<tuple<string, u32>>)
  }

  record json {
    nodes: list<json-node>,
  }
}

This transformation can be generalized to mutually recursive structures.

Now, while it's possible to do this transformation manually, using such a data structure directly from any programming language suffers from poor ergonomics and the possibility of bugs that lead to runtime errors. So when encoding recursive values in this way, one should take care to define a translation from a higher-level, truly recursive structure, in the guest language, to the lower-level structure, and then back again, by using a hash map based on reference or pointer equality.

Perhaps recursive values could be supported by allowing higher-level syntax, but creating a formal specification as to how these recursive values are mapped to a lower-level subset of types, which could form the basis of the ABI?

Metadata

Metadata

Assignees

No one assigned

    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