type_render.mx raw

   1  package runtime
   2  
   3  // Structural rendering for the compiler-synthesized String methods.
   4  //
   5  // Moxie has no reflect and fmt takes ...Stringer, so a struct or a named func
   6  // type with no declared String() had no printable form at all: stage4 accepted
   7  // the fmt call and printed '?', legacy refused it. The compiler now synthesizes
   8  // a String method for every such type (a declared one always wins), and this
   9  // file is the body it calls for structs. It walks the same type descriptors the
  10  // codec and the interface machinery use.
  11  //
  12  // The format follows Go's %+v: a struct is {field:value ...}, a slice is
  13  // [a b], a pointer is &<elem> (nil prints nil). A func value has no signature in
  14  // its descriptor, so the compiler passes that text to the method it generates;
  15  // a func-typed FIELD prints func. Kinds with no state to walk (map, chan) print
  16  // their kind word rather than guessing at a representation.
  17  
  18  import (
  19  	"moxielang.dev/pkg/mxutil"
  20  	"unsafe"
  21  )
  22  
  23  // RenderValue returns the structural text of the value at p, described by t.
  24  func RenderValue(t *RawType, p unsafe.Pointer) (s string) {
  25  	var b []byte
  26  	b = renderValue(b, t, p)
  27  	return string(b)
  28  }
  29  
  30  func renderValue(b []byte, t *RawType, p unsafe.Pointer) (out []byte) {
  31  	out = b
  32  	if t == nil || p == nil {
  33  		return renderStr(out, "nil")
  34  	}
  35  	u := t.Underlying()
  36  	switch u.Kind() {
  37  	case Bool:
  38  		return renderStr(out, BoolText(*(*bool)(p)))
  39  	case Int, Int32:
  40  		return renderStr(out, Int64Text(int64(*(*int32)(p))))
  41  	case Int8:
  42  		return renderStr(out, Int64Text(int64(*(*int8)(p))))
  43  	case Int16:
  44  		return renderStr(out, Int64Text(int64(*(*int16)(p))))
  45  	case Int64:
  46  		return renderStr(out, Int64Text(*(*int64)(p)))
  47  	case Uint, Uint32:
  48  		return renderStr(out, Uint64Text(uint64(*(*uint32)(p))))
  49  	case Uint8:
  50  		return renderStr(out, Uint64Text(uint64(*(*uint8)(p))))
  51  	case Uint16:
  52  		return renderStr(out, Uint64Text(uint64(*(*uint16)(p))))
  53  	case Uint64:
  54  		return renderStr(out, Uint64Text(*(*uint64)(p)))
  55  	case Uintptr:
  56  		return renderStr(out, UintptrText(*(*uintptr)(p)))
  57  	case Float32:
  58  		return renderStr(out, Float32Text(*(*float32)(p)))
  59  	case Float64:
  60  		return renderStr(out, Float64Text(*(*float64)(p)))
  61  	case KindBytes:
  62  		return renderStr(out, *(*string)(p))
  63  	case KindPointer, KindUnsafePointer:
  64  		q := *(*unsafe.Pointer)(p)
  65  		if q == nil {
  66  			return renderStr(out, "nil")
  67  		}
  68  		if u.Kind() == KindUnsafePointer || u.Elem() == nil {
  69  			return renderStr(out, "ptr")
  70  		}
  71  		out = renderStr(out, "&")
  72  		return renderValue(out, u.Elem(), q)
  73  	case KindChan:
  74  		return renderStr(out, "chan")
  75  	case KindFunc:
  76  		return renderStr(out, "func")
  77  	case KindMap:
  78  		return renderStr(out, "map")
  79  	case KindSlice, KindArray:
  80  		elem := u.Elem()
  81  		n := int32(0)
  82  		if u.Kind() == KindArray {
  83  			n = u.ArrayLen()
  84  		} else {
  85  			n = int32((*_string)(p).length)
  86  		}
  87  		base := *(*unsafe.Pointer)(p)
  88  		sz := elem.Size()
  89  		out = renderStr(out, "[")
  90  		for i := int32(0); i < n; i++ {
  91  			if i > 0 {
  92  				out = renderStr(out, " ")
  93  			}
  94  			out = renderValue(out, elem, unsafe.Add(base, uintptr(i)*sz))
  95  		}
  96  		return renderStr(out, "]")
  97  	case KindStruct:
  98  		nf := int32(u.NumField())
  99  		out = renderStr(out, "{")
 100  		for i := int32(0); i < nf; i++ {
 101  			if i > 0 {
 102  				out = renderStr(out, " ")
 103  			}
 104  			out = renderStr(out, u.StructFieldName(i))
 105  			out = renderStr(out, ":")
 106  			out = renderValue(out, u.StructFieldType(i), unsafe.Add(p, u.StructFieldOffset(i)))
 107  		}
 108  		return renderStr(out, "}")
 109  	case KindInterface:
 110  		it := (*_interface)(p)
 111  		if it.typecode == nil {
 112  			return renderStr(out, "nil")
 113  		}
 114  		inner := (*RawType)(it.typecode)
 115  		data := it.value
 116  		if inner.Size() <= unsafe.Sizeof(uintptr(0)) {
 117  			data = unsafe.Pointer(&it.value)
 118  		}
 119  		return renderValue(out, inner, data)
 120  	}
 121  	return renderStr(out, "?")
 122  }
 123  
 124  func renderStr(b []byte, s string) (out []byte) {
 125  	out = mxutil.Ensure(b, len(s))
 126  	return push(out, s...)
 127  }
 128