123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- package stdlib
- import (
- "os"
- "fmt"
- "bytes"
- "image"
- "strings"
- "reflect"
- "math/big"
- "image/png"
- "path/filepath"
- )
- /* IMPORTANT: this Go file should be consistent with corresponding km files */
- const Mod_core = "core"
- const Mod_rpc = "rpc"
- const Mod_ui = "ui"
- var __ModuleDirectories = [] string {
- "core", "time", "l10n",
- "io", "os", "json", "net", "rpc", "image", "ui",
- }
- func GetModuleDirectoryNames() ([] string) { return __ModuleDirectories }
- func GetDirectoryPath() string {
- var exe_path, err = os.Executable()
- if err != nil { panic(err) }
- var exe_dir = filepath.Dir(exe_path)
- var stdlib_dir = filepath.Join(exe_dir, "stdlib")
- return stdlib_dir
- }
- // types.km
- const ProjRef = "ProjRef"
- const CaseRef = "CaseRef"
- const Bool = "Bool"
- const Yes = "Yes"
- const No = "No"
- const ( YesIndex = iota; NoIndex )
- const Maybe = "Maybe"
- const Some = "Some"
- const None = "None"
- const ( SomeIndex = iota; NoneIndex )
- const Result = "Result"
- const Success = "Success"
- const Failure = "Failure"
- const ( SuccessIndex = iota; FailureIndex )
- const Ordering = "Ordering"
- const Smaller = "<<"
- const Equal = "=="
- const Bigger = ">>"
- const ( SmallerIndex = iota; EqualIndex; BiggerIndex )
- const Optional = "Optional"
- // error.km
- const Error = "Error"
- // binary.km
- const Bit = "Bit"
- const Byte = "Byte"
- const Word = "Word"
- const Dword = "Dword"
- const Qword = "Qword"
- const Bytes = "Bytes"
- // numeric.km
- const Number = "Number"
- const Integer = "Integer"
- const Float = "Float"
- const NormalFloat = "NormalFloat"
- const Complex = "Complex"
- const NormalComplex = "NormalComplex"
- // containers.km
- const Seq = "Seq"
- const List = "List"
- const Heap = "Heap"
- const Set = "Set"
- const Map = "Map"
- const FlexList = "FlexList"
- const FlexListKey = "FlexListKey"
- // rx.km
- const Observable = "Observable"
- const Async = "Async"
- const Sync = "Sync"
- const Source = "Source"
- const Computed = "Computed"
- const Sink = "Sink"
- const Bus = "Bus"
- const Reactive = "Reactive"
- const ReactiveEntity = "ReactiveEntity"
- const ReactiveSnapshots = "ReactiveSnapshots"
- const Mutex = "Mutex"
- const Mutable = "Mutable"
- const Buffer = "Buffer"
- const HashMap = "HashMap"
- // string.km
- const Char = "Char"
- const String = "String"
- // rpc
- const ServiceIdentifier_rpc = "ServiceIdentifier"
- const Method_rpc = "Method"
- const MethodMulti_rpc = "Method*"
- // ui
- const AssetFile_ui = "AssetFile"
- type AssetFile struct {
- Path string
- }
- func GetPrimitiveReflectType(name string) (reflect.Type, bool) {
- switch name {
- case Integer, Number:
- return reflect.TypeOf(big.NewInt(0)), true
- case Float, NormalFloat:
- return reflect.TypeOf(float64(0)), true
- case Complex, NormalComplex:
- return reflect.TypeOf(complex128(complex(0,1))), true
- case Bit:
- return reflect.TypeOf(true), true
- case Byte:
- return reflect.TypeOf(uint8(0)), true
- case Word:
- return reflect.TypeOf(uint16(0)), true
- case Dword:
- return reflect.TypeOf(uint32(0)), true
- case Qword:
- return reflect.TypeOf(uint64(0)), true
- case Char:
- return reflect.TypeOf(int32(0)), true
- default:
- return nil, false
- }
- }
- // rpc_service_template.km
- const ServiceInstanceType = "Instance"
- const ServiceArgumentType = "Argument"
- const ServiceMethodsType = "Methods"
- const ServiceIdentifierConst = "Identifier"
- const ServiceCreateFunction = "create"
- // path
- type Path ([] string)
- var __PathSep = string([] rune { os.PathSeparator })
- func (path Path) String() string {
- return strings.Join(path, __PathSep)
- }
- func (path Path) Join(segments ([] string)) Path {
- for _, seg := range segments {
- if strings.Contains(seg, __PathSep) {
- panic(fmt.Sprintf("invalid path segment %s", seg))
- }
- }
- var new_path = make(Path, (len(path) + len(segments)))
- copy(new_path, path)
- copy(new_path[len(path):], segments)
- return new_path
- }
- func ParsePath(str string) Path {
- var raw = strings.Split(str, __PathSep)
- var path = make([] string, 0, len(raw))
- for i, segment := range raw {
- if i != 0 && segment == "" {
- continue
- }
- path = append(path, segment)
- }
- return path
- }
- // loader types
- // image
- const Image_M = "Image"
- type Image interface { GetPixelData() image.Image }
- // image:raw
- const RawImage_T = "RawImage"
- type RawImage struct {
- Data image.Image
- }
- func (img *RawImage) GetPixelData() image.Image { return img.Data }
- // image:png
- const PNG_T = "PNG"
- type PNG struct {
- Data [] byte
- }
- func (img *PNG) GetPixelData() image.Image {
- var reader = bytes.NewReader(img.Data)
- var decoded, err = png.Decode(reader)
- if err != nil { panic(fmt.Errorf("failed to decode png data: %w", err)) }
- return decoded
- }
- // ui
- var qtWidgetTypeNameMap = map[string] string {
- "QWidget": "Widget",
- "QMainWindow": "Window",
- "QWebView": "PlainWebView",
- "WebView": "WebView",
- "QLabel": "NativeLabel",
- "QLineEdit": "NativeInput",
- "QPlainTextEdit": "NativeInputMultiLine",
- "QPushButton": "NativeButton",
- "QCheckBox": "NativeCheckbox",
- "QComboBox": "NativeSelect",
- "QListWidget": "NativeList",
- }
- const QtActionType = "Action"
- func GetQtWidgetTypeName(widget_name string) (string, bool) {
- var type_name, exists = qtWidgetTypeNameMap[widget_name]
- return type_name, exists
- }
|