forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjects_rps.cc
2384 lines (2185 loc) · 78.7 KB
/
objects_rps.cc
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 objects_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
*
* Low-level implementation of objects.
*
* Author(s):
* Basile Starynkevitch <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright 2019 - 2022 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_objects_gitid[];
const char rps_objects_gitid[]= RPS_GITID;
extern "C" const char rps_objects_date[];
const char rps_objects_date[]= __DATE__;
std::unordered_map<Rps_Id,Rps_ObjectZone*,Rps_Id::Hasher> Rps_ObjectZone::ob_idmap_(50777);
std::map<Rps_Id,Rps_ObjectZone*> Rps_ObjectZone::ob_idbucketmap_[Rps_Id::maxbuckets];
std::recursive_mutex Rps_ObjectZone::ob_idmtx_;
// build an object from its existing string oid, or else fail with C++ exception
Rps_ObjectRef::Rps_ObjectRef(Rps_CallFrame*callerframe, const char*oidstr, Rps_ObjIdStrTag)
{
if (!oidstr)
throw RPS_RUNTIME_ERROR_OUT("Rps_ObjectRef: null oidstr");
RPS_DEBUG_LOG(LOWREP, "Rps_ObjectRef oidstr=" << oidstr
<< " from " << Rps_ShowCallFrame(callerframe));
const char*end = nullptr;
bool ok=false;
Rps_Id oid(oidstr,&end,&ok);
if (!end || *end)
throw RPS_RUNTIME_ERROR_OUT("Rps_ObjectRef: invalid compile-time oidstr=" << oidstr);
*this = find_object_or_fail_by_oid(callerframe, oid);
} // end Rps_ObjectRef::Rps_ObjectRef(Rps_CallFrame*, constexpr const char*oidstr, Rps_ObjIdStrTag)
void
Rps_ObjectRef::output(std::ostream&outs) const
{
if (is_empty())
outs << "__";
else
{
Rps_Value valname = obptr()->get_physical_attr(RPS_ROOT_OB(_1EBVGSfW2m200z18rx)); //name
outs << "◌" /*U+25CC DOTTED CIRCLE*/
<< obptr()->oid().to_string();
if (valname.is_string())
{
outs << "/" << valname.as_cstring();
}
else if (auto symbpayl = obptr()-> get_dynamic_payload<Rps_PayloadSymbol>())
{
outs << "!sy°" << symbpayl->symbol_name();
}
else if (auto classpayl = obptr()-> get_dynamic_payload<Rps_PayloadClassInfo>())
{
outs << "!cla°" << classpayl->class_name_str();
}
};
} // end Rps_ObjectRef::output
const std::string
Rps_ObjectRef::as_string(void) const
{
if (_optr == nullptr)
return std::string{"__"};
else if (_optr == RPS_EMPTYSLOT)
return std::string{"_⁂_"}; //U+2042 ASTERISM
std::lock_guard<std::recursive_mutex> gu(*_optr->objmtxptr());
if (const Rps_PayloadSymbol*symbpayl = _optr->get_dynamic_payload<Rps_PayloadSymbol>())
{
const std::string& syna = symbpayl->symbol_name();
if (!syna.empty())
return std::string{"¤" /*U+00A4 CURRENCY SIGN*/} + syna;
}
else if (const Rps_PayloadClassInfo*classpayl = _optr->get_dynamic_payload<Rps_PayloadClassInfo>())
{
const std::string clana = classpayl->class_name_str();
if (!clana.empty())
return std::string{"∋" /*U+220B CONTAINS AS MEMBER*/} + clana;
}
else if (const Rps_Value namval
= _optr->get_physical_attr(RPS_ROOT_OB(_4FBkYDlynyC02QtkfG)) /*"name"∈named_attribute*/)
{
if (namval.is_string())
{
std::string str {"⁑" /*U+2051 TWO ASTERISKS ALIGNED VERTICALLY*/};
str.append (namval.as_cppstring());
return str;
}
}
const Rps_Id curoid = _optr->oid();
return curoid.to_string();
} // end Rps_ObjectRef::as_string
const std::string
Rps_ObjectZone::payload_type_name(void) const
{
Rps_Payload* payl = get_payload();
if (!payl)
return "*no-payload*";
if ((void*)payl == RPS_EMPTYSLOT)
return "*empty-payload*";
return payl-> payload_type_name();
} // end Rps_ObjectZone::payload_type_name
void
Rps_ObjectZone::val_output(std::ostream&out, unsigned int depth) const
{
out << oid().to_string();
if (depth<2)
{
std::lock_guard<std::recursive_mutex> gu(ob_idmtx_);
out << "⟦"; // U+27E6 MATHEMATICAL LEFT WHITE SQUARE BRACKET
auto namit = ob_attrs.find(RPS_ROOT_OB(_1EBVGSfW2m200z18rx)); //name∈named_attribute);
if (namit != ob_attrs.end())
{
Rps_Value namv = namit->second;
if (namv.is_string())
{
out << "⏵"; // U+23F5 BLACK MEDIUM RIGHT-POINTING TRIANGLE
out << namv.as_cstring();
}
}
auto obcl = ob_class.load();
if (obcl)
{
auto obclpayl = obcl->get_dynamic_payload<Rps_PayloadClassInfo>();
if (obclpayl)
{
out << "∈"; //U+2208 ELEMENT OF
out << obclpayl->class_name_str();
}
};
out << "⟧"; // U+27E7 MATHEMATICAL RIGHT WHITE SQUARE BRACKET
};
} // end Rps_ObjectZone::val_output
void
Rps_ObjectZone::register_objzone(Rps_ObjectZone*obz)
{
RPS_ASSERT(obz != nullptr);
std::lock_guard<std::recursive_mutex> gu(ob_idmtx_);
auto oid = obz->oid();
RPS_DEBUG_LOG(LOWREP, "register_objzone obz=" << obz << " oid=" << oid
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "register_objzone"));
if (ob_idmap_.find(oid) != ob_idmap_.end())
RPS_FATALOUT("Rps_ObjectZone::register_objzone duplicate oid " << oid);
ob_idmap_.insert({oid,obz});
ob_idbucketmap_[oid.bucket_num()].insert({oid,obz});
} // end Rps_ObjectZone::register_objzone
Rps_Id
Rps_ObjectZone::fresh_random_oid(Rps_ObjectZone*obz)
{
Rps_Id oid;
std::lock_guard<std::recursive_mutex> gu(ob_idmtx_);
while(true)
{
oid = Rps_Id::random();
if (RPS_UNLIKELY(ob_idmap_.find(oid) != ob_idmap_.end()))
continue;
if (obz)
ob_idmap_.insert({oid,obz});
RPS_DEBUG_LOG(LOWREP, "Rps_ObjectZone::fresh_random_oid obz=" << obz
<< " -> oid=" << oid);
return oid;
}
}
Rps_ObjectZone::Rps_ObjectZone(Rps_Id oid, registermode_en regmod)
: Rps_ZoneValue(Rps_Type::Object),
ob_oid(oid), ob_mtx(), ob_class(nullptr),
ob_space(nullptr), ob_mtime(0.0),
ob_attrs(), ob_comps(), ob_payload(nullptr),
ob_magicgetterfun(nullptr),
ob_applyingfun(nullptr)
{
RPS_DEBUG_LOG(LOWREP, "Rps_ObjectZone oid=" << oid << ' '
<< (regmod==OBZ_DONT_REGISTER?"non-":"") << "registering"
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(2, "Rps_ObjectZone")
<< std::endl);
if (regmod == OBZ_REGISTER)
{
register_objzone(this);
}
// In principle, the below initialization of ob_class should be
// useless, because it should be done elsewhere. In practice, we
// need it and prefer to initialize ob_class several times instead
// of none.
///////
// Every object should have a class, initially `object`; the
// ob_class can later be replaced, but we need something which is
// not null.... That atomic field could be later overwritten.
ob_class.store(RPS_ROOT_OB(_5yhJGgxLwLp00X0xEQ)); //object∈class
} // end Rps_ObjectZone::Rps_ObjectZone
Rps_ObjectZone::~Rps_ObjectZone()
{
// RPS_INFORMOUT("destroying object " << oid());
Rps_Id curid = oid();
clear_payload();
ob_attrs.clear();
ob_comps.clear();
ob_class.store(nullptr);
ob_mtime.store(0.0);
std::lock_guard<std::recursive_mutex> gu(ob_idmtx_);
RPS_DEBUG_LOG(LOWREP,"~Rps_ObjectZone curid=" << curid << " this=" << this);
ob_idmap_.erase(curid);
ob_idbucketmap_[curid.bucket_num()].erase(curid);
} // end Rps_ObjectZone::~Rps_ObjectZone()
Rps_ObjectZone::Rps_ObjectZone() :
Rps_ObjectZone::Rps_ObjectZone(fresh_random_oid(this),
Rps_ObjectZone::OBZ_DONT_REGISTER)
{
RPS_DEBUG_LOG(LOWREP, "Rps_ObjectZone this=" << this
<< " oid=" << oid());
} // end Rps_ObjectZone::Rps_ObjectZone
void
Rps_ObjectZone::put_applying_function(rps_applyingfun_t*afun)
{
auto oldappfun = ob_applyingfun.exchange(afun);
if (oldappfun)
{
RPS_WARNOUT("Rps_ObjectZone::put_applying_function for oid=" << oid()
<< " did overwrite applying function to " << afun
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "put_applying_function"));
};
} // end Rps_ObjectZone::put_applying_function
Rps_ObjectZone*
Rps_ObjectZone::make(void)
{
Rps_Id oid = fresh_random_oid(nullptr);
RPS_DEBUG_LOG(LOWREP, "Rps_ObjectZone::make start oid=" << oid
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_ObjectZone::make start"));
Rps_ObjectZone*obz= Rps_QuasiZone::rps_allocate<Rps_ObjectZone,Rps_Id,registermode_en>(oid,OBZ_REGISTER);
*(const_cast<Rps_Id*>(&obz->ob_oid)) = oid;
double rtime = rps_wallclock_real_time();
obz->ob_mtime.store(rtime);
RPS_DEBUG_LOG(LOWREP, "Rps_ObjectZone::make oid=" << oid
<< " obz=" << obz << " mtime=" << rtime);
// Every object should have a class, initially `object`; the
// ob_class can later be replaced, but we need something which is
// not null.... That atomic field could be later overwritten.
obz->ob_class.store(RPS_ROOT_OB(_5yhJGgxLwLp00X0xEQ)); //object∈class
RPS_DEBUG_LOG(LOWREP, "Rps_ObjectZone::make oid=" << oid << " obz=" << obz
<< std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "Rps_ObjectZone::make"));
return obz;
} // end Rps_ObjectZone::make
Rps_ObjectZone*
Rps_ObjectZone::make_loaded(Rps_Id oid, Rps_Loader* ld)
{
#warning Rps_ObjectZone::make_loaded might be incomplete
RPS_DEBUG_LOG(LOAD, "make_loaded oid="<< oid);
RPS_ASSERT(oid.valid());
RPS_ASSERT(ld != nullptr);
Rps_ObjectZone*obz= Rps_QuasiZone::rps_allocate<Rps_ObjectZone,Rps_Id,registermode_en>(oid, OBZ_REGISTER);
// Every object should have a class, initially `object`; the
// ob_class can later be replaced, but we need something which is
// not null.... The loader could later overwrite that.
obz->ob_class.store(RPS_ROOT_OB(_5yhJGgxLwLp00X0xEQ)); //object∈class
return obz;
} // end Rps_ObjectZone::make_loaded
Rps_ObjectZone*
Rps_ObjectZone::find(Rps_Id oid)
{
if (!oid.valid())
return nullptr;
std::lock_guard<std::recursive_mutex> gu(ob_idmtx_);
auto obr = ob_idmap_.find(oid);
if (obr != ob_idmap_.end())
return obr->second;
return nullptr;
} // end Rps_ObjectZone::find
void
Rps_ObjectZone::gc_mark(Rps_GarbageCollector&gc, unsigned) const
{
if (is_gcmarked(gc)) return;
std::lock_guard<std::recursive_mutex> gu(ob_mtx);
gc.mark_obj(this);
const_cast<Rps_ObjectZone*>(this)->set_gcmark(gc);
} // end of Rps_ObjectZone::gc_mark
void
Rps_ObjectZone::mark_gc_inside(Rps_GarbageCollector&gc)
{
std::lock_guard<std::recursive_mutex> gu(ob_mtx);
#warning perhaps the _gcinfo should be used here
Rps_ObjectZone* obcla = ob_class.load();
RPS_ASSERT(obcla != nullptr);
gc.mark_obj(obcla);
for (auto atit: ob_attrs)
{
gc.mark_obj(atit.first);
if (atit.second.is_ptr())
gc.mark_value(atit.second);
}
for (auto compv: ob_comps)
{
if (compv.is_ptr())
gc.mark_value(compv);
};
Rps_Payload*payl = ob_payload.load();
if (payl && payl->owner() == this)
payl->gc_mark(gc);
} // end Rps_ObjectZone::mark_gc_inside
void
Rps_ObjectZone::put_space(Rps_ObjectRef obr)
{
if (obr)
{
if (obr->get_class() != RPS_ROOT_OB(_2i66FFjmS7n03HNNBx))
throw std::runtime_error("invalid space object");
};
ob_space.store(obr);
ob_mtime.store(rps_wallclock_real_time());
} // end Rps_ObjectZone::put_space
void
Rps_ObjectZone::remove_attr(const Rps_ObjectRef obattr)
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (obattr.is_empty() || obattr->stored_type() != Rps_Type::Object)
return;
rps_magicgetterfun_t*getfun = obattr->ob_magicgetterfun.load();
{
if (RPS_UNLIKELY(getfun))
throw RPS_RUNTIME_ERROR_OUT("cannot remove magic attribute " << obattr
<< " in " << Rps_ObjectRef(this));
}
std::lock_guard<std::recursive_mutex> gu(ob_mtx);
ob_attrs.erase(obattr);
ob_mtime.store(rps_wallclock_real_time());
} // end Rps_ObjectZone::remove_attr
Rps_Value
Rps_ObjectZone::set_of_attributes([[maybe_unused]] Rps_CallFrame*stkf) const
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
std::lock_guard<std::recursive_mutex> gu(ob_mtx);
unsigned nbat = ob_attrs.size();
std::vector<Rps_ObjectRef> vecat;
vecat.reserve(nbat);
for (auto it : ob_attrs)
vecat.push_back(it.first);
return Rps_SetValue(vecat);
} // end of Rps_ObjectZone::set_of_attributes
unsigned
Rps_ObjectZone::nb_attributes([[maybe_unused]] Rps_CallFrame*stkf) const
{
std::lock_guard<std::recursive_mutex> gu(ob_mtx);
unsigned nbat = ob_attrs.size();
return nbat;
} // end Rps_ObjectZone::nb_attributes
Rps_Value
Rps_ObjectZone::get_attr1(Rps_CallFrame*stkf,const Rps_ObjectRef obattr0) const
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (obattr0.is_empty() || obattr0->stored_type() != Rps_Type::Object)
return nullptr;
Rps_Value val0;
std::lock_guard<std::recursive_mutex> gu(ob_mtx);
{
rps_magicgetterfun_t*getfun0 = obattr0->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun0))
val0 = (*getfun0)(stkf, *this, obattr0);
else
{
auto it0 = ob_attrs.find(obattr0);
if (it0 != ob_attrs.end())
val0 = it0->second;
}
}
return val0;
} // end Rps_ObjectZone::get_attr1
Rps_Value
Rps_ObjectZone::get_physical_attr(const Rps_ObjectRef obattr0) const
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (obattr0.is_empty() || obattr0->stored_type() != Rps_Type::Object)
return nullptr;
Rps_Value val0;
std::lock_guard<std::recursive_mutex> gu(ob_mtx);
auto it0 = ob_attrs.find(obattr0);
if (it0 != ob_attrs.end())
val0 = it0->second;
return val0;
} // end Rps_ObjectZone::get_physical_attr
Rps_TwoValues
Rps_ObjectZone::get_attr2(Rps_CallFrame*stkf, const Rps_ObjectRef obattr0, const Rps_ObjectRef obattr1) const
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (obattr0.is_empty() || obattr0->stored_type() != Rps_Type::Object)
return Rps_TwoValues(nullptr,nullptr);
if (obattr1.is_empty() || obattr1->stored_type() != Rps_Type::Object)
return Rps_TwoValues(nullptr,nullptr);
Rps_Value val0;
Rps_Value val1;
std::lock_guard<std::recursive_mutex> gu(ob_mtx);
{
rps_magicgetterfun_t*getfun0 = obattr0->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun0))
val0 = (*getfun0)(stkf, *this, obattr0);
else
{
auto it0 = ob_attrs.find(obattr0);
if (it0 != ob_attrs.end())
val0 = it0->second;
}
}
{
rps_magicgetterfun_t*getfun1 = obattr1->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun1))
val0 = (*getfun1)(stkf, *this, obattr1);
else
{
auto it1 = ob_attrs.find(obattr1);
if (it1 != ob_attrs.end())
val1 = it1->second;
}
}
return Rps_TwoValues(val0, val1);
} // end Rps_ObjectZone::get_attr2
void
Rps_ObjectZone::put_attr(const Rps_ObjectRef obattr, const Rps_Value valattr)
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (obattr.is_empty() || obattr->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun = obattr->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr
<< " in " << Rps_ObjectRef(this));
}
std::lock_guard gu(ob_mtx);
if (valattr.is_empty())
ob_attrs.erase(obattr);
else
ob_attrs.insert({obattr, valattr});
ob_mtime.store(rps_wallclock_real_time());
} // end Rps_ObjectZone::put_attr
void
Rps_ObjectZone::put_attr2(const Rps_ObjectRef obattr0, const Rps_Value valattr0,
const Rps_ObjectRef obattr1, const Rps_Value valattr1)
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (obattr0.is_empty() || obattr0->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun0 = obattr0->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun0))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr0
<< " in " << Rps_ObjectRef(this));
}
if (obattr1.is_empty() || obattr1->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun1 = obattr1->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun1))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr1
<< " in " << Rps_ObjectRef(this));
}
std::lock_guard gu(ob_mtx);
if (valattr0.is_empty())
ob_attrs.erase(obattr0);
else
ob_attrs.insert({obattr0, valattr0});
if (valattr1.is_empty())
ob_attrs.erase(obattr1);
else
ob_attrs.insert({obattr1, valattr1});
ob_mtime.store(rps_wallclock_real_time());
} // end Rps_ObjectZone::put_attr2
void
Rps_ObjectZone::put_attr3(const Rps_ObjectRef obattr0, const Rps_Value valattr0,
const Rps_ObjectRef obattr1, const Rps_Value valattr1,
const Rps_ObjectRef obattr2, const Rps_Value valattr2)
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (obattr0.is_empty() || obattr0->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun0 = obattr0->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun0))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr0
<< " in " << Rps_ObjectRef(this));
}
if (obattr1.is_empty() || obattr1->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun1 = obattr1->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun1))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr1
<< " in " << Rps_ObjectRef(this));
}
if (obattr2.is_empty() || obattr2->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun2 = obattr2->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun2))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr2
<< " in " << Rps_ObjectRef(this));
}
std::lock_guard gu(ob_mtx);
if (valattr0.is_empty())
ob_attrs.erase(obattr0);
else
ob_attrs.insert({obattr0, valattr0});
if (valattr1.is_empty())
ob_attrs.erase(obattr1);
else
ob_attrs.insert({obattr1, valattr1});
if (valattr2.is_empty())
ob_attrs.erase(obattr2);
else
ob_attrs.insert({obattr2, valattr2});
ob_mtime.store(rps_wallclock_real_time());
} // end Rps_ObjectZone::put_attr3
void
Rps_ObjectZone::put_attr4(const Rps_ObjectRef obattr0, const Rps_Value valattr0,
const Rps_ObjectRef obattr1, const Rps_Value valattr1,
const Rps_ObjectRef obattr2, const Rps_Value valattr2,
const Rps_ObjectRef obattr3, const Rps_Value valattr3)
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (obattr0.is_empty() || obattr0->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun0 = obattr0->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun0))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr0
<< " in " << Rps_ObjectRef(this));
}
if (obattr1.is_empty() || obattr1->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun1 = obattr1->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun1))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr1
<< " in " << Rps_ObjectRef(this));
}
if (obattr2.is_empty() || obattr2->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun2 = obattr2->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun2))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr2
<< " in " << Rps_ObjectRef(this));
}
if (obattr3.is_empty() || obattr3->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun3 = obattr3->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun3))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr3
<< " in " << Rps_ObjectRef(this));
}
std::lock_guard gu(ob_mtx);
if (valattr0.is_empty())
ob_attrs.erase(obattr0);
else
ob_attrs.insert({obattr0, valattr0});
if (valattr1.is_empty())
ob_attrs.erase(obattr1);
else
ob_attrs.insert({obattr1, valattr1});
if (valattr2.is_empty())
ob_attrs.erase(obattr2);
else
ob_attrs.insert({obattr2, valattr2});
if (valattr3.is_empty())
ob_attrs.erase(obattr3);
else
ob_attrs.insert({obattr3, valattr3});
ob_mtime.store(rps_wallclock_real_time());
} // end Rps_ObjectZone::put_attr4
void
Rps_ObjectZone::exchange_attr(const Rps_ObjectRef obattr, const Rps_Value valattr, Rps_Value*poldval)
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (obattr.is_empty() || obattr->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun = obattr->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr
<< " in " << Rps_ObjectRef(this));
}
std::lock_guard gu(ob_mtx);
Rps_Value oldval;
if (poldval)
{
auto it = ob_attrs.find(obattr);
if (it != ob_attrs.end())
oldval = it->second;
}
if (valattr.is_empty())
ob_attrs.erase(obattr);
else
ob_attrs.insert({obattr, valattr});
if (poldval)
*poldval = oldval;
ob_mtime.store(rps_wallclock_real_time());
} // end Rps_ObjectZone::exchange_attr
void
Rps_ObjectZone::exchange_attr2(const Rps_ObjectRef obattr0, const Rps_Value valattr0, Rps_Value*poldval0,
const Rps_ObjectRef obattr1, const Rps_Value valattr1, Rps_Value*poldval1)
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (obattr0.is_empty() || obattr0->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun0 = obattr0->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun0))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr0
<< " in " << Rps_ObjectRef(this));
}
if (obattr1.is_empty() || obattr1->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun1 = obattr1->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun1))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr1
<< " in " << Rps_ObjectRef(this));
}
std::lock_guard gu(ob_mtx);
Rps_Value oldval0;
Rps_Value oldval1;
if (poldval0)
{
auto it = ob_attrs.find(obattr0);
if (it != ob_attrs.end())
oldval0 = it->second;
}
if (poldval1)
{
auto it = ob_attrs.find(obattr1);
if (it != ob_attrs.end())
oldval1 = it->second;
}
if (valattr0.is_empty())
ob_attrs.erase(obattr0);
else
ob_attrs.insert({obattr0, valattr0});
if (valattr1.is_empty())
ob_attrs.erase(obattr1);
else
ob_attrs.insert({obattr1, valattr1});
if (poldval0)
*poldval0 = oldval0;
if (poldval1)
*poldval1 = oldval1;
ob_mtime.store(rps_wallclock_real_time());
} // end Rps_ObjectZone::exchange_attr2
void
Rps_ObjectZone::exchange_attr3(const Rps_ObjectRef obattr0, const Rps_Value valattr0, Rps_Value*poldval0,
const Rps_ObjectRef obattr1, const Rps_Value valattr1, Rps_Value*poldval1,
const Rps_ObjectRef obattr2, const Rps_Value valattr2, Rps_Value*poldval2)
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (obattr0.is_empty() || obattr0->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun0 = obattr0->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun0))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr0
<< " in " << Rps_ObjectRef(this));
}
if (obattr1.is_empty() || obattr1->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun1 = obattr1->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun1))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr1
<< " in " << Rps_ObjectRef(this));
}
if (obattr2.is_empty() || obattr2->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun2 = obattr2->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun2))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr2
<< " in " << Rps_ObjectRef(this));
}
std::lock_guard gu(ob_mtx);
Rps_Value oldval0;
Rps_Value oldval1;
Rps_Value oldval2;
if (poldval0)
{
auto it = ob_attrs.find(obattr0);
if (it != ob_attrs.end())
oldval0 = it->second;
}
if (poldval1)
{
auto it = ob_attrs.find(obattr1);
if (it != ob_attrs.end())
oldval1 = it->second;
}
if (poldval2)
{
auto it = ob_attrs.find(obattr2);
if (it != ob_attrs.end())
oldval2 = it->second;
}
if (valattr0.is_empty())
ob_attrs.erase(obattr0);
else
ob_attrs.insert({obattr0, valattr0});
if (valattr1.is_empty())
ob_attrs.erase(obattr1);
else
ob_attrs.insert({obattr1, valattr1});
if (valattr2.is_empty())
ob_attrs.erase(obattr2);
else
ob_attrs.insert({obattr2, valattr2});
if (poldval0)
*poldval0 = oldval0;
if (poldval1)
*poldval1 = oldval1;
if (poldval2)
*poldval1 = oldval2;
ob_mtime.store(rps_wallclock_real_time());
} // end Rps_ObjectZone::exchange_attr3
void
Rps_ObjectZone::exchange_attr4(const Rps_ObjectRef obattr0, const Rps_Value valattr0, Rps_Value*poldval0,
const Rps_ObjectRef obattr1, const Rps_Value valattr1, Rps_Value*poldval1,
const Rps_ObjectRef obattr2, const Rps_Value valattr2, Rps_Value*poldval2,
const Rps_ObjectRef obattr3, const Rps_Value valattr3, Rps_Value*poldval3)
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (obattr0.is_empty() || obattr0->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun0 = obattr0->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun0))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr0
<< " from " << Rps_ObjectRef(this));
}
if (obattr1.is_empty() || obattr1->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun1 = obattr1->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun1))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr1
<< " from " << Rps_ObjectRef(this));
}
if (obattr2.is_empty() || obattr2->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun2 = obattr2->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun2))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr2
<< " from " << Rps_ObjectRef(this));
}
if (obattr3.is_empty() || obattr3->stored_type() != Rps_Type::Object)
return;
{
rps_magicgetterfun_t*getfun3 = obattr3->ob_magicgetterfun.load();
if (RPS_UNLIKELY(getfun3))
throw RPS_RUNTIME_ERROR_OUT("cannot put magic attribute " << obattr3
<< " from " << Rps_ObjectRef(this));
}
std::lock_guard gu(ob_mtx);
Rps_Value oldval0;
Rps_Value oldval1;
Rps_Value oldval2;
Rps_Value oldval3;
if (poldval0)
{
auto it = ob_attrs.find(obattr0);
if (it != ob_attrs.end())
oldval0 = it->second;
}
if (poldval1)
{
auto it = ob_attrs.find(obattr1);
if (it != ob_attrs.end())
oldval1 = it->second;
}
if (poldval2)
{
auto it = ob_attrs.find(obattr2);
if (it != ob_attrs.end())
oldval2 = it->second;
}
if (poldval3)
{
auto it = ob_attrs.find(obattr3);
if (it != ob_attrs.end())
oldval3 = it->second;
}
if (valattr0.is_empty())
ob_attrs.erase(obattr0);
else
ob_attrs.insert({obattr0, valattr0});
if (valattr1.is_empty())
ob_attrs.erase(obattr1);
else
ob_attrs.insert({obattr1, valattr1});
if (valattr2.is_empty())
ob_attrs.erase(obattr2);
else
ob_attrs.insert({obattr2, valattr2});
if (valattr3.is_empty())
ob_attrs.erase(obattr3);
else
ob_attrs.insert({obattr3, valattr3});
if (poldval0)
*poldval0 = oldval0;
if (poldval1)
*poldval1 = oldval1;
if (poldval2)
*poldval1 = oldval2;
if (poldval3)
*poldval1 = oldval3;
ob_mtime.store(rps_wallclock_real_time());
} // end Rps_ObjectZone::exchange_attr4
//////////////// components
unsigned
Rps_ObjectZone::nb_components([[maybe_unused]] Rps_CallFrame*stkf) const
{
std::lock_guard<std::recursive_mutex> gu(ob_mtx);
unsigned nbcomp = ob_comps.size();
return nbcomp;
} // end Rps_ObjectZone::nb_components
Rps_Value
Rps_ObjectZone::component_at ([[maybe_unused]] Rps_CallFrame*stkf, int rk, bool dontfail) const
{
std::lock_guard<std::recursive_mutex> gu(ob_mtx);
unsigned nbcomp = ob_comps.size();
if (rk<0) rk += nbcomp;
if (rk>=0 && rk<(int)nbcomp)
return ob_comps[rk];
if (dontfail)
return nullptr;
throw std::range_error("Rps_ObjectZone::component_at index out of range");
} // end Rps_ObjectZone::component_at
void
Rps_ObjectZone::append_comp1(Rps_Value comp0)
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (RPS_UNLIKELY(comp0.is_empty()))
comp0.clear();
std::lock_guard gu(ob_mtx);
ob_comps.push_back(comp0);
} // end Rps_ObjectZone::append_comp1
void
Rps_ObjectZone::append_comp2(Rps_Value comp0, Rps_Value comp1)
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (RPS_UNLIKELY(comp0.is_empty()))
comp0.clear();
if (RPS_UNLIKELY(comp1.is_empty()))
comp1.clear();
std::lock_guard gu(ob_mtx);
// we want to avoid too frequent resizes, so....
if (RPS_UNLIKELY(ob_comps.capacity() < ob_comps.size() + 2))
{
auto newsiz = rps_prime_above(9*ob_comps.size()/8 + 2);
ob_comps.reserve(newsiz);
};
ob_comps.push_back(comp0);
ob_comps.push_back(comp1);
} // end Rps_ObjectZone::append_comp2
void
Rps_ObjectZone::append_comp3(Rps_Value comp0, Rps_Value comp1, Rps_Value comp2)
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (RPS_UNLIKELY(comp0.is_empty()))
comp0.clear();
if (RPS_UNLIKELY(comp1.is_empty()))
comp1.clear();
if (RPS_UNLIKELY(comp2.is_empty()))
comp2.clear();
std::lock_guard<std::recursive_mutex> gu(ob_mtx);
// we want to avoid too frequent resizes, so....
if (RPS_UNLIKELY(ob_comps.capacity() < ob_comps.size() + 3))
{
auto newsiz = rps_prime_above(9*ob_comps.size()/8 + 3);
ob_comps.reserve(newsiz);
};
ob_comps.push_back(comp0);
ob_comps.push_back(comp1);
ob_comps.push_back(comp2);
} // end Rps_ObjectZone::append_comp3
void
Rps_ObjectZone::append_comp4(Rps_Value comp0, Rps_Value comp1, Rps_Value comp2, Rps_Value comp3)
{
RPS_ASSERT(stored_type() == Rps_Type::Object);
if (RPS_UNLIKELY(comp0.is_empty()))
comp0.clear();
if (RPS_UNLIKELY(comp1.is_empty()))
comp1.clear();
if (RPS_UNLIKELY(comp2.is_empty()))
comp2.clear();
if (RPS_UNLIKELY(comp3.is_empty()))
comp3.clear();
std::lock_guard<std::recursive_mutex> gu(ob_mtx);
// we want to avoid too frequent resizes, so....
if (RPS_UNLIKELY(ob_comps.capacity() < ob_comps.size() + 4))
{
auto newsiz = rps_prime_above(9*ob_comps.size()/8 + 4);
ob_comps.reserve(newsiz);
};
ob_comps.push_back(comp0);
ob_comps.push_back(comp1);
ob_comps.push_back(comp2);
ob_comps.push_back(comp3);
} // end Rps_ObjectZone::append_comp4