Helpers for building interface-style wrappers and concrete implementations over named method descriptions.
Model
Method descriptions declare parameters and results. interface turns them into a factory with a vtable of function addresses; its CALL constructs interface base objects with forwarding members.
implement turns an interface-base supplier ([Mover]) and a concrete-object supplier into a factory; its CALL constructs concrete objects containing base and the supplied fields.
obj.base is the borrowed interface view accepted where the interface is expected.
The factory owns the vtable; keep it alive and unmoved while its objects or views use it.
interface
(getMethods -- interfaceFactory) Builds a callable factory from a method-description supplier.- Calling the factory constructs an interface base object with a reference to the factory,
DIE,SIZE, and forwarding members.
Generated interface shape
Method-description grammar
getMethodsis a Block of labels, one per method; its labels name the fields ofMETHODS, and it must leave nothing on the stack. Each label names a Block that leaves two objects: the parameters and the result descriptor. The parameters are a Struct with one field per parameter, or()for none.- Use
()as the result descriptor for no output; a non-Meta Struct result is one Struct object, not several stack outputs. - Use
Ref/Creffields to request reference parameters and aRef/Crefdescriptor to request a reference result. GeneratedCodeprepends aNatxself address. - A call through a base member, such as
y x @object.base.method, for[{x: Int32; y: Int32;} Int32], supplies explicit arguments in reverse description order; the selected forwarding member has effect(y x -- result). A direct call through the corresponding vtable entry additionally takes the concrete object'sNatxaddress on top:(y x selfAddress -- result). - An empty method collection is permitted; an individual method still needs its result descriptor.
Generated fields
Interface factory
METHODS: the static Struct of the method descriptions.vtable: the stored vtable.CALL: a static Block with effect(-- base)that constructs an interface base object. Factory CALL returns a non-reference object.
Vtable
- A
Natxarray of function addresses: two entries, then one per declared method in declaration order. - Entry 0: a function with effect
(selfAddress --)that destroys the concrete object; the interface factory's own does nothing. - Entry 1: a function with effect
(-- byteCount)that returns the concrete object's storage size asNatx. - Method entries: one function per method, called with its generated signature. In the interface factory's own vtable, entry 1 and the method entries print
Interface method call: "NAME",NAMEbeingSIZEor the method's name, and exit with status 1.
Interface base
base: a stored reference through which the object reads its factory's vtable.DIE: static generated destruction forwarding through vtable entry 0.SIZE: static generated size forwarding through vtable entry 1.- Named forwarding members are callable Dicts with static
KEY, the method's ordinal among the declared methods, andCALL. A declared method namedCALLis instead exposed as the base object'sCALLBlock.
implement
(getBase getObject -- implementationFactory) Builds a callable implementation factory using an interface-base supplier and a concrete-object supplier.- Calling the factory constructs a concrete object with the base adapter and the supplied fields.
Remarks
getObjectis processed while forming the factory and while constructing instances; see implementation lifetime.
Generated implementation shape
Implementation factory
getBase: the static interface-base supplier.getObject: the static concrete-object supplier.vtable: the populated vtable.CALL: a static Block with effect(-- concrete). It constructs a concrete object; it does not merely return a schema or a borrowed base. Factory CALL returns a non-reference object.
Concrete object and base adapter
- The concrete object contains
basefollowed by the supplied fields in source order. - The
getObjectStruct supplies a member under each declared method name. The generated function selects that member withcallField, and its resulting stack must match the declared signature. - A Block is called with the concrete fields in scope, so
move: [offset +]reads the fieldoffset; a Code receives the declared parameters and no extra self parameter, as inmove: {value: Int32;} Int32 {} codeRef; [1 +] !move. A callable Dict is also valid. - Forming the implementation writes into every method entry a function with effect
(parameters... selfAddress -- result)that reconstructs the concrete object fromselfAddressand appliescallFieldto that member. base: the base adapter with static Base and CALL with effect(-- baseRef)and a stored reference to the implementation vtable. Calling the adapter —obj.base(immutable view) or@obj.base(mutable view) — produces a borrowed interface view of the concrete object's storage;@obj.@basereads the adapter itself without calling it.
Result and view rules
- Results have the declared result schema. Value results are returned as values; Ref/Cref results remain references with their declared mutability. A reference result can alias the concrete object and must not outlive its target.
- Forwarding does not infer constness from an immutable base view. The concrete self address is used for dispatch, and a method declared to return Ref or mutate state can still do so through such a view. Use an explicitly read-only interface contract where needed.
Implementation lifetime
- Base objects returned by an interface factory borrow its vtable; concrete objects returned by an implementation factory borrow that factory's vtable. Keep the relevant factory and vtable storage alive and unmoved while those objects or views use them. A concrete object's base view borrows that object's storage; it is not a separate independently destroyable copy.
- Concrete fields are reconstructed individually. A Ref-like field (
Ref,CodeorText) keeps the reference it holds. Any other field is moved withnewwhenisStaticreturnsTRUEfor it, that is, when its value is known, and is otherwise created withnewVarOfTheSameType, so the supplied value is not transferred. Private fields become public; a static field stays static. This is neither a whole-object ownership move nor a generic deep copy. getObjectis processed while forming the implementation as well as when constructing instances. Supplier runtime effects and cleanup can occur during factory construction; do not use it as an exactly-once acquisition hook.- The generated destructor destroys the complete concrete representation, not just the base adapter. Do not destroy both a base view and its still-live concrete owner. Borrowed reference fields do not transfer ownership of their targets.
Validation and reserved names
- Forming an implementation checks the generated forwarding functions against the declared signatures.
- A method or concrete field must not hide a name the library's own code creates or still uses inside the generated object it becomes a member of, such as
base,call,new,selforwhen, and for a method alsoDIE,METHODS,SIZE,closure,fillMethodsormethodKey. The compiler does not reject every such name: for example, a method namedMETHODScan silently drop the methods declared after it, and a concrete field namedselfcan make the base view refer to the wrong storage. The declaredCALLname is supported specially and makes the base callable; a method namedPREdeclared beside it can make that call fail to compile. With a declaredCALLthe concrete object is reached as@obj(21 @obj.base call). - A parameter, or a field inside a non-reference parameter, must not hide a name the library's own code still uses while it copies the parameters, such as
dict,ioriterate. - In the copy of a non-reference parameter private fields become public, so a parameter in which a field is private, such as a
Span, must be a reference parameter.
Examples
Example: generated implementation call
move of MoverImpl returns 42 for 41, both when called directly and through a Mover Cref passed to another function.
"String" use
"control" use
"interface" use
Mover: [move: [{value: Int32;} Int32];] interface;
MoverImpl: [Mover] [{
move: {value: Int32;} Int32 {} codeRef;
[1 +] !move
}] implement;
{base: Mover Cref;} Int32 {} [
base:;
41 base.move
] "useMover" exportFunction
{} Int32 {} [
obj: MoverImpl;
("direct: " 41 obj.base.move LF) printList
("through a Mover Cref: " obj.base useMover LF) printList
0
] "main" exportFunction
Expected Output
direct: 42
through a Mover Cref: 42
Example: method as a Block reading a concrete field
The Block method add reads the implementation's baseValue field, so adding 41 gives 51.
"String" use
"control" use
"interface" use
Adder: [add: [{amount: Int32;} Int32];] interface;
AdderImpl: [Adder] [{
baseValue: 10;
add: [baseValue +];
}] implement;
{} Int32 {} [
object: AdderImpl;
("result=" 41 @object.base.add LF) printList
0
] "main" exportFunction
Expected Output
result=51
Module helper
sameImplementation
(object0 object1 -- cond) Returns TRUE when both objects use the vtable of one factory; returns FALSE otherwise.- Each input is a concrete object, its base view, or an interface base object.
- The result is unknown at compile time.
See also
- Function: Callable wrapper schema and callable adaptation helpers.