stdlib.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package stdlib
  2. import (
  3. "os"
  4. "fmt"
  5. "bytes"
  6. "image"
  7. "strings"
  8. "reflect"
  9. "math/big"
  10. "image/png"
  11. "path/filepath"
  12. )
  13. /* IMPORTANT: this Go file should be consistent with corresponding km files */
  14. const Mod_core = "core"
  15. const Mod_rpc = "rpc"
  16. const Mod_ui = "ui"
  17. var __ModuleDirectories = [] string {
  18. "core", "time", "l10n",
  19. "io", "os", "json", "net", "rpc", "image", "ui",
  20. }
  21. func GetModuleDirectoryNames() ([] string) { return __ModuleDirectories }
  22. func GetDirectoryPath() string {
  23. var exe_path, err = os.Executable()
  24. if err != nil { panic(err) }
  25. var exe_dir = filepath.Dir(exe_path)
  26. var stdlib_dir = filepath.Join(exe_dir, "stdlib")
  27. return stdlib_dir
  28. }
  29. // types.km
  30. const ProjRef = "ProjRef"
  31. const CaseRef = "CaseRef"
  32. const Bool = "Bool"
  33. const Yes = "Yes"
  34. const No = "No"
  35. const ( YesIndex = iota; NoIndex )
  36. const Maybe = "Maybe"
  37. const Some = "Some"
  38. const None = "None"
  39. const ( SomeIndex = iota; NoneIndex )
  40. const Result = "Result"
  41. const Success = "Success"
  42. const Failure = "Failure"
  43. const ( SuccessIndex = iota; FailureIndex )
  44. const Ordering = "Ordering"
  45. const Smaller = "<<"
  46. const Equal = "=="
  47. const Bigger = ">>"
  48. const ( SmallerIndex = iota; EqualIndex; BiggerIndex )
  49. const Optional = "Optional"
  50. // error.km
  51. const Error = "Error"
  52. // binary.km
  53. const Bit = "Bit"
  54. const Byte = "Byte"
  55. const Word = "Word"
  56. const Dword = "Dword"
  57. const Qword = "Qword"
  58. const Bytes = "Bytes"
  59. // numeric.km
  60. const Number = "Number"
  61. const Integer = "Integer"
  62. const Float = "Float"
  63. const NormalFloat = "NormalFloat"
  64. const Complex = "Complex"
  65. const NormalComplex = "NormalComplex"
  66. // containers.km
  67. const Seq = "Seq"
  68. const List = "List"
  69. const Heap = "Heap"
  70. const Set = "Set"
  71. const Map = "Map"
  72. const FlexList = "FlexList"
  73. const FlexListKey = "FlexListKey"
  74. // rx.km
  75. const Observable = "Observable"
  76. const Async = "Async"
  77. const Sync = "Sync"
  78. const Source = "Source"
  79. const Computed = "Computed"
  80. const Sink = "Sink"
  81. const Bus = "Bus"
  82. const Reactive = "Reactive"
  83. const ReactiveEntity = "ReactiveEntity"
  84. const ReactiveSnapshots = "ReactiveSnapshots"
  85. const Mutex = "Mutex"
  86. const Mutable = "Mutable"
  87. const Buffer = "Buffer"
  88. const HashMap = "HashMap"
  89. // string.km
  90. const Char = "Char"
  91. const String = "String"
  92. // rpc
  93. const ServiceIdentifier_rpc = "ServiceIdentifier"
  94. const Method_rpc = "Method"
  95. const MethodMulti_rpc = "Method*"
  96. // ui
  97. const AssetFile_ui = "AssetFile"
  98. type AssetFile struct {
  99. Path string
  100. }
  101. func GetPrimitiveReflectType(name string) (reflect.Type, bool) {
  102. switch name {
  103. case Integer, Number:
  104. return reflect.TypeOf(big.NewInt(0)), true
  105. case Float, NormalFloat:
  106. return reflect.TypeOf(float64(0)), true
  107. case Complex, NormalComplex:
  108. return reflect.TypeOf(complex128(complex(0,1))), true
  109. case Bit:
  110. return reflect.TypeOf(true), true
  111. case Byte:
  112. return reflect.TypeOf(uint8(0)), true
  113. case Word:
  114. return reflect.TypeOf(uint16(0)), true
  115. case Dword:
  116. return reflect.TypeOf(uint32(0)), true
  117. case Qword:
  118. return reflect.TypeOf(uint64(0)), true
  119. case Char:
  120. return reflect.TypeOf(int32(0)), true
  121. default:
  122. return nil, false
  123. }
  124. }
  125. // rpc_service_template.km
  126. const ServiceInstanceType = "Instance"
  127. const ServiceArgumentType = "Argument"
  128. const ServiceMethodsType = "Methods"
  129. const ServiceIdentifierConst = "Identifier"
  130. const ServiceCreateFunction = "create"
  131. // path
  132. type Path ([] string)
  133. var __PathSep = string([] rune { os.PathSeparator })
  134. func (path Path) String() string {
  135. return strings.Join(path, __PathSep)
  136. }
  137. func (path Path) Join(segments ([] string)) Path {
  138. for _, seg := range segments {
  139. if strings.Contains(seg, __PathSep) {
  140. panic(fmt.Sprintf("invalid path segment %s", seg))
  141. }
  142. }
  143. var new_path = make(Path, (len(path) + len(segments)))
  144. copy(new_path, path)
  145. copy(new_path[len(path):], segments)
  146. return new_path
  147. }
  148. func ParsePath(str string) Path {
  149. var raw = strings.Split(str, __PathSep)
  150. var path = make([] string, 0, len(raw))
  151. for i, segment := range raw {
  152. if i != 0 && segment == "" {
  153. continue
  154. }
  155. path = append(path, segment)
  156. }
  157. return path
  158. }
  159. // loader types
  160. // image
  161. const Image_M = "Image"
  162. type Image interface { GetPixelData() image.Image }
  163. // image:raw
  164. const RawImage_T = "RawImage"
  165. type RawImage struct {
  166. Data image.Image
  167. }
  168. func (img *RawImage) GetPixelData() image.Image { return img.Data }
  169. // image:png
  170. const PNG_T = "PNG"
  171. type PNG struct {
  172. Data [] byte
  173. }
  174. func (img *PNG) GetPixelData() image.Image {
  175. var reader = bytes.NewReader(img.Data)
  176. var decoded, err = png.Decode(reader)
  177. if err != nil { panic(fmt.Errorf("failed to decode png data: %w", err)) }
  178. return decoded
  179. }
  180. // ui
  181. var qtWidgetTypeNameMap = map[string] string {
  182. "QWidget": "Widget",
  183. "QMainWindow": "Window",
  184. "QWebView": "PlainWebView",
  185. "WebView": "WebView",
  186. "QLabel": "NativeLabel",
  187. "QLineEdit": "NativeInput",
  188. "QPlainTextEdit": "NativeInputMultiLine",
  189. "QPushButton": "NativeButton",
  190. "QCheckBox": "NativeCheckbox",
  191. "QComboBox": "NativeSelect",
  192. "QListWidget": "NativeList",
  193. }
  194. const QtActionType = "Action"
  195. func GetQtWidgetTypeName(widget_name string) (string, bool) {
  196. var type_name, exists = qtWidgetTypeNameMap[widget_name]
  197. return type_name, exists
  198. }