-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsmallfolk.cpp
More file actions
1351 lines (1233 loc) · 35 KB
/
smallfolk.cpp
File metadata and controls
1351 lines (1233 loc) · 35 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
#include "smallfolk.h"
#include <map>
#include <climits>
#include <iomanip> // std::setprecision
#include <sstream> // std::stringstream
#include <cmath> // std::floor, std::isfinite
#include <cstdlib> // std::strtod
#include <cstdio> // std::snprintf
#include <cstring> // std::strcmp
#include <limits>
#include <stdarg.h> // va_start
#include <functional> // std::hash
#include <mutex>
#include <vector>
namespace
{
LoadLimits g_load_limits;
std::mutex g_load_limits_mutex;
std::string path_segment(LuaVal const & key)
{
if (key.isstring())
return std::string(".") + key.str();
if (key.isnumber())
{
char buffer[32];
std::snprintf(buffer, sizeof(buffer), "[%u]", static_cast<unsigned>(key.num()));
return buffer;
}
return "[?]";
}
std::string format_path(std::initializer_list<LuaVal> const & keys, size_t segment_count)
{
std::string path = "$";
size_t i = 0;
for (LuaVal const & key : keys)
{
if (i >= segment_count)
break;
path += path_segment(key);
++i;
}
return path;
}
void ensure_path_key(LuaVal const & key, char const * api_name)
{
if (key.isnil())
throw smallfolk_exception("using %s with nil key segment", api_name);
}
enum class PathWalkResult
{
Found,
MissingKey,
NotTable,
};
PathWalkResult walk_path_read(
LuaVal const & root,
std::initializer_list<LuaVal> const & keys,
LuaVal const ** out,
size_t * fail_index,
PathWalkResult * fail_reason)
{
LuaVal const * cur = &root;
size_t i = 0;
for (LuaVal const & key : keys)
{
ensure_path_key(key, "path lookup");
if (!cur->istable())
{
if (fail_index)
*fail_index = i;
if (fail_reason)
*fail_reason = PathWalkResult::NotTable;
return PathWalkResult::NotTable;
}
LuaVal const * next = cur->try_get(key);
if (!next)
{
if (fail_index)
*fail_index = i;
if (fail_reason)
*fail_reason = PathWalkResult::MissingKey;
return PathWalkResult::MissingKey;
}
cur = next;
++i;
}
*out = cur;
return PathWalkResult::Found;
}
LoadLimits read_load_limits()
{
std::lock_guard<std::mutex> lock(g_load_limits_mutex);
return g_load_limits;
}
struct ParseContext
{
LoadLimits const & limits;
size_t value_count;
unsigned depth;
void on_value_created()
{
++value_count;
if (value_count > limits.max_value_count)
throw smallfolk_exception(
"load limit exceeded: max value count %zu",
limits.max_value_count);
}
struct DepthGuard
{
ParseContext & ctx;
explicit DepthGuard(ParseContext & c) : ctx(c)
{
++ctx.depth;
if (ctx.depth > ctx.limits.max_nesting_depth)
throw smallfolk_exception(
"load limit exceeded: max nesting depth %u",
ctx.limits.max_nesting_depth);
}
~DepthGuard() { --ctx.depth; }
};
};
void skip_whitespace(std::string const & string, size_t & i)
{
while (i < string.length() && (string[i] == ' ' || string[i] == '\t'))
++i;
}
}
namespace Serializer
{
typedef std::vector<LuaVal> TABLES;
typedef std::unordered_map<LuaVal, unsigned int, LuaVal::LuaValHasher> MEMO;
typedef std::stringstream ACC;
inline bool is_nan_value(double value)
{
return value != value;
}
inline bool is_inf_value(double value)
{
return !is_nan_value(value) && !std::isfinite(value);
}
inline std::string tostring(const double d)
{
char arr[128];
// %.17g matches minimum lua number precision for round-trip.
std::snprintf(arr, sizeof(arr), "%.17g", d);
return arr;
}
inline void append_number_token(ACC & acc, double value)
{
if (is_nan_value(value))
{
std::string const nn = tostring(value);
if (nn.size() >= 4 && nn.compare(0, 4, "-nan") == 0)
acc << 'N';
else
acc << 'Q';
return;
}
if (is_inf_value(value))
{
acc << (value < 0.0 ? 'i' : 'I');
return;
}
char buf[64];
std::snprintf(buf, sizeof(buf), "%.17g", value);
acc << buf;
}
inline std::string tostring(LuaVal::TblPtr const & ptr)
{
char arr[128];
std::snprintf(arr, sizeof(arr), "table: %p", static_cast<void*>(ptr.get()));
return arr;
}
unsigned int dump_type_table(LuaVal const & object, unsigned int nmemo, MEMO& memo, ACC& acc);
unsigned int dump_object(LuaVal const & object, unsigned int nmemo, MEMO& memo, ACC& acc);
std::string escape_quotes(const std::string &before, char quote);
std::string unescape_quotes(const std::string &before, char quote);
bool nonzero_digit(char c);
bool is_digit(char c);
char strat(std::string const & string, std::string::size_type i);
LuaVal expect_number(std::string const & string, size_t& start, ParseContext & ctx);
LuaVal expect_object(std::string const & string, size_t& i, TABLES& tables, ParseContext & ctx);
}
LoadLimits const & LuaVal::default_load_limits()
{
static LoadLimits const defaults;
return defaults;
}
LoadLimits LuaVal::untrusted_load_limits()
{
LoadLimits limits;
limits.max_input_size = 256 * 1024;
limits.max_string_length = 64 * 1024;
limits.max_nesting_depth = 64;
limits.max_value_count = 10000;
limits.max_table_entries = 10000;
limits.require_consumed_input = true;
limits.reject_non_finite_numbers = true;
return limits;
}
LoadLimits LuaVal::get_load_limits()
{
return read_load_limits();
}
void LuaVal::set_load_limits(LoadLimits limits)
{
std::lock_guard<std::mutex> lock(g_load_limits_mutex);
g_load_limits = limits;
}
LuaVal const LuaVal::nil(TNIL);
LuaVal::LuaVal(std::initializer_list<LuaVal> const & l)
: tag(TTABLE), tbl_ptr(new LuaTable()), d(0), b(false)
{
InitializeSequence(l);
}
void LuaVal::InitializeSequence(std::initializer_list<LuaVal> const & l)
{
unsigned int i = 0;
for (auto const & v : l)
{
LuaVal vv(v);
if (vv.isnil())
++i;
else
set(static_cast<int>(++i), std::move(vv));
}
}
LuaVal lua_val::map(std::initializer_list<std::pair<LuaVal, LuaVal>> const & entries)
{
LuaVal value(TTABLE);
for (auto const & entry : entries)
value.set(entry.first, entry.second);
return value;
}
LuaVal LuaVal::merge(LuaVal const & l, LuaVal const & r)
{
LuaVal result = l;
for (auto const & entry : r.tbl())
result.set(entry.first, entry.second);
return result;
}
LuaVal LuaVal::merge(LuaVal && l, LuaVal && r)
{
return merge(std::move(l), static_cast<LuaVal const &>(r));
}
LuaVal LuaVal::merge(LuaVal && l, LuaVal const & r)
{
for (auto const & entry : r.tbl())
l.set(entry.first, entry.second);
return std::move(l);
}
LuaVal LuaVal::merge(LuaVal const & l, LuaVal && r)
{
for (auto const & entry : l.tbl())
r.setignore(entry.first, entry.second);
return std::move(r);
}
std::string LuaVal::tostring() const
{
switch (tag)
{
case TBOOL:
if (b)
return "true";
else
return "false";
case TNIL:
return "nil";
case TSTRING:
return s;
case TNUMBER:
return Serializer::tostring(d);
case TTABLE:
return Serializer::tostring(tbl_ptr);
}
throw smallfolk_exception("tostring invalid or unhandled tag %i", tag);
}
size_t LuaVal::LuaValHasher::operator()(LuaVal const & v) const
{
return LuaValHash(v);
}
size_t LuaValHash(LuaVal const & v)
{
switch (v.tag)
{
case TBOOL:
return std::hash<bool>()(v.b);
case TNIL:
return std::hash<int>()(0);
case TSTRING:
return std::hash<std::string>()(v.s);
case TNUMBER:
return std::hash<double>()(v.d);
case TTABLE:
return std::hash<LuaVal::TblPtr>()(v.tbl_ptr);
}
return std::hash<std::string>()(v.tostring());
}
LuaVal & LuaVal::operator[](LuaVal const & k)
{
if (!istable())
throw smallfolk_exception("using [] on non table object");
if (k.isnil())
throw smallfolk_exception("using [] with nil key");
LuaTable & tbl = (*tbl_ptr);
return tbl[k];
}
LuaVal const & LuaVal::operator[](LuaVal const & k) const
{
return get(k);
}
LuaVal const & LuaVal::get(LuaVal const & k) const
{
if (!istable())
throw smallfolk_exception("using get on non table object");
if (k.isnil())
throw smallfolk_exception("using get with nil key");
LuaTable & tbl = (*tbl_ptr);
auto it = tbl.find(k);
if (it != tbl.end())
return it->second;
return nil;
}
LuaVal const & LuaVal::get(std::string const & k) const
{
return get(LuaVal(k));
}
LuaVal const & LuaVal::get(int k) const
{
return get(LuaVal(k));
}
LuaVal const * LuaVal::try_get(LuaVal const & k) const
{
if (!istable())
throw smallfolk_exception("using try_get on non table object");
if (k.isnil())
throw smallfolk_exception("using try_get with nil key");
LuaTable const & tbl = (*tbl_ptr);
auto it = tbl.find(k);
if (it == tbl.end())
return nullptr;
return &it->second;
}
LuaVal const * LuaVal::try_get(std::string const & k) const
{
return try_get(LuaVal(k));
}
LuaVal const * LuaVal::try_get(int k) const
{
return try_get(LuaVal(k));
}
LuaVal & LuaVal::at(LuaVal const & k)
{
if (!istable())
throw smallfolk_exception("using at on non table object");
if (k.isnil())
throw smallfolk_exception("using at with nil key");
LuaTable & tbl = (*tbl_ptr);
auto it = tbl.find(k);
if (it == tbl.end())
throw smallfolk_exception("at: key not found");
return it->second;
}
LuaVal const & LuaVal::at(LuaVal const & k) const
{
if (!istable())
throw smallfolk_exception("using at on non table object");
if (k.isnil())
throw smallfolk_exception("using at with nil key");
LuaTable const & tbl = (*tbl_ptr);
auto it = tbl.find(k);
if (it == tbl.end())
throw smallfolk_exception("at: key not found");
return it->second;
}
LuaVal & LuaVal::at(std::string const & k)
{
return at(LuaVal(k));
}
LuaVal const & LuaVal::at(std::string const & k) const
{
return at(LuaVal(k));
}
LuaVal & LuaVal::at(int k)
{
return at(LuaVal(k));
}
LuaVal const & LuaVal::at(int k) const
{
return at(LuaVal(k));
}
bool LuaVal::has(LuaVal const & k) const
{
if (!istable())
throw smallfolk_exception("using has on non table object");
if (k.isnil())
throw smallfolk_exception("using has with nil key");
LuaTable & tbl = (*tbl_ptr);
auto it = tbl.find(k);
return it != tbl.end();
}
bool LuaVal::has(std::string const & k) const
{
return has(LuaVal(k));
}
bool LuaVal::has(int k) const
{
return has(LuaVal(k));
}
LuaVal const * LuaVal::try_get_path(std::initializer_list<LuaVal> keys) const
{
if (keys.size() == 0)
return this;
if (!istable())
throw smallfolk_exception("using try_get_path on non table object");
LuaVal const * found = nullptr;
PathWalkResult const result = walk_path_read(*this, keys, &found, nullptr, nullptr);
if (result == PathWalkResult::Found)
return found;
return nullptr;
}
LuaVal const & LuaVal::get_path(std::initializer_list<LuaVal> keys) const
{
if (keys.size() == 0)
return *this;
LuaVal const * found = try_get_path(keys);
if (found)
return *found;
return nil;
}
LuaVal & LuaVal::at_path(std::initializer_list<LuaVal> keys)
{
if (keys.size() == 0)
return *this;
if (!istable())
throw smallfolk_exception("using at_path on non table object");
LuaVal const * found = nullptr;
size_t fail_index = 0;
PathWalkResult fail_reason = PathWalkResult::MissingKey;
PathWalkResult const result = walk_path_read(*this, keys, &found, &fail_index, &fail_reason);
if (result == PathWalkResult::Found)
return const_cast<LuaVal &>(*found);
if (fail_reason == PathWalkResult::NotTable)
throw smallfolk_exception("at_path: not a table at %s", format_path(keys, fail_index).c_str());
throw smallfolk_exception("at_path: key not found at %s", format_path(keys, fail_index + 1).c_str());
}
LuaVal const & LuaVal::at_path(std::initializer_list<LuaVal> keys) const
{
if (keys.size() == 0)
return *this;
if (!istable())
throw smallfolk_exception("using at_path on non table object");
LuaVal const * found = nullptr;
size_t fail_index = 0;
PathWalkResult fail_reason = PathWalkResult::MissingKey;
PathWalkResult const result = walk_path_read(*this, keys, &found, &fail_index, &fail_reason);
if (result == PathWalkResult::Found)
return *found;
if (fail_reason == PathWalkResult::NotTable)
throw smallfolk_exception("at_path: not a table at %s", format_path(keys, fail_index).c_str());
throw smallfolk_exception("at_path: key not found at %s", format_path(keys, fail_index + 1).c_str());
}
bool LuaVal::has_path(std::initializer_list<LuaVal> keys) const
{
if (keys.size() == 0)
return true;
if (!istable())
throw smallfolk_exception("using has_path on non table object");
LuaVal const * found = nullptr;
size_t fail_index = 0;
PathWalkResult fail_reason = PathWalkResult::MissingKey;
PathWalkResult const result = walk_path_read(*this, keys, &found, &fail_index, &fail_reason);
(void)fail_index;
(void)fail_reason;
return result == PathWalkResult::Found;
}
LuaVal & LuaVal::set_path(std::initializer_list<LuaVal> keys, LuaVal const & v)
{
if (keys.size() == 0)
throw smallfolk_exception("using set_path with empty path");
if (!istable())
throw smallfolk_exception("using set_path on non table object");
LuaVal * cur = this;
auto it = keys.begin();
auto const end = keys.end();
LuaVal const * last = end - 1;
size_t index = 0;
for (; it != last; ++it, ++index)
{
ensure_path_key(*it, "set_path");
if (!cur->istable())
throw smallfolk_exception("set_path: not a table at %s", format_path(keys, index).c_str());
cur = &(*cur)[*it];
}
ensure_path_key(*last, "set_path");
return cur->set(*last, v);
}
LuaVal & LuaVal::set_path(std::initializer_list<LuaVal> keys, LuaVal && v)
{
if (keys.size() == 0)
throw smallfolk_exception("using set_path with empty path");
if (!istable())
throw smallfolk_exception("using set_path on non table object");
LuaVal * cur = this;
auto it = keys.begin();
auto const end = keys.end();
LuaVal const * last = end - 1;
size_t index = 0;
for (; it != last; ++it, ++index)
{
ensure_path_key(*it, "set_path");
if (!cur->istable())
throw smallfolk_exception("set_path: not a table at %s", format_path(keys, index).c_str());
cur = &(*cur)[*it];
}
ensure_path_key(*last, "set_path");
return cur->set(*last, std::move(v));
}
LuaVal & LuaVal::erase_path(std::initializer_list<LuaVal> keys)
{
if (keys.size() == 0)
throw smallfolk_exception("using erase_path with empty path");
if (!istable())
throw smallfolk_exception("using erase_path on non table object");
if (keys.size() == 1)
return erase(*keys.begin());
LuaVal * cur = this;
auto it = keys.begin();
auto const end = keys.end();
LuaVal const * last = end - 1;
for (; it != last; ++it)
{
ensure_path_key(*it, "erase_path");
if (!cur->istable())
return *this;
LuaVal const * next = cur->try_get(*it);
if (!next)
return *this;
cur = const_cast<LuaVal *>(next);
}
if (!cur->istable())
return *this;
return cur->erase(*last);
}
LuaVal & LuaVal::set(LuaVal const & k, LuaVal const & v)
{
if (!istable())
throw smallfolk_exception("using set on non table object");
if (k.isnil())
throw smallfolk_exception("using set with nil key");
LuaTable & tbl = (*tbl_ptr);
if (v.isnil()) // on nil value erase key
tbl.erase(k);
else
tbl[k] = v; // normally set pair
return *this;
}
LuaVal & LuaVal::set(LuaVal const & k, LuaVal && v)
{
if (!istable())
throw smallfolk_exception("using set on non table object");
if (k.isnil())
throw smallfolk_exception("using set with nil key");
LuaTable & tbl = (*tbl_ptr);
if (v.isnil()) // on nil value erase key
tbl.erase(k);
else
tbl[k] = std::move(v); // normally set pair
return *this;
}
LuaVal & LuaVal::set(std::string const & k, LuaVal const & v)
{
return set(LuaVal(k), v);
}
LuaVal & LuaVal::set(std::string const & k, LuaVal && v)
{
return set(LuaVal(k), std::move(v));
}
LuaVal & LuaVal::set(int k, LuaVal const & v)
{
return set(LuaVal(k), v);
}
LuaVal & LuaVal::set(int k, LuaVal && v)
{
return set(LuaVal(k), std::move(v));
}
LuaVal & LuaVal::setignore(LuaVal const & k, LuaVal const & v)
{
if (!istable())
throw smallfolk_exception("using setignore on non table object");
if (k.isnil())
throw smallfolk_exception("using setignore with nil key");
if (v.isnil())
return *this;
LuaTable & tbl = (*tbl_ptr);
tbl.emplace(k, v);
return *this;
}
LuaVal & LuaVal::setignore(LuaVal const & k, LuaVal && v)
{
if (!istable())
throw smallfolk_exception("using setignore on non table object");
if (k.isnil())
throw smallfolk_exception("using setignore with nil key");
if (v.isnil())
return *this;
LuaTable & tbl = (*tbl_ptr);
tbl.emplace(k, std::move(v));
return *this;
}
LuaVal & LuaVal::erase(LuaVal const & k)
{
if (!istable())
throw smallfolk_exception("using erase on non table object");
if (k.isnil())
throw smallfolk_exception("using erase with nil key");
LuaTable & tbl = (*tbl_ptr);
tbl.erase(k);
return *this;
}
unsigned int LuaVal::len() const
{
if (!istable())
throw smallfolk_exception("using len on non table object");
LuaTable & tbl = (*tbl_ptr);
unsigned int i = 0;
for (;;)
{
if (i == UINT_MAX)
return i;
++i;
auto it = tbl.find(i);
if (it == tbl.end() || it->second.isnil())
return i - 1;
}
}
LuaVal & LuaVal::insert(LuaVal const & v, LuaVal const & pos)
{
if (!istable())
throw smallfolk_exception("using insert on non table object");
LuaTable & tbl = (*tbl_ptr);
if (pos.isnil())
{
if (!v.isnil())
tbl[len() + 1] = v;
return *this;
}
if (!pos.isnumber())
throw smallfolk_exception("using insert with non number pos");
if (std::floor(pos.num()) != pos.num())
throw smallfolk_exception("using insert with invalid number key");
unsigned int max = len() + 1;
unsigned int val = static_cast<unsigned int>(pos.num());
if (val <= 0 || val > max)
throw smallfolk_exception("using insert with out of bounds key");
for (unsigned int i = max; i > val; --i)
tbl[i] = tbl[i - 1];
if (v.isnil())
tbl.erase(val);
else
tbl[val] = v;
return *this;
}
LuaVal & LuaVal::insert(LuaVal && v, LuaVal const & pos)
{
if (!istable())
throw smallfolk_exception("using insert on non table object");
LuaTable & tbl = (*tbl_ptr);
if (pos.isnil())
{
if (!v.isnil())
tbl[len() + 1] = std::move(v);
return *this;
}
if (!pos.isnumber())
throw smallfolk_exception("using insert with non number pos");
if (std::floor(pos.num()) != pos.num())
throw smallfolk_exception("using insert with invalid number key");
unsigned int max = len() + 1;
unsigned int val = static_cast<unsigned int>(pos.num());
if (val <= 0 || val > max)
throw smallfolk_exception("using insert with out of bounds key");
for (unsigned int i = max; i > val; --i)
tbl[i] = std::move(tbl[i - 1]);
if (v.isnil())
tbl.erase(val);
else
tbl[val] = std::move(v);
return *this;
}
LuaVal & LuaVal::remove(LuaVal const & pos)
{
if (!istable())
throw smallfolk_exception("using remove on non table object");
LuaTable & tbl = (*tbl_ptr);
if (pos.isnil())
{
if (unsigned int i = len())
tbl.erase(i);
return *this;
}
if (!pos.isnumber())
throw smallfolk_exception("using remove with non number key");
if (std::floor(pos.num()) != pos.num())
throw smallfolk_exception("using remove with invalid number key");
unsigned int max = len();
unsigned int val = static_cast<unsigned int>(pos.num());
if (val <= 0 || val > max + 1)
throw smallfolk_exception("using remove with out of bounds key");
for (unsigned int i = val; i < max; ++i)
tbl[i] = tbl[i + 1];
tbl.erase(max);
return *this;
}
double LuaVal::num() const
{
if (!isnumber())
throw smallfolk_exception("using num on non number object");
return d;
}
bool LuaVal::boolean() const
{
if (!isbool())
throw smallfolk_exception("using boolean on non bool object");
return b;
}
std::string const & LuaVal::str() const
{
if (!isstring())
throw smallfolk_exception("using str on non string object");
return s;
}
LuaVal::LuaTable const & LuaVal::tbl() const
{
if (!istable() || !tbl_ptr)
throw smallfolk_exception("using tbl on non table object");
return *tbl_ptr;
}
bool LuaVal::try_as_number(double & out) const
{
if (!isnumber())
return false;
out = d;
return true;
}
bool LuaVal::try_as_string(std::string const *& out) const
{
if (!isstring())
return false;
out = &s;
return true;
}
bool LuaVal::try_as_bool(bool & out) const
{
if (!isbool())
return false;
out = b;
return true;
}
std::string LuaVal::type(LuaTypeTag tag)
{
switch (tag)
{
case TBOOL:
return "boolean";
case TNIL:
return "nil";
case TSTRING:
return "string";
case TNUMBER:
return "number";
case TTABLE:
return "table";
}
throw smallfolk_exception("tostring invalid or unhandled tag %i", tag);
}
std::string LuaVal::dumps(std::string * errmsg) const
{
try
{
Serializer::ACC acc;
acc << std::setprecision(17); // min lua precision
unsigned int nmemo = 0;
Serializer::MEMO memo;
Serializer::dump_object(*this, nmemo, memo, acc);
return acc.str();
}
catch (smallfolk_exception const & e)
{
if (errmsg)
*errmsg = e.what();
}
return std::string();
}
std::string LuaVal::dumps_or_throw() const
{
std::string err;
std::string value = dumps(&err);
if (!err.empty())
throw smallfolk_exception("%s", err.c_str());
return value;
}
LuaVal LuaVal::loads(std::string const & string, std::string * errmsg)
{
return loads(string, read_load_limits(), errmsg);
}
LuaVal LuaVal::loads(std::string const & string, LoadLimits const & limits, std::string * errmsg)
{
try
{
if (string.length() > limits.max_input_size)
throw smallfolk_exception(
"load limit exceeded: max input size %zu",
limits.max_input_size);
Serializer::TABLES tables;
ParseContext ctx{ limits, 0, 0 };
size_t i = 0;
LuaVal result = Serializer::expect_object(string, i, tables, ctx);
skip_whitespace(string, i);
if (limits.require_consumed_input && i != string.length())
throw smallfolk_exception("unexpected trailing input at position %zu", i);
return result;
}
catch (smallfolk_exception const & e)
{
if (errmsg)
*errmsg = e.what();
}
return LuaVal::nil;
}
LuaVal LuaVal::loads_or_throw(std::string const & string)
{
return loads_or_throw(string, read_load_limits());
}
LuaVal LuaVal::loads_or_throw(std::string const & string, LoadLimits const & limits)
{
std::string err;
LuaVal value = loads(string, limits, &err);
if (!err.empty())
throw smallfolk_exception("%s", err.c_str());
return value;
}
bool LuaVal::operator==(LuaVal const& rhs) const
{
if (tag != rhs.tag)
return false;
switch (tag)
{
case TBOOL:
return b == rhs.b;
case TNIL:
return true;
case TSTRING:
return s == rhs.s;
case TNUMBER:
return d == rhs.d;
case TTABLE:
return tbl_ptr == rhs.tbl_ptr;
}
throw smallfolk_exception("operator== invalid or unhandled tag %i", tag);
}
LuaVal::operator bool() const
{
return !isnil() && (!isbool() || boolean());
}
LuaVal& LuaVal::operator=(LuaVal const& val)
{
tag = val.tag;
if (istable())
tbl_ptr.reset(new LuaTable(*val.tbl_ptr));
else
tbl_ptr = nullptr;
s = val.s;
d = val.d;
b = val.b;
return *this;
}
unsigned int Serializer::dump_type_table(LuaVal const & object, unsigned int nmemo, MEMO & memo, ACC & acc)
{
if (!object.istable())
throw smallfolk_exception("using dump_type_table on non table object");
/*
// @ circular table references are disabled; deep copy on assign avoids shared refs.
auto it = memo.find(object);
if (it != memo.end())
{
acc << '@' << it->second;
return nmemo;
}
memo[object] = ++nmemo;
*/
acc << '{';
bool first = true;
std::map<unsigned int, const LuaVal*> arr;
std::unordered_map<const LuaVal*, const LuaVal*> hash;
for (auto&& v : object.tbl())
{