object2.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. package core
  2. import (
  3. "time"
  4. "math"
  5. "regexp"
  6. "reflect"
  7. "math/big"
  8. "kumachan/standalone/qt"
  9. "kumachan/standalone/ctn"
  10. "kumachan/standalone/util/pseudounion"
  11. )
  12. var pseudoUnionTagReflectType = reflect.TypeOf(pseudounion.Tag(0))
  13. func ToObject[T any] (v T) Object {
  14. return MakeObject(v, nil)
  15. }
  16. func MakeObject(v interface{}, h RuntimeHandle) Object {
  17. if ((v == nil) || (v == struct{}{})) {
  18. v = Object(nil)
  19. }
  20. if o, is_object := v.(Object); is_object {
  21. return o
  22. }
  23. var o = (func() ObjectImpl {
  24. switch V := v.(type) {
  25. // Primitive
  26. // Bool
  27. case bool:
  28. return Bool(V)
  29. // Int
  30. case *big.Int:
  31. return Int { V }
  32. case int:
  33. return Int { big.NewInt(int64(V)) }
  34. // Float
  35. case float64:
  36. return Float(V)
  37. // Bytes
  38. case [] byte:
  39. return Bytes(V)
  40. // String
  41. case string:
  42. return String(V)
  43. // Char
  44. case int32: // = rune
  45. return Char(V)
  46. // RegExp
  47. case *regexp.Regexp:
  48. return RegExp { V }
  49. // Time
  50. case time.Time:
  51. return Time(V)
  52. // File:
  53. case File:
  54. return V
  55. // Error
  56. case error:
  57. return Error { V }
  58. // Reflect
  59. // ReflectType
  60. case ReflectType:
  61. return V
  62. // ReflectValue
  63. case ReflectValue:
  64. return V
  65. // Rx
  66. // Observable
  67. case Observable:
  68. return V
  69. // Subject
  70. case Subject:
  71. return V
  72. // Interface and Lambda (Object)
  73. // Interface
  74. case Interface:
  75. return V
  76. // Lambda (Object)
  77. case Lambda:
  78. return V
  79. // Container (Object)
  80. // List (Object)
  81. case List:
  82. return V
  83. // Seq
  84. case Seq:
  85. return V
  86. // Queue
  87. case Queue:
  88. return V
  89. // Heap
  90. case Heap:
  91. return V
  92. // Set
  93. case Set:
  94. return V
  95. // Map
  96. case Map:
  97. return V
  98. // GUI
  99. // Action
  100. case Action:
  101. return V
  102. // Widget
  103. case Widget:
  104. return V
  105. // Signal
  106. case Signal:
  107. return V
  108. // Events
  109. case Events:
  110. return V
  111. // Prop
  112. case Prop:
  113. return V
  114. // Union (Object)
  115. case Union:
  116. return V
  117. // check for mistake
  118. case reflect.Value:
  119. panic("invalid argument")
  120. default:
  121. var rv = reflect.ValueOf(v)
  122. var rt = rv.Type()
  123. // Enum (int)
  124. if rt.Kind() == reflect.Int {
  125. var v = rv.Convert(reflect.TypeOf(int(0))).Interface().(int)
  126. return Enum(v)
  127. }
  128. // Union (ctn.Maybe)
  129. if _, ok := ctn.ReflectTypeMatchMaybe(rt); ok {
  130. if inner_rv, ok := ctn.ReflectMaybeValue(rv); ok {
  131. var inner_v = inner_rv.Interface()
  132. return Union {
  133. Index: OkIndex,
  134. Object: MakeObject(inner_v, h),
  135. }
  136. } else {
  137. return Union { Index: NgIndex }
  138. }
  139. }
  140. // Record (ctn.Pair)
  141. if _, _, ok := ctn.ReflectTypeMatchPair(rt); ok {
  142. var a_rv, b_rv = ctn.ReflectPairUnpack(rv)
  143. var a_v = a_rv.Interface()
  144. var b_v = b_rv.Interface()
  145. var a = MakeObject(a_v, h)
  146. var b = MakeObject(b_v, h)
  147. return Record {
  148. Objects: [] Object { a, b },
  149. }
  150. }
  151. // Union (pseudo-union)
  152. if rt.Kind() == reflect.Struct {
  153. if rt.NumField() >= 1 {
  154. if rt.Field(0).Type == pseudoUnionTagReflectType {
  155. var index = int(rv.Field(0).Int())
  156. var object_rv = rv.Field(index)
  157. var object_v = object_rv.Interface()
  158. var object = MakeObject(object_v, h)
  159. return Union {
  160. Index: (index - 1),
  161. Object: object,
  162. }
  163. }}
  164. // Record (struct)
  165. var n = rt.NumField()
  166. var objects = make([] Object, n)
  167. for i := 0; i < n; i += 1 {
  168. var field_rv = rv.Field(i)
  169. var field_v = field_rv.Interface()
  170. objects[i] = MakeObject(field_v, h)
  171. }
  172. return Record { objects }
  173. }
  174. // Container (generic)
  175. // List (generic)
  176. if rt.Kind() == reflect.Slice {
  177. var n = rv.Len()
  178. var nodes = make([] ListNode, n)
  179. for i := 0; i < n; i += 1 {
  180. var item_rv = rv.Index(i)
  181. var item_v = item_rv.Interface()
  182. nodes[i].Value = MakeObject(item_v, h)
  183. }
  184. return NodesToList(nodes)
  185. }
  186. // Interface and Lambda (generic)
  187. // Lambda (generic)
  188. if isLambdaFuncReflectType(rt) {
  189. var get_in_rv func(Object)([] reflect.Value)
  190. if rt.NumIn() == 0 {
  191. get_in_rv = func(_ Object) ([] reflect.Value) {
  192. return nil
  193. }
  194. } else if rt.NumIn() == 1 {
  195. get_in_rv = func(arg Object) ([] reflect.Value) {
  196. var arg_ptr, arg_rv = reflectNew(rt.In(0))
  197. ConvertObject(arg, arg_ptr, h)
  198. return [] reflect.Value { arg_rv }
  199. }
  200. } else if rt.NumIn() == 2 {
  201. get_in_rv = func(arg Object) ([] reflect.Value) {
  202. var a_ptr, a_rv = reflectNew(rt.In(0))
  203. var b_ptr, b_rv = reflectNew(rt.In(1))
  204. var r = (*arg).(Record) // ctn.Pair
  205. ConvertObject(r.Objects[0], a_ptr, h)
  206. ConvertObject(r.Objects[1], b_ptr, h)
  207. return [] reflect.Value { a_rv, b_rv }
  208. }
  209. } else {
  210. panic("something went wrong")
  211. }
  212. var call = func(arg Object) Object {
  213. var in_rv = get_in_rv(arg)
  214. var out_rv = rv.Call(in_rv)
  215. var out = out_rv[0].Interface()
  216. return MakeObject(out, h)
  217. }
  218. var l = Lambda { call }
  219. if rt.Name() == "" {
  220. return l
  221. } else {
  222. if rt.NumIn() == 0 {
  223. return CraftSamInterface(l.Call(nil))
  224. } else {
  225. return CraftSamInterface(Obj(l))
  226. }
  227. }
  228. }
  229. panic("unsupported value type: " + rt.String())
  230. } })()
  231. return &o
  232. }
  233. func FromObject[T any] (o Object) T {
  234. var t T
  235. var p = &t
  236. ConvertObject(o, p, nil)
  237. return t
  238. }
  239. func ConvertObject(o Object, p interface{}, h RuntimeHandle) {
  240. if o == nil {
  241. return
  242. }
  243. if _, is_empty_struct := p.(*struct{}); is_empty_struct {
  244. panic("cannot assign non-nil object to struct{}")
  245. }
  246. if o_ptr, is_object_ptr := p.(*Object); is_object_ptr {
  247. *o_ptr = o
  248. return
  249. }
  250. { var o = *o
  251. switch P := p.(type) {
  252. // Primitive
  253. // Bool
  254. case *(bool):
  255. *P = bool(o.(Bool))
  256. // Int
  257. case *(*big.Int):
  258. *P = o.(Int).Value
  259. case *(int):
  260. *P = clampTo32Int(o.(Int).Value)
  261. // Float
  262. case *(float64):
  263. *P = float64(o.(Float))
  264. // Bytes
  265. case *([] byte):
  266. *P = o.(Bytes)
  267. // String
  268. case *(string):
  269. *P = string(o.(String))
  270. // Char
  271. case *(int32): // = rune
  272. *P = int32(o.(Char))
  273. // RegExp
  274. case *(*regexp.Regexp):
  275. *P = o.(RegExp).Value
  276. // Time
  277. case *(time.Time):
  278. *P = time.Time(o.(Time))
  279. // File
  280. case *(File):
  281. *P = o.(File)
  282. // Error
  283. case *(error):
  284. *P = o.(Error).Value
  285. // Reflect
  286. // ReflectType
  287. case *(ReflectType):
  288. *P = o.(ReflectType)
  289. // ReflectValue
  290. case *(ReflectValue):
  291. *P = o.(ReflectValue)
  292. // Rx
  293. // Observable
  294. case *(Observable):
  295. *P = o.(Observable)
  296. // Subject
  297. case *(Subject):
  298. *P = o.(Subject)
  299. // Interface and Lambda (Object)
  300. // Interface
  301. case *(Interface):
  302. *P = o.(Interface)
  303. // Lambda (Object)
  304. case *(Lambda):
  305. *P = o.(Lambda)
  306. // Container (Object)
  307. // List (Object)
  308. case *(List):
  309. *P = o.(List)
  310. // Seq
  311. case *(Seq):
  312. *P = o.(Seq)
  313. // Queue
  314. case *(Queue):
  315. *P = o.(Queue)
  316. // Heap
  317. case *(Heap):
  318. *P = o.(Heap)
  319. // Set
  320. case *(Set):
  321. *P = o.(Set)
  322. // Map
  323. case *(Map):
  324. *P = o.(Map)
  325. // GUI
  326. // Action
  327. case *(Action):
  328. *P = o.(Action)
  329. // Widget
  330. case *(Widget):
  331. *P = o.(Widget)
  332. // Signal
  333. case *(Signal):
  334. *P = o.(Signal)
  335. // Events
  336. case *(Events):
  337. *P = o.(Events)
  338. // Prop
  339. case *(Prop):
  340. *P = o.(Prop)
  341. // Union (Object)
  342. case *(Union):
  343. *P = o.(Union)
  344. default:
  345. var ptr_rv = reflect.ValueOf(p)
  346. // check for mistake
  347. if ptr_rv.Kind() != reflect.Ptr {
  348. panic("invalid argument")
  349. }
  350. var rv = ptr_rv.Elem()
  351. var rt = rv.Type()
  352. // Enum (int)
  353. if rt.Kind() == reflect.Int {
  354. var v = int(o.(Enum))
  355. rv.Set(reflect.ValueOf(v).Convert(rt))
  356. return
  357. }
  358. // Union (ctn.Maybe)
  359. if inner_rt, ok := ctn.ReflectTypeMatchMaybe(rt); ok {
  360. var u = o.(Union)
  361. if u.Index == OkIndex {
  362. var inner_ptr, inner_rv = reflectNew(inner_rt)
  363. ConvertObject(u.Object, inner_ptr, h)
  364. rv.Set(ctn.ReflectJust(inner_rv))
  365. } else if u.Index == NgIndex {
  366. rv.Set(ctn.ReflectNothing(inner_rt))
  367. } else {
  368. panic("something went wrong")
  369. }
  370. return
  371. }
  372. // Record (ctn.Pair)
  373. if a_rt, b_rt, ok := ctn.ReflectTypeMatchPair(rt); ok {
  374. var r = o.(Record)
  375. var a_ptr, a_rv = reflectNew(a_rt)
  376. var b_ptr, b_rv = reflectNew(b_rt)
  377. ConvertObject(r.Objects[0], a_ptr, h)
  378. ConvertObject(r.Objects[1], b_ptr, h)
  379. rv.Set(ctn.ReflectMakePair(a_rv, b_rv))
  380. return
  381. }
  382. // Union (pseudo-union)
  383. if rt.Kind() == reflect.Struct {
  384. if rt.NumField() >= 1 {
  385. if rt.Field(0).Type == pseudoUnionTagReflectType {
  386. var u = o.(Union)
  387. var i = (u.Index + 1)
  388. var object_ptr_rv = rv.Field(i).Addr()
  389. var object_ptr = object_ptr_rv.Interface()
  390. rv.Field(0).SetInt(int64(i))
  391. ConvertObject(u.Object, object_ptr, h)
  392. return
  393. }}
  394. // Record (struct)
  395. var r = o.(Record)
  396. var n = len(r.Objects)
  397. for i := 0; i < n; i += 1 {
  398. var field_ptr_rv = rv.Field(i).Addr()
  399. var field_ptr = field_ptr_rv.Interface()
  400. ConvertObject(r.Objects[i], field_ptr, h)
  401. }
  402. return
  403. }
  404. // Container (generic)
  405. // List (generic)
  406. if rt.Kind() == reflect.Slice {
  407. var l = o.(List)
  408. var slice_rv = reflect.MakeSlice(rt, 0, 0)
  409. l.ForEach(func(el_obj Object) {
  410. var el_ptr_rv = reflect.New(rt.Elem())
  411. var el_ptr = el_ptr_rv.Interface()
  412. ConvertObject(el_obj, el_ptr, h)
  413. var el_rv = el_ptr_rv.Elem()
  414. slice_rv = reflect.Append(slice_rv, el_rv)
  415. })
  416. rv.Set(slice_rv)
  417. return
  418. }
  419. // Interface and Lambda (generic)
  420. // Lambda (generic)
  421. if isLambdaFuncReflectType(rt) {
  422. var l = (func() Lambda {
  423. if rt.Name() == "" {
  424. return o.(Lambda)
  425. } else {
  426. if h == nil {
  427. panic("SAM conversion without RuntimeHandle")
  428. }
  429. var I = o.(Interface)
  430. if rt.NumIn() == 0 {
  431. return getSamInterfaceValueAsLambda(I, h)
  432. } else {
  433. return getSamInterfaceLambda(I, h)
  434. }
  435. }
  436. })()
  437. var get_arg_obj func([] reflect.Value) Object
  438. if rt.NumIn() == 0 {
  439. get_arg_obj = func(_ ([] reflect.Value)) Object {
  440. return nil
  441. }
  442. } else if rt.NumIn() == 1 {
  443. get_arg_obj = func(in ([] reflect.Value)) Object {
  444. var in0_v = in[0].Interface()
  445. return MakeObject(in0_v, h)
  446. }
  447. } else if rt.NumIn() == 2 {
  448. get_arg_obj = func(in ([] reflect.Value)) Object {
  449. var in0_v = in[0].Interface()
  450. var in1_v = in[1].Interface()
  451. var a = MakeObject(in0_v, h)
  452. var b = MakeObject(in1_v, h)
  453. var pair = [] Object { a, b }
  454. var o = ObjectImpl(Record { pair })
  455. return &o
  456. }
  457. } else {
  458. panic("something went wrong")
  459. }
  460. var out_rt = rt.Out(0)
  461. var f_rv = reflect.MakeFunc(rt, func(in ([] reflect.Value)) ([] reflect.Value) {
  462. var arg = get_arg_obj(in)
  463. var ret = l.Call(arg)
  464. var out_ptr, out_rv = reflectNew(out_rt)
  465. ConvertObject(ret, out_ptr, h)
  466. var out = [] reflect.Value { out_rv }
  467. return out
  468. })
  469. rv.Set(f_rv)
  470. return
  471. }
  472. panic("unsupported pointer type: " + rt.String())
  473. } }
  474. }
  475. func MakeNativeFunction(v interface{}) NativeFunction {
  476. var rv = reflect.ValueOf(v)
  477. var rt = rv.Type()
  478. if !((rt.Kind() == reflect.Func) && !(rt.IsVariadic())) {
  479. panic("invalid argument")
  480. }
  481. return NativeFunction(func(args ([] Object), ctx ([] Object), h RuntimeHandle) Object {
  482. var num_in = rt.NumIn()
  483. var num_out = rt.NumOut()
  484. var in = make([] reflect.Value, num_in)
  485. for i := 0; i < num_in; i += 1 {
  486. var in_t = rt.In(i)
  487. if i < len(args) {
  488. var arg = args[i]
  489. var arg_ptr, arg_rv = reflectNew(in_t)
  490. ConvertObject(arg, arg_ptr, h)
  491. in[i] = arg_rv
  492. } else {
  493. var j = (i - len(args))
  494. if j < len(ctx) {
  495. var item = ctx[j]
  496. var item_ptr, item_rv = reflectNew(in_t)
  497. ConvertObject(item, item_ptr, h)
  498. in[i] = item_rv
  499. } else {
  500. in[i] = reflect.ValueOf(h)
  501. }
  502. }
  503. }
  504. var out = rv.Call(in)
  505. if num_out == 0 {
  506. return nil
  507. } else if num_out == 1 {
  508. var out_v = out[0].Interface()
  509. return MakeObject(out_v, h)
  510. } else {
  511. panic("invalid argument")
  512. }
  513. })
  514. }
  515. func retrieveObject[T any] (o Observable, h RuntimeHandle, k func(T)(Observable)) Observable {
  516. return Observable(func(pub DataPublisher) {
  517. var ctx, ob = pub.useInheritedContext()
  518. pub.run(o, ctx, awaitNoexceptObserver(ob, h, func(obj Object) {
  519. pub.run(k(FromObject[T](obj)), ctx, ob)
  520. }))
  521. })
  522. }
  523. func retrieveObjectInChildContext[T any] (parent *context, o Observable, h RuntimeHandle, k func(T,*context,func())(Observable)) Observable {
  524. return Observable(func(pub DataPublisher) {
  525. var _, ob = pub.useInheritedContext()
  526. var ctx, dispose = parent.createChild()
  527. pub.run(o, ctx, awaitNoexceptObserver(ob, h, func(obj Object) {
  528. pub.run(k(FromObject[T](obj), ctx, dispose), ctx, ob)
  529. }))
  530. })
  531. }
  532. func doSync(k func()) Observable {
  533. return Observable(func(pub DataPublisher) {
  534. pub.SyncReturn(func() (Object, error) {
  535. k()
  536. return nil, nil
  537. })
  538. })
  539. }
  540. func doSync1[T any] (k func()(T)) Observable {
  541. return Observable(func(pub DataPublisher) {
  542. pub.SyncReturn(func() (Object, error) {
  543. var t = k()
  544. return ToObject(t), nil
  545. })
  546. })
  547. }
  548. func doSync2[T any] (k func()(T,func())) Observable {
  549. return Observable(func(pub DataPublisher) {
  550. pub.SyncReturn(func() (Object, error) {
  551. var t, c = k()
  552. pub.context.registerCleaner(c)
  553. return ToObject(t), nil
  554. })
  555. })
  556. }
  557. func onSync(k func()(func(qt.Pkg,func()))) Observable {
  558. return Observable(func(pub DataPublisher) {
  559. var pkg, dispose = qt.CreatePkg()
  560. pub.context.registerCleaner(dispose)
  561. k()(pkg, func() {
  562. pub.observer.value(nil)
  563. })
  564. })
  565. }
  566. func onSync1[T any] (k func()(func(qt.Pkg,func(T)))) Observable {
  567. return Observable(func(pub DataPublisher) {
  568. var pkg, dispose = qt.CreatePkg()
  569. pub.context.registerCleaner(dispose)
  570. k()(pkg, func(v T) {
  571. pub.observer.value(ToObject(v))
  572. })
  573. })
  574. }
  575. func reflectNew(t reflect.Type) (interface{}, reflect.Value) {
  576. var ptr_rv = reflect.New(t)
  577. var ptr = ptr_rv.Interface()
  578. var rv = ptr_rv.Elem()
  579. return ptr, rv
  580. }
  581. func isLambdaFuncReflectType(t reflect.Type) bool {
  582. return (t.Kind() == reflect.Func) &&
  583. !(t.IsVariadic()) &&
  584. (t.NumIn() == 0 || t.NumIn() == 1 || t.NumIn() == 2) &&
  585. (t.NumOut() == 1)
  586. }
  587. func getSamInterfaceLambda(I Interface, h RuntimeHandle) Lambda {
  588. if len(I.DispatchTable.Methods) == 1 && len(I.DispatchTable.Children) == 0 {
  589. return (*(CallFirstMethod(I, h))).(Lambda)
  590. } else {
  591. panic("expect SAM interface but got non-SAM interface")
  592. }
  593. }
  594. func getSamInterfaceValueAsLambda(I Interface, h RuntimeHandle) Lambda {
  595. if len(I.DispatchTable.Methods) == 1 && len(I.DispatchTable.Children) == 0 {
  596. var value = CallFirstMethod(I, h)
  597. return Lambda { func(_ Object) Object {
  598. return value
  599. }}
  600. } else {
  601. panic("expect SAM interface but got non-SAM interface")
  602. }
  603. }
  604. var maxInt32 = big.NewInt(math.MaxInt32)
  605. var minInt32 = big.NewInt(math.MinInt32)
  606. func clampTo32Int(n *big.Int) int {
  607. if n.Cmp(maxInt32) > 0 {
  608. return math.MaxInt32
  609. } else if n.Cmp(minInt32) < 0 {
  610. return math.MinInt32
  611. } else {
  612. return int(n.Int64())
  613. }
  614. }
  615. func ObjInt(n int) Object {
  616. return ObjIntFromInt64(int64(n))
  617. }
  618. func ObjIntFromBigInt(n *big.Int) Object {
  619. return Obj(Int { n })
  620. }
  621. func ObjIntFromInt64(n int64) Object {
  622. return Obj(Int { big.NewInt(n) })
  623. }
  624. func ObjBool(p bool) Object {
  625. return Obj(Bool(p))
  626. }
  627. func ObjFloat(x float64) Object {
  628. return Obj(Float(x))
  629. }
  630. func ObjString(s string) Object {
  631. return Obj(String(s))
  632. }
  633. func ObjBytes(b ([] byte)) Object {
  634. return Obj(Bytes(b))
  635. }
  636. func ObjPair(a Object, b Object) Object {
  637. return Obj(Record { Objects: [] Object { a, b } })
  638. }
  639. func ObjList(l ([] Object)) Object {
  640. var buf ListBuilder
  641. for _, item := range l {
  642. buf.Append(item)
  643. }
  644. return Obj(buf.Collect())
  645. }
  646. func ObjQueue(q ctn.Queue[Object]) Object {
  647. return Obj(Queue(q))
  648. }
  649. func ObjTime(t time.Time) Object {
  650. return Obj(Time(t))
  651. }
  652. func ObjTimeNow() Object {
  653. return ObjTime(time.Now())
  654. }
  655. func ObjFile(path string) Object {
  656. return Obj(File { path })
  657. }
  658. func GetBool(o Object) bool {
  659. return bool((*o).(Bool))
  660. }
  661. func GetInt(o Object) int {
  662. return clampTo32Int((*o).(Int).Value)
  663. }
  664. func GetIntAsRawBigInt(o Object) *big.Int {
  665. return (*o).(Int).Value
  666. }
  667. func GetFloat(o Object) float64 {
  668. return float64((*o).(Float))
  669. }
  670. func GetChar(o Object) rune {
  671. return int32((*o).(Char))
  672. }
  673. func GetString(o Object) string {
  674. return string((*o).(String))
  675. }
  676. func GetBytes(o Object) ([] byte) {
  677. return ([] byte)((*o).(Bytes))
  678. }
  679. func GetError(o Object) error {
  680. return (*o).(Error).Value
  681. }
  682. func GetPair(o Object) (Object, Object) {
  683. return FromObject[ctn.Pair[Object,Object]](o)()
  684. }
  685. func GetList(o Object) List {
  686. return (*o).(List)
  687. }
  688. func GetSeq(o Object) Seq {
  689. return (*o).(Seq)
  690. }
  691. func GetQueue(o Object) ctn.Queue[Object] {
  692. return ctn.Queue[Object]((*o).(Queue))
  693. }
  694. func GetHeap(o Object) ctn.Heap[Object] {
  695. return ctn.Heap[Object]((*o).(Heap))
  696. }
  697. func GetSet(o Object) ctn.Set[Object] {
  698. return ctn.Set[Object]((*o).(Set))
  699. }
  700. func GetMap(o Object) ctn.Map[Object,Object] {
  701. return ctn.Map[Object,Object]((*o).(Map))
  702. }
  703. func GetTime(o Object) time.Time {
  704. return time.Time((*o).(Time))
  705. }
  706. func GetFile(o Object) File {
  707. return (*o).(File)
  708. }
  709. func GetObservable(o Object) Observable {
  710. return (*o).(Observable)
  711. }
  712. func GetWidget(o Object) Widget {
  713. return (*o).(Widget)
  714. }
  715. func YieldObservables(o ...Observable) func(func(Observable)) {
  716. return func(yield func(Observable)) {
  717. for _, o := range o {
  718. yield(o)
  719. }
  720. }
  721. }
  722. func ListToPair(o Object) Object {
  723. var l = GetList(o)
  724. a, l, ok1 := l.Shifted()
  725. b, l, ok2 := l.Shifted()
  726. if !(ok1 && ok2) { panic("invalid argument") }
  727. return ObjPair(a, b)
  728. }
  729. func QueueToPair(o Object) Object {
  730. var q = GetQueue(o)
  731. a, q, ok1 := q.Shifted()
  732. b, q, ok2 := q.Shifted()
  733. if !(ok1 && ok2) { panic("invalid argument") }
  734. return ObjPair(a, b)
  735. }