-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
embind.test.js
2969 lines (2523 loc) · 112 KB
/
embind.test.js
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
module({
Emscripten: '../../../../build/embind_test.js',
}, function(imports) {
/*jshint sub:true */
/* global console */
var cm = imports.Emscripten;
var CheckForLeaks = fixture("check for leaks", function() {
this.setUp(function() {
cm.setDelayFunction(undefined);
if (typeof INVOKED_FROM_EMSCRIPTEN_TEST_RUNNER === "undefined") { // TODO: Enable this to work in Emscripten runner as well!
assert.equal(0, cm.count_emval_handles());
}
});
this.tearDown(function() {
cm.flushPendingDeletes();
if (typeof INVOKED_FROM_EMSCRIPTEN_TEST_RUNNER === "undefined") { // TODO: Enable this to work in Emscripten runner as well!
assert.equal(0, cm.count_emval_handles());
}
});
});
var BaseFixture = CheckForLeaks;
BaseFixture.extend("temp jig", function() {
test("temp test", function() {
});
});
BaseFixture.extend("access to base class members", function() {
test("method name in derived class silently overrides inherited name", function() {
var derived = new cm.Derived();
assert.equal("Derived", derived.getClassName());
derived.delete();
});
test("can reference base method from derived class", function(){
var derived = new cm.Derived();
assert.equal("Base", derived.getClassNameFromBase());
derived.delete();
});
test("can reference base method from doubly derived class", function() {
var derivedTwice = new cm.DerivedTwice();
assert.equal("Base", derivedTwice.getClassNameFromBase());
derivedTwice.delete();
});
test("can reference base method through unbound classes", function() {
var derivedThrice = new cm.DerivedThrice();
assert.equal("Base", derivedThrice.getClassNameFromBase());
derivedThrice.delete();
});
test("property name in derived class hides identically named property in base class for set", function() {
var derived = new cm.Derived();
derived.setMember(7);
derived.member = 17;
assert.equal(17, derived.getMember());
derived.delete();
});
test("can reference base property from derived class for get", function(){
var derived = new cm.Derived();
derived.setBaseMember(5);
assert.equal(5, derived.baseMember);
derived.delete();
});
test("can reference property of any base class for get when multiply derived", function(){
var derived = new cm.MultiplyDerived();
derived.setBaseMember(11);
assert.equal(11, derived.baseMember);
derived.delete();
});
test("can reference base property from derived class for set", function(){
var derived = new cm.Derived();
derived.baseMember = 32;
assert.equal(32, derived.getBaseMember());
derived.delete();
});
test("can reference property of any base for set when multiply derived", function(){
var derived = new cm.MultiplyDerived();
derived.setBaseMember(97);
derived.baseMember = 32;
assert.equal(32, derived.getBaseMember());
derived.delete();
});
test("can reach around derived property to access base property with same name for get", function() {
var derived = new cm.Derived();
derived.setMember(12);
derived.delete();
});
test("if deriving from second base adjusts pointer", function() {
var derived = new cm.HasTwoBases;
assert.equal("Base2", derived.getField());
derived.delete();
});
test("properties adjust pointer", function() {
var derived = new cm.HasTwoBases;
derived.field = "Foo";
assert.equal("Foo", derived.getField());
assert.equal("Foo", derived.field);
derived.delete();
});
test("class functions are inherited in subclasses", function() {
assert.equal("Base", cm.Base.classFunction());
assert.equal("Derived", cm.Derived.classFunction());
assert.equal("Derived", cm.DerivedTwice.classFunction());
});
test("calling method on unrelated class throws error", function() {
var a = new cm.HasTwoBases;
var e = assert.throws(cm.BindingError, function() {
cm.Derived.prototype.setMember.call(a, "foo");
});
assert.equal('Expected null or instance of Derived, got an instance of Base2', e.message);
a.delete();
// Base1 and Base2 both have the method 'getField()' exposed - make sure
// that calling the Base2 function with a 'this' instance of Base1 doesn't accidentally work!
var b = new cm.Base1;
var e = assert.throws(cm.BindingError, function() {
cm.Base2.prototype.getField.call(b);
});
assert.equal('Expected null or instance of Base2, got an instance of Base1', e.message);
b.delete();
});
test("calling method with invalid this throws error", function() {
var e = assert.throws(cm.BindingError, function() {
cm.Derived.prototype.setMember.call(undefined, "foo");
});
assert.matches(/Cannot pass "(undefined|\[object global\])" as a Derived\*/, e.message);
var e = assert.throws(cm.BindingError, function() {
cm.Derived.prototype.setMember.call(true, "foo");
});
assert.equal('Cannot pass "true" as a Derived*', e.message);
if (cm.getCompilerSetting('STRICT_JS') && !cm.getCompilerSetting('DYNAMIC_EXECUTION')) {
var e = assert.throws(TypeError, function() {
cm.Derived.prototype.setMember.call(null, "foo");
});
assert.equal('Cannot convert "foo" to int', e.message)
} else {
var e = assert.throws(cm.BindingError, function() {
cm.Derived.prototype.setMember.call(null, "foo");
});
assert.equal('Cannot pass "[object global]" as a Derived*', e.message)
}
var e = assert.throws(cm.BindingError, function() {
cm.Derived.prototype.setMember.call(42, "foo");
});
assert.equal('Cannot pass "42" as a Derived*', e.message);
var e = assert.throws(cm.BindingError, function() {
cm.Derived.prototype.setMember.call("this", "foo");
});
assert.equal('Cannot pass "this" as a Derived*', e.message);
var e = assert.throws(cm.BindingError, function() {
cm.Derived.prototype.setMember.call({}, "foo");
});
assert.matches(/Cannot pass "(undefined|\[object Object\])" as a Derived\*/, e.message);
});
test("setting and getting property on unrelated class throws error", function() {
var a = new cm.HasTwoBases;
var e = assert.throws(cm.BindingError, function() {
Object.getOwnPropertyDescriptor(cm.HeldBySmartPtr.prototype, 'i').set.call(a, 10);
});
assert.equal('HeldBySmartPtr.i setter incompatible with "this" of type HasTwoBases', e.message);
var e = assert.throws(cm.BindingError, function() {
Object.getOwnPropertyDescriptor(cm.HeldBySmartPtr.prototype, 'i').get.call(a);
});
assert.equal('HeldBySmartPtr.i getter incompatible with "this" of type HasTwoBases', e.message);
a.delete();
});
});
BaseFixture.extend("automatic upcasting of parameters passed to C++", function() {
// raw
test("raw pointer argument is upcast to parameter type", function() {
var derived = new cm.Derived();
var name = cm.embind_test_get_class_name_via_base_ptr(derived);
assert.equal("Base", name);
derived.delete();
});
test("automatic raw pointer upcasting works with multiple inheritance", function() {
var derived = new cm.MultiplyDerived();
var name = cm.embind_test_get_class_name_via_base_ptr(derived);
assert.equal("Base", name);
derived.delete();
});
test("automatic raw pointer upcasting does not change local pointer", function() {
var derived = new cm.MultiplyDerived();
cm.embind_test_get_class_name_via_base_ptr(derived);
assert.equal("MultiplyDerived", derived.getClassName());
derived.delete();
});
test("passing incompatible raw pointer to method throws exception", function() {
var base = new cm.Base();
assert.throws(cm.BindingError, function() {
cm.embind_test_get_class_name_via_second_base_ptr(base);
});
base.delete();
});
// raw polymorphic
test("polymorphic raw pointer argument is upcast to parameter type", function() {
var derived = new cm.PolyDerived();
var name = cm.embind_test_get_class_name_via_polymorphic_base_ptr(derived);
assert.equal("PolyBase", name);
derived.delete();
});
test("automatic polymorphic raw pointer upcasting works with multiple inheritance", function() {
var derived = new cm.PolyMultiplyDerived();
var name = cm.embind_test_get_class_name_via_polymorphic_base_ptr(derived);
assert.equal("PolyBase", name);
derived.delete();
});
test("passing incompatible raw polymorphic pointer to method throws exception", function() {
var base = new cm.PolyBase();
assert.throws(cm.BindingError, function() {
cm.embind_test_get_class_name_via_polymorphic_second_base_ptr(base);
});
base.delete();
});
// smart
test("can pass smart pointer to raw pointer parameter", function() {
var smartBase = cm.embind_test_return_smart_base_ptr();
assert.equal("Base", cm.embind_test_get_class_name_via_base_ptr(smartBase));
smartBase.delete();
});
test("can pass and upcast smart pointer to raw pointer parameter", function() {
var smartDerived = cm.embind_test_return_smart_derived_ptr();
assert.equal("Base", cm.embind_test_get_class_name_via_base_ptr(smartDerived));
smartDerived.delete();
});
test("smart pointer argument is upcast to parameter type", function() {
var derived = cm.embind_test_return_smart_derived_ptr();
assert.instanceof(derived, cm.Derived);
assert.instanceof(derived, cm.Base);
var name = cm.embind_test_get_class_name_via_smart_base_ptr(derived);
assert.equal("Base", name);
derived.delete();
});
test("return smart derived ptr as base", function() {
var derived = cm.embind_test_return_smart_derived_ptr_as_base();
assert.equal("PolyDerived", cm.embind_test_get_virtual_class_name_via_smart_polymorphic_base_ptr(derived));
assert.equal("PolyDerived", derived.getClassName());
derived.delete();
});
test("return smart derived ptr as val", function() {
var derived = cm.embind_test_return_smart_derived_ptr_as_val();
assert.equal("PolyDerived", cm.embind_test_get_virtual_class_name_via_smart_polymorphic_base_ptr(derived));
derived.delete();
});
test("automatic smart pointer upcasting works with multiple inheritance", function() {
var derived = cm.embind_test_return_smart_multiply_derived_ptr();
var name = cm.embind_test_get_class_name_via_smart_base_ptr(derived);
assert.equal("Base", name);
derived.delete();
});
test("automatically upcasted smart pointer parameter shares ownership with original argument", function() {
var derived = cm.embind_test_return_smart_multiply_derived_ptr();
assert.equal(1, cm.MultiplyDerived.getInstanceCount());
cm.embind_save_smart_base_pointer(derived);
assert.equal(1, cm.MultiplyDerived.getInstanceCount());
derived.delete();
assert.equal(1, cm.MultiplyDerived.getInstanceCount());
cm.embind_save_smart_base_pointer(null);
assert.equal(0, cm.MultiplyDerived.getInstanceCount());
});
// smart polymorphic
test("smart polymorphic pointer argument is upcast to parameter type", function() {
var derived = cm.embind_test_return_smart_polymorphic_derived_ptr();
var name = cm.embind_test_get_class_name_via_smart_polymorphic_base_ptr(derived);
assert.equal("PolyBase", name);
derived.delete();
});
test("automatic smart polymorphic pointer upcasting works with multiple inheritance", function() {
var derived = cm.embind_test_return_smart_polymorphic_multiply_derived_ptr();
var name = cm.embind_test_get_class_name_via_smart_polymorphic_base_ptr(derived);
assert.equal("PolyBase", name);
derived.delete();
});
});
BaseFixture.extend("automatic downcasting of return values received from C++", function() {
// raw
test("non-polymorphic raw pointers are not downcast and do not break automatic casting mechanism", function() {
var base = cm.embind_test_return_raw_derived_ptr_as_base();
assert.equal("Base", base.getClassName());
assert.instanceof(base, cm.Base);
base.delete();
});
// raw polymorphic
test("polymorphic raw pointer return value is downcast to allocated type (if that is bound)", function() {
var derived = cm.embind_test_return_raw_polymorphic_derived_ptr_as_base();
assert.instanceof(derived, cm.PolyBase);
assert.instanceof(derived, cm.PolyDerived);
assert.equal("PolyDerived", derived.getClassName());
var siblingDerived = cm.embind_test_return_raw_polymorphic_sibling_derived_ptr_as_base();
assert.equal("PolySiblingDerived", siblingDerived.getClassName());
siblingDerived.delete();
derived.delete();
});
test("polymorphic raw pointer return value is downcast to the most derived bound type", function() {
var derivedThrice = cm.embind_test_return_raw_polymorphic_derived_four_times_not_bound_as_base();
// if the actual returned type is not bound, then don't assume anything
assert.equal("PolyBase", derivedThrice.getClassName());
// if we ever fix this, then reverse the assertion
//assert.equal("PolyDerivedThrice", derivedThrice.getClassName());
derivedThrice.delete();
});
test("polymorphic smart pointer return value is downcast to the most derived type which has an associated smart pointer", function() {
var derived = cm.embind_test_return_poly_derived_twice_without_smart_pointer_as_poly_base();
// if the actual returned type is not bound, then don't assume anything
assert.equal("PolyBase", derived.getClassName());
// if we ever fix this, then remove the assertion
//assert.equal("PolyDerived", derived.getClassName());
derived.delete();
});
test("automatic downcasting works with multiple inheritance", function() {
var base = cm.embind_test_return_raw_polymorphic_multiply_derived_ptr_as_base();
var secondBase = cm.embind_test_return_raw_polymorphic_multiply_derived_ptr_as_second_base();
assert.equal("PolyMultiplyDerived", base.getClassName());
// embind does not support multiple inheritance
//assert.equal("PolyMultiplyDerived", secondBase.getClassName());
secondBase.delete();
base.delete();
});
// smart
test("non-polymorphic smart pointers do not break automatic casting mechanism", function() {
});
// smart polymorphic
test("automatically downcasting a smart pointer does not change the underlying pointer", function() {
cm.PolyDerived.setPtrDerived();
assert.equal("PolyBase", cm.PolyDerived.getPtrClassName());
var derived = cm.PolyDerived.getPtr();
assert.equal("PolyDerived", derived.getClassName());
assert.equal("PolyBase", cm.PolyDerived.getPtrClassName());
derived.delete();
cm.PolyDerived.releasePtr();
});
test("polymorphic smart pointer return value is actual allocated type (when bound)", function() {
var derived = cm.embind_test_return_smart_polymorphic_derived_ptr_as_base();
assert.equal("PolyDerived", derived.getClassName());
var siblingDerived = cm.embind_test_return_smart_polymorphic_sibling_derived_ptr_as_base();
assert.equal("PolySiblingDerived", siblingDerived.getClassName());
siblingDerived.delete();
derived.delete();
});
});
BaseFixture.extend("string", function() {
var stdStringIsUTF8 = cm.getCompilerSetting('EMBIND_STD_STRING_IS_UTF8');
test("non-ascii strings", function() {
var expected = '';
if(stdStringIsUTF8) {
//ASCII
expected = 'aei';
//Latin-1 Supplement
expected += '\u00E1\u00E9\u00ED';
//Greek
expected += '\u03B1\u03B5\u03B9';
//Cyrillic
expected += '\u0416\u041B\u0424';
//CJK
expected += '\u5F9E\u7345\u5B50';
//Euro sign
expected += '\u20AC';
} else {
for (var i = 0; i < 128; ++i) {
expected += String.fromCharCode(128 + i);
}
}
assert.equal(expected, cm.get_non_ascii_string(stdStringIsUTF8));
});
if(!stdStringIsUTF8) {
test("passing non-8-bit strings from JS to std::string throws", function() {
assert.throws(cm.BindingError, function() {
cm.emval_test_take_and_return_std_string("\u1234");
});
});
}
test("can't pass integers as strings", function() {
var e = assert.throws(cm.BindingError, function() {
cm.emval_test_take_and_return_std_string(10);
});
});
test("can pass Uint8Array to std::string", function() {
var e = cm.emval_test_take_and_return_std_string(new Uint8Array([65, 66, 67, 68]));
assert.equal('ABCD', e);
});
test("can pass Uint8ClampedArray to std::string", function() {
var e = cm.emval_test_take_and_return_std_string(new Uint8ClampedArray([65, 66, 67, 68]));
assert.equal('ABCD', e);
});
test("can pass Int8Array to std::string", function() {
var e = cm.emval_test_take_and_return_std_string(new Int8Array([65, 66, 67, 68]));
assert.equal('ABCD', e);
});
test("can pass ArrayBuffer to std::string", function() {
var e = cm.emval_test_take_and_return_std_string((new Int8Array([65, 66, 67, 68])).buffer);
assert.equal('ABCD', e);
});
test("can pass Uint8Array to std::basic_string<unsigned char>", function() {
var e = cm.emval_test_take_and_return_std_basic_string_unsigned_char(new Uint8Array([65, 66, 67, 68]));
assert.equal('ABCD', e);
});
test("can pass long string to std::basic_string<unsigned char>", function() {
var s = 'this string is long enough to exceed the short string optimization';
var e = cm.emval_test_take_and_return_std_basic_string_unsigned_char(s);
assert.equal(s, e);
});
test("can pass Uint8ClampedArray to std::basic_string<unsigned char>", function() {
var e = cm.emval_test_take_and_return_std_basic_string_unsigned_char(new Uint8ClampedArray([65, 66, 67, 68]));
assert.equal('ABCD', e);
});
test("can pass Int8Array to std::basic_string<unsigned char>", function() {
var e = cm.emval_test_take_and_return_std_basic_string_unsigned_char(new Int8Array([65, 66, 67, 68]));
assert.equal('ABCD', e);
});
test("can pass ArrayBuffer to std::basic_string<unsigned char>", function() {
var e = cm.emval_test_take_and_return_std_basic_string_unsigned_char((new Int8Array([65, 66, 67, 68])).buffer);
assert.equal('ABCD', e);
});
test("can pass string to std::string", function() {
var string = stdStringIsUTF8?"aeiáéíαειЖЛФ從獅子€":"ABCD";
var e = cm.emval_test_take_and_return_std_string(string);
assert.equal(string, e);
});
var utf16TestString = String.fromCharCode(10) +
String.fromCharCode(1234) +
String.fromCharCode(2345) +
String.fromCharCode(65535);
var utf32TestString = String.fromCharCode(10) +
String.fromCharCode(1234) +
String.fromCharCode(2345) +
String.fromCharCode(55357) +
String.fromCharCode(56833) +
String.fromCharCode(55357) +
String.fromCharCode(56960);
test("non-ascii wstrings", function() {
assert.equal(utf16TestString, cm.get_non_ascii_wstring());
});
test("non-ascii u16strings", function() {
assert.equal(utf16TestString, cm.get_non_ascii_u16string());
});
test("non-ascii u32strings", function() {
assert.equal(utf32TestString, cm.get_non_ascii_u32string());
});
test("passing unicode (wide) string into C++", function() {
assert.equal(utf16TestString, cm.take_and_return_std_wstring(utf16TestString));
});
test("passing unicode (utf-16) string into C++", function() {
assert.equal(utf16TestString, cm.take_and_return_std_u16string(utf16TestString));
});
test("passing unicode (utf-32) string into C++", function() {
assert.equal(utf32TestString, cm.take_and_return_std_u32string(utf32TestString));
});
if (cm.isMemoryGrowthEnabled) {
test("can access a literal wstring after a memory growth", function() {
cm.force_memory_growth();
assert.equal("get_literal_wstring", cm.get_literal_wstring());
});
test("can access a literal u16string after a memory growth", function() {
cm.force_memory_growth();
assert.equal("get_literal_u16string", cm.get_literal_u16string());
});
test("can access a literal u32string after a memory growth", function() {
cm.force_memory_growth();
assert.equal("get_literal_u32string", cm.get_literal_u32string());
});
}
});
BaseFixture.extend("embind", function() {
test("value creation", function() {
assert.equal(15, cm.emval_test_new_integer());
assert.equal("Hello everyone", cm.emval_test_new_string());
assert.equal("Hello everyone", cm.emval_test_get_string_from_val({key: "Hello everyone"}));
var object = cm.emval_test_new_object();
assert.equal('bar', object.foo);
assert.equal(1, object.baz);
});
test("pass const reference to primitive", function() {
assert.equal(3, cm.const_ref_adder(1, 2));
});
test("get instance pointer as value", function() {
var v = cm.emval_test_instance_pointer();
assert.instanceof(v, cm.DummyForPointer);
});
test("cast value to instance pointer using as<T*>", function() {
var v = cm.emval_test_instance_pointer();
var p_value = cm.emval_test_value_from_instance_pointer(v);
assert.equal(42, p_value);
});
test("passthrough", function() {
var a = {foo: 'bar'};
var b = cm.emval_test_passthrough(a);
a.bar = 'baz';
assert.equal('baz', b.bar);
assert.equal(0, cm.count_emval_handles());
});
test("void return converts to undefined", function() {
assert.equal(undefined, cm.emval_test_return_void());
});
test("booleans can be marshalled", function() {
assert.equal(false, cm.emval_test_not(true));
assert.equal(true, cm.emval_test_not(false));
});
test("val.is_undefined() is functional",function() {
assert.equal(true, cm.emval_test_is_undefined(undefined));
assert.equal(false, cm.emval_test_is_undefined(true));
assert.equal(false, cm.emval_test_is_undefined(false));
assert.equal(false, cm.emval_test_is_undefined(null));
assert.equal(false, cm.emval_test_is_undefined({}));
});
test("val.is_null() is functional",function() {
assert.equal(true, cm.emval_test_is_null(null));
assert.equal(false, cm.emval_test_is_null(true));
assert.equal(false, cm.emval_test_is_null(false));
assert.equal(false, cm.emval_test_is_null(undefined));
assert.equal(false, cm.emval_test_is_null({}));
});
test("val.is_true() is functional",function() {
assert.equal(true, cm.emval_test_is_true(true));
assert.equal(false, cm.emval_test_is_true(false));
assert.equal(false, cm.emval_test_is_true(null));
assert.equal(false, cm.emval_test_is_true(undefined));
assert.equal(false, cm.emval_test_is_true({}));
});
test("val.is_false() is functional",function() {
assert.equal(true, cm.emval_test_is_false(false));
assert.equal(false, cm.emval_test_is_false(true));
assert.equal(false, cm.emval_test_is_false(null));
assert.equal(false, cm.emval_test_is_false(undefined));
assert.equal(false, cm.emval_test_is_false({}));
});
test("val.equals() is functional",function() {
var values = [undefined, null, true, false, {}];
for(var i=0;i<values.length;++i){
var first = values[i];
for(var j=i;j<values.length;++j)
{
var second = values[j];
/*jshint eqeqeq:false*/
assert.equal((first == second), cm.emval_test_equals(first, second));
}
}
});
test("val.strictlyEquals() is functional", function() {
var values = [undefined, null, true, false, {}];
for(var i=0;i<values.length;++i){
var first = values[i];
for(var j=i;j<values.length;++j)
{
var second = values[j];
assert.equal(first===second, cm.emval_test_strictly_equals(first, second));
}
}
});
test("can pass booleans as integers", function() {
assert.equal(1, cm.emval_test_as_unsigned(true));
assert.equal(0, cm.emval_test_as_unsigned(false));
});
test("can pass booleans as floats", function() {
assert.equal(2, cm.const_ref_adder(true, true));
});
test("passing Symbol or BigInt as floats always throws", function() {
assert.throws(TypeError, function() { cm.const_ref_adder(Symbol('0'), 1); });
assert.throws(TypeError, function() { cm.const_ref_adder(0n, 1); });
});
if (cm.getCompilerSetting('ASSERTIONS')) {
test("can pass only number and boolean as floats with assertions", function() {
assert.throws(TypeError, function() { cm.const_ref_adder(1, undefined); });
assert.throws(TypeError, function() { cm.const_ref_adder(1, null); });
assert.throws(TypeError, function() { cm.const_ref_adder(1, '2'); });
});
} else {
test("can pass other types as floats without assertions", function() {
assert.equal(3, cm.const_ref_adder(1, '2'));
assert.equal(1, cm.const_ref_adder(1, null)); // null => 0
assert.true(isNaN(cm.const_ref_adder(1, 'cannot parse')));
assert.true(isNaN(cm.const_ref_adder(1, undefined))); // undefined => NaN
});
}
test("convert double to unsigned", function() {
var rv = cm.emval_test_as_unsigned(1.5);
assert.equal('number', typeof rv);
assert.equal(1, rv);
assert.equal(0, cm.count_emval_handles());
});
test("get length of array", function() {
assert.equal(10, cm.emval_test_get_length([0, 1, 2, 3, 4, 5, 'a', 'b', 'c', 'd']));
assert.equal(0, cm.count_emval_handles());
});
test("add a bunch of things", function() {
assert.equal(66.0, cm.emval_test_add(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11));
assert.equal(0, cm.count_emval_handles());
});
test("sum array", function() {
assert.equal(66, cm.emval_test_sum([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]));
assert.equal(0, cm.count_emval_handles());
});
test("strings", function() {
assert.equal("foobar", "foo" + "bar");
assert.equal("foobar", cm.emval_test_take_and_return_std_string("foobar"));
assert.equal("foobar", cm.emval_test_take_and_return_std_string_const_ref("foobar"));
});
test("nuls pass through strings", function() {
assert.equal("foo\0bar", cm.emval_test_take_and_return_std_string("foo\0bar"));
});
test("no memory leak when passing strings in by const reference", function() {
cm.emval_test_take_and_return_std_string_const_ref("foobar");
});
test("val callback arguments are not destroyed", function() {
cm.emval_test_callback_arg_lifetime(function() {});
});
test("can get global", function(){
/*jshint evil:true*/
assert.equal((new Function("return this;"))(), cm.embind_test_getglobal());
});
test("can create new object", function() {
assert.deepEqual({}, cm.embind_test_new_Object());
});
test("can invoke constructors with arguments", function() {
function constructor(i, s, argument) {
this.i = i;
this.s = s;
this.argument = argument;
}
constructor.prototype.method = function() {
return this.argument;
};
var x = {};
var instance = cm.embind_test_new_factory(constructor, x);
assert.equal(10, instance.i);
assert.equal("hello", instance.s);
assert.equal(x, instance.argument);
});
test("can return module property objects", function() {
assert.equal(cm.HEAP8, cm.get_module_property("HEAP8"));
});
test("can return big class instances", function() {
var c = cm.embind_test_return_big_class_instance();
assert.equal(11, c.member);
c.delete();
});
test("can return small class instances", function() {
var c = cm.embind_test_return_small_class_instance();
assert.equal(7, c.member);
c.delete();
});
test("can pass small class instances", function() {
var c = new cm.SmallClass();
var m = cm.embind_test_accept_small_class_instance(c);
assert.equal(7, m);
c.delete();
});
test("can pass big class instances", function() {
var c = new cm.BigClass();
var m = cm.embind_test_accept_big_class_instance(c);
assert.equal(11, m);
c.delete();
});
test("can pass unique_ptr", function() {
var p = cm.embind_test_return_unique_ptr(42);
var m = cm.embind_test_accept_unique_ptr(p);
assert.equal(42, m);
});
test("can pass unique_ptr to constructor", function() {
var c = new cm.embind_test_construct_class_with_unique_ptr(42);
assert.equal(42, c.getValue());
c.delete();
});
test("can get member classes then call its member functions", function() {
var p = new cm.ParentClass();
var c = p.getBigClass();
var m = c.getMember();
assert.equal(11, m);
c.delete();
p.delete();
});
test('C++ -> JS primitive type range checks', function() {
// all types should have zero.
assert.equal("0", cm.char_to_string(0));
assert.equal("0", cm.signed_char_to_string(0));
assert.equal("0", cm.unsigned_char_to_string(0));
assert.equal("0", cm.short_to_string(0));
assert.equal("0", cm.unsigned_short_to_string(0));
assert.equal("0", cm.int_to_string(0));
assert.equal("0", cm.unsigned_int_to_string(0));
assert.equal("0", cm.long_to_string(0));
assert.equal("0", cm.unsigned_long_to_string(0));
// all types should have positive values.
assert.equal("5", cm.char_to_string(5));
assert.equal("5", cm.signed_char_to_string(5));
assert.equal("5", cm.unsigned_char_to_string(5));
assert.equal("5", cm.short_to_string(5));
assert.equal("5", cm.unsigned_short_to_string(5));
assert.equal("5", cm.int_to_string(5));
assert.equal("5", cm.unsigned_int_to_string(5));
assert.equal("5", cm.long_to_string(5));
assert.equal("5", cm.unsigned_long_to_string(5));
// signed types should have negative values.
assert.equal("-5", cm.char_to_string(-5)); // Assuming char as signed.
assert.equal("-5", cm.signed_char_to_string(-5));
assert.equal("-5", cm.short_to_string(-5));
assert.equal("-5", cm.int_to_string(-5));
assert.equal("-5", cm.long_to_string(-5));
// assumptions: char == signed char == 8 bits
// unsigned char == 8 bits
// short == 16 bits
// int == long == 32 bits
// all types should have their max positive values.
assert.equal("127", cm.char_to_string(127));
assert.equal("127", cm.signed_char_to_string(127));
assert.equal("255", cm.unsigned_char_to_string(255));
assert.equal("32767", cm.short_to_string(32767));
assert.equal("65535", cm.unsigned_short_to_string(65535));
assert.equal("2147483647", cm.int_to_string(2147483647));
assert.equal("4294967295", cm.unsigned_int_to_string(4294967295));
assert.equal("2147483647", cm.long_to_string(2147483647));
assert.equal("4294967295", cm.unsigned_long_to_string(4294967295));
// signed types should have their min negative values.
assert.equal("-128", cm.char_to_string(-128));
assert.equal("-128", cm.signed_char_to_string(-128));
assert.equal("-32768", cm.short_to_string(-32768));
assert.equal("-2147483648", cm.int_to_string(-2147483648));
assert.equal("-2147483648", cm.long_to_string(-2147483648));
// passing out of range values should fail with assertions.
if (cm.getCompilerSetting('ASSERTIONS')) {
assert.throws(TypeError, function() { cm.char_to_string(-129); });
assert.throws(TypeError, function() { cm.char_to_string(128); });
assert.throws(TypeError, function() { cm.signed_char_to_string(-129); });
assert.throws(TypeError, function() { cm.signed_char_to_string(128); });
assert.throws(TypeError, function() { cm.unsigned_char_to_string(-1); });
assert.throws(TypeError, function() { cm.unsigned_char_to_string(256); });
assert.throws(TypeError, function() { cm.short_to_string(-32769); });
assert.throws(TypeError, function() { cm.short_to_string(32768); });
assert.throws(TypeError, function() { cm.unsigned_short_to_string(-1); });
assert.throws(TypeError, function() { cm.unsigned_short_to_string(65536); });
assert.throws(TypeError, function() { cm.int_to_string(-2147483649); });
assert.throws(TypeError, function() { cm.int_to_string(2147483648); });
assert.throws(TypeError, function() { cm.unsigned_int_to_string(-1); });
assert.throws(TypeError, function() { cm.unsigned_int_to_string(4294967296); });
if (cm.getCompilerSetting('MEMORY64')) {
assert.throws(TypeError, function() { cm.long_to_string(-18446744073709551616n); });
assert.throws(TypeError, function() { cm.long_to_string(18446744073709551616n); });
assert.throws(TypeError, function() { cm.unsigned_long_to_string(-1n); });
assert.throws(TypeError, function() { cm.unsigned_long_to_string(18446744073709551616n); });
} else {
assert.throws(TypeError, function() { cm.long_to_string(-2147483649); });
assert.throws(TypeError, function() { cm.long_to_string(2147483648); });
assert.throws(TypeError, function() { cm.unsigned_long_to_string(-1); });
assert.throws(TypeError, function() { cm.unsigned_long_to_string(4294967296); });
}
} else {
// test that an out of range value doesn't throw without assertions.
assert.equal("-129", cm.char_to_string(-129));
}
});
test("unsigned values are correctly returned when stored in memory", function() {
cm.store_unsigned_char(255);
assert.equal(255, cm.load_unsigned_char());
cm.store_unsigned_short(32768);
assert.equal(32768, cm.load_unsigned_short());
cm.store_unsigned_int(2147483648);
assert.equal(2147483648, cm.load_unsigned_int());
cm.store_unsigned_long(2147483648);
assert.equal(2147483648, cm.load_unsigned_long());
});
if (cm.getCompilerSetting('ASSERTIONS')) {
test("throws type error when attempting to coerce null to int", function() {
var e = assert.throws(TypeError, function() {
cm.int_to_string(null);
});
assert.equal('Cannot convert "null" to int', e.message);
});
} else {
test("null is converted to 0 without assertions", function() {
assert.equal('0', cm.int_to_string(null));
});
}
test("access multiple class ctors", function() {
var a = new cm.MultipleCtors(10);
assert.equal(a.WhichCtorCalled(), 1);
var b = new cm.MultipleCtors(20, 20);
assert.equal(b.WhichCtorCalled(), 2);
var c = new cm.MultipleCtors(30, 30, 30);
assert.equal(c.WhichCtorCalled(), 3);
a.delete();
b.delete();
c.delete();
});
test("access multiple smart ptr ctors", function() {
var a = new cm.MultipleSmartCtors(10);
assert.equal(a.WhichCtorCalled(), 1);
var b = new cm.MultipleCtors(20, 20);
assert.equal(b.WhichCtorCalled(), 2);
a.delete();
b.delete();
});
test("wrong number of constructor arguments throws", function() {
assert.throws(cm.BindingError, function() { new cm.MultipleCtors(); });
assert.throws(cm.BindingError, function() { new cm.MultipleCtors(1,2,3,4); });
});
test("overloading of free functions", function() {
var a = cm.overloaded_function(10);
assert.equal(a, 1);
var b = cm.overloaded_function(20, 20);
assert.equal(b, 2);
});
test("wrong number of arguments to an overloaded free function", function() {
assert.throws(cm.BindingError, function() { cm.overloaded_function(); });
assert.throws(cm.BindingError, function() { cm.overloaded_function(30, 30, 30); });
});
test("overloading of class member functions", function() {
var foo = new cm.MultipleOverloads();
assert.equal(foo.Func(10), 1);
assert.equal(foo.WhichFuncCalled(), 1);
assert.equal(foo.Func(20, 20), 2);
assert.equal(foo.WhichFuncCalled(), 2);
foo.delete();
});
test("wrong number of arguments to an overloaded class member function", function() {
var foo = new cm.MultipleOverloads();
assert.throws(cm.BindingError, function() { foo.Func(); });
assert.throws(cm.BindingError, function() { foo.Func(30, 30, 30); });
foo.delete();
});
test("wrong number of arguments to an overloaded class static function", function() {
assert.throws(cm.BindingError, function() { cm.MultipleOverloads.StaticFunc(); });
assert.throws(cm.BindingError, function() { cm.MultipleOverloads.StaticFunc(30, 30, 30); });
});
test("overloading of derived class member functions", function() {
var foo = new cm.MultipleOverloadsDerived();
// NOTE: In C++, default lookup rules will hide overloads from base class if derived class creates them.
// In JS, we make the base class overloads implicitly available. In C++, they would need to be explicitly
// invoked, like foo.MultipleOverloads::Func(10);
assert.equal(foo.Func(10), 1);
assert.equal(foo.WhichFuncCalled(), 1);
assert.equal(foo.Func(20, 20), 2);
assert.equal(foo.WhichFuncCalled(), 2);
assert.equal(foo.Func(30, 30, 30), 3);
assert.equal(foo.WhichFuncCalled(), 3);
assert.equal(foo.Func(40, 40, 40, 40), 4);
assert.equal(foo.WhichFuncCalled(), 4);
foo.delete();
});
test("overloading of class static functions", function() {
assert.equal(cm.MultipleOverloads.StaticFunc(10), 1);
assert.equal(cm.MultipleOverloads.WhichStaticFuncCalled(), 1);
assert.equal(cm.MultipleOverloads.StaticFunc(20, 20), 2);
assert.equal(cm.MultipleOverloads.WhichStaticFuncCalled(), 2);
});
test("overloading of derived class static functions", function() {
assert.equal(cm.MultipleOverloadsDerived.StaticFunc(30, 30, 30), 3);
// TODO: Cannot access static member functions of a Base class via Derived.
// assert.equal(cm.MultipleOverloadsDerived.WhichStaticFuncCalled(), 3);
assert.equal(cm.MultipleOverloads.WhichStaticFuncCalled(), 3);
assert.equal(cm.MultipleOverloadsDerived.StaticFunc(40, 40, 40, 40), 4);
// TODO: Cannot access static member functions of a Base class via Derived.
// assert.equal(cm.MultipleOverloadsDerived.WhichStaticFuncCalled(), 4);
assert.equal(cm.MultipleOverloads.WhichStaticFuncCalled(), 4);
});
/*
test("can get templated member classes then call its member functions", function() {
var p = new cm.ContainsTemplatedMemberClass();
var c = p.getTestTemplate();