-
Notifications
You must be signed in to change notification settings - Fork 1
/
js_interop.cpp
959 lines (732 loc) · 24.1 KB
/
js_interop.cpp
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
#include "js_interop.hpp"
#include <toolkit/fs_helpers.hpp>
#include <iostream>
#include <vec/value.hpp>
#include <cctype>
#include <imgui/imgui.h>
#include <vec/vec.hpp>
#define M_E 2.7182818284590452354
#define M_LOG2E 1.4426950408889634074
#define M_LOG10E 0.43429448190325182765
#define M_LN2 0.69314718055994530942
#define M_LN10 2.30258509299404568402
#define M_PI 3.14159265358979323846
#define M_PI_2 1.57079632679489661923
#define M_PI_4 0.78539816339744830962
#define M_1_PI 0.31830988618379067154
#define M_2_PI 0.63661977236758134308
#define M_2_SQRTPI 1.12837916709551257390
#define M_SQRT2 1.41421356237309504880
#define M_SQRT1_2 0.70710678118654752440
std::string to_high_precision_string(float v)
{
std::ostringstream oss;
oss << std::setprecision(32) << std::fixed << std::showpoint << v;
std::string str = oss.str();
while(str.size() > 0 && str.back() == '0')
str.pop_back();
if(str.size() > 0 && str.back() == '.')
str += "0";
return str;
}
void config_variables::add(const std::string& name, float val)
{
for(const std::string& existing : names)
{
if(name == existing)
return;
}
names.push_back(name);
default_values.push_back(val);
current_values.push_back(val);
}
void config_variables::set_default(const std::string& name, float val)
{
for(int i=0; i < (int)names.size(); i++)
{
const std::string& existing = names[i];
if(name == existing)
{
default_values[i] = val;
current_values[i] = val;
return;
}
}
names.push_back(name);
default_values.push_back(val);
current_values.push_back(val);
}
bool config_variables::display()
{
bool any_modified = false;
for(int i=0; i < (int)names.size(); i++)
{
std::string as_high_precision = to_high_precision_string(current_values[i]);
bool after_dot = false;
bool in_leading_zeroes = true;
int leading_zeroes = 0;
int trailing_sig_figs = 0;
for(int idx = 0; idx < (int)as_high_precision.size(); idx++)
{
if(as_high_precision[idx] == '.')
{
after_dot = true;
continue;
}
if(!after_dot)
continue;
if(as_high_precision[idx] == '0' && in_leading_zeroes)
leading_zeroes++;
if(as_high_precision[idx] != '0')
in_leading_zeroes = false;
if(!in_leading_zeroes)
trailing_sig_figs++;
}
int values_to_display = 4;
int format_width = leading_zeroes + values_to_display;
std::string format_str = "%." + std::to_string(format_width) + "f";
any_modified |= ImGui::DragFloat((names[i] + "##df").c_str(), ¤t_values[i], 0.1f, 0.f, 0.f, format_str.c_str());
}
return any_modified;
}
std::map<std::string, std::string> config_variables::as_substitution_map()
{
std::map<std::string, std::string> ret;
for(int i=0; i < (int)names.size(); i++)
{
ret["cfg->" + names[i]] = to_high_precision_string(current_values[i]);
}
return ret;
}
struct storage
{
int which = 0;
dual d;
dual_complex c;
storage(){}
storage(const dual& d_in)
{
d = d_in;
which = 0;
}
storage(const dual_complex& c_in)
{
c = c_in;
which = 1;
}
};
template<typename T>
storage func(const storage& s1, T&& functor)
{
if(s1.which == 0)
{
storage s(functor(s1.d));
return s;
}
else if(s1.which == 1)
{
storage s(functor(s1.c));
return s;
}
else
{
throw std::runtime_error("Invalid type in dual/complex internals");
}
}
template<typename T>
storage func_real(const storage& s1, T&& functor)
{
if(s1.which == 0)
{
storage s(functor(s1.d));
return s;
}
else
{
throw std::runtime_error("Invalid type in dual/complex internals, this is a real-only function");
}
}
template<typename T>
storage func(const storage& s1, const storage& s2, T&& functor)
{
if(s1.which == 0 && s2.which == 0)
{
storage s(functor(s1.d, s2.d));
return s;
}
else if(s1.which == 0 && s2.which == 1)
{
storage s(functor(dual_complex(s1.d), s2.c));
return s;
}
else if(s1.which == 1 && s2.which == 0)
{
storage s(functor(s1.c, dual_complex(s2.d)));
return s;
}
else if(s1.which == 1 && s2.which == 1)
{
storage s(functor(s1.c, s2.c));
return s;
}
else
{
throw std::runtime_error("Invalid types in dual/complex internals");
}
}
storage s_add(const storage& s1, const storage& s2)
{
return func(s1, s2, [](auto v1, auto v2)
{
return v1 + v2;
});
}
storage s_sub(const storage& s1, const storage& s2)
{
return func(s1, s2, [](auto v1, auto v2)
{
return v1 - v2;
});
}
storage s_mul(const storage& s1, const storage& s2)
{
return func(s1, s2, [](auto v1, auto v2)
{
return v1 * v2;
});
}
storage s_jdiv(const storage& s1, const storage& s2)
{
return func(s1, s2, [](auto v1, auto v2)
{
return v1 / v2;
});
}
storage s_neg(const storage& s1)
{
return func(s1, [](auto v1)
{
return -v1;
});
}
storage s_lt(const storage& s1, const storage& s2)
{
if(s1.which == 0 && s2.which == 0)
return (dual)(s1.d < s2.d);
else
throw std::runtime_error("Can only use < on real values");
}
storage s_eq(const storage& s1, const storage& s2)
{
if(s1.which == 0 && s2.which == 0)
return (dual)(s1.d == s2.d);
else
throw std::runtime_error("Can only use == on real values");
}
storage s_lte(const storage& s1, const storage& s2)
{
if(s1.which == 0 && s2.which == 0)
return (dual)(s1.d <= s2.d);
else
throw std::runtime_error("Can only use <= on real values");
}
storage s_gt(const storage& s1, const storage& s2)
{
if(s1.which == 0 && s2.which == 0)
return (dual)(s1.d > s2.d);
else
throw std::runtime_error("Can only use > on real values");
}
storage s_gte(const storage& s1, const storage& s2)
{
if(s1.which == 0 && s2.which == 0)
return (dual)(s1.d > s2.d);
else
throw std::runtime_error("Can only use >= on real values");
}
js::value to_class(js::value_context& vctx, js::value in)
{
js::value global = js::get_global(vctx);
const std::string name = "make_class";
return js::call_prop(global, name, in).second;
}
void san(js::value& v)
{
if(!v.has_hidden("v"))
{
storage s;
js::value next(*v.vctx);
next.allocate_in_heap(s);
v.add_hidden("v", next);
}
}
storage get(js::value& v)
{
if(v.is_undefined())
return storage();
if(v.is_number())
{
double val = v;
storage s;
s.d = val;
return s;
}
if(v.is_boolean())
{
double val = (bool)v;
storage s;
s.d = val;
return s;
}
san(v);
return *v.get_hidden("v").get_ptr<storage>();
}
js::value pin(js::value_context* vctx, js::value in)
{
if(!in.has_hidden("v"))
return in;
storage* s = in.get_hidden("v").get_ptr<storage>();
assert(s);
sandbox* sand = js::get_sandbox_data<sandbox>(*vctx);
assert(sand);
if(s->which == 0)
{
sand->ctx.pin(s->d.real);
sand->ctx.pin(s->d.dual);
}
else
{
sand->ctx.pin(Real(s->c.real));
sand->ctx.pin(Imaginary(s->c.real));
sand->ctx.pin(Real(s->c.dual));
sand->ctx.pin(Imaginary(s->c.dual));
}
return in;
}
dual getr(js::value& v)
{
storage s = get(v);
if(s.which == 0)
return s.d;
else if(s.which == 1)
return Real(s.c);
else
throw std::runtime_error("getr which");
}
js::value to_value(js::value_context& vctx, const dual& in)
{
storage s;
s.d = in;
js::value v(vctx);
js::value as_object(vctx);
as_object.allocate_in_heap(s);
v.add_hidden("v", as_object);
return to_class(vctx, v);
}
js::value to_value(js::value_context& vctx, const dual_complex& in)
{
storage s;
s.which = 1;
s.c = in;
js::value v(vctx);
js::value as_object(vctx);
as_object.allocate_in_heap(s);
v.add_hidden("v", as_object);
assert(v.has_hidden("v"));
return to_class(vctx, v);
}
js::value to_value(js::value_context& vctx, const storage& in)
{
js::value v(vctx);
storage* ptr = new storage(in);
js::value as_object(vctx);
as_object.set_ptr(ptr);
v.add_hidden("v", as_object);
return to_class(vctx, v);
}
void construct(js::value_context* vctx, js::value js_this, js::value v2)
{
storage v = get(v2);
js::value as_object(*vctx);
as_object.allocate_in_heap(v);
js_this.add_hidden("v", as_object);
}
#define BINOP(name) \
js::value name(js::value_context* vctx, js::value v1, js::value v2) \
{ \
storage pv1 = get(v1); \
storage pv2 = get(v2); \
\
return to_value(*vctx, s_##name (pv1, pv2)); \
}
BINOP(add);
BINOP(mul);
BINOP(sub);
BINOP(jdiv);
BINOP(lt);
BINOP(lte);
BINOP(eq);
BINOP(gt);
BINOP(gte);
js::value neg(js::value_context* vctx, js::value v1)
{
storage pv1 = get(v1);
return to_value(*vctx, s_neg(pv1));
}
namespace CMath
{
void debug(js::value_context* vctx, js::value v);
#define UNARY_JS(name) js::value name(js::value_context* vctx, js::value in) { \
storage v = get(in); \
auto result = func(v, [](auto in){return name(in);}); \
return to_value(*vctx, result); \
}
#define UNARY_JS_REAL(name) js::value name(js::value_context* vctx, js::value in) { \
storage v = get(in); \
auto result = func_real(v, [](auto in){return name(in);}); \
return to_value(*vctx, result); \
}
#define BINARY_JS(name) js::value name(js::value_context* vctx, js::value in, js::value in2) { \
dual v = get(in).d; \
dual v2 = get(in2).d; \
return to_value(*vctx, dual_types::name(v, v2)); \
}
#define TERNARY_JS(name) js::value name(js::value_context* vctx, js::value in, js::value in2, js::value in3) { \
dual v = get(in).d; \
dual v2 = get(in2).d; \
dual v3 = get(in3).d; \
return to_value(*vctx, dual_types::name(v, v2, v3)); \
}
UNARY_JS(sin);
UNARY_JS(cos);
UNARY_JS_REAL(tan);
UNARY_JS_REAL(asin);
UNARY_JS_REAL(acos);
UNARY_JS_REAL(atan);
BINARY_JS(atan2);
//BINARY_JS(pow);
UNARY_JS(fabs);
UNARY_JS_REAL(log);
UNARY_JS(sqrt);
UNARY_JS(psqrt);
UNARY_JS_REAL(exp);
UNARY_JS_REAL(sinh);
UNARY_JS_REAL(cosh);
UNARY_JS_REAL(tanh);
UNARY_JS(conjugate);
UNARY_JS(self_conjugate_multiply);;
TERNARY_JS(fast_length);
js::value smooth_fmod(js::value_context* vctx, js::value in, js::value in2)
{
dual v = get(in).d;
dual v2 = get(in2).d;
return to_value(*vctx, dual_types::smooth_fmod(v, v2.real));
}
js::value select(js::value_context* vctx, js::value condition, js::value if_true, js::value if_false)
{
dual dcondition = get(condition).d;
dual dif_true = get(if_true).d;
dual dif_false = get(if_false).d;
dual selected = dual_if(dcondition.real, [&](){return dif_true;}, [&](){return dif_false;});
return to_value(*vctx, selected);
}
js::value Real(js::value_context* vctx, js::value v)
{
storage s = get(v);
if(s.which == 0)
return to_value(*vctx, s.d);
else if(s.which == 1)
return to_value(*vctx, Real(s.c));
else
throw std::runtime_error("Some kind of weird error in Real");
}
js::value Imaginary(js::value_context* vctx, js::value v)
{
storage s = get(v);
if(s.which == 0)
return to_value(*vctx, (dual)(0.f));
else if(s.which == 1)
return to_value(*vctx, Imaginary(s.c));
else
throw std::runtime_error("Some kind of weird error in Imaginary");
}
js::value csqrt(js::value_context* vctx, js::value v)
{
storage s = get(v);
if(s.which == 0)
return to_value(*vctx, csqrt(s.d));
else if(s.which == 1)
throw std::runtime_error("csqrt must be used with purely real arguments");
else
throw std::runtime_error("Some kind of weird error in Imaginary");
}
js::value pow(js::value_context* vctx, js::value in1, js::value in2)
{
storage s1 = get(in1);
storage s2 = get(in2);
if(s2.which != 0)
throw std::runtime_error("Pow cannot be used with a complex second argument");
//if(!s2.d.real.is_constant())
// throw std::runtime_error("Pow's second argument must be constant");
if(s1.which == 0)
{
return to_value(*vctx, pow(s1.d, s2.d));
}
if(!s2.d.real.is_constant())
throw std::runtime_error("Pow's second argument must be constant if arg1 is complex");
double exponent = s2.d.real.get_constant();
if(s1.which == 1)
{
if(exponent != (int)exponent)
throw std::runtime_error("With a complex first argument, the exponent must be integral");
return to_value(*vctx, pow(s1.c, (int)exponent));
}
throw std::runtime_error("Cannot call pow with two complex arguments");
}
void debug(js::value_context* vctx, js::value v)
{
/*if(v.is_string())
{
std::cout << (std::string)v << std::endl;
return;
}
storage base = get(v);
if(base.which == 0)
{
std::cout << "base " << type_to_string(base.d.real) << std::endl;
}
else
{
std::cout << "Basi " << type_to_string(Real(base.c.real)) << " I " << type_to_string(Imaginary(base.c.real)) << std::endl;
}
std::cout << "well hello there " << std::endl;
js::value real_part = Real(vctx, v);
std::cout << "Hello1\n";
storage s2 = get(real_part);
std::cout << "Hello2\n";
js::value imaginary = Imaginary(vctx, v);
std::cout << "Hello3\n";
storage s3 = get(imaginary);
std::cout << "Val " << type_to_string(s2.d.real) << std::endl;
std::cout << "Vali " << type_to_string(Imaginary(s3.c.real)) << std::endl;*/
}
}
js::value get_unit_i(js::value_context* vctx)
{
dual_complex i(dual_types::unit_i<value>());
return to_value(*vctx, i);
}
js::value extract_function(js::value_context& vctx, const std::string& script_data)
{
std::string wrapper = file::read("./number.js", file::mode::TEXT);
JS_AddIntrinsicBigFloat(vctx.ctx);
JS_AddIntrinsicBigDecimal(vctx.ctx);
JS_AddIntrinsicOperators(vctx.ctx);
JS_EnableBignumExt(vctx.ctx, true);
js::value cshim(vctx);
js::add_key_value(cshim, "add", js::function<add>);
js::add_key_value(cshim, "mul", js::function<mul>);
js::add_key_value(cshim, "sub", js::function<sub>);
js::add_key_value(cshim, "div", js::function<jdiv>);
js::add_key_value(cshim, "neg", js::function<neg>);
js::add_key_value(cshim, "construct", js::function<construct>);
js::value global = js::get_global(vctx);
js::add_key_value(global, "CShim", cshim);
js::value cmath(vctx);
js::add_key_value(cmath, "sin", js::function<CMath::sin>);
js::add_key_value(cmath, "cos", js::function<CMath::cos>);
js::add_key_value(cmath, "tan", js::function<CMath::tan>);
js::add_key_value(cmath, "asin", js::function<CMath::asin>);
js::add_key_value(cmath, "acos", js::function<CMath::acos>);
js::add_key_value(cmath, "atan", js::function<CMath::atan>);
js::add_key_value(cmath, "atan2", js::function<CMath::atan2>);
js::add_key_value(cmath, "fabs", js::function<CMath::fabs>);
js::add_key_value(cmath, "log", js::function<CMath::log>);
js::add_key_value(cmath, "select", js::function<CMath::select>);
js::add_key_value(cmath, "pow", js::function<CMath::pow>);
js::add_key_value(cmath, "sqrt", js::function<CMath::sqrt>);
js::add_key_value(cmath, "psqrt", js::function<CMath::psqrt>);
js::add_key_value(cmath, "csqrt", js::function<CMath::csqrt>);
js::add_key_value(cmath, "exp", js::function<CMath::exp>);
js::add_key_value(cmath, "fast_length", js::function<CMath::fast_length>);
js::add_key_value(cmath, "length", js::function<CMath::fast_length>);
js::add_key_value(cmath, "smooth_fmod", js::function<CMath::smooth_fmod>);
js::add_key_value(cmath, "sinh", js::function<CMath::sinh>);
js::add_key_value(cmath, "cosh", js::function<CMath::cosh>);
js::add_key_value(cmath, "tanh", js::function<CMath::tanh>);
js::add_key_value(cmath, "M_PI", js::make_value(vctx, M_PI));
js::add_key_value(cmath, "PI", js::make_value(vctx, M_PI));
js::add_key_value(cmath, "conjugate", js::function<CMath::conjugate>);
js::add_key_value(cmath, "self_conjugate_multiply", js::function<CMath::self_conjugate_multiply>);
js::add_key_value(cmath, "Real", js::function<CMath::Real>);
js::add_key_value(cmath, "Imaginary", js::function<CMath::Imaginary>);
js::add_key_value(cmath, "debug", js::function<CMath::debug>);
js::add_key_value(cmath, "get_i", js::function<get_unit_i>);
js::add_key_value(cmath, "lt", js::function<lt>);
js::add_key_value(cmath, "lte", js::function<lte>);
js::add_key_value(cmath, "eq", js::function<eq>);
js::add_key_value(cmath, "gt", js::function<gt>);
js::add_key_value(cmath, "gte", js::function<gte>);
js::add_key_value(global, "CMath", cmath);
js::add_key_value(global, "M_PI", js::make_value(vctx, M_PI));
js::add_key_value(global, "$pin", js::function<pin>);
js::value result = js::eval(vctx, wrapper);
printj((std::string)result);
return js::eval(vctx, script_data);
}
std::pair<js::value, js::value> get_proxy_handlers(js::value_context& vctx)
{
js::value dummy_func = js::make_value(vctx, js::function<js::empty_function>);
js::value dummy_obj(vctx);
return {dummy_func, dummy_obj};
}
void validate(const std::string& in)
{
for(char c : in)
{
if(!(std::isalnum(c) || c == '_'))
throw std::runtime_error("Value must be alphanumeric or _");
}
}
js::value setter_set_default(js::value_context* vctx, js::value value)
{
js::value object = js::get_this(*vctx);
storage s = get(object);
if(s.which != 0)
throw std::runtime_error("Something really weirds happened in setter_set_default");
if(!s.d.real.is_value())
throw std::runtime_error("Must be pseudoconstant value in $default set");
std::string name = type_to_string(s.d.real);
if(name.starts_with("cfg->"))
{
for(int i=0; i < (int)strlen("cfg->"); i++)
name.erase(name.begin());
}
validate(name);
float valf = (double)value;
sandbox* sand = js::get_sandbox_data<sandbox>(*vctx);
assert(sand);
sand->cfg.set_default(name, valf);
return js::make_value(*vctx, 0.f);
}
js::value cfg_proxy_get(js::value_context* vctx, js::value target, js::value prop, js::value receiver)
{
std::string key = prop;
validate(key);
sandbox* sand = js::get_sandbox_data<sandbox>(*vctx);
assert(sand);
sand->cfg.add(key, 0.f);
dual v;
v.make_constant("cfg->" + key);
js::value result = to_value(*vctx, v);
js::add_getter_setter(result, "$default", js::function<js::empty_function>, js::function<setter_set_default>);
return result;
}
js::value cfg_proxy_set(js::value_context* vctx, js::value target, js::value prop, js::value val, js::value receiver)
{
std::string key = prop;
std::cout << "Warning, setting a config from js" << std::endl;
return js::make_success(*vctx);
}
js::value finish_proxy(js::value& func, js::value& object)
{
object.get("get") = js::function<cfg_proxy_get>;
object.get("set") = js::function<cfg_proxy_set>;
return js::make_proxy(func, object);
}
js::value cfg_getter(js::value_context* vctx)
{
auto [func, object] = get_proxy_handlers(*vctx);
return finish_proxy(func, object);
}
void inject_config(js::value_context& vctx)
{
js::value global = js::get_global(vctx);
js::add_getter_setter(global, "$cfg", js::function<cfg_getter>, js::function<js::empty_function>);
}
js_metric::js_metric(sandbox& sand, const std::string& script_data) : vctx(nullptr, &sand), func(vctx)
{
func = extract_function(vctx, script_data);
inject_config(vctx);
}
std::array<dual, 16> js_metric::operator()(dual t, dual r, dual theta, dual phi)
{
js::value v1 = to_value(vctx, t);
js::value v2 = to_value(vctx, r);
js::value v3 = to_value(vctx, theta);
js::value v4 = to_value(vctx, phi);
if(func.is_error() || func.is_exception())
{
std::cout << "Function object error (16x4) " << func.to_error_message() << std::endl;
throw std::runtime_error("Err");
}
if(!func.is_function())
{
std::cout << "Expected function in eval of script" << std::endl;
throw std::runtime_error("Func eval fail");
}
auto [success, result] = js::call(func, v1, v2, v3, v4);
if(!success)
throw std::runtime_error("Error in script exec (16x4) " + (std::string)result.to_error_message());
if(!result.is_array())
throw std::runtime_error("Must return array");
std::vector<js::value> values = result;
if(values.size() == 4)
{
return {getr(values[0]), 0, 0, 0,
0, getr(values[1]), 0, 0,
0, 0, getr(values[2]), 0,
0, 0, 0, getr(values[3])};
}
else
{
if(values.size() != 16)
throw std::runtime_error("Must return array length of 4 or 16");
return {getr(values[0]), getr(values[1]), getr(values[2]), getr(values[3]),
getr(values[4]), getr(values[5]), getr(values[6]), getr(values[7]),
getr(values[8]), getr(values[9]), getr(values[10]),getr(values[11]),
getr(values[12]),getr(values[13]),getr(values[14]),getr(values[15])};
}
}
js_function::js_function(sandbox& sand, const std::string& script_data) : vctx(nullptr, &sand), func(vctx)
{
func = extract_function(vctx, script_data);
inject_config(vctx);
}
std::array<dual, 4> js_function::operator()(dual iv1, dual iv2, dual iv3, dual iv4)
{
js::value v1 = to_value(vctx, iv1);
js::value v2 = to_value(vctx, iv2);
js::value v3 = to_value(vctx, iv3);
js::value v4 = to_value(vctx, iv4);
if(func.is_error() || func.is_exception())
{
std::cout << "Function object error (4x4) " << func.to_error_message() << std::endl;
throw std::runtime_error("Err");
}
auto [success, result] = js::call(func, v1, v2, v3, v4);
if(!success)
throw std::runtime_error("Error in 4x4 execution " + (std::string)result.to_error_message());
if(!result.is_array())
throw std::runtime_error("Must return array");
std::vector<js::value> values = result;
if(values.size() != 4)
throw std::runtime_error("Must return array size of 4");
return {getr(values[0]), getr(values[1]), getr(values[2]), getr(values[3])};
}
js_single_function::js_single_function(sandbox& sand, const std::string& script_data) : vctx(nullptr, &sand), func(vctx)
{
func = extract_function(vctx, script_data);
inject_config(vctx);
}
dual js_single_function::operator()(dual iv1, dual iv2, dual iv3, dual iv4)
{
js::value v1 = to_value(vctx, iv1);
js::value v2 = to_value(vctx, iv2);
js::value v3 = to_value(vctx, iv3);
js::value v4 = to_value(vctx, iv4);
auto [success, result] = js::call(func, v1, v2, v3, v4);
if(!success)
throw std::runtime_error("Error in 1x4 exec " + (std::string)result.to_error_message());
return {getr(result)};
}