forked from ChaiScript/ChaiScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdispatchkit.hpp
More file actions
1470 lines (1181 loc) · 47.9 KB
/
Copy pathdispatchkit.hpp
File metadata and controls
1470 lines (1181 loc) · 47.9 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
// This file is distributed under the BSD License.
// See "license.txt" for details.
// Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
// Copyright 2009-2018, Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#ifndef CHAISCRIPT_DISPATCHKIT_HPP_
#define CHAISCRIPT_DISPATCHKIT_HPP_
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <set>
#include <stdexcept>
#include <string>
#include <string_view>
#include <typeinfo>
#include <utility>
#include <vector>
#include "../chaiscript_defines.hpp"
#include "../chaiscript_threading.hpp"
#include "bad_boxed_cast.hpp"
#include "boxed_cast.hpp"
#include "boxed_cast_helper.hpp"
#include "boxed_value.hpp"
#include "type_conversions.hpp"
#include "dynamic_object.hpp"
#include "proxy_constructors.hpp"
#include "proxy_functions.hpp"
#include "type_info.hpp"
#include "short_alloc.hpp"
#include "../utility/quick_flat_map.hpp"
namespace chaiscript {
class Boxed_Number;
} // namespace chaiscript
namespace chaiscript {
namespace parser {
class ChaiScript_Parser_Base;
}
namespace dispatch {
class Dynamic_Proxy_Function;
class Proxy_Function_Base;
struct Placeholder_Object;
} // namespace dispatch
} // namespace chaiscript
/// \namespace chaiscript::dispatch
/// \brief Classes and functions specific to the runtime dispatch side of ChaiScript. Some items may be of use to the end user.
namespace chaiscript
{
namespace exception
{
/// Exception thrown in the case that an object name is invalid because it is a reserved word
class reserved_word_error : public std::runtime_error
{
public:
explicit reserved_word_error(const std::string &t_word) noexcept
: std::runtime_error("Reserved word not allowed in object name: " + t_word), m_word(t_word)
{
}
reserved_word_error(const reserved_word_error &) = default;
~reserved_word_error() noexcept override = default;
std::string word() const
{
return m_word;
}
private:
std::string m_word;
};
/// Exception thrown in the case that an object name is invalid because it contains illegal characters
class illegal_name_error : public std::runtime_error
{
public:
explicit illegal_name_error(const std::string &t_name) noexcept
: std::runtime_error("Reserved name not allowed in object name: " + t_name), m_name(t_name)
{
}
illegal_name_error(const illegal_name_error &) = default;
~illegal_name_error() noexcept override = default;
std::string name() const
{
return m_name;
}
private:
std::string m_name;
};
/// Exception thrown in the case that an object name is invalid because it already exists in current context
class name_conflict_error : public std::runtime_error
{
public:
explicit name_conflict_error(const std::string &t_name) noexcept
: std::runtime_error("Name already exists in current context " + t_name), m_name(t_name)
{
}
name_conflict_error(const name_conflict_error &) = default;
~name_conflict_error() noexcept override = default;
std::string name() const
{
return m_name;
}
private:
std::string m_name;
};
/// Exception thrown in the case that a non-const object was added as a shared object
class global_non_const : public std::runtime_error
{
public:
global_non_const() noexcept
: std::runtime_error("a global object must be const")
{
}
global_non_const(const global_non_const &) = default;
~global_non_const() noexcept override = default;
};
}
/// \brief Holds a collection of ChaiScript settings which can be applied to the ChaiScript runtime.
/// Used to implement loadable module support.
class Module
{
public:
Module &add(Type_Info ti, std::string name)
{
m_typeinfos.emplace_back(ti, std::move(name));
return *this;
}
Module &add(Type_Conversion d)
{
m_conversions.push_back(std::move(d));
return *this;
}
Module &add(Proxy_Function f, std::string name)
{
m_funcs.emplace_back(std::move(f), std::move(name));
return *this;
}
Module &add_global_const(Boxed_Value t_bv, std::string t_name)
{
if (!t_bv.is_const())
{
throw chaiscript::exception::global_non_const();
}
m_globals.emplace_back(std::move(t_bv), std::move(t_name));
return *this;
}
//Add a bit of ChaiScript to eval during module implementation
Module &eval(std::string str)
{
m_evals.push_back(std::move(str));
return *this;
}
template<typename Eval, typename Engine>
void apply(Eval &t_eval, Engine &t_engine) const
{
apply(m_typeinfos.begin(), m_typeinfos.end(), t_engine);
apply(m_funcs.begin(), m_funcs.end(), t_engine);
apply_eval(m_evals.begin(), m_evals.end(), t_eval);
apply_single(m_conversions.begin(), m_conversions.end(), t_engine);
apply_globals(m_globals.begin(), m_globals.end(), t_engine);
}
bool has_function(const Proxy_Function &new_f, const std::string_view &name) noexcept
{
return std::any_of(m_funcs.begin(), m_funcs.end(),
[&](const std::pair<Proxy_Function, std::string> &existing_f) {
return existing_f.second == name && *(existing_f.first) == *(new_f);
}
);
}
private:
std::vector<std::pair<Type_Info, std::string>> m_typeinfos;
std::vector<std::pair<Proxy_Function, std::string>> m_funcs;
std::vector<std::pair<Boxed_Value, std::string>> m_globals;
std::vector<std::string> m_evals;
std::vector<Type_Conversion> m_conversions;
template<typename T, typename InItr>
static void apply(InItr begin, const InItr end, T &t)
{
for_each(begin, end,
[&t](const auto &obj) {
try {
t.add(obj.first, obj.second);
} catch (const chaiscript::exception::name_conflict_error &) {
/// \todo Should we throw an error if there's a name conflict
/// while applying a module?
}
}
);
}
template<typename T, typename InItr>
static void apply_globals(InItr begin, InItr end, T &t)
{
while (begin != end)
{
t.add_global_const(begin->first, begin->second);
++begin;
}
}
template<typename T, typename InItr>
static void apply_single(InItr begin, InItr end, T &t)
{
while (begin != end)
{
t.add(*begin);
++begin;
}
}
template<typename T, typename InItr>
static void apply_eval(InItr begin, InItr end, T &t)
{
while (begin != end)
{
t.eval(*begin);
++begin;
}
}
};
/// Convenience typedef for Module objects to be added to the ChaiScript runtime
using ModulePtr = std::shared_ptr<Module>;
namespace detail
{
/// A Proxy_Function implementation that is able to take
/// a vector of Proxy_Functions and perform a dispatch on them. It is
/// used specifically in the case of dealing with Function object variables
class Dispatch_Function final : public dispatch::Proxy_Function_Base
{
public:
explicit Dispatch_Function(std::vector<Proxy_Function> t_funcs)
: Proxy_Function_Base(build_type_infos(t_funcs), calculate_arity(t_funcs)),
m_funcs(std::move(t_funcs))
{
}
bool operator==(const dispatch::Proxy_Function_Base &rhs) const noexcept override
{
try {
const auto &dispatch_fun = dynamic_cast<const Dispatch_Function &>(rhs);
return m_funcs == dispatch_fun.m_funcs;
} catch (const std::bad_cast &) {
return false;
}
}
std::vector<Const_Proxy_Function> get_contained_functions() const override
{
return std::vector<Const_Proxy_Function>(m_funcs.begin(), m_funcs.end());
}
static int calculate_arity(const std::vector<Proxy_Function> &t_funcs) noexcept
{
if (t_funcs.empty()) {
return -1;
}
const auto arity = t_funcs.front()->get_arity();
for (const auto &func : t_funcs)
{
if (arity != func->get_arity())
{
// The arities in the list do not match, so it's unspecified
return -1;
}
}
return arity;
}
bool call_match(const Function_Params &vals, const Type_Conversions_State &t_conversions) const noexcept override
{
return std::any_of(std::begin(m_funcs), std::end(m_funcs),
[&vals, &t_conversions](const Proxy_Function &f){ return f->call_match(vals, t_conversions); });
}
protected:
Boxed_Value do_call(const Function_Params ¶ms, const Type_Conversions_State &t_conversions) const override
{
return dispatch::dispatch(m_funcs, params, t_conversions);
}
private:
std::vector<Proxy_Function> m_funcs;
static std::vector<Type_Info> build_type_infos(const std::vector<Proxy_Function> &t_funcs)
{
auto begin = t_funcs.cbegin();
const auto &end = t_funcs.cend();
if (begin != end)
{
std::vector<Type_Info> type_infos = (*begin)->get_param_types();
++begin;
bool size_mismatch = false;
while (begin != end)
{
std::vector<Type_Info> param_types = (*begin)->get_param_types();
if (param_types.size() != type_infos.size())
{
size_mismatch = true;
}
for (size_t i = 0; i < type_infos.size() && i < param_types.size(); ++i)
{
if (!(type_infos[i] == param_types[i]))
{
type_infos[i] = detail::Get_Type_Info<Boxed_Value>::get();
}
}
++begin;
}
assert(!type_infos.empty() && " type_info vector size is < 0, this is only possible if something else is broken");
if (size_mismatch)
{
type_infos.resize(1);
}
return type_infos;
}
return std::vector<Type_Info>();
}
};
}
namespace detail
{
struct Stack_Holder
{
//template <class T, std::size_t BufSize = sizeof(T)*20000>
// using SmallVector = std::vector<T, short_alloc<T, BufSize>>;
template <class T>
using SmallVector = std::vector<T>;
using Scope = utility::QuickFlatMap<std::string, Boxed_Value, str_equal>;
using StackData = SmallVector<Scope>;
using Stacks = SmallVector<StackData>;
using Call_Param_List = SmallVector<Boxed_Value>;
using Call_Params = SmallVector<Call_Param_List>;
Stack_Holder()
{
push_stack();
push_call_params();
}
void push_stack_data()
{
stacks.back().emplace_back();
// stacks.back().emplace_back(Scope(scope_allocator));
}
void push_stack()
{
stacks.emplace_back(1);
}
void push_call_params()
{
call_params.emplace_back();
}
Stacks stacks;
Call_Params call_params;
int call_depth = 0;
};
/// Main class for the dispatchkit. Handles management
/// of the object stack, functions and registered types.
class Dispatch_Engine
{
public:
using Type_Name_Map = std::map<std::string, chaiscript::Type_Info, str_less>;
using Scope = utility::QuickFlatMap<std::string, Boxed_Value, str_equal>;
using StackData = Stack_Holder::StackData;
struct State
{
utility::QuickFlatMap<std::string, std::shared_ptr<std::vector<Proxy_Function>>, str_equal> m_functions;
utility::QuickFlatMap<std::string, Proxy_Function, str_equal> m_function_objects;
utility::QuickFlatMap<std::string, Boxed_Value, str_equal> m_boxed_functions;
std::map<std::string, Boxed_Value, str_less> m_global_objects;
Type_Name_Map m_types;
};
explicit Dispatch_Engine(chaiscript::parser::ChaiScript_Parser_Base &parser)
: m_stack_holder(),
m_parser(parser)
{
}
/// \brief casts an object while applying any Dynamic_Conversion available
template<typename Type>
decltype(auto) boxed_cast(const Boxed_Value &bv) const
{
Type_Conversions_State state(m_conversions, m_conversions.conversion_saves());
return(chaiscript::boxed_cast<Type>(bv, &state));
}
/// Add a new conversion for upcasting to a base class
void add(const Type_Conversion &d)
{
m_conversions.add_conversion(d);
}
/// Add a new named Proxy_Function to the system
void add(const Proxy_Function &f, const std::string &name)
{
add_function(f, name);
}
/// Set the value of an object, by name. If the object
/// is not available in the current scope it is created
void add(Boxed_Value obj, const std::string &name)
{
auto &stack = get_stack_data();
for (auto stack_elem = stack.rbegin(); stack_elem != stack.rend(); ++stack_elem)
{
if (auto itr = stack_elem->find(name); itr != stack_elem->end())
{
itr->second = std::move(obj);
return;
}
}
add_object(name, std::move(obj));
}
/// Adds a named object to the current scope
/// \warning This version does not check the validity of the name
/// it is meant for internal use only
Boxed_Value &add_get_object(std::string t_name, Boxed_Value obj, Stack_Holder &t_holder)
{
auto &stack_elem = get_stack_data(t_holder).back();
if (auto result = stack_elem.insert(std::pair{std::move(t_name), std::move(obj)}); result.second)
{
return result.first->second;
} else {
//insert failed
throw chaiscript::exception::name_conflict_error(result.first->first);
}
}
/// Adds a named object to the current scope
/// \warning This version does not check the validity of the name
/// it is meant for internal use only
void add_object(std::string t_name, Boxed_Value obj, Stack_Holder &t_holder)
{
auto &stack_elem = get_stack_data(t_holder).back();
if (auto result = stack_elem.insert(std::pair{std::move(t_name), std::move(obj)}); !result.second)
{
//insert failed
throw chaiscript::exception::name_conflict_error(result.first->first);
}
}
/// Adds a named object to the current scope
/// \warning This version does not check the validity of the name
/// it is meant for internal use only
void add_object(const std::string &name, Boxed_Value obj)
{
add_object(name, std::move(obj), get_stack_holder());
}
/// Adds a new global shared object, between all the threads
void add_global_const(const Boxed_Value &obj, const std::string &name)
{
if (!obj.is_const())
{
throw chaiscript::exception::global_non_const();
}
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
if (m_state.m_global_objects.find(name) != m_state.m_global_objects.end())
{
throw chaiscript::exception::name_conflict_error(name);
} else {
m_state.m_global_objects.insert(std::make_pair(name, obj));
}
}
/// Adds a new global (non-const) shared object, between all the threads
Boxed_Value add_global_no_throw(Boxed_Value obj, std::string name)
{
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
return m_state.m_global_objects.insert(std::pair{std::move(name), std::move(obj)}).first->second;
}
/// Adds a new global (non-const) shared object, between all the threads
void add_global(Boxed_Value obj, std::string name)
{
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
if (auto result = m_state.m_global_objects.insert(std::pair{std::move(name), std::move(obj)}); !result.second) {
// insert failed
throw chaiscript::exception::name_conflict_error(result.first->first);
}
}
/// Updates an existing global shared object or adds a new global shared object if not found
void set_global(Boxed_Value obj, std::string name)
{
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
m_state.m_global_objects.insert_or_assign(std::move(name), std::move(obj));
}
/// Adds a new scope to the stack
void new_scope()
{
new_scope(*m_stack_holder);
}
/// Pops the current scope from the stack
void pop_scope()
{
pop_scope(*m_stack_holder);
}
/// Adds a new scope to the stack
static void new_scope(Stack_Holder &t_holder)
{
t_holder.push_stack_data();
t_holder.push_call_params();
}
/// Pops the current scope from the stack
static void pop_scope(Stack_Holder &t_holder)
{
t_holder.call_params.pop_back();
StackData &stack = get_stack_data(t_holder);
assert(!stack.empty());
stack.pop_back();
}
/// Pushes a new stack on to the list of stacks
static void new_stack(Stack_Holder &t_holder)
{
// add a new Stack with 1 element
t_holder.push_stack();
}
static void pop_stack(Stack_Holder &t_holder)
{
t_holder.stacks.pop_back();
}
/// Searches the current stack for an object of the given name
/// includes a special overload for the _ place holder object to
/// ensure that it is always in scope.
Boxed_Value get_object(const std::string_view &name, std::atomic_uint_fast32_t &t_loc, Stack_Holder &t_holder) const
{
enum class Loc : uint_fast32_t {
located = 0x80000000,
is_local = 0x40000000,
stack_mask = 0x0FFF0000,
loc_mask = 0x0000FFFF
};
uint_fast32_t loc = t_loc;
if (loc == 0)
{
auto &stack = get_stack_data(t_holder);
// Is it in the stack?
for (auto stack_elem = stack.rbegin(); stack_elem != stack.rend(); ++stack_elem)
{
for (auto s = stack_elem->begin(); s != stack_elem->end(); ++s )
{
if (s->first == name) {
t_loc = static_cast<uint_fast32_t>(std::distance(stack.rbegin(), stack_elem) << 16)
| static_cast<uint_fast32_t>(std::distance(stack_elem->begin(), s))
| static_cast<uint_fast32_t>(Loc::located)
| static_cast<uint_fast32_t>(Loc::is_local);
return s->second;
}
}
}
t_loc = static_cast<uint_fast32_t>(Loc::located);
} else if ((loc & static_cast<uint_fast32_t>(Loc::is_local)) != 0u) {
auto &stack = get_stack_data(t_holder);
return stack[stack.size() - 1 - ((loc & static_cast<uint_fast32_t>(Loc::stack_mask)) >> 16)].at_index(loc & static_cast<uint_fast32_t>(Loc::loc_mask));
}
// Is the value we are looking for a global or function?
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
const auto itr = m_state.m_global_objects.find(name);
if (itr != m_state.m_global_objects.end())
{
return itr->second;
}
// no? is it a function object?
auto obj = get_function_object_int(name, loc);
if (obj.first != loc) { t_loc = uint_fast32_t(obj.first); }
return obj.second;
}
/// Registers a new named type
void add(const Type_Info &ti, const std::string &name)
{
add_global_const(const_var(ti), name + "_type");
chaiscript::detail::threading::unique_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
m_state.m_types.insert(std::make_pair(name, ti));
}
/// Returns the type info for a named type
Type_Info get_type(std::string_view name, bool t_throw = true) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
const auto itr = m_state.m_types.find(name);
if (itr != m_state.m_types.end())
{
return itr->second;
}
if (t_throw) {
throw std::range_error("Type Not Known: " + std::string(name));
} else {
return Type_Info();
}
}
/// Returns the registered name of a known type_info object
/// compares the "bare_type_info" for the broadest possible
/// match
std::string get_type_name(const Type_Info &ti) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
for (const auto & elem : m_state.m_types)
{
if (elem.second.bare_equal(ti))
{
return elem.first;
}
}
return ti.bare_name();
}
/// Return all registered types
std::vector<std::pair<std::string, Type_Info> > get_types() const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
return std::vector<std::pair<std::string, Type_Info> >(m_state.m_types.begin(), m_state.m_types.end());
}
std::shared_ptr<std::vector<Proxy_Function>> get_method_missing_functions() const
{
uint_fast32_t method_missing_loc = m_method_missing_loc;
auto method_missing_funs = get_function("method_missing", method_missing_loc);
if (method_missing_funs.first != method_missing_loc) {
m_method_missing_loc = uint_fast32_t(method_missing_funs.first);
}
return std::move(method_missing_funs.second);
}
/// Return a function by name
std::pair<size_t, std::shared_ptr<std::vector< Proxy_Function>>> get_function(const std::string_view &t_name, const size_t t_hint) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
const auto &funs = get_functions_int();
if (const auto itr = funs.find(t_name, t_hint); itr != funs.end())
{
return std::make_pair(std::distance(funs.begin(), itr), itr->second);
} else {
return std::make_pair(size_t(0), std::make_shared<std::vector<Proxy_Function>>());
}
}
/// \returns a function object (Boxed_Value wrapper) if it exists
/// \throws std::range_error if it does not
Boxed_Value get_function_object(const std::string &t_name) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
return get_function_object_int(t_name, 0).second;
}
/// \returns a function object (Boxed_Value wrapper) if it exists
/// \throws std::range_error if it does not
/// \warn does not obtain a mutex lock. \sa get_function_object for public version
std::pair<size_t, Boxed_Value> get_function_object_int(const std::string_view &t_name, const size_t t_hint) const
{
const auto &funs = get_boxed_functions_int();
if (const auto itr = funs.find(t_name, t_hint); itr != funs.end())
{
return std::make_pair(std::distance(funs.begin(), itr), itr->second);
} else {
throw std::range_error("Object not found: " + std::string(t_name));
}
}
/// Return true if a function exists
bool function_exists(const std::string_view &name) const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
return get_functions_int().count(name) > 0;
}
/// \returns All values in the local thread state in the parent scope, or if it doesn't exist,
/// the current scope.
std::map<std::string, Boxed_Value> get_parent_locals() const
{
auto &stack = get_stack_data();
if (stack.size() > 1)
{
return std::map<std::string, Boxed_Value>(stack[1].begin(), stack[1].end());
} else {
return std::map<std::string, Boxed_Value>(stack[0].begin(), stack[0].end());
}
}
/// \returns All values in the local thread state, added through the add() function
std::map<std::string, Boxed_Value> get_locals() const
{
auto &stack = get_stack_data();
auto &scope = stack.front();
return std::map<std::string, Boxed_Value>(scope.begin(), scope.end());
}
/// \brief Sets all of the locals for the current thread state.
///
/// \param[in] t_locals The map<name, value> set of variables to replace the current state with
///
/// Any existing locals are removed and the given set of variables is added
void set_locals(const std::map<std::string, Boxed_Value> &t_locals)
{
auto &stack = get_stack_data();
auto &scope = stack.front();
scope.assign(t_locals.begin(), t_locals.end());
}
///
/// Get a map of all objects that can be seen from the current scope in a scripting context
///
std::map<std::string, Boxed_Value> get_scripting_objects() const
{
const Stack_Holder &s = *m_stack_holder;
// We don't want the current context, but one up if it exists
const StackData &stack = (s.stacks.size()==1)?(s.stacks.back()):(s.stacks[s.stacks.size()-2]);
std::map<std::string, Boxed_Value> retval;
// note: map insert doesn't overwrite existing values, which is why this works
for (auto itr = stack.rbegin(); itr != stack.rend(); ++itr)
{
retval.insert(itr->begin(), itr->end());
}
// add the global values
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
retval.insert(m_state.m_global_objects.begin(), m_state.m_global_objects.end());
return retval;
}
///
/// Get a map of all functions that can be seen from a scripting context
///
std::map<std::string, Boxed_Value> get_function_objects() const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
const auto &funs = get_function_objects_int();
std::map<std::string, Boxed_Value> objs;
for (const auto & fun : funs)
{
objs.insert(std::make_pair(fun.first, const_var(fun.second)));
}
return objs;
}
/// Get a vector of all registered functions
std::vector<std::pair<std::string, Proxy_Function > > get_functions() const
{
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l(m_mutex);
std::vector<std::pair<std::string, Proxy_Function> > rets;
const auto &functions = get_functions_int();
for (const auto & function : functions)
{
for (const auto & internal_func : *function.second)
{
rets.emplace_back(function.first, internal_func);
}
}
return rets;
}
const Type_Conversions &conversions() const noexcept
{
return m_conversions;
}
static bool is_attribute_call(const std::vector<Proxy_Function> &t_funs, const Function_Params &t_params,
bool t_has_params, const Type_Conversions_State &t_conversions) noexcept
{
if (!t_has_params || t_params.empty()) {
return false;
}
return std::any_of(std::begin(t_funs), std::end(t_funs),
[&](const auto &fun) {
return fun->is_attribute_function() && fun->compare_first_type(t_params[0], t_conversions);
}
);
}
#ifdef CHAISCRIPT_MSVC
// MSVC is unable to recognize that "rethrow_exception" causes the function to return
// so we must disable it here.
#pragma warning(push)
#pragma warning(disable : 4715)
#endif
Boxed_Value call_member(const std::string &t_name, std::atomic_uint_fast32_t &t_loc, const Function_Params ¶ms, bool t_has_params,
const Type_Conversions_State &t_conversions)
{
uint_fast32_t loc = t_loc;
const auto funs = get_function(t_name, loc);
if (funs.first != loc) { t_loc = uint_fast32_t(funs.first); }
const auto do_attribute_call =
[this](int l_num_params, Function_Params l_params, const std::vector<Proxy_Function> &l_funs, const Type_Conversions_State &l_conversions)->Boxed_Value
{
Function_Params attr_params(l_params.begin(), l_params.begin() + l_num_params);
Boxed_Value bv = dispatch::dispatch(l_funs, attr_params, l_conversions);
if (l_num_params < int(l_params.size()) || bv.get_type_info().bare_equal(user_type<dispatch::Proxy_Function_Base>())) {
struct This_Foist {
This_Foist(Dispatch_Engine &e, const Boxed_Value &t_bv) : m_e(e) {
m_e.get().new_scope();
m_e.get().add_object("__this", t_bv);
}
~This_Foist() {
m_e.get().pop_scope();
}
std::reference_wrapper<Dispatch_Engine> m_e;
};
This_Foist fi(*this, l_params.front());
try {
auto func = boxed_cast<const dispatch::Proxy_Function_Base *>(bv);
try {
return (*func)({l_params.begin() + l_num_params, l_params.end()}, l_conversions);
} catch (const chaiscript::exception::bad_boxed_cast &) {
} catch (const chaiscript::exception::arity_error &) {
} catch (const chaiscript::exception::guard_error &) {
}
throw chaiscript::exception::dispatch_error({l_params.begin() + l_num_params, l_params.end()},
std::vector<Const_Proxy_Function>{boxed_cast<Const_Proxy_Function>(bv)});
} catch (const chaiscript::exception::bad_boxed_cast &) {
// unable to convert bv into a Proxy_Function_Base
throw chaiscript::exception::dispatch_error({l_params.begin() + l_num_params, l_params.end()},
std::vector<Const_Proxy_Function>(l_funs.begin(), l_funs.end()));
}
} else {
return bv;
}
};
if (is_attribute_call(*funs.second, params, t_has_params, t_conversions)) {
return do_attribute_call(1, params, *funs.second, t_conversions);
} else {
std::exception_ptr except;
if (!funs.second->empty()) {
try {
return dispatch::dispatch(*funs.second, params, t_conversions);
} catch(chaiscript::exception::dispatch_error&) {
except = std::current_exception();
}
}
// If we get here we know that either there was no method with that name,
// or there was no matching method
const auto functions = [&]()->std::vector<Proxy_Function> {
std::vector<Proxy_Function> fs;
const auto method_missing_funs = get_method_missing_functions();
for (const auto &f : *method_missing_funs)
{
if(f->compare_first_type(params[0], t_conversions)) {
fs.push_back(f);
}
}
return fs;
}();
const bool is_no_param = [&]()->bool{
for (const auto &f : functions) {
if (f->get_arity() != 2) {