forked from lefticus/EmbeddedScripting
/
RubyInterpreter.hpp
1564 lines (1264 loc) · 31.5 KB
/
RubyInterpreter.hpp
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
/**********************************************************************
* Copyright (c) 2008-2016, Alliance for Sustainable Energy.
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**********************************************************************/
#ifndef UTILITIES_CORE_RUBYINTERPRETER_HPP
#define UTILITIES_CORE_RUBYINTERPRETER_HPP
#include "RubyException.hpp"
#include <vector>
#include <string>
#include <map>
#include <typeinfo>
#include <sstream>
// SWIGRubyRuntime.hxx includes ruby.h which includes ruby/win32.h, which has some brain damaged notions of
// what standard errno values should be.
#ifdef _MSC_VER
#ifdef EWOULDBLOCK
#pragma push_macro("EWOULDBLOCK")
#undef EWOULDBLOCK
#define EWOULDBLOCK_macro_found
#endif
#ifdef EINPROGRESS
#pragma push_macro("EINPROGRESS")
#undef EINPROGRESS
#define EINPROGRESS_macro_found
#endif
#ifdef EALREADY
#pragma push_macro("EALREADY")
#undef EALREADY
#define EALREADY_macro_found
#endif
#ifdef ENOTSOCK
#pragma push_macro("ENOTSOCK")
#undef ENOTSOCK
#define ENOTSOCK_macro_found
#endif
#ifdef EDESTADDRREQ
#pragma push_macro("EDESTADDRREQ")
#undef EDESTADDRREQ
#define EDESTADDRREQ_macro_found
#endif
#ifdef EMSGSIZE
#pragma push_macro("EMSGSIZE")
#undef EMSGSIZE
#define EMSGSIZE_macro_found
#endif
#ifdef EPROTOTYPE
#pragma push_macro("EPROTOTYPE")
#undef EPROTOTYPE
#define EPROTOTYPE_macro_found
#endif
#ifdef ENOPROTOOPT
#pragma push_macro("ENOPROTOOPT")
#undef ENOPROTOOPT
#define ENOPROTOOPT_macro_found
#endif
#ifdef EPROTONOSUPPORT
#pragma push_macro("EPROTONOSUPPORT")
#undef EPROTONOSUPPORT
#define EPROTONOSUPPORT_macro_found
#endif
#ifdef EOPNOTSUPP
#pragma push_macro("EOPNOTSUPP")
#undef EOPNOTSUPP
#define EOPNOTSUPP_macro_found
#endif
#ifdef EAFNOSUPPORT
#pragma push_macro("EAFNOSUPPORT")
#undef EAFNOSUPPORT
#define EAFNOSUPPORT_macro_found
#endif
#ifdef EADDRINUSE
#pragma push_macro("EADDRINUSE")
#undef EADDRINUSE
#define EADDRINUSE_macro_found
#endif
#ifdef EADDRNOTAVAIL
#pragma push_macro("EADDRNOTAVAIL")
#undef EADDRNOTAVAIL
#define EADDRNOTAVAIL_macro_found
#endif
#ifdef ENETDOWN
#pragma push_macro("ENETDOWN")
#undef ENETDOWN
#define ENETDOWN_macro_found
#endif
#ifdef ENETUNREACH
#pragma push_macro("ENETUNREACH")
#undef ENETUNREACH
#define ENETUNREACH_macro_found
#endif
#ifdef ENETRESET
#pragma push_macro("ENETRESET")
#undef ENETRESET
#define ENETRESET_macro_found
#endif
#ifdef ECONNABORTED
#pragma push_macro("ECONNABORTED")
#undef ECONNABORTED
#define ECONNABORTED_macro_found
#endif
#ifdef ECONNRESET
#pragma push_macro("ECONNRESET")
#undef ECONNRESET
#define ECONNRESET_macro_found
#endif
#ifdef ENOBUFS
#pragma push_macro("ENOBUFS")
#undef ENOBUFS
#define ENOBUFS_macro_found
#endif
#ifdef EISCONN
#pragma push_macro("EISCONN")
#undef EISCONN
#define EISCONN_macro_found
#endif
#ifdef ENOTCONN
#pragma push_macro("ENOTCONN")
#undef ENOTCONN
#define ENOTCONN_macro_found
#endif
#ifdef ETIMEDOUT
#pragma push_macro("ETIMEDOUT")
#undef ETIMEDOUT
#define ETIMEDOUT_macro_found
#endif
#ifdef ECONNREFUSED
#pragma push_macro("ECONNREFUSED")
#undef ECONNREFUSED
#define ECONNREFUSED_macro_found
#endif
#ifdef ELOOP
#pragma push_macro("ELOOP")
#undef ELOOP
#define ELOOP_macro_found
#endif
#ifdef EHOSTUNREACH
#pragma push_macro("EHOSTUNREACH")
#undef EHOSTUNREACH
#define EHOSTUNREACH_macro_found
#endif
#ifdef try
#pragma push_macro("try")
#undef try
#define try_macro_found
#endif
#ifdef except
#pragma push_macro("except")
#undef except
#define except_macro_found
#endif
#ifdef finally
#pragma push_macro("finally")
#undef finally
#define finally_macro_found
#endif
#ifdef leave
#pragma push_macro("leave")
#undef leave
#define leave_macro_found
#endif
#ifdef OpenFile
#pragma push_macro("OpenFile")
#undef OpenFile
#define OpenFile_macro_found
#endif
#ifdef CharNext
#pragma push_macro("CharNext")
#undef CharNext
#define CharNext_macro_found
#endif
#ifdef rb_w32_iswinnt
#pragma push_macro("rb_w32_iswinnt")
#undef rb_w
#define rb_w_macro_found32_iswinnt
#endif
#ifdef rb_w32_iswin95
#pragma push_macro("rb_w32_iswin95")
#undef rb_w
#define rb_w_macro_found32_iswin95
#endif
#ifdef getc
#pragma push_macro("getc")
#undef getc
#define getc_macro_found
#endif
#ifdef putc
#pragma push_macro("putc")
#undef putc
#define putc_macro_found
#endif
#ifdef fgetc
#pragma push_macro("fgetc")
#undef fgetc
#define fgetc_macro_found
#endif
#ifdef fputc
#pragma push_macro("fputc")
#undef fputc
#define fputc_macro_found
#endif
#ifdef getchar
#pragma push_macro("getchar")
#undef getchar
#define getchar_macro_found
#endif
#ifdef putchar
#pragma push_macro("putchar")
#undef putchar
#define putchar_macro_found
#endif
#ifdef fgetchar
#pragma push_macro("fgetchar")
#undef fgetchar
#define fgetchar_macro_found
#endif
#ifdef fputchar
#pragma push_macro("fputchar")
#undef fputchar
#define fputchar_macro_found
#endif
#ifdef utime
#pragma push_macro("utime")
#undef utime
#define utime_macro_found
#endif
#ifdef strcasecmp
#pragma push_macro("strcasecmp")
#undef strcasecmp
#define strcasecmp_macro_found
#endif
#ifdef strncasecmp
#pragma push_macro("strncasecmp")
#undef strncasecmp
#define strncasecmp_macro_found
#endif
#ifdef close
#pragma push_macro("close")
#undef close
#define close_macro_found
#endif
#ifdef fclose
#pragma push_macro("fclose")
#undef fclose
#define fclose_macro_found
#endif
#ifdef read
#pragma push_macro("read")
#undef read
#define read_macro_found
#endif
#ifdef write
#pragma push_macro("write")
#undef write
#define write_macro_found
#endif
#ifdef getpid
#pragma push_macro("getpid")
#undef getpid
#define getpid_macro_found
#endif
#ifdef sleep
#pragma push_macro("sleep")
#undef sleep
#define sleep_macro_found
#endif
#ifdef creat
#pragma push_macro("creat")
#undef creat
#define creat_macro_found
#endif
#ifdef eof
#pragma push_macro("eof")
#undef eof
#define eof_macro_found
#endif
#ifdef filelength
#pragma push_macro("filelength")
#undef filelength
#define filelength_macro_found
#endif
#ifdef mktemp
#pragma push_macro("mktemp")
#undef mktemp
#define mktemp_macro_found
#endif
#ifdef tell
#pragma push_macro("tell")
#undef tell
#define tell_macro_found
#endif
#ifdef unlink
#pragma push_macro("unlink")
#undef unlink
#define unlink_macro_found
#endif
#ifdef _open
#pragma push_macro("_open")
#undef _open
#define _open_macro_found
#endif
#ifdef sopen
#pragma push_macro("sopen")
#undef sopen
#define sopen_macro_found
#endif
#ifdef fopen
#pragma push_macro("fopen")
#undef fopen
#define fopen_macro_found
#endif
#ifdef fdopen
#pragma push_macro("fdopen")
#undef fdopen
#define fdopen_macro_found
#endif
#ifdef fsopen
#pragma push_macro("fsopen")
#undef fsopen
#define fsopen_macro_found
#endif
#ifdef fsync
#pragma push_macro("fsync")
#undef fsync
#define fsync_macro_found
#endif
#ifdef stat
#pragma push_macro("stat")
#undef stat
#define stat_macro_found
#endif
#ifdef execv
#pragma push_macro("execv")
#undef execv
#define execv_macro_found
#endif
#ifdef isatty
#pragma push_macro("isatty")
#undef isatty
#define isatty_macro_found
#endif
#ifdef mkdir
#pragma push_macro("mkdir")
#undef mkdir
#define mkdir_macro_found
#endif
#ifdef rmdir
#pragma push_macro("rmdir")
#undef rmdir
#define rmdir_macro_found
#endif
#ifdef isascii
#pragma push_macro("isascii")
#undef isascii
#define isascii_macro_found
#endif
#ifdef vsnprintf
#pragma push_macro("vsnprintf")
#undef vsnprintf
#define vsnprintf_macro_found
#endif
#ifdef snprintf
#pragma push_macro("snprintf")
#undef snprintf
#define snprintf_macro_found
#endif
#ifdef isnan
#pragma push_macro("isnan")
#undef isnan
#define isnan_macro_found
#endif
#ifdef finite
#pragma push_macro("finite")
#undef finite
#define finite_macro_found
#endif
#ifdef copysign
#pragma push_macro("copysign")
#undef copysign
#define copysign_macro_found
#endif
#ifdef scalb
#pragma push_macro("scalb")
#undef scalb
#define scalb_macro_found
#endif
#ifdef accept
#pragma push_macro("accept")
#undef accept
#define accept_macro_found
#endif
#ifdef bind
#pragma push_macro("bind")
#undef bind
#define bind_macro_found
#endif
#ifdef connect
#pragma push_macro("connect")
#undef connect
#define connect_macro_found
#endif
#ifdef FD_SET
#pragma push_macro("FD_SET")
#undef FD_SET
#define FD_SET_macro_found
#endif
#ifdef FD_CLR
#pragma push_macro("FD_CLR")
#undef FD_CLR
#define FD_CLR_macro_found
#endif
#ifdef FD_ISSET
#pragma push_macro("FD_ISSET")
#undef FD_ISSET
#define FD_ISSET_macro_found
#endif
#ifdef select
#pragma push_macro("select")
#undef select
#define select_macro_found
#endif
#ifdef getpeername
#pragma push_macro("getpeername")
#undef getpeername
#define getpeername_macro_found
#endif
#ifdef getsockname
#pragma push_macro("getsockname")
#undef getsockname
#define getsockname_macro_found
#endif
#ifdef getsockopt
#pragma push_macro("getsockopt")
#undef getsockopt
#define getsockopt_macro_found
#endif
#ifdef ioctlsocket
#pragma push_macro("ioctlsocket")
#undef ioctlsocket
#define ioctlsocket_macro_found
#endif
#ifdef listen
#pragma push_macro("listen")
#undef listen
#define listen_macro_found
#endif
#ifdef recv
#pragma push_macro("recv")
#undef recv
#define recv_macro_found
#endif
#ifdef recvfrom
#pragma push_macro("recvfrom")
#undef recvfrom
#define recvfrom_macro_found
#endif
#ifdef send
#pragma push_macro("send")
#undef send
#define send_macro_found
#endif
#ifdef sendto
#pragma push_macro("sendto")
#undef sendto
#define sendto_macro_found
#endif
#ifdef setsockopt
#pragma push_macro("setsockopt")
#undef setsockopt
#define setsockopt_macro_found
#endif
#ifdef shutdown
#pragma push_macro("shutdown")
#undef shutdown
#define shutdown_macro_found
#endif
#ifdef socket
#pragma push_macro("socket")
#undef socket
#define socket_macro_found
#endif
#ifdef gethostbyaddr
#pragma push_macro("gethostbyaddr")
#undef gethostbyaddr
#define gethostbyaddr_macro_found
#endif
#ifdef gethostbyname
#pragma push_macro("gethostbyname")
#undef gethostbyname
#define gethostbyname_macro_found
#endif
#ifdef gethostname
#pragma push_macro("gethostname")
#undef gethostname
#define gethostname_macro_found
#endif
#ifdef getprotobyname
#pragma push_macro("getprotobyname")
#undef getprotobyname
#define getprotobyname_macro_found
#endif
#ifdef getprotobynumber
#pragma push_macro("getprotobynumber")
#undef getprotobynumber
#define getprotobynumber_macro_found
#endif
#ifdef getservbyname
#pragma push_macro("getservbyname")
#undef getservbyname
#define getservbyname_macro_found
#endif
#ifdef getservbyport
#pragma push_macro("getservbyport")
#undef getservbyport
#define getservbyport_macro_found
#endif
#ifdef get_osfhandle
#define OLD_get_osfhandle get_osfhandle
#undef get_osfhandle
#define get_osfhandle_macro_found
#endif
#ifdef getcwd
#pragma push_macro("getcwd")
#undef getcwd
#define getcwd_macro_found
#endif
#ifdef getenv
#pragma push_macro("getenv")
#undef getenv
#define getenv_macro_found
#endif
#ifdef rename
#pragma push_macro("rename")
#undef rename
#define rename_macro_found
#endif
#ifdef times
#pragma push_macro("times")
#undef times
#define times_macro_found
#endif
#ifdef Sleep
#pragma push_macro("Sleep")
#undef Sleep
#define Sleep_macro_found
#endif
#endif
#include "SWIGRubyRuntime.hxx"
class RubyInterpreter
{
private:
// dummy arguments passed to ruby_options
int m_argc;
char** m_argv;
static void addIncludePath(std::vector<std::string>& includePaths, const std::string& includePath)
{
includePaths.push_back("-I");
includePaths.push_back(includePath);
}
public:
explicit RubyInterpreter(const std::vector<std::string> &t_includePaths)
{
//// set load paths
//std::vector<std::string> rubyArgs;
//rubyArgs.emplace_back("-EUTF-8");
//for (const auto &p : t_includePaths) {
// addIncludePath(rubyArgs, p);
//}
//// and now give the interpreter something to parse, so that it doesn't sit
//// waiting on stdin from us
//rubyArgs.emplace_back("-e");
//rubyArgs.emplace_back("");
//m_argc = static_cast<int>(rubyArgs.size());
//m_argv = new char*[m_argc];
//for (int i = 0; i < m_argc; ++i){
// m_argv[i] = new char[rubyArgs[i].size() + 1];
// strcpy(m_argv[i], rubyArgs[i].c_str());
//}
//// the return value of ruby_options is the parsed node of our "script"
//// from the -e "" we passed in. This could be used to actually parse / eval something if we wanted
//ruby_options(m_argc, m_argv);
// register some default types that we want to pass in / out of the ruby system
registerType<int>("int");
registerType<long>("long");
registerType<size_t>("size_t");
registerType<double>("double");
registerType<std::string>("std::string");
registerType<float>("float");
// set the script name if desired
//ruby_script("script_name");
}
~RubyInterpreter()
{
for (int i = 0; i < m_argc; i++){
delete[] m_argv[i];
}
delete[] m_argv;
}
/// Register a type along with its vector versions so that it can be used
/// from within the Ruby wrapper. These are necessary because there's no way to divine
/// the unmangled type name.
template<typename Type>
void registerType(const std::string &t_name)
{
m_types[typeid(Type).name()] = t_name;
m_types[typeid(Type *).name()] = t_name + " *";
m_types[typeid(std::vector<Type>).name()] = "std::vector<" + t_name + ">";
m_types[typeid(std::vector<Type> *).name()] = "std::vector<" + t_name + "> *";
}
// evaluate a ruby statement with no return value. If a ruby exception is raised
// the description is translated into a C++ exception, which is thrown as an openstudio::RubyException.
void evalString(const std::string &t_str)
{
VALUE val = rb_str_new2(t_str.c_str());
int error;
// save and restore the current working directory in case the call to ruby upsets it
// QDir cwd = QDir::current();
rb_protect(evaluateSimpleImpl,val,&error);
// QDir::setCurrent(cwd.dirName());
if (error != 0)
{
VALUE errval = rb_eval_string("$!.to_s");
char *str = StringValuePtr(errval);
std::string err(str);
VALUE locval = rb_eval_string("$@.to_s");
str = StringValuePtr(locval);
std::string loc(str);
throw RubyException(err, loc);
}
}
/// Execute a function by name with 0 parameters and no return value
template<typename ReturnType>
void exec(const std::string &t_functionName)
{
std::vector<VALUE> params;
makeProtectedCall(t_functionName, params);
}
/// Execute a function by name with 1 parameter and no return value
template<typename ReturnType, typename Param1>
void exec(
const std::string &t_functionName,
Param1 t_param1)
{
std::vector<VALUE> params;
params.push_back(newPointerObj(&t_param1));
makeProtectedCall(t_functionName, params);
}
/// Execute a function by name with 2 parameters and no return value
template<typename ReturnType, typename Param1, typename Param2>
ReturnType exec(
const std::string &t_functionName,
Param1 t_param1,
Param2 t_param2)
{
std::vector<VALUE> params;
params.push_back(newPointerObj(&t_param1));
params.push_back(newPointerObj(&t_param2));
makeProtectedCall(t_functionName, params);
}
/// Execute a function by name with 3 parameters and no return value
template<typename ReturnType, typename Param1, typename Param2, typename Param3>
ReturnType exec(
const std::string &t_functionName,
Param1 t_param1,
Param2 t_param2,
Param3 t_param3)
{
std::vector<VALUE> params;
params.push_back(newPointerObj(&t_param1));
params.push_back(newPointerObj(&t_param2));
params.push_back(newPointerObj(&t_param3));
makeProtectedCall(t_functionName, params);
}
/// Execute a function by name with 0 parameters and a return value
template<typename ReturnType>
ReturnType execWithReturn(const std::string &t_functionName)
{
std::vector<VALUE> params;
VALUE retval = makeProtectedCall(t_functionName, params);
return asType<ReturnType>(retval);
}
/// Execute a function by name with 1 parameter and a return value
template<typename ReturnType, typename Param1>
ReturnType execWithReturn(
const std::string &t_functionName,
Param1 t_param1)
{
std::vector<VALUE> params;
params.push_back(newPointerObj(&t_param1));
VALUE retval = makeProtectedCall(t_functionName, params);
return asType<ReturnType>(retval);
}
/// Execute a function by name with 2 parameters and a return value
template<typename ReturnType, typename Param1, typename Param2>
ReturnType execWithReturn(
const std::string &t_functionName,
Param1 t_param1,
Param2 t_param2)
{
std::vector<VALUE> params;
params.push_back(newPointerObj(&t_param1));
params.push_back(newPointerObj(&t_param2));
VALUE retval = makeProtectedCall(t_functionName, params);
return asType<ReturnType>(retval);
}
/// Execute a function by name with 3 parameters and a return value
template<typename ReturnType, typename Param1, typename Param2, typename Param3>
ReturnType execWithReturn(
const std::string &t_functionName,
Param1 t_param1,
Param2 t_param2,
Param3 t_param3)
{
std::vector<VALUE> params;
params.push_back(newPointerObj(&t_param1));
params.push_back(newPointerObj(&t_param2));
params.push_back(newPointerObj(&t_param3));
VALUE retval = makeProtectedCall(t_functionName, params);
return asType<ReturnType>(retval);
}
/// Add a named global object to the ruby runtime
template<typename ObjectType>
void addObject(const std::string &t_objectName, ObjectType *t_obj)
{
VALUE r_obj = SWIG_NewPointerObj(t_obj, getTypeInfo<ObjectType *>(), 0);
rb_define_variable(t_objectName.c_str(), &r_obj);
}
private:
RubyInterpreter &operator=(const RubyInterpreter &) = delete;
RubyInterpreter(const RubyInterpreter &) = delete;
std::map<std::string, std::string> m_types;
// Used for our rb_protect calls.
static VALUE evaluateSimpleImpl(VALUE arg)
{
return rb_eval_string(StringValuePtr(arg));
}
// Returns the swig_type_info for the templated type if it's been registered previously
// with registerType
template<typename Type>
swig_type_info *getTypeInfo() const
{
auto itr = m_types.find(typeid(Type).name());
if (itr == m_types.end())
{
throw std::runtime_error(std::string("Type has not been registered: ") + typeid(Type).name());
}
std::string t_typeName = itr->second;
swig_type_info *sti = SWIG_TypeQuery(t_typeName.c_str());
if (sti == nullptr) {
throw std::runtime_error("Unable to lookup type info for type: " + t_typeName);
}
return sti;
}
VALUE makeProtectedCall(const std::string &t_functionName, std::vector<VALUE> &t_params)
{
// we go through all of this because we cannot rb_protect a call to
// rb_funcall. At least not in any way I can find.
std::stringstream params;
for (size_t i = 0; i < t_params.size(); ++i)
{
std::stringstream ss;
ss << "$embedded_ruby_param_" << i;
rb_define_variable(ss.str().c_str(), &t_params[i]);
params << ss.str();
if (i < t_params.size() - 1)
{
params << ", ";
}
}
std::string funcall = "$embedded_ruby_return = " + t_functionName + "(" + params.str() + ")";
evalString(funcall);
VALUE retval = rb_gv_get("$embedded_ruby_return");
return retval;
}
template<typename Param>
VALUE newPointerObj(Param t_param)
{
swig_type_info *ti = getTypeInfo<Param>();
return SWIG_NewPointerObj(t_param, ti, 0);
}
VALUE newPointerObj(double *d)
{
return rb_float_new(*d);
}
VALUE newPointerObj(float *d)
{
return rb_float_new(*d);
}
VALUE newPointerObj(int *d)
{
return rb_to_int(rb_float_new(*d));
}
VALUE newPointerObj(long *d)
{
return rb_to_int(rb_float_new(*d));
}
VALUE newPointerObj(std::string *s)
{
return rb_str_new2(s->c_str());
}
template<typename Type>
Type asType(VALUE v)