forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalues_rps.cc
More file actions
1373 lines (1260 loc) · 47.5 KB
/
Copy pathvalues_rps.cc
File metadata and controls
1373 lines (1260 loc) · 47.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************
* file values_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
* Implementation related to immutable values and quasivalues
* See also morevalues_rps.cc file
*
* Author(s):
* Basile Starynkevitch <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright (C) 2019 - 2025 The Reflective Persistent System Team
* team@refpersys.org & http://refpersys.org/
*
* License:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "refpersys.hh"
extern "C" const char rps_values_gitid[];
const char rps_values_gitid[]= RPS_GITID;
extern "C" const char rps_values_date[];
const char rps_values_date[]= __DATE__;
extern "C" const char rps_values_shortgitid[];
const char rps_values_shortgitid[]= RPS_SHORTGITID;
void
Rps_Id::to_cbuf24(char cbuf[]) const
{
/// example cbuf = "_0abcdefghijABCDEFG"
/// |0 |11 |19
static_assert(sizeof("_0abcdefghijABCDEFG")-1
== 1+nbdigits_hi+nbdigits_lo);
RPS_ASSERT (cbuf != nullptr);
memset(cbuf, 0, buflen);
char*last = cbuf+nbdigits_hi;
auto pc = last;
cbuf[0] = '_';
uint64_t n = _id_hi;
do
{
unsigned d = n % base;
n = n / base;
*pc = b62digits[d];
pc--;
}
while (pc>cbuf);
auto start = cbuf+nbdigits_hi;
last = start+nbdigits_lo;
pc = last;
n = _id_lo;
do
{
unsigned d = n % base;
n = n / base;
*pc = b62digits[d];
pc--;
}
while (pc>start);
}; // end Rps_Id::to_cbuf24
/// opposite conversion from cbuf to oid
Rps_Id::Rps_Id (const char*cbuf, const char**pend, bool *pok) : Rps_Id ()
{
auto lasthi = cbuf+nbdigits_hi+1;
auto lastlo = lasthi + nbdigits_lo;
uint64_t hi=0, lo=0;
if (cbuf[0] != '_' && !isdigit(cbuf[1])) goto fail;
for (auto pcb = cbuf+1; *pcb && pcb<lasthi; pcb++)
{
auto pcs = strchr(b62digits, *pcb);
if (!pcs) goto fail;
hi = hi*62 + (pcs-b62digits);
}
if ((hi > 0 && hi < min_hi) || hi >= max_hi)
goto fail;
for (auto pcb = lasthi; *pcb && pcb < lastlo; pcb++)
{
auto pcs = strchr(b62digits, *pcb);
if (!pcs) goto fail;
lo = lo*62 + (pcs-b62digits);
}
if ((lo > 0 && lo < min_lo) || lo >= max_lo)
goto fail;
_id_hi = hi;
_id_lo = lo;
if (pend)
*pend = lastlo;
if (pok)
*pok = true;
return;
fail:
if (pend)
*pend = cbuf;
if (pok)
*pok = false;
return;
} // end Rps_Id::Rps_Id (const char*, char**, bool*)
//////////////////////////////////////////////// quasi values
std::recursive_mutex Rps_QuasiZone::qz_mtx;
std::vector<Rps_QuasiZone*> Rps_QuasiZone::qz_zonvec(100);
uint32_t Rps_QuasiZone::qz_cnt;
std::atomic<uint64_t> Rps_QuasiZone::qz_alloc_cumulw;
void
Rps_QuasiZone::initialize(void)
{
static bool inited;
if (inited) return;
inited = true;
std::lock_guard<std::recursive_mutex> gu(qz_mtx);
qz_zonvec.reserve(100);
qz_zonvec.push_back(nullptr);
} // end Rps_QuasiZone::initialize
Rps_QuasiZone::~Rps_QuasiZone()
{
unregister_in_zonevec();
} // end of Rps_QuasiZone::~Rps_QuasiZone
void
Rps_QuasiZone::register_in_zonevec(void)
{
std::lock_guard<std::recursive_mutex> gu(qz_mtx);
if (RPS_UNLIKELY(9 * qz_zonvec.capacity() <= 8 * qz_zonvec.size()))
{
auto newcap = rps_prime_above(19*qz_zonvec.size()/16
+ 200);
qz_zonvec.reserve(newcap);
}
if (RPS_LIKELY(qz_zonvec.size() > 128))
{
uint32_t rk = 1 + Rps_Random::random_32u() % (qz_zonvec.size() - 16);
uint32_t endrk = rk + 24;
if (endrk > (uint32_t)qz_zonvec.size())
endrk = (uint32_t)qz_zonvec.size();
for (uint32_t ix= rk; ix < endrk; ix++)
if (qz_zonvec[ix] == nullptr)
{
qz_zonvec[ix] = this;
this->qz_rank = ix;
qz_cnt++;
return;
}
}
this->qz_rank = (uint32_t)qz_zonvec.size();
qz_cnt++;
qz_zonvec.push_back(this);
} // end of Rps_QuasiZone::register_in_zonevec
void
Rps_QuasiZone::unregister_in_zonevec(void)
{
std::lock_guard<std::recursive_mutex> gu(qz_mtx);
RPS_ASSERT(this->qz_rank>0
&& this->qz_rank < (uint32_t)qz_zonvec.size());
RPS_ASSERT(qz_cnt>0 && qz_cnt<(uint32_t)qz_zonvec.size());
RPS_ASSERT(qz_zonvec[this->qz_rank] == this);
qz_zonvec[this->qz_rank] = nullptr;
qz_cnt--;
} // end of Rps_QuasiZone::unregister_in_zonevec
void
Rps_QuasiZone::clear_all_gcmarks(Rps_GarbageCollector&gc)
{
std::lock_guard<std::recursive_mutex> gu(qz_mtx);
for (Rps_QuasiZone *qz : qz_zonvec)
{
if (!qz) continue;
qz->clear_gcmark(gc);
}
} // end of Rps_QuasiZone::clear_all_gcmarks
std::mutex Rps_LazyHashedZoneValue::lazy_mtxarr[Rps_LazyHashedZoneValue::lazy_nbmutexes];
/* Printing routine likely to be called by GDB */
void
rps_print_value(const Rps_Value val)
{
std::cout << Rps_OutputValue(val, 0, Rps_Value::debug_maxdepth) << std::endl;
} // end rps_print_value
void
rps_print_ptr_value(const void*v)
{
static_assert(sizeof(Rps_Value) == sizeof(v));
Rps_Value val;
memcpy((void*)&val, (const void*)&v, sizeof(v));
std::cout << Rps_OutputValue(val, 0, Rps_Value::debug_maxdepth) << std::endl;
} // end rps_print_ptr_value
void
rps_limited_print_value(const Rps_Value val, unsigned depth, unsigned maxdepth)
{
std::cout << Rps_OutputValue(val, depth, maxdepth) << std::endl;
} // end rps_limited_print_value
void
rps_limited_print_ptr_value(const void*v, unsigned depth, unsigned maxdepth)
{
static_assert(sizeof(Rps_Value) == sizeof(v));
Rps_Value val;
memcpy((void*)&val, (const void*)&v, sizeof(v));
std::cout << Rps_OutputValue(val, depth, maxdepth) << std::endl;
} // end rps_limited_print_ptr_value
//////////////////////////////////////////////// sets
Rps_SetOb::Rps_SetOb(const std::set<Rps_ObjectRef>& setob, Rps_SetTag)
: Rps_SetOb::Rps_SetOb((unsigned) setob.size(), Rps_SetTag{})
{
int ix=0;
for (auto ob : setob)
{
RPS_ASSERT (ob);
_seqob[ix++] = ob;
}
} // end Rps_SetOb::Rps_SetOb
Rps_SetOb Rps_SetOb::_setob_emptyset_(0,Rps_SetTag{});
const Rps_SetOb*
Rps_SetOb::make(const std::set<Rps_ObjectRef>& setob)
{
auto setsiz = setob.size();
if (RPS_UNLIKELY(setsiz >= Rps_SeqObjRef::maxsize))
throw std::length_error("Rps_SetOb::make with too many elements");
for (auto ob : setob)
if (RPS_UNLIKELY(!ob))
throw std::invalid_argument("empty element to Rps_SetOb::make");
return
rps_allocate_with_wordgap<Rps_SetOb,const std::set<Rps_ObjectRef>&,Rps_SetTag>
(setsiz,setob,Rps_SetTag{});
} // end of Rps_SetOb::make with set
const Rps_SetOb*
Rps_SetOb::make(const std::initializer_list<Rps_ObjectRef>&elemil)
{
std::set<Rps_ObjectRef>elemset;
for (auto elem: elemil)
if (elem)
elemset.insert(elem);
return make(elemset);
} // end of Rps_SetOb::make with initializer_list
const Rps_SetOb*
Rps_SetOb::make(const std::vector<Rps_ObjectRef>&vecob)
{
std::set<Rps_ObjectRef>elemset;
for (auto ob: vecob)
if (ob)
elemset.insert(ob);
return make(elemset);
} // end of Rps_SetOb::make with vector
const Rps_SetOb*
Rps_SetOb::collect(const std::vector<Rps_Value>&vecval)
{
std::set<Rps_ObjectRef>elemset;
for (auto val: vecval)
{
if (val.is_object())
elemset.insert(Rps_ObjectRef(val.as_object()));
else if (val.is_tuple())
{
auto tup = val.as_tuple();
for (auto ob: *tup)
if (ob)
elemset.insert(ob);
}
else if (val.is_set())
{
auto set = val.as_set();
for (auto ob: *set)
elemset.insert(ob);
}
}
return make(elemset);
} // end of Rps_SetOb::collect with vector
const Rps_SetOb*
Rps_SetOb::collect(const std::initializer_list<Rps_Value>&ilval)
{
std::set<Rps_ObjectRef>elemset;
for (auto val: ilval)
{
if (val.is_object())
elemset.insert(Rps_ObjectRef(val.as_object()));
else if (val.is_tuple())
{
auto tup = val.as_tuple();
for (auto ob: *tup)
if (ob)
elemset.insert(ob);
}
else if (val.is_set())
{
auto set = val.as_set();
for (auto ob: *set)
elemset.insert(ob);
}
}
return make(elemset);
} // end of Rps_SetOb::collect with initializer_list
void
Rps_SetOb::val_output(std::ostream&out, unsigned depth, unsigned maxdepth) const
{
if (depth>maxdepth)
{
out << "??";
return;
};
out << "{";
int cnt=0;
for (Rps_ObjectRef ob: *this)
{
if (cnt>0) out <<", ";
ob.output(out, depth+1, maxdepth);
cnt++;
}
out << "}";
} // end Rps_SetOb::val_output
Rps_ObjectRef
Rps_SetOb::compute_class(Rps_CallFrame*) const
{
return RPS_ROOT_OB(_6JYterg6iAu00cV9Ye); // the `set` class
} // end Rps_SetOb::compute_class
void
Rps_SetOb::repeat_increasing_each_element_until(Rps_CallFrame*cf, void*data,
const std::function<bool(Rps_CallFrame*,void*/*data*/,Rps_ObjectRef/*elem*/)>& func) const
{
RPS_ASSERT (cf==nullptr || cf->is_good_call_frame());
RPS_ASSERT (this->type() == Rps_Type::Set);
unsigned card = cnt();
if (card > 0)
{
const Rps_ObjectRef* arr = raw_const_data();
for (unsigned ix=0; ix<card; ix++)
if (func(cf,data,arr[ix]))
return;
};
} // end Rps_SetOb::repeat_increasing_each_element_until
void
Rps_SetOb::repeat_decreasing_each_element_until(Rps_CallFrame*cf, void*data,
const std::function<bool(Rps_CallFrame*,void*/*data*/,Rps_ObjectRef/*elem*/)>& func) const
{
RPS_ASSERT (cf==nullptr || cf->is_good_call_frame());
RPS_ASSERT (this->type() == Rps_Type::Set);
unsigned card = cnt();
if (card > 0)
{
const Rps_ObjectRef* arr = raw_const_data();
for (int ix=(int)card-1; ix>=0; ix--)
if (func(cf,data,arr[ix]))
return;
};
} // end Rps_SetOb::repeat_decreasing_each_element_each_element_until
//////////////////////////////////////// tuples
const Rps_TupleOb*
Rps_TupleOb::make(const std::vector<Rps_ObjectRef>& vecob)
{
unsigned nbob = 0;
{
for (auto ob: vecob)
if (ob)
nbob++;
}
if (RPS_UNLIKELY(nbob > maxsize))
throw std::length_error("Rps_TupleOb::make too many objects");
if (RPS_LIKELY(nbob == vecob.size()))
{
auto tup =
rps_allocate_with_wordgap<Rps_TupleOb, unsigned, Rps_TupleTag>
(nbob, nbob, Rps_TupleTag{});
auto rd = tup->raw_data();
for (int ix=0; ix<(int)nbob; ix++) rd[ix] = vecob[ix];
return tup;
}
else
{
std::vector<Rps_ObjectRef> vec;
vec.reserve(nbob);
for (auto ob: vecob)
if (ob)
vec.push_back(ob);
return make(vec);
}
} // end Rps_TupleOb::make from vector
const Rps_TupleOb*
Rps_TupleOb::make(const std::initializer_list<Rps_ObjectRef>&compil)
{
std::vector<Rps_ObjectRef> vec(compil);
return make(vec);
} // end of Rps_TupleOb::make from initializer_list
const Rps_TupleOb*
Rps_TupleOb::collect(const std::vector<Rps_Value>& vecval)
{
unsigned nbcomp = 0;
for (auto v : vecval)
{
if (v.is_object() && v.to_object() != nullptr)
nbcomp++;
else if (v.is_tuple())
nbcomp += v.to_tuple()->cnt();
else if (v.is_set())
nbcomp += v.to_set()->cnt();
else continue;
if (RPS_UNLIKELY(nbcomp > maxsize))
throw std::length_error("Rps_TupleOb::collect too many objects");
}
std::vector<Rps_ObjectRef> vecob;
vecob.reserve(nbcomp);
for (auto v : vecval)
{
if (v.is_object())
{
auto ob = v.to_object();
if (ob)
vecob.push_back(ob);
}
else if (v.is_tuple())
{
auto tup = v.to_tuple();
for (auto ob : *tup)
if (ob)
vecob.push_back(ob);
}
else if (v.is_set())
{
auto set = v.to_set();
for (auto ob : *set)
if (ob)
vecob.push_back(ob);
}
else continue;
}
return make(vecob);
} // end of Rps_TupleOb::collect from vector
const Rps_TupleOb*
Rps_TupleOb::collect(const std::initializer_list<Rps_Value>&valil)
{
std::vector<Rps_Value> vecval(valil);
return collect(vecval);
} // end Rps_TupleOb::collect from initializer_list
Rps_ObjectRef
Rps_TupleOb::compute_class( Rps_CallFrame*) const
{
return RPS_ROOT_OB(_6NVM7sMcITg01ug5TC); // the `tuple` class
} // end Rps_TupleOb::compute_class
void
Rps_TupleOb::val_output(std::ostream&out, unsigned depth, unsigned maxdepth) const
{
if (depth > maxdepth)
{
out << "??";
return;
};
out << "[";
int cnt=0;
for (Rps_ObjectRef ob: *this)
{
if (cnt>0) out <<", ";
ob.output(out,depth+1,maxdepth);
cnt++;
}
out << "]";
} // end Rps_TupleOb::val_output
////////////////////////////////////////////////// closures
Rps_ClosureZone*
Rps_ClosureZone::make(Rps_ObjectRef connob, const std::initializer_list<Rps_Value>& valil)
{
if (!connob)
return nullptr;
auto nbsons = valil.size();
Rps_ClosureZone* cloz
= Rps_QuasiZone::rps_allocate_with_wordgap<Rps_ClosureZone,unsigned,Rps_ObjectRef,Rps_ClosureTag>((nbsons*sizeof(Rps_Value)/sizeof(void*)),
(unsigned)nbsons, connob, Rps_ClosureTag{});
int ix=0;
Rps_Value*sonarr = cloz->raw_data_sons();
for (auto val: valil)
sonarr[ix++] = val;
return cloz;
} // end ClosureZone::make
Rps_ClosureZone*
Rps_ClosureZone::make(Rps_ObjectRef connob, const std::vector<Rps_Value>& valvec)
{
if (!connob)
return nullptr;
auto nbsons = valvec.size();
Rps_ClosureZone* cloz
= Rps_QuasiZone::rps_allocate_with_wordgap<Rps_ClosureZone,unsigned,Rps_ObjectRef,Rps_ClosureTag>((nbsons*sizeof(Rps_Value)/sizeof(void*)),
(unsigned)nbsons, connob, Rps_ClosureTag{});
int ix=0;
Rps_Value*sonarr = cloz->raw_data_sons();
for (auto val: valvec)
sonarr[ix++] = val;
return cloz;
} // end ClosureZone::make
Rps_TwoValues
Rps_ClosureValue::apply_vect(Rps_CallFrame*callerframe, const std::vector<Rps_Value>& argvec) const
{
//RPS_ASSERT(callerframe && callerframe->stored_type() == Rps_Type::CallFrame);
RPS_ASSERT_CALLFRAME (callerframe);
if (is_empty() || !is_closure())
return nullptr;
Rps_ObjectRef obconn = connob();
if (!obconn)
return nullptr;
auto arity = argvec.size();
switch (arity)
{
case 0:
return apply0(callerframe);
case 1:
return apply1(callerframe, argvec[0]);
case 2:
return apply2(callerframe, argvec[0], argvec[1]);
case 3:
return apply3(callerframe, argvec[0], argvec[1], argvec[2]);
case 4:
return apply4(callerframe, argvec[0], argvec[1], argvec[2], argvec[3]);
default:
{
rps_applyingfun_t*appfun = obconn->get_applyingfun(*this);
if (!appfun)
return nullptr;
std::vector<Rps_Value> restvec(arity-4);
for (unsigned ix=4; ix<(unsigned)arity; ix++)
restvec[ix-4] = argvec[ix];
callerframe->set_closure(*this);
Rps_Value res= appfun(callerframe,argvec[0], argvec[1], argvec[2], argvec[3], &restvec);
callerframe->clear_closure();
return res;
}
}
} // end Rps_ClosureValue::apply_vect
Rps_TwoValues
Rps_ClosureValue::apply_ilist(Rps_CallFrame*callerframe, const std::initializer_list<Rps_Value>& argil) const
{
std::vector <Rps_Value> argvec(argil);
return apply_vect(callerframe,argvec);
} // end Rps_ClosureValue::apply_ilist
void
Rps_ClosureZone::val_output(std::ostream&out, unsigned depth, unsigned maxdepth) const
{
if (depth>maxdepth)
{
out << "??";
return;
};
out << "%";
conn().output(out, depth+1, maxdepth);
if (depth > Rps_Value::max_output_depth || depth>maxdepth)
{
out << "(...)";
}
else
{
out << "(";
int cnt=0;
for (auto val: *this)
{
if (cnt>0) out <<", ";
val.output(out, depth+1, maxdepth);
cnt++;
}
out << ")";
}
if (depth<2)
out << std::flush;
} // end Rps_ClosureZone::val_output
Rps_ObjectRef
Rps_ClosureZone::compute_class(Rps_CallFrame*) const
{
return RPS_ROOT_OB(_4jISxMJ4PYU0050nUl); // the `closure` class
} // end Rps_ClosureZone::compute_class
//////////////// attributes
Rps_Value
Rps_Value::get_attr(Rps_CallFrame*stkf, const Rps_ObjectRef obattr) const
{
// in principle, obattr type is always Object, but we need to be
// absolutely sure, even in case of bugs, so we do check it
if (obattr.is_empty() || obattr->stored_type() != Rps_Type::Object)
return nullptr;
rps_magicgetterfun_t*getfun = obattr->ob_magicgetterfun.load();
if (getfun)
return (*getfun)(stkf, *this, obattr);
if (is_object())
{
const Rps_ObjectZone*thisob = as_object();
std::lock_guard gu(thisob->ob_mtx);
auto it = thisob->ob_attrs.find(obattr);
if (it != thisob->ob_attrs.end())
return it->second;
};
return nullptr;
} // end Rps_Value::get_attr
Rps_ObjectRef
Rps_Value::compute_class(Rps_CallFrame*stkf) const
{
if (is_empty())
return nullptr;
else if (is_int())
return RPS_ROOT_OB(_2A2mrPpR3Qf03p6o5b); // the `int` class
else if (is_string())
return RPS_ROOT_OB(_62LTwxwKpQ802SsmjE); // the `string` class
else if (is_double())
return RPS_ROOT_OB(_98sc8kSOXV003i86w5); // the `double` class
else if (is_tuple())
return RPS_ROOT_OB(_6NVM7sMcITg01ug5TC); // the `tuple` class
else if (is_set())
return RPS_ROOT_OB(_6JYterg6iAu00cV9Ye); // the `set` class
else if (is_closure())
return RPS_ROOT_OB(_4jISxMJ4PYU0050nUl); // the `closure` class
else if (is_ptr())
return as_ptr()->compute_class(stkf);
return nullptr;
} // end Rps_Value::compute_class
// the below member function computes, for the current value, the
// closure for the RefPerSys method of selector obselector. It is so
// important that it deserves a describing symbol of its own.
Rps_ClosureValue
Rps_Value::closure_for_method_selector(Rps_CallFrame*callerframe, Rps_ObjectRef obselectorarg) const
{
// our frame descriptor is the `closure_for_method_selector` symbol
RPS_LOCALFRAME(RPS_ROOT_OB(_6JbWqOsjX5T03M1eGM),
callerframe,
Rps_Value val; // the current value
Rps_ObjectRef obselect; // the attribute
Rps_ObjectRef obcurclass; // the current class
Rps_ClosureValue closval; // the resulting closure
);
_f.val = Rps_Value(*this);
_f.obselect = obselectorarg;
_f.obcurclass = _f.val.compute_class(&_);
int loopcount = 0;
RPS_DEBUG_LOG(MSGSEND, "closure_for_method_selector start val=" << _f.val
<< " obcurclass=" << _f.obcurclass
<< " obselect=" << _f.obselect);
RPS_ASSERT(RPS_ROOT_OB(_6XLY6QfcDre02922jz)); // the `value` class exists, it has been loaded
while (loopcount++ < (int)maximal_inheritance_depth)
{
RPS_DEBUG_LOG(MSGSEND, "closure_for_method_selector obcurclass=" << _f.obcurclass << " obselect=" << _f.obselect << " loopcount=" << loopcount);
if (!_f.obcurclass) // should never happen
{
if (_f.val.is_object())
RPS_FATALOUT("object @" << (void*)_f.val.unsafe_wptr()
<< " of oid " << _f.val.to_object()->oid()
<< " has no class");
else
RPS_FATALOUT("value @" << (void*)_f.val.unsafe_wptr()
<< " of type#" << (int)(_f.val.to_ptr()?_f.val.to_ptr()->stored_type():Rps_Type::None) << " has no class");
}
std::lock_guard<std::recursive_mutex> gucurclass(*(_f.obcurclass->objmtxptr()));
if (_f.obcurclass == RPS_ROOT_OB(_6XLY6QfcDre02922jz) // the topmost `value` class ends the loop
)
{
/// if the `value` class is not a genuine RefPerSys class, it
/// means that RefPerSys persistent heap is broken beyond
/// repair. So the asserts below are always satisfied.
auto valclasspayl = reinterpret_cast<Rps_PayloadClassInfo*>(_f.obcurclass->get_payload());
RPS_ASSERT(valclasspayl != nullptr);
// the below check is faster than a C++ dynamic_cast:
RPS_ASSERT(valclasspayl->stored_type() == Rps_Type::PaylClassInfo);
_f.closval = valclasspayl->get_own_method(_f.obselect);
RPS_DEBUG_LOG(MSGSEND, "closure_for_method_selector!value closval=" << _f.closval);
if (_f.closval && _f.closval.is_closure()) // should be always true! But we need to check
return _f.closval;
else
return Rps_ClosureValue(nullptr);
}
/// usual common case:
if (_f.obcurclass->get_class() == RPS_ROOT_OB(_41OFI3r0S1t03qdB2E) // the `class` class
)
{
auto valclasspayl = reinterpret_cast<Rps_PayloadClassInfo*>(_f.obcurclass->get_payload());
RPS_ASSERT(valclasspayl != nullptr);
// the below check is faster than a C++ dynamic_cast:
RPS_ASSERT(valclasspayl->stored_type() == Rps_Type::PaylClassInfo);
_f.closval = valclasspayl->get_own_method(_f.obselect);
RPS_DEBUG_LOG(MSGSEND, "closure_for_method_selector!class closval=" << _f.closval);
if (_f.closval && _f.closval.is_closure()) // should be always true! But we need to check
return _f.closval;
else
{
_f.obcurclass = valclasspayl->superclass();
continue;
}
}
/// special case for dynamic_cast, which is unlikely to happen
else if (auto valclasspayl = _f.obcurclass->get_dynamic_payload<Rps_PayloadClassInfo>())
{
// could happen latter when _f.obcurclass is a subclass of `class` but unlikely
RPS_ASSERT(valclasspayl != nullptr);
// the below check is faster than a C++ dynamic_cast:
RPS_ASSERT(valclasspayl->stored_type() == Rps_Type::PaylClassInfo);
_f.closval = valclasspayl->get_own_method(_f.obselect);
RPS_DEBUG_LOG(MSGSEND, "closure_for_method_selector!sub-class closval=" << _f.closval);
if (_f.closval && _f.closval.is_closure()) // should be always true! But we need to check
return _f.closval;
else
{
_f.obcurclass = valclasspayl->superclass();
continue;
}
}
};
/// we should never reach this point.... if we do, the persistent heap was corrupted...
_f.obcurclass = _f.val.compute_class(&_);
RPS_FATALOUT("failed to compute closure_for_method_selector for value " <<
_f.val << " of class " << _f.obcurclass
<< " for selector " << _f.obselect);
} // end of Rps_Value::closure_for_method_selector
////////////////////////////////////////////////////////////////
//////////////// message sending protocol //////////////////////
////////////////////////////////////////////////////////////////
Rps_TwoValues
Rps_Value::send0(Rps_CallFrame*callerframe, const Rps_ObjectRef obselarg) const
{
//RPS_ASSERT(callerframe && callerframe->stored_type() == Rps_Type::CallFrame);
RPS_ASSERT_CALLFRAME (callerframe);
RPS_LOCALFRAME(RPS_ROOT_OB(_5yQcFbU0seU018B48Z), // `message_sending` symbol
callerframe,
Rps_Value selfv; // the receiver
Rps_ClosureValue closv; // the closure
Rps_ObjectRef obsel; // the selector
);
_f.selfv = Rps_Value(*this);
_f.obsel = obselarg;
RPS_DEBUG_LOG(MSGSEND, "send0 selfv=" << _f.selfv
<< " of class:" << _f.selfv.compute_class(&_)
<< ", obsel=" << _f.obsel);
_f.closv = _f.selfv.closure_for_method_selector(&_,_f.obsel);
RPS_DEBUG_LOG(MSGSEND, "send0 selfv=" << _f.selfv
<< ", closv=" << _f.closv);
if (_f.closv.is_closure())
return _f.closv.apply1(&_, _f.selfv);
else
RPS_DEBUG_LOG(MSGSEND, "send0 applying selfv=" << _f.selfv
<< " of class:" << _f.selfv.compute_class(&_) << std::endl
<< "… with obsel=" << _f.obsel
<< " of class:" << _f.obsel->compute_class(&_) << std::endl
<< ".... non closure closv=" << _f.closv);
return Rps_TwoValues(nullptr,nullptr);
} // end Rps_Value::send0
Rps_TwoValues
Rps_Value::send1(Rps_CallFrame*callerframe, const Rps_ObjectRef obselarg,
Rps_Value arg0) const
{
//RPS_ASSERT(callerframe && callerframe->stored_type() == Rps_Type::CallFrame);
RPS_ASSERT_CALLFRAME (callerframe);
RPS_LOCALFRAME(RPS_ROOT_OB(_5yQcFbU0seU018B48Z), // `message_sending` symbol
callerframe,
Rps_Value selfv; // the receiver
Rps_ClosureValue closv; // the closure
Rps_ObjectRef obsel; // the selector
Rps_Value arg0v; // the argument#0
);
_f.selfv = Rps_Value(*this);
_f.obsel = obselarg;
_f.arg0v = arg0;
RPS_DEBUG_LOG(MSGSEND, "send1 selfv=" << _f.selfv
<< " of class:" << _f.selfv.compute_class(&_)
<< ", obsel=" << _f.obsel
<< ", arg0v=" << _f.arg0v);
_f.closv = _f.selfv.closure_for_method_selector(&_,_f.obsel);
RPS_DEBUG_LOG(MSGSEND, "send1 selfv=" << _f.selfv
<< ", closv=" << _f.closv);
if (_f.closv.is_closure())
return _f.closv.apply2(&_, _f.selfv, _f.arg0v);
else
RPS_DEBUG_LOG(MSGSEND, "send1 applying selfv=" << _f.selfv
<< " of class:" << _f.selfv.compute_class(&_) << std::endl
<< "… with obsel=" << _f.obsel
<< " of class:" << _f.obsel->compute_class(&_) << std::endl
<< ".... non closure closv=" << _f.closv);
return Rps_TwoValues(nullptr,nullptr);
} // end Rps_Value::send1
Rps_TwoValues
Rps_Value::send2(Rps_CallFrame*callerframe, const Rps_ObjectRef obselarg,
Rps_Value arg0, const Rps_Value arg1) const
{
//RPS_ASSERT(callerframe && callerframe->stored_type() == Rps_Type::CallFrame);
RPS_ASSERT_CALLFRAME (callerframe);
//RPS_ASSERT(callerframe && callerframe->stored_type() == Rps_Type::CallFrame);
RPS_LOCALFRAME(RPS_ROOT_OB(_5yQcFbU0seU018B48Z), // `message_sending` symbol
callerframe,
Rps_Value selfv; // the receiver
Rps_ClosureValue closv; // the closure
Rps_ObjectRef obsel; // the selector
Rps_Value arg0v; // the argument#0
Rps_Value arg1v; // the argument#1
);
_f.selfv = Rps_Value(*this);
_f.obsel = obselarg;
_f.arg0v = arg0;
_f.arg1v = arg1;
RPS_DEBUG_LOG(MSGSEND, "send2 selfv=" << _f.selfv
<< " of class:" << _f.selfv.compute_class(&_)
<< ", obsel=" << _f.obsel
<< ", arg0v=" << _f.arg0v
<< ", arg1v=" << _f.arg1v);
_f.closv = _f.selfv.closure_for_method_selector(&_,_f.obsel);
RPS_DEBUG_LOG(MSGSEND, "send2 selfv=" << _f.selfv
<< ", obsel=" << _f.obsel
<< ", closv=" << _f.closv);
if (_f.closv.is_closure())
{
RPS_DEBUG_LOG(MSGSEND, "send2 applying to selfv=" << _f.selfv
<< " of class:" << _f.selfv.compute_class(&_) << std::endl
<< "… obsel=" << _f.obsel
<< ", closv=" << _f.closv
<< ", arg0v=" << _f.arg0v
<< ", arg1v=" << _f.arg1v);
return _f.closv.apply3(&_, _f.selfv, _f.arg0v, _f.arg1v);
}
else
RPS_DEBUG_LOG(MSGSEND, "send2 applying selfv=" << _f.selfv
<< " of class:" << _f.selfv.compute_class(&_) << std::endl
<< "… with obsel=" << _f.obsel
<< " of class:" << _f.obsel->compute_class(&_) << std::endl
<< ".... non closure closv=" << _f.closv);
return Rps_TwoValues(nullptr,nullptr);
} // end Rps_Value::send2
Rps_TwoValues
Rps_Value::send3(Rps_CallFrame*callerframe, const Rps_ObjectRef obselarg,
const Rps_Value arg0, const Rps_Value arg1, const Rps_Value arg2) const
{
//RPS_ASSERT(callerframe && callerframe->stored_type() == Rps_Type::CallFrame);
RPS_ASSERT_CALLFRAME (callerframe);
RPS_LOCALFRAME(RPS_ROOT_OB(_5yQcFbU0seU018B48Z), // `message_sending` symbol
callerframe,
Rps_Value selfv; // the receiver
Rps_ClosureValue closv; // the closure
Rps_ObjectRef obsel; // the selector
Rps_Value arg0v; // the argument#0
Rps_Value arg1v; // the argument#1
Rps_Value arg2v; // the argument#2
);
_f.selfv = Rps_Value(*this);
_f.obsel = obselarg;
_f.arg0v = arg0;
_f.arg1v = arg1;
_f.arg2v = arg2;
RPS_DEBUG_LOG(MSGSEND, "send3 selfv=" << _f.selfv
<< " of class:" << _f.selfv.compute_class(&_)
<< ", obsel=" << _f.obsel
<< ", arg0v=" << _f.arg0v
<< ", arg1v=" << _f.arg1v
<< ", arg2v=" << _f.arg2v);
_f.closv = _f.selfv.closure_for_method_selector(&_,_f.obsel);
RPS_DEBUG_LOG(MSGSEND, "send3 selfv=" << _f.selfv
<< ", obsel=" << _f.obsel
<< ", closv=" << _f.closv);
if (_f.closv.is_closure())
return _f.closv.apply4(&_, _f.selfv, _f.arg0v, _f.arg1v, _f.arg2v);
else
RPS_DEBUG_LOG(MSGSEND, "send3 applying selfv=" << _f.selfv
<< " of class:" << _f.selfv.compute_class(&_) << std::endl
<< "… with obsel=" << _f.obsel
<< " of class:" << _f.obsel->compute_class(&_) << std::endl
<< ".... non closure closv=" << _f.closv);
return Rps_TwoValues(nullptr,nullptr);
} // end Rps_Value::send3
Rps_TwoValues
Rps_Value::send4(Rps_CallFrame*callerframe, const Rps_ObjectRef obselarg,
const Rps_Value arg0, const Rps_Value arg1,
const Rps_Value arg2, const Rps_Value arg3) const
{
//RPS_ASSERT(callerframe && callerframe->stored_type() == Rps_Type::CallFrame);
RPS_ASSERT_CALLFRAME (callerframe);
RPS_LOCALFRAME(RPS_ROOT_OB(_5yQcFbU0seU018B48Z), // `message_sending` symbol
callerframe,
Rps_Value selfv; // the receiver
Rps_ClosureValue closv; // the closure
Rps_ObjectRef obsel; // the selector
Rps_Value arg0v; // the argument#0
Rps_Value arg1v; // the argument#1
Rps_Value arg2v; // the argument#2
Rps_Value arg3v; // the argument#3
);
_f.selfv = Rps_Value(*this);
_f.obsel = obselarg;
_f.arg0v = arg0;
_f.arg1v = arg1;
_f.arg2v = arg2;
_f.arg3v = arg3;
RPS_DEBUG_LOG(MSGSEND, "send4 selfv=" << _f.selfv
<< " of class:" << _f.selfv.compute_class(&_)
<< ", obsel=" << _f.obsel
<< ", arg0v=" << _f.arg0v
<< ", arg1v=" << _f.arg1v
<< ", arg2v=" << _f.arg2v
<< ", arg3v=" << _f.arg3v);
_f.closv = _f.selfv.closure_for_method_selector(&_,_f.obsel);
RPS_DEBUG_LOG(MSGSEND, "send4 selfv=" << _f.selfv
<< ", obsel=" << _f.obsel
<< ", closv=" << _f.closv);
if (_f.closv.is_closure())
return _f.closv.apply5(&_, _f.selfv, _f.arg0v, _f.arg1v, _f.arg2v, _f.arg3v);
else
RPS_DEBUG_LOG(MSGSEND, "send4 applying selfv=" << _f.selfv
<< " of class:" << _f.selfv.compute_class(&_) << std::endl
<< "… with obsel=" << _f.obsel
<< " of class:" << _f.obsel->compute_class(&_) << std::endl
<< ".... non closure closv=" << _f.closv);
return Rps_TwoValues(nullptr,nullptr);
} // end Rps_Value::send4
Rps_TwoValues