Source

Coin / src / vrml97 / JS_VRMLClasses.cpp

Full commit
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H

#if defined(HAVE_VRML97) && defined(COIN_HAVE_JAVASCRIPT)

/**************************************************************************\
 * Copyright (c) Kongsberg Oil & Gas Technologies AS
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the copyright holder nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\**************************************************************************/

#include "JS_VRMLClasses.h"

#include <Inventor/actions/SoWriteAction.h>
#include <Inventor/misc/SoJavaScriptEngine.h>
#include <Inventor/C/glue/spidermonkey.h>
#include <Inventor/fields/SoSFBool.h>
#include <Inventor/fields/SoSFInt32.h>
#include <Inventor/fields/SoSFEnum.h>
#include <Inventor/fields/SoSFFloat.h>
#include <Inventor/fields/SoSFString.h>
#include <Inventor/fields/SoSFTime.h>
#include <Inventor/fields/SoSFColor.h>
#include <Inventor/fields/SoSFNode.h>
#include <Inventor/fields/SoSFImage.h>
#include <Inventor/fields/SoSFVec2f.h>
#include <Inventor/fields/SoSFVec3f.h>
#include <Inventor/fields/SoSFVec3d.h>
#include <Inventor/fields/SoSFRotation.h>
#include <Inventor/fields/SoMFInt32.h>
#include <Inventor/fields/SoMFFloat.h>
#include <Inventor/fields/SoMFString.h>
#include <Inventor/fields/SoMFTime.h>
#include <Inventor/fields/SoMFColor.h>
#include <Inventor/fields/SoMFNode.h>
#include <Inventor/fields/SoMFVec2f.h>
#include <Inventor/fields/SoMFVec3f.h>
#include <Inventor/fields/SoMFVec3d.h>
#include <Inventor/fields/SoMFRotation.h>
#include <Inventor/nodes/SoGroup.h>
#include <Inventor/nodes/SoSeparator.h>
#include <Inventor/SoDB.h>
#include <Inventor/SoInput.h>
#include <Inventor/SoOutput.h>
#include <Inventor/VRMLnodes/SoVRMLGroup.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/sensors/SoNodeSensor.h>

#include "misc/SbHash.h"
#include "tidbitsp.h"

// FIXME: toString() missing for all classes

// "namespace" for all vrml classes
struct CoinVrmlJs {
  struct ClassDescriptor {
    JSClass cls;
    JSFunctionSpec * functions;

  };

  static ClassDescriptor SFColor;
  static ClassDescriptor SFNode;
  static ClassDescriptor SFRotation;
  static ClassDescriptor SFVec2f;
  static ClassDescriptor SFVec3f;
  static ClassDescriptor SFVec3d;

  static ClassDescriptor MFColor;
  static ClassDescriptor MFFloat;
  static ClassDescriptor MFInt32;
  static ClassDescriptor MFNode;
  static ClassDescriptor MFRotation;
  static ClassDescriptor MFString;
  static ClassDescriptor MFTime;
  static ClassDescriptor MFVec2f;
  static ClassDescriptor MFVec3f;
  static ClassDescriptor MFVec3d;
};

// Struct and SbHash for keeping track of SoNodeSensors for recycling
// purposes.
struct CoinVrmlJs_SensorInfo {
  SbList <JSObject *> objects;
};
SbHash<unsigned long, void *> * CoinVrmlJs_sensorinfohash = NULL;


const char * CoinVrmlJs_SFColorAliases[] = {"r", "g", "b"};
const char * CoinVrmlJs_SFRotationAliases[] = {"x", "y", "z", "angle"};
float CoinVrmlJs_SFdefaultValues[] = {0.0, 0.0, 0.0, 0.0};
double CoinVrmlJs_SFdefaultValuesDouble[] = {0.0, 0.0, 0.0, 0.0};
float CoinVrmlJs_SFRotationDefaultValues[] = {0.0, 1.0, 0.0, 0.0};

// Macros for instance checking
#define JSVAL_IS_SFVEC2F(cx, jsval) (JSVAL_IS_OBJECT(jsval) && spidermonkey()->JS_InstanceOf(cx, JSVAL_TO_OBJECT(jsval), &CoinVrmlJs::SFVec2f.cls, NULL))
#define JSVAL_IS_SFVEC3F(cx, jsval) (JSVAL_IS_OBJECT(jsval) && spidermonkey()->JS_InstanceOf(cx, JSVAL_TO_OBJECT(jsval), &CoinVrmlJs::SFVec3f.cls, NULL))
#define JSVAL_IS_SFVEC3D(cx, jsval) (JSVAL_IS_OBJECT(jsval) && spidermonkey()->JS_InstanceOf(cx, JSVAL_TO_OBJECT(jsval), &CoinVrmlJs::SFVec3d.cls, NULL))
#define JSVAL_IS_SFCOLOR(cx, jsval) (JSVAL_IS_OBJECT(jsval) && spidermonkey()->JS_InstanceOf(cx, JSVAL_TO_OBJECT(jsval), &CoinVrmlJs::SFColor.cls, NULL))
#define JSVAL_IS_SFROTATION(cx, jsval) (JSVAL_IS_OBJECT(jsval) && spidermonkey()->JS_InstanceOf(cx, JSVAL_TO_OBJECT(jsval), &CoinVrmlJs::SFRotation.cls, NULL))

// Handlers
#define SFColorHandler CoinVrmlJsSFHandler<SbColor, 3, CoinVrmlJs_SFColorAliases, float, CoinVrmlJs_SFdefaultValues>
#define SFRotationHandler CoinVrmlJsSFHandler<SbVec4f, 4, CoinVrmlJs_SFRotationAliases, float, CoinVrmlJs_SFRotationDefaultValues>
#define SFVec2fHandler CoinVrmlJsSFHandler<SbVec2f, 2, CoinVrmlJs_SFRotationAliases, float, CoinVrmlJs_SFdefaultValues>
#define SFVec3fHandler CoinVrmlJsSFHandler<SbVec3f, 3, CoinVrmlJs_SFRotationAliases, float, CoinVrmlJs_SFdefaultValues>
#define SFVec3dHandler CoinVrmlJsSFHandler<SbVec3d, 3, CoinVrmlJs_SFRotationAliases, double, CoinVrmlJs_SFdefaultValuesDouble>

#define MFColorHandler CoinVrmlJsMFHandler<SoMFColor, SoSFColor, &CoinVrmlJs::MFColor>
#define MFFloatHandler CoinVrmlJsMFHandler<SoMFFloat, SoSFFloat, &CoinVrmlJs::MFFloat>
#define MFInt32Handler CoinVrmlJsMFHandler<SoMFInt32, SoSFInt32, &CoinVrmlJs::MFInt32>
#define MFNodeHandler CoinVrmlJsMFHandler<SoMFNode, SoSFNode, &CoinVrmlJs::MFNode>
#define MFRotationHandler CoinVrmlJsMFHandler<SoMFRotation, SoSFRotation, &CoinVrmlJs::MFRotation>
#define MFStringHandler CoinVrmlJsMFHandler<SoMFString, SoSFString, &CoinVrmlJs::MFString>
#define MFTimeHandler CoinVrmlJsMFHandler<SoMFTime, SoSFTime, &CoinVrmlJs::MFTime>
#define MFVec2fHandler CoinVrmlJsMFHandler<SoMFVec2f, SoSFVec2f, &CoinVrmlJs::MFVec2f>
#define MFVec3fHandler CoinVrmlJsMFHandler<SoMFVec3f, SoSFVec3f, &CoinVrmlJs::MFVec3f>
#define MFVec3dHandler CoinVrmlJsMFHandler<SoMFVec3d, SoSFVec3d, &CoinVrmlJs::MFVec3d>

static JSFunctionSpec MFFunctions[] = {
//  {"toString", MF_toString, 0, 0, 0},
  {NULL, NULL, 0, 0, 0}
};

static JSBool SFRotationConstructor(JSContext * cx, JSObject * obj,
                                    uintN argc, jsval * argv, jsval * rval);

// Factory methods for converting to javascript objects
static JSObject * SFColorFactory(JSContext * cx, const SbColor & self);
static JSObject * SFNodeFactory(JSContext * cx, SoNode * container);
static JSObject * SFRotationFactory(JSContext * cx, const SbRotation & self);
static JSObject * SFVec2fFactory(JSContext * cx, const SbVec2f & self);
static JSObject * SFVec3fFactory(JSContext * cx, const SbVec3f & self);
static JSObject * SFVec3dFactory(JSContext * cx, const SbVec3d & self);

static SbList <JSObject *> * garbagecollectedobjects = NULL;
static SbList <SoNodeSensor *> * nodesensorstobedeleted = NULL;

// getIndex returns -1 if id is not an alias or in range 0-max
static JSBool getIndex(JSContext * cx, jsval id, const char * aliases[], int max)
{
  int index;

  if (JSVAL_IS_INT(id)) {
    index = JSVAL_TO_INT(id);
    if (index < 0 || index >= max) {
      spidermonkey()->JS_ReportError(cx, "index must be between 0 and %d", max);
      return -1;
    }
    return index;
  }
  else {
    JSString * jsstr = spidermonkey()->JS_ValueToString(cx, id);
    const char * str = spidermonkey()->JS_GetStringBytes(jsstr);

    for (index=0; index<max; ++index) {
      if (strcmp(str, aliases[index]) == 0) {
        return index;
      }
    }
    return -1;
  }
}

// *************************************************************************
// handlers
bool jsval2int(JSContext *cx, const jsval v, int32_t &value)
{
  if (JSVAL_IS_NULL(v)) return false;
  int32_t tempval;
  if (!spidermonkey()->JS_ValueToECMAInt32(cx, v, &tempval)) {
    return false;
  }
  value = tempval;
  return true;
}

bool jsval2double(JSContext *cx, const jsval v, double &value)
{
  if (JSVAL_IS_NULL(v)) return false;
  double tempval;
  if (!spidermonkey()->JS_ValueToNumber(cx, v, &tempval)) {
    return false;
  }
  value = tempval;
  return true;
}


// FIXME: number of aliases must not be lower than max. This may lead to
// unsafe programming. 20050721 erikgors.
template <class Base, int max, const char * aliases[], class basetype, basetype defaultValues[]>
struct CoinVrmlJsSFHandler {
  static JSBool get(JSContext * cx, JSObject * obj, jsval id, jsval * rval)
  {
    int index = getIndex(cx, id, aliases, max);
    if (index == -1) {
      return JS_TRUE;
    }

    Base * data = (Base *)spidermonkey()->JS_GetPrivate(cx, obj);
    assert(data != NULL);
    basetype var = (*data)[index];
    SbBool ok = spidermonkey()->JS_NewDoubleValue(cx, (double)var, rval);
    assert(ok && "JS_NewDoubleValue failed");
    return JS_TRUE;
  }

  static JSBool set(JSContext * cx, JSObject * obj, jsval id, jsval * val)
  {
    int index = getIndex(cx, id, aliases, max);
    if (index == -1) {
      return JS_FALSE;
    }

    Base * data = (Base *)spidermonkey()->JS_GetPrivate(cx, obj);
    assert(data != NULL);

    // FIXME: number may be NaN, PositiveInfinity and NegativeInfinity.
    // Should be checked for every time we run JS_ValueToNumber.
    // ie: "blipp" will become NaN. 20050720 erikgors.
    double number;
    spidermonkey()->JS_ValueToNumber(cx, *val, &number);
    (*data)[index] = (basetype)number;
    return JS_TRUE;
  }

  static JSBool constructor(JSContext * cx, JSObject * obj,
                            uintN argc, jsval * argv, jsval * rval)
  {
    basetype vals[max];

    // convert all arguments to numbers or use defaultValues if missing
    uint32_t i;
    for (i=0; i<max; ++i) {
      vals[i] = defaultValues[i];
      if (i<argc) {
        double val;
        if (spidermonkey()->JS_ValueToNumber(cx, argv[i], &val)) {
          vals[i] = (basetype)val;
        }
        else {
          spidermonkey()->JS_ReportError(cx, "WARNING: failed converting argument %d "
                                             "to a double", i + 1);
        }
      }
    }

    Base * data = new Base(vals);
    spidermonkey()->JS_SetPrivate(cx, obj, data);
    *rval = OBJECT_TO_JSVAL(obj);
    return JS_TRUE;
  }
  static void destructor(JSContext * cx, JSObject * obj)
  {
    Base * data = (Base *)spidermonkey()->JS_GetPrivate(cx, obj);
    // FIXME: We cannot assume this since the class object itself is an
    // instance of this JSClass. kintel 20050804.
    //    assert(data != NULL);
    delete data;
  }
};

template <class MFFieldClass, class SFFieldClass, CoinVrmlJs::ClassDescriptor * desc>
struct CoinVrmlJsMFHandler {
  static JSBool constructor(JSContext * cx, JSObject * obj, uintN argc, jsval * argv, jsval * COIN_UNUSED_ARG(rval))
  {
    jsval * val = new jsval;
    JSObject * array = spidermonkey()->JS_NewArrayObject(cx, 0, NULL);
    *val = OBJECT_TO_JSVAL(array);
    SbBool ok = spidermonkey()->JS_AddRoot(cx, val);
    assert(ok && "JS_AddRoot failed");
    spidermonkey()->JS_SetPrivate(cx, obj, val);

    SFFieldClass * field = (SFFieldClass *)SFFieldClass::createInstance();
    uintN i;

    for (i=0; i<argc; ++i) {
      if (SoJavaScriptEngine::getEngine(cx)->jsval2field(argv[i], field)) {
        SbBool ok = spidermonkey()->JS_SetElement(cx, array, i, &argv[i]);
        assert(ok && "JS_SetElement failed");
      }
      else {
        // FIXME: should we insert a default value? 20050727 erikgors.
        spidermonkey()->JS_ReportError(cx, "argv %d is of wrong type", i);
      }
    }
    delete field;
    return JS_TRUE;
  }

  static void destructor(JSContext * cx, JSObject * obj)
  {
    jsval * val = (jsval *)spidermonkey()->JS_GetPrivate(cx, obj);
    if (val != NULL) {
      SbBool ok = spidermonkey()->JS_RemoveRoot(cx, val);
      assert(ok && "JS_RemoveRoot failed");
      delete val;
    }
  }

  static JSObject * init(JSContext * cx, JSObject * obj)
  {
    return spidermonkey()->JS_InitClass(cx, obj, NULL, &desc->cls,
                                        constructor, 0,
                                        NULL, MFFunctions, NULL, NULL);
  }

  static void resize(JSContext * cx, JSObject * array, uint32_t newLength)
  {
    uint32_t length;
    SbBool ok = spidermonkey()->JS_GetArrayLength(cx, array, &length);
    assert(ok && "JS_GetArrayLength failed");

    if (length > newLength) {
      spidermonkey()->JS_SetArrayLength(cx, array, newLength);
    }
    else {
      SoType type = MFFieldClass::getClassTypeId();

      // expand and fill with new objects
      for (; length<newLength; ++length) {
        jsval val;

        if (type == SoMFInt32::getClassTypeId() ||
            type == SoMFFloat::getClassTypeId()) {
          val = INT_TO_JSVAL(0);
        }
        else if (type == SoMFString::getClassTypeId()) {
          JSString * str = spidermonkey()->JS_NewStringCopyZ(cx, "");
          val = STRING_TO_JSVAL(str);
        }
        else if (type == SoMFNode::getClassTypeId()) {
          // All elements not explicitly initialized are set to NULL
          val = JSVAL_VOID;
        }
        else if (type == SoMFColor::getClassTypeId()) {
          JSObject * newObj =
            spidermonkey()->JS_NewObject(cx, &CoinVrmlJs::SFColor.cls, NULL, NULL);
          assert(newObj != NULL);
          SFColorHandler::constructor(cx, newObj, 0, NULL, &val);
          val = OBJECT_TO_JSVAL(newObj);
        }
        else if (type == SoMFRotation::getClassTypeId()) {
          JSObject * newObj =
            spidermonkey()->JS_NewObject(cx, &CoinVrmlJs::SFRotation.cls, NULL, NULL);
          assert(newObj != NULL);
          SFRotationConstructor(cx, newObj, 0, NULL, &val);
          val = OBJECT_TO_JSVAL(newObj);
        }
        else if (type == SoMFVec2f::getClassTypeId()) {
          JSObject * newObj =
            spidermonkey()->JS_NewObject(cx, &CoinVrmlJs::SFVec2f.cls, NULL, NULL);
          assert(newObj != NULL);
          SFVec2fHandler::constructor(cx, newObj, 0, NULL, &val);
          val = OBJECT_TO_JSVAL(newObj);
        }
        else if (type == SoMFVec3f::getClassTypeId()) {
          JSObject * newObj =
            spidermonkey()->JS_NewObject(cx, &CoinVrmlJs::SFVec3f.cls, NULL, NULL);
          assert(newObj != NULL);
          SFVec3fHandler::constructor(cx, newObj, 0, NULL, &val);
          val = OBJECT_TO_JSVAL(newObj);
        }
        else if (type == SoMFVec3d::getClassTypeId()) {
          JSObject * newObj =
            spidermonkey()->JS_NewObject(cx, &CoinVrmlJs::SFVec3d.cls, NULL, NULL);
          assert(newObj != NULL);
          SFVec3dHandler::constructor(cx, newObj, 0, NULL, &val);
          val = OBJECT_TO_JSVAL(newObj);
        }
        else {
          assert(0 && "this should not happen");
        }
        SbBool ok = spidermonkey()->JS_SetElement(cx, array, length, &val);
        assert(ok && "JS_SetElement failed");
      }
    }
  }

  static JSBool get(JSContext * cx, JSObject * obj, jsval id, jsval * rval)
  {

    jsval * array = (jsval *)spidermonkey()->JS_GetPrivate(cx, obj);

    if (JSVAL_IS_INT(id)) {
      assert(array != NULL);
      int index = JSVAL_TO_INT(id);
      return spidermonkey()->JS_GetElement(cx, JSVAL_TO_OBJECT(*array), index, rval);
    }
    else if (JSVAL_IS_STRING(id)) {
      const char * str = spidermonkey()->JS_GetStringBytes(JSVAL_TO_STRING(id));
      if (SbName("length") == str) {
        assert(array != NULL);
        uint32_t length;
        SbBool ok = spidermonkey()->JS_GetArrayLength(cx, JSVAL_TO_OBJECT(*array), &length);
        assert(ok && "JS_GetArrayLength failed");
        *rval = INT_TO_JSVAL(length);
        return JS_TRUE;
      }
    }

    return JS_TRUE;
  }

  static JSBool set(JSContext * cx, JSObject * obj, jsval id, jsval * val)
  {
    jsval * array = (jsval *)spidermonkey()->JS_GetPrivate(cx, obj);

    if (JSVAL_IS_INT(id)) {
      int index = JSVAL_TO_INT(id);

      // check for bounds
      if (index < 0) {
        return JS_FALSE;
      }

      // resize if necessary
      uint32_t length;
      SbBool ok = spidermonkey()->JS_GetArrayLength(cx, JSVAL_TO_OBJECT(*array), &length);
      assert(ok && "JS_GetArrayLength failed");
      if (index >= (int)length) {
        resize(cx, JSVAL_TO_OBJECT(*array), index+1);
      }

      SFFieldClass * field = (SFFieldClass *)SFFieldClass::createInstance();
      // Check if val is not of wrong type
      if (SoJavaScriptEngine::getEngine(cx)->jsval2field(*val, field)) {
        // assign it
        SbBool ok = spidermonkey()->JS_SetElement(cx, JSVAL_TO_OBJECT(*array), index, val);
        assert(ok && "JS_SetElement failed");
        return JS_TRUE;
      }
      delete field;
    }
    else if (JSVAL_IS_STRING(id)) {
      const char * str = spidermonkey()->JS_GetStringBytes(JSVAL_TO_STRING(id));
      if (SbName("length") == str) {
        double number;
        spidermonkey()->JS_ValueToNumber(cx, *val, &number);
        if (number < 0) {
          spidermonkey()->JS_ReportError(cx, "RangeError: invalid array length");
        }
        else {
          resize(cx, JSVAL_TO_OBJECT(*array), (uint32_t)number);
        }
        return JS_TRUE;
      }
    }

    return JS_FALSE;
  }


  static SbBool jsval2field(JSContext * cx, const jsval v, SoField * f)
  {
    if (JSVAL_IS_OBJECT(v) &&
        spidermonkey()->JS_InstanceOf(cx, JSVAL_TO_OBJECT(v), &desc->cls, NULL)) {
      JSObject * obj = JSVAL_TO_OBJECT(v);
      jsval * array = (jsval *)spidermonkey()->JS_GetPrivate(cx, obj);
      assert(array != NULL);

      jsval element;
      uint32_t i;
      uint32_t num;
      JSBool ok = spidermonkey()->JS_GetArrayLength(cx, JSVAL_TO_OBJECT(*array), &num);

      SFFieldClass * field = (SFFieldClass *)SFFieldClass::createInstance();

      for (i=0; i<num; ++i) {
        ok = spidermonkey()->JS_GetElement(cx, obj, i, &element);
        assert(ok);

        ok = SoJavaScriptEngine::getEngine(cx)->jsval2field(element, field);
        assert(ok && "jsval2field failed");
        ((MFFieldClass *)f)->set1Value(i, field->getValue());
      }
      delete field;
      return TRUE;
    }
    return FALSE;
  }

  static void field2jsval(JSContext * cx, const SoField * f, jsval * v)
  {
    JSObject * obj = spidermonkey()->JS_NewObject(cx, &desc->cls, NULL, NULL);
    spidermonkey()->JS_DefineFunctions(cx, obj, desc->functions);

    int num = ((SoMField *)f)->getNum();
    jsval * vals = new jsval[num];

    MFFieldClass & mf = *(MFFieldClass *)f;

    SFFieldClass * field = (SFFieldClass *)SFFieldClass::createInstance();
    for (int i=0; i<num; ++i) {
      field->setValue(mf[i]);
      SbBool ok = SoJavaScriptEngine::getEngine(cx)->field2jsval(field, &vals[i]);
      assert(ok && "field2jsval failed");
    }

    jsval rval;
    constructor(cx, obj, num, vals, &rval);
    *v = OBJECT_TO_JSVAL(obj);
    delete field;
    delete [] vals;
  }
};

// *************************************************************************
// constructors

static JSBool SFRotationConstructor(JSContext * cx, JSObject * obj,
                                    uintN argc, jsval * argv, jsval * rval)
{
  if (argc == 2) {
    if (JSVAL_IS_SFVEC3F(cx, argv[0])) {
      SbVec3f & vec =
        *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));

      SbVec4f * data = new SbVec4f();
      spidermonkey()->JS_SetPrivate(cx, obj, data);
      *rval = OBJECT_TO_JSVAL(obj);
      // new SFRotation(SFVec3f fromVector, SFVec3f toVector)
      if (JSVAL_IS_SFVEC3F(cx, argv[1])) {
        SbVec3f & vec2 =
          *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[1]));

        SbRotation rot(vec, vec2);
        SbVec3f axis;
        float rad;
        rot.getValue(axis, rad);

        data->setValue(axis[0], axis[1], axis[2], rad);
        return JS_TRUE;
      }
      // new SFRotation(SFVec3f axis, numeric angle)
      else {
        SbVec4f * data = new SbVec4f();
        spidermonkey()->JS_SetPrivate(cx, obj, data);
        *rval = OBJECT_TO_JSVAL(obj);

        double number = 0.0;
        spidermonkey()->JS_ValueToNumber(cx, argv[1], &number);

        data->setValue(vec[0], vec[1], vec[2], (float)number);
        return JS_TRUE;
      }
    }
  }
  // new SFRotation(numeric x, numeric y, numeric z, numeric angle)
  // Missing values default to 0.0, except y, which defaults to 1.0.
  //
  // SbRotation will default to 0.0, 0.0, 1.0, when angle is 0.0
  // So we use SbVec4f to hold values for SFRotation, since we need to support
  // patterns like this:
  // var colors = new MFColor();
  // colors.length = 10;
  // colors[0].x = 1
  // colors[0].y = 0
  // colors[0].z = 0
  // colors[0].angle = 1.8
  //
  // This will not work when SbRotation holds the values. 20050714 erikgors.

  return SFRotationHandler::constructor(cx, obj, argc, argv, rval);
}

// *************************************************************************
// functions

static JSBool SFNode_ref(JSContext * cx, JSObject * obj, uintN COIN_UNUSED_ARG(argc),
                         jsval * COIN_UNUSED_ARG(argv), jsval * COIN_UNUSED_ARG(rval))
{
  // Check if the JS object has already been garbage collected. This
  // must be done to prevent a Java script from crashing the
  // application.
  if (garbagecollectedobjects->find(obj) != -1) {
    if (SoJavaScriptEngine::debug())
      SoDebugError::postInfo("SFNode_ref", "WARNING! Trying to ref a deleted node.");
    return JSVAL_FALSE;
  }

  SoNode & node = *(SoNode *)spidermonkey()->JS_GetPrivate(cx, obj);
  node.ref();
  return JSVAL_TRUE;
}

static JSBool SFNode_unref(JSContext * cx, JSObject * obj, uintN COIN_UNUSED_ARG(argc),
                           jsval * COIN_UNUSED_ARG(argv), jsval * COIN_UNUSED_ARG(rval))
{
  // Check if the JS object has already been garbage collected. This
  // must be done to prevent a Java script from crashing the
  // application.
  if (garbagecollectedobjects->find(obj) != -1) {
    if (SoJavaScriptEngine::debug())
      SoDebugError::postInfo("SFNode_unref", "WARNING! Trying to unref an already deleted node.");
    return JSVAL_FALSE;
  }

  SoNode & node = *(SoNode *)spidermonkey()->JS_GetPrivate(cx, obj);
  node.unref();
  return JSVAL_TRUE;
}

static void *
buffer_realloc(void * bufptr, size_t size)
{
  char *buffer = (char *)realloc(bufptr, size);
  return buffer;
}

static JSBool SFNode_toString(JSContext * cx, JSObject * obj, uintN COIN_UNUSED_ARG(argc),
                              jsval * COIN_UNUSED_ARG(argv), jsval * rval)
{
  // Check if the JS object has already been garbage collected. This
  // must be done to prevent a Java script from crashing the
  // application.
  if (garbagecollectedobjects->find(obj) != -1) {
    if (SoJavaScriptEngine::debug())
      SoDebugError::postInfo("SFNode_toString", "WARNING! Trying to access "
                             "an already deleted node.");
    return JSVAL_FALSE;
  }

  SoNode *node = (SoNode *)spidermonkey()->JS_GetPrivate(cx, obj);

  SoOutput out;
  out.setHeaderString("#VRML V2.0 utf8");
  size_t buffer_size = 1024;
  void *buffer = (void *)malloc(buffer_size);
  out.setBuffer(buffer, buffer_size, buffer_realloc);

  SoWriteAction wa(&out);
  wa.apply(node);

  out.getBuffer(buffer, buffer_size);

  *rval = STRING_TO_JSVAL(spidermonkey()->JS_NewStringCopyZ(cx,
    (char *)buffer));

  free(buffer);

  return JSVAL_TRUE;
}

static void SFNode_deleteCB(void * COIN_UNUSED_ARG(data), SoSensor * sensor)
{
  SoNode * node = ((SoNodeSensor *) sensor)->getAttachedNode();
  void * tmp;
  if(!CoinVrmlJs_sensorinfohash->get((unsigned long) node, tmp)) {
    assert(FALSE && "Trying to delete an unregistered SoNodeSensor. Internal error.");
    return;
  }

  CoinVrmlJs_SensorInfo * si = (CoinVrmlJs_SensorInfo *) tmp;

  // Delete all JSObjects which were connected to this SoNode
  while (si->objects.getLength()) {
    JSObject * obj = si->objects[0];
    garbagecollectedobjects->append(obj);
    si->objects.removeFast(0);
  }

  // Store the sensor-pointer so that it can be properly deleted later
  nodesensorstobedeleted->append((SoNodeSensor *) sensor);
  CoinVrmlJs_sensorinfohash->erase((unsigned long) node);
  delete si;
}

static void cleanupObsoleteNodeSensors(void)
{
  // Delete all SoNodeSensors which no longer have a node attached.
  while(nodesensorstobedeleted->getLength() > 0) {
    SoNodeSensor * ns = (SoNodeSensor *) (*nodesensorstobedeleted)[0];
    nodesensorstobedeleted->removeItem(ns);
    delete ns;
  }
}

static JSBool SFVec2f_add(JSContext * cx, JSObject * obj, uintN argc,
                          jsval * argv, jsval * rval)
{
  SbVec2f & vec1 = *(SbVec2f *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (argc >= 1 && JSVAL_IS_SFVEC2F(cx, argv[0])) {
    SbVec2f & vec2 = *(SbVec2f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));
    SbVec2f result = vec1 + vec2;
    *rval = OBJECT_TO_JSVAL(SFVec2fFactory(cx, result));
    return JS_TRUE;
  }

  return JS_FALSE;
}

static JSBool SFVec3f_add(JSContext * cx, JSObject * obj, uintN argc,
                          jsval * argv, jsval * rval)
{
  SbVec3f & vec1 = *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (argc >= 1 && JSVAL_IS_SFVEC3F(cx, argv[0])) {
    SbVec3f & vec2 = *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));
    SbVec3f result = vec1 + vec2;
    *rval = OBJECT_TO_JSVAL(SFVec3fFactory(cx, result));
    return JS_TRUE;
  }

  return JS_FALSE;
}

static JSBool SFVec3d_add(JSContext * cx, JSObject * obj, uintN argc,
                          jsval * argv, jsval * rval)
{
  SbVec3d & vec1 = *(SbVec3d *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (argc >= 1 && JSVAL_IS_SFVEC3D(cx, argv[0])) {
    SbVec3d & vec2 = *(SbVec3d *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));
    SbVec3d result = vec1 + vec2;
    *rval = OBJECT_TO_JSVAL(SFVec3dFactory(cx, result));
    return JS_TRUE;
  }

  return JS_FALSE;
}

static JSBool SFVec2f_divide(JSContext * cx, JSObject * obj, uintN argc,
                             jsval * argv, jsval * rval)
{
  SbVec2f & vec = *(SbVec2f *)spidermonkey()->JS_GetPrivate(cx, obj);

  double number;
  if (argc >= 1 && jsval2double(cx, argv[0], number)) {
    SbVec2f newVec = vec / (float)number;
    *rval = OBJECT_TO_JSVAL(SFVec2fFactory(cx, newVec));
    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSBool SFVec3f_divide(JSContext * cx, JSObject * obj, uintN argc,
                             jsval * argv, jsval * rval)
{
  SbVec3f & vec = *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, obj);

  double number;
  if (argc >= 1 && jsval2double(cx, argv[0], number)) {
    SbVec3f newVec = vec / (float)number;
    *rval = OBJECT_TO_JSVAL(SFVec3fFactory(cx, newVec));
    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSBool SFVec3d_divide(JSContext * cx, JSObject * obj, uintN argc,
                             jsval * argv, jsval * rval)
{
  SbVec3d & vec = *(SbVec3d *)spidermonkey()->JS_GetPrivate(cx, obj);

  double number;
  if (argc >= 1 && jsval2double(cx, argv[0], number)) {
    SbVec3d newVec = vec / number;
    *rval = OBJECT_TO_JSVAL(SFVec3dFactory(cx, newVec));
    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSBool SFVec2f_dot(JSContext *cx, JSObject * COIN_UNUSED_ARG(obj), uintN argc,
                          jsval *argv, jsval * COIN_UNUSED_ARG(rval))
{
  //SbVec2f & vec1 = *(SbVec2f *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (argc >= 1 && JSVAL_IS_SFVEC2F(cx, argv[0])) {
    //SbVec2f & vec2 = *(SbVec2f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));

    //float dot = vec1.dot(vec2);
    //JSBool ok = spidermonkey()->JS_NewDoubleValue(cx, dot, rval);
    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSBool SFVec3f_dot(JSContext *cx, JSObject * COIN_UNUSED_ARG(obj), uintN argc,
                          jsval *argv, jsval * COIN_UNUSED_ARG(rval))
{
  //SbVec3f & vec = *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (argc >= 1 && JSVAL_IS_SFVEC3F(cx, argv[0])) {

    //SbVec3f & vec2 = *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));

    //float dot = vec.dot(vec2);
    //JSBool ok = spidermonkey()->JS_NewDoubleValue(cx, dot, rval);
    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSBool SFVec3d_dot(JSContext *cx, JSObject * COIN_UNUSED_ARG(obj), uintN argc,
                          jsval *argv, jsval * COIN_UNUSED_ARG(rval))
{
  //SbVec3d & vec = *(SbVec3d *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (argc >= 1 && JSVAL_IS_SFVEC3D(cx, argv[0])) {

    //SbVec3d & vec2 = *(SbVec3d *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));

    //double dot = vec.dot(vec2);
    //JSBool ok = spidermonkey()->JS_NewDoubleValue(cx, dot, rval);
    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSBool SFVec2_length(JSContext * COIN_UNUSED_ARG(cx), JSObject * COIN_UNUSED_ARG(obj), uintN COIN_UNUSED_ARG(argc),
                            jsval * COIN_UNUSED_ARG(argv), jsval * COIN_UNUSED_ARG(rval))
{
  //SbVec2f * vec = (SbVec2f *)spidermonkey()->JS_GetPrivate(cx, obj);
  //JSBool ok = spidermonkey()->JS_NewDoubleValue(cx, vec->length(), rval);
  return JS_TRUE;
}

static JSBool SFVec3f_length(JSContext * COIN_UNUSED_ARG(cx), JSObject * COIN_UNUSED_ARG(obj), uintN COIN_UNUSED_ARG(argc),
                            jsval * COIN_UNUSED_ARG(argv), jsval * COIN_UNUSED_ARG(rval))
{
  //SbVec3f * vec = (SbVec3f *)spidermonkey()->JS_GetPrivate(cx, obj);
  //JSBool ok = spidermonkey()->JS_NewDoubleValue(cx, vec->length(), rval);
  return JS_TRUE;
}

static JSBool SFVec3d_length(JSContext * COIN_UNUSED_ARG(cx), JSObject * COIN_UNUSED_ARG(obj), uintN COIN_UNUSED_ARG(argc),
                            jsval * COIN_UNUSED_ARG(argv), jsval * COIN_UNUSED_ARG(rval))
{
  //SbVec3d * vec = (SbVec3d *)spidermonkey()->JS_GetPrivate(cx, obj);
  //JSBool ok = spidermonkey()->JS_NewDoubleValue(cx, vec->length(), rval);
  return JS_TRUE;
}

static JSBool SFVec2f_multiply(JSContext * cx, JSObject * obj, uintN argc,
                               jsval * argv, jsval * rval)
{

  SbVec2f & vec = *(SbVec2f *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (argc >= 1 && JSVAL_IS_NUMBER(argv[0])) {
    double number;
    spidermonkey()->JS_ValueToNumber(cx, argv[0], &number);

    SbVec2f newVec = vec * (float)number;

    *rval = OBJECT_TO_JSVAL(SFVec2fFactory(cx, newVec));

    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSBool SFVec3f_multiply(JSContext * cx, JSObject * obj, uintN argc,
                               jsval * argv, jsval * rval)
{
  SbVec3f & vec = *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, obj);

  double number;
  if (argc >= 1 && jsval2double(cx, argv[0], number)) {
    SbVec3f newVec = vec * (float)number;
    *rval = OBJECT_TO_JSVAL(SFVec3fFactory(cx, newVec));
    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSBool SFVec3d_multiply(JSContext * cx, JSObject * obj, uintN argc,
                               jsval * argv, jsval * rval)
{
  SbVec3d & vec = *(SbVec3d *)spidermonkey()->JS_GetPrivate(cx, obj);

  double number;
  if (argc >= 1 && jsval2double(cx, argv[0], number)) {
    SbVec3d newVec = vec * number;
    *rval = OBJECT_TO_JSVAL(SFVec3dFactory(cx, newVec));
    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSBool SFVec2f_normalize(JSContext * cx, JSObject * obj, uintN COIN_UNUSED_ARG(argc),
                                jsval * COIN_UNUSED_ARG(argv), jsval * rval)
{
  SbVec2f vec = *(SbVec2f *)spidermonkey()->JS_GetPrivate(cx, obj);
  vec.normalize();
  *rval = OBJECT_TO_JSVAL(SFVec2fFactory(cx, vec));
  return JS_TRUE;
}

static JSBool SFVec3f_normalize(JSContext * cx, JSObject * obj, uintN COIN_UNUSED_ARG(argc),
                                jsval * COIN_UNUSED_ARG(argv), jsval * rval)
{
  SbVec3f vec = *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, obj);
  vec.normalize();
  *rval = OBJECT_TO_JSVAL(SFVec3fFactory(cx, vec));
  return JS_TRUE;
}

static JSBool SFVec3d_normalize(JSContext * cx, JSObject * obj, uintN COIN_UNUSED_ARG(argc),
                                jsval * COIN_UNUSED_ARG(argv), jsval * rval)
{
  SbVec3d vec = *(SbVec3d *)spidermonkey()->JS_GetPrivate(cx, obj);
  vec.normalize();
  *rval = OBJECT_TO_JSVAL(SFVec3dFactory(cx, vec));
  return JS_TRUE;
}

static JSBool SFVec3f_negate(JSContext * cx, JSObject * obj, uintN COIN_UNUSED_ARG(argc),
                                     jsval * COIN_UNUSED_ARG(argv), jsval * rval)
{
  SbVec3f vec = *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, obj);
  vec.negate();
  *rval = OBJECT_TO_JSVAL(SFVec3fFactory(cx, vec));
  return JS_TRUE;
}

static JSBool SFVec3d_negate(JSContext * cx, JSObject * obj, uintN COIN_UNUSED_ARG(argc),
                                     jsval * COIN_UNUSED_ARG(argv), jsval * rval)
{
  SbVec3d vec = *(SbVec3d *)spidermonkey()->JS_GetPrivate(cx, obj);
  vec.negate();
  *rval = OBJECT_TO_JSVAL(SFVec3dFactory(cx, vec));
  return JS_TRUE;
}

static JSBool SFVec2f_subtract(JSContext * cx, JSObject * obj, uintN argc,
                               jsval * argv, jsval * rval)
{
  SbVec2f & vec1 = *(SbVec2f *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (argc >= 1 && JSVAL_IS_SFVEC2F(cx, argv[0])) {
    SbVec2f & vec2 = *(SbVec2f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));
    SbVec2f result = vec1 - vec2;
    *rval = OBJECT_TO_JSVAL(SFVec2fFactory(cx, result));
    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSBool SFVec3f_subtract(JSContext * cx, JSObject * obj, uintN argc,
                               jsval * argv, jsval * rval)
{
  SbVec3f & vec1 = *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (argc >= 1 && JSVAL_IS_SFVEC3F(cx, argv[0])) {
    SbVec3f & vec2 = *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));
    SbVec3f result = vec1 - vec2;
    *rval = OBJECT_TO_JSVAL(SFVec3fFactory(cx, result));
    return JS_TRUE;
  }

  return JS_FALSE;
}

static JSBool SFVec3d_subtract(JSContext * cx, JSObject * obj, uintN argc,
                               jsval * argv, jsval * rval)
{
  SbVec3d & vec1 = *(SbVec3d *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (argc >= 1 && JSVAL_IS_SFVEC3D(cx, argv[0])) {
    SbVec3d & vec2 = *(SbVec3d *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));
    SbVec3d result = vec1 - vec2;
    *rval = OBJECT_TO_JSVAL(SFVec3dFactory(cx, result));
    return JS_TRUE;
  }

  return JS_FALSE;
}

static JSBool SFColor_setHSV(JSContext * cx, JSObject * obj, uintN argc,
                             jsval * argv, jsval * rval)
{
  if (argc != 3) {
    return JS_FALSE;
  }
  SbColor & color = *(SbColor *)spidermonkey()->JS_GetPrivate(cx, obj);

  float vals[3];
  int i;

  for (i=0; i<3; ++i) {
    double number;
    spidermonkey()->JS_ValueToNumber(cx, argv[i], &number);
    vals[i] = (float)number;
  }

  color.setHSVValue(vals);

  *rval = JSVAL_VOID;
  return JS_TRUE;
}

static JSBool SFColor_getHSV(JSContext * cx, JSObject * obj, uintN COIN_UNUSED_ARG(argc),
                             jsval * COIN_UNUSED_ARG(argv), jsval * rval)
{
  SbColor & color = *(SbColor *)spidermonkey()->JS_GetPrivate(cx, obj);

  float vals[3];
  color.getHSVValue(vals);

  jsval vector[3];
  for (int i=0; i<3; ++i) {
    spidermonkey()->JS_NewDoubleValue(cx, vals[i], &vector[i]);
  }

  *rval = OBJECT_TO_JSVAL(spidermonkey()->JS_NewArrayObject(cx, 3, vector));

  return JS_TRUE;
}

static JSBool SFRotation_getAxis(JSContext * cx, JSObject * obj, uintN COIN_UNUSED_ARG(argc),
                                 jsval * COIN_UNUSED_ARG(argv), jsval * rval)
{
  SbVec4f & rot = *(SbVec4f *)spidermonkey()->JS_GetPrivate(cx, obj);
  SbVec3f axis(rot[0], rot[1], rot[2]);
  *rval = OBJECT_TO_JSVAL(SFVec3fFactory(cx, axis));
  return JS_TRUE;
}

static JSBool SFRotation_inverse(JSContext * cx, JSObject * obj, uintN COIN_UNUSED_ARG(argc),
                                 jsval * COIN_UNUSED_ARG(argv), jsval * rval)
{
  SbVec4f & rot = *(SbVec4f *)spidermonkey()->JS_GetPrivate(cx, obj);

  SbVec3f axis(rot[0], rot[1], rot[2]);
  SbRotation newRot(axis, rot[3]);
  newRot.invert();

  *rval = OBJECT_TO_JSVAL(SFRotationFactory(cx, newRot));
  return JS_TRUE;
}

static JSBool SFRotation_multiply(JSContext * cx, JSObject * obj, uintN argc,
                                  jsval * argv, jsval * rval)
{
  SbVec4f & vec1 = *(SbVec4f *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (argc >= 1 && JSVAL_IS_SFROTATION(cx, argv[0])) {
    SbVec4f & vec2 = *(SbVec4f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));
    SbVec3f axis1(vec1[0], vec1[1], vec1[2]);
    SbVec3f axis2(vec2[0], vec2[1], vec2[2]);
    SbRotation result = SbRotation(axis1, vec1[3]) * SbRotation(axis2, vec2[3]);

    *rval = OBJECT_TO_JSVAL(SFRotationFactory(cx, result));
    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSBool SFRotation_multVec(JSContext * cx, JSObject * obj, uintN argc,
                                 jsval * argv, jsval * rval)
{
  SbVec4f & vec = *(SbVec4f *)spidermonkey()->JS_GetPrivate(cx, obj);
  SbVec3f axis(vec[0], vec[1], vec[2]);
  SbRotation rot(axis, vec[3]);

  if (argc >= 1 && JSVAL_IS_SFVEC3F(cx, argv[0])) {
    SbVec3f & src = *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));
    SbVec3f dst;
    rot.multVec(src, dst);

    *rval = OBJECT_TO_JSVAL(SFVec3fFactory(cx, dst));
    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSBool SFRotation_setAxis(JSContext * cx, JSObject * obj, uintN argc,
                                 jsval * argv, jsval * rval)
{
  SbVec4f & rot = *(SbVec4f *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (argc >= 1 && JSVAL_IS_SFVEC3F(cx, argv[0])) {
    SbVec3f & axis = *(SbVec3f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));
    rot[0] = axis[0];
    rot[1] = axis[1];
    rot[2] = axis[2];

    *rval = JSVAL_VOID;
    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSBool SFRotation_slerp(JSContext * cx, JSObject * obj, uintN argc,
                               jsval * argv, jsval * rval)
{
  SbVec4f & vec = *(SbVec4f *)spidermonkey()->JS_GetPrivate(cx, obj);
  SbVec3f axis(vec[0], vec[1], vec[2]);
  SbRotation rot(axis, vec[3]);

  double number;
  if (argc >= 2 && JSVAL_IS_SFROTATION(cx, argv[0]) && jsval2double(cx, argv[1], number)) {
    SbVec4f & vec2 = *(SbVec4f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0]));
    SbVec3f axis2(vec2[0], vec2[1], vec2[2]);
    SbRotation dest(axis2, vec2[3]);

    SbRotation result = SbRotation::slerp(rot, dest, (float)number);

    *rval = OBJECT_TO_JSVAL(SFRotationFactory(cx, result));
    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSFunctionSpec SFNodeFunctions[] = {
  {"ref", SFNode_ref, 0, 0, 0},
  {"unref", SFNode_unref, 0, 0, 0},
  {"toString", SFNode_toString, 0, 0, 0},
  {NULL, NULL, 0, 0, 0}
};

static JSFunctionSpec SFVec2fFunctions[] = {
  {"add", SFVec2f_add, 1, 0, 0},
  {"divide", SFVec2f_divide, 1, 0, 0},
  {"dot", SFVec2f_dot, 1, 0, 0},
  {"length", SFVec2_length, 0, 0, 0},
  {"multiply", SFVec2f_multiply, 1, 0, 0},
  {"normalize", SFVec2f_normalize, 0, 0, 0},
  {"subtract", SFVec2f_subtract, 1, 0, 0},
  {NULL, NULL, 0, 0, 0}
};

static JSFunctionSpec SFVec3fFunctions[] = {
  {"add", SFVec3f_add, 1, 0, 0},
  {"divide", SFVec3f_divide, 1, 0, 0},
  {"dot", SFVec3f_dot, 1, 0, 0},
  {"length", SFVec3f_length, 0, 0, 0},
  {"multiply", SFVec3f_multiply, 1, 0, 0},
  {"normalize", SFVec3f_normalize, 0, 0, 0},
  {"negate", SFVec3f_negate, 0, 0, 0},
  {"subtract", SFVec3f_subtract, 1, 0, 0},
  {NULL, NULL, 0, 0, 0}
};

static JSFunctionSpec SFVec3dFunctions[] = {
  {"add", SFVec3d_add, 1, 0, 0},
  {"divide", SFVec3d_divide, 1, 0, 0},
  {"dot", SFVec3d_dot, 1, 0, 0},
  {"length", SFVec3d_length, 0, 0, 0},
  {"multiply", SFVec3d_multiply, 1, 0, 0},
  {"normalize", SFVec3d_normalize, 0, 0, 0},
  {"negate", SFVec3d_negate, 0, 0, 0},
  {"subtract", SFVec3d_subtract, 1, 0, 0},
  {NULL, NULL, 0, 0, 0}
};

static JSFunctionSpec SFColorFunctions[] = {
  {"setHSV", SFColor_setHSV, 3, 0, 0},
  {"getHSV", SFColor_getHSV, 0, 0, 0},
  {NULL, NULL, 0, 0, 0}
};

static JSFunctionSpec SFRotationFunctions[] = {
  {"getAxis", SFRotation_getAxis, 0, 0, 0},
  {"inverse", SFRotation_inverse, 0, 0, 0},
  {"multiply", SFRotation_multiply, 1, 0, 0},
  {"multVec", SFRotation_multVec, 1, 0, 0},
  {"setAxis", SFRotation_setAxis, 1, 0, 0},
  {"slerp", SFRotation_slerp, 2, 0, 0},
  {NULL, NULL, 0, 0, 0}
};

// *************************************************************************
// factory

static JSObject * SFColorFactory(JSContext * cx, const SbColor & self)
{
  JSObject * obj =
    spidermonkey()->JS_NewObject(cx, &CoinVrmlJs::SFColor.cls, NULL, NULL);
  spidermonkey()->JS_DefineFunctions(cx, obj, SFColorFunctions);

  SbColor * color = new SbColor(self);
  spidermonkey()->JS_SetPrivate(cx, obj, color);

  return obj;
}

static JSObject * SFRotationFactory(JSContext * cx, const SbRotation & self)
{
  JSObject * obj =
    spidermonkey()->JS_NewObject(cx, &CoinVrmlJs::SFRotation.cls, NULL, NULL);
  spidermonkey()->JS_DefineFunctions(cx, obj, SFRotationFunctions);

  SbVec3f axis;
  float angle;
  self.getValue(axis, angle);
  SbVec4f * data = new SbVec4f(axis[0], axis[1], axis[2], angle);
  spidermonkey()->JS_SetPrivate(cx, obj, data);

  return obj;
}

static JSObject * SFVec2fFactory(JSContext * cx, const SbVec2f & self)
{
  JSObject * obj =
    spidermonkey()->JS_NewObject(cx, &CoinVrmlJs::SFVec2f.cls, NULL, NULL);
  spidermonkey()->JS_DefineFunctions(cx, obj, SFVec2fFunctions);

  SbVec2f * data = new SbVec2f(self);
  spidermonkey()->JS_SetPrivate(cx, obj, data);
  return obj;
}

static JSObject * SFVec3fFactory(JSContext * cx, const SbVec3f & self)
{
  JSObject * obj =
    spidermonkey()->JS_NewObject(cx, &CoinVrmlJs::SFVec3f.cls, NULL, NULL);
  spidermonkey()->JS_DefineFunctions(cx, obj, SFVec3fFunctions);

  SbVec3f * data = new SbVec3f(self);
  spidermonkey()->JS_SetPrivate(cx, obj, data);

  return obj;
}

static JSObject * SFVec3dFactory(JSContext * cx, const SbVec3d & self)
{
  JSObject * obj =
    spidermonkey()->JS_NewObject(cx, &CoinVrmlJs::SFVec3d.cls, NULL, NULL);
  spidermonkey()->JS_DefineFunctions(cx, obj, SFVec3dFunctions);

  SbVec3d * data = new SbVec3d(self);
  spidermonkey()->JS_SetPrivate(cx, obj, data);

  return obj;
}

static JSObject * SFVec2f_init(JSContext * cx, JSObject * obj)
{
  return spidermonkey()->JS_InitClass(cx, obj, NULL, &CoinVrmlJs::SFVec2f.cls,
                                      SFVec2fHandler::constructor, 0,
                                      NULL, SFVec2fFunctions, NULL, NULL);
}

static JSObject * SFVec3f_init(JSContext * cx, JSObject * obj)
{
  return spidermonkey()->JS_InitClass(cx, obj, NULL, &CoinVrmlJs::SFVec3f.cls,
                                      SFVec3fHandler::constructor, 0,
                                      NULL, SFVec3fFunctions, NULL, NULL);
}

static JSObject * SFVec3d_init(JSContext * cx, JSObject * obj)
{
  return spidermonkey()->JS_InitClass(cx, obj, NULL, &CoinVrmlJs::SFVec3d.cls,
                                      SFVec3dHandler::constructor, 0,
                                      NULL, SFVec3dFunctions, NULL, NULL);
}

static JSObject * SFColor_init(JSContext * cx, JSObject * obj)
{
  return spidermonkey()->JS_InitClass(cx, obj, NULL, &CoinVrmlJs::SFColor.cls,
                                      SFColorHandler::constructor, 0,
                                      NULL, SFColorFunctions, NULL, NULL);
}

static JSObject * SFRotation_init(JSContext * cx, JSObject * obj)
{
  return spidermonkey()->JS_InitClass(cx, obj, NULL, &CoinVrmlJs::SFRotation.cls,
                                      SFRotationConstructor, 0,
                                      NULL, SFRotationFunctions, NULL, NULL);
}

// *************************************************************************
// SFNode

static JSBool SFNode_get(JSContext * cx, JSObject * obj, jsval id, jsval * rval)
{

  if (garbagecollectedobjects->find(obj) != -1) {
    spidermonkey()->JS_ReportError(cx, "Trying to access an object with refcount=0.");
    return JS_FALSE;
  }

  SoNode * container = (SoNode *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (container == NULL) {
    // this will only happen when JS_NewObject calls "constructor"
    // or the node is "undefined"

    if (JSVAL_IS_STRING(id)) {
      const char * str = spidermonkey()->JS_GetStringBytes(JSVAL_TO_STRING(id));
      if (SbName("constructor") == str) {
        return JS_TRUE;
      }
    }
    spidermonkey()->JS_ReportError(cx, "node is undefined");
    return JS_FALSE;
  }

  if (JSVAL_IS_STRING(id)) {
    SbString str(spidermonkey()->JS_GetStringBytes(JSVAL_TO_STRING(id)));

    SoField * out = container->getField(SbName(str));

    int len = str.getLength();
    static const char post[] = "_changed";
    static const size_t postLen = sizeof(post) - 1;

    if (out == NULL && len > (int)postLen &&
        str.getSubString(len - postLen) == post) {
      out = container->getField(SbName(str.getSubString(0, len - postLen - 1)));
    }

    if (out != NULL) {
      SoJavaScriptEngine::getEngine(cx)->field2jsval(out, rval);
      return JS_TRUE;
    }
  }

  /* Note: If we're unable to find the field, we return JS_TRUE
     instead of JS_FALSE, which might seem as the logical choice
     for indicating a failure. If we return JS_FALSE, execution of
     the script will halt. One side-effect of this is that it is not
     possible to extend the SFNode JavaScript object by adding
     properties, which can be very useful in some cases. This is also
     more in line with the JavaScript philosophy that one can
     dynamically add and remove properties for any object at any time.

     2005-11-23 thammer.

     Update: We should look more closely into the return values
     JS_TRUE / JS_FALSE for all getters and setters, and possibly for
     other functions as well. It might be more robust to report the
     error (using JS_ReportError) and return JS_TRUE to allow the
     script to continue running than to abort the script.

     2006-06-21 thammer.
  */
  return JS_TRUE;
}

static JSBool SFNode_set(JSContext * cx, JSObject * obj, jsval id, jsval * rval)
{
  SoNode * container = (SoNode *)spidermonkey()->JS_GetPrivate(cx, obj);

  if (container == NULL) {
    spidermonkey()->JS_ReportError(cx, "node is undefined");
    return JS_FALSE;
  }

  if (JSVAL_IS_STRING(id)) {
    SbString str(spidermonkey()->JS_GetStringBytes(JSVAL_TO_STRING(id)));

    SoField * in = container->getField(SbName(str));

    int len = str.getLength();
    static const char pre[] = "set_";
    static const size_t preLen = sizeof(pre) - 1;

    if (in == NULL && len > (int)preLen &&
        str.getSubString(0, preLen - 1) == pre) {
      in = container->getField(SbName(str.getSubString(preLen)));
    }

    if (in != NULL) {
      SoJavaScriptEngine::getEngine(cx)->jsval2field(*rval, in);
      if (SoJavaScriptEngine::debug()) {
        SoDebugError::postInfo("SFNode_set", "setting field %s", str.getString());
      }
    } else if (SoJavaScriptEngine::debug())
      SoDebugError::postWarning("SFNode_set", "no such field %s", str.getString());
  }

  // See note in SFNode_get() about return value. 2005-11-23 thammer.
  return JS_TRUE;
}

static void SFNodeDestructor(JSContext * cx, JSObject * obj)
{
  // Delete all SoNodeSensors which no longer has a node attached.
  cleanupObsoleteNodeSensors();
  if(garbagecollectedobjects->find(obj) != -1) { // Pointer is marked as garbage-collected
    garbagecollectedobjects->removeItem(obj);
  }

  SoNode * container = (SoNode *)spidermonkey()->JS_GetPrivate(cx, obj);
  // FIXME: We cannot assume this since the class object itself is an
  // instance of this JSClass. kintel 20050804.
  //  assert(container != NULL);
  if (SoJavaScriptEngine::getEngine(cx)->getAutoNodeUnrefState())
    if (container) container->unref();
}

// Called via coin_atexit() when Coin exits.
static void deleteSensorInfoHash(void)
{
  CoinVrmlJs_sensorinfohash->clear();
  delete CoinVrmlJs_sensorinfohash;
}

/*
  Attach an SoNodeSensor to node. Adds a 'delete callback' to the
  sensor which includes the 'obj' parameter.
*/
static void attachSensorToNode(SoNode * node, JSObject * obj)
{
  // Has the hash-table been initialized?
  if (!CoinVrmlJs_sensorinfohash) {
    CoinVrmlJs_sensorinfohash = new SbHash<unsigned long, void *>;
    coin_atexit(deleteSensorInfoHash, CC_ATEXIT_NORMAL);
  }

  // Is a sensor already attached to this SoNode?
  void * tmp;
  if (CoinVrmlJs_sensorinfohash->get((unsigned long) node, tmp)) {
    CoinVrmlJs_SensorInfo * si = (CoinVrmlJs_SensorInfo *) tmp;
    si->objects.append(obj);
  }
  else {
    SoNodeSensor * ns = new SoNodeSensor();
    ns->setDeleteCallback(SFNode_deleteCB, obj);
    ns->attach(node);
    CoinVrmlJs_SensorInfo * si = new CoinVrmlJs_SensorInfo;
    si->objects.append(obj);
    CoinVrmlJs_sensorinfohash->put((unsigned long) node, si);
  }
}

static JSObject * SFNodeFactory(JSContext * cx, SoNode * container)
{
  // Delete all SoNodeSensors which no longer has a node attached.
  cleanupObsoleteNodeSensors();

  JSObject * obj = spidermonkey()->JS_NewObject(cx, &CoinVrmlJs::SFNode.cls, NULL, NULL);

  if(garbagecollectedobjects->find(obj) != -1) // Pointer has been used before. Remove from list.
    garbagecollectedobjects->removeItem(obj);

  spidermonkey()->JS_SetPrivate(cx, obj, container);
  spidermonkey()->JS_DefineFunctions(cx, obj, SFNodeFunctions);

  // FIXME: If the node has enums, define them here. 2007-03-08 thammer.

  attachSensorToNode(container, obj);

  if (SoJavaScriptEngine::getEngine(cx)->getAutoNodeUnrefState())
    container->ref();

  return obj;
}

static JSBool SFNodeConstructor(JSContext * cx, JSObject * obj,
                                uintN argc, jsval * argv, jsval *rval)
{
  // Delete all SoNodeSensors which no longer has a node attached.
  cleanupObsoleteNodeSensors();

  // spidermonkey ignores the return value
  if (argc >= 1 && JSVAL_IS_STRING(argv[0])) {
    JSString * js = JSVAL_TO_STRING(argv[0]);
    char * str = spidermonkey()->JS_GetStringBytes(js);
    //size_t len = spidermonkey()->JS_GetStringLength(js);

    // FIXME: what about UTF8? 20050701 erikgors.

    if (SoJavaScriptEngine::debug()) {
      SoDebugError::postInfo("SFNodeConstructor",
                             "creating new node with str = '%s'", str);
    }

    SoInput input;
    const char * array[2];
    array[0] = str;
    array[1] = NULL;
    input.setStringArray(array);

    SoGroup * group;

    if (input.isFileVRML2())
      group = SoDB::readAllVRML(&input);
    else
      group = SoDB::readAll(&input);

    if (group == NULL) {
      spidermonkey()->JS_ReportError(cx, "input is not legal VRML string");
      return JS_FALSE;
    }
    if (group->getNumChildren() == 0) {
      spidermonkey()->JS_ReportError(cx, "no top-level node, result is undefined");
      *rval = JSVAL_VOID;
      return JS_FALSE;
    }

    if(garbagecollectedobjects->find(obj) != -1) { // Pointer has been used before. Remove from list.
      garbagecollectedobjects->removeItem(obj);
    }

    attachSensorToNode(group, obj);

    if (SoJavaScriptEngine::getEngine(cx)->getAutoNodeUnrefState())
      group->ref();

    spidermonkey()->JS_SetPrivate(cx, obj, group);
    spidermonkey()->JS_DefineFunctions(cx, obj, SFNodeFunctions);

    return JS_TRUE;
  }
  return JS_FALSE;
}

static JSObject * SFNode_init(JSContext * cx, JSObject * obj)
{
  return spidermonkey()->JS_InitClass(cx, obj, NULL, &CoinVrmlJs::SFNode.cls,
                                      SFNodeConstructor, 0,
                                      NULL, NULL, NULL, NULL);
}


// *************************************************************************
// jsval2field

static SbBool SFBool_jsval2field(JSContext * cx, const jsval v, SoField * f)
{
  if (JSVAL_IS_BOOLEAN(v)) {
    const SbBool b = JSVAL_TO_BOOLEAN(v);
    ((SoSFBool *)f)->setValue(b);
    return TRUE;
  }
  else {
    JSBool b;
    if (spidermonkey()->JS_ValueToBoolean(cx, v, &b)) {
      ((SoSFBool *)f)->setValue(b);
      return TRUE;
    }
  }
  return FALSE;
}

static SbBool SFColor_jsval2field(JSContext * cx, const jsval v, SoField * f)
{
  if (JSVAL_IS_SFCOLOR(cx, v)) {
    SbColor * color = (SbColor *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(v));
    assert(color != NULL);
    ((SoSFColor *)f)->setValue(*color);
    return TRUE;
  }
  return FALSE;
}

static SbBool SFFloat_jsval2field(JSContext * cx, const jsval v, SoField * f)
{
  double number;
  if (jsval2double(cx, v, number)) {
    ((SoSFFloat *)f)->setValue((float)number);
    return TRUE;
  }
  return FALSE;
}

static SbBool SFInt32_jsval2field(JSContext * cx, const jsval v, SoField * f)
{
  int32_t val;
  if (jsval2int(cx, v, val)) {
    ((SoSFInt32 *)f)->setValue(val);
    return TRUE;
  }
  return FALSE;
}

static SbBool SFEnum_jsval2field(JSContext * cx, const jsval v, SoField * f)
{
  int32_t val;
  if (jsval2int(cx, v, val)) {
    ((SoSFInt32 *)f)->setValue(val);
    return TRUE;
  }
  return FALSE;
}

static SbBool SFNode_jsval2field(JSContext * cx, const jsval v, SoField * f)
{
  if (JSVAL_IS_NULL(v)) {
    ((SoSFNode *)f)->setValue(NULL);
    return TRUE;
  }
  if (JSVAL_IS_OBJECT(v) &&
      spidermonkey()->JS_InstanceOf(cx, JSVAL_TO_OBJECT(v), &CoinVrmlJs::SFNode.cls, NULL)) {
    SoNode * node = (SoNode *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(v));
    ((SoSFNode *)f)->setValue(node);
    return TRUE;
  }
  return FALSE;
}

static SbBool SFRotation_jsval2field(JSContext * cx, const jsval v, SoField * f)
{
  if (JSVAL_IS_SFROTATION(cx, v)) {
    SbVec4f * rot = (SbVec4f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(v));
    assert(rot != NULL);
    SbVec3f axis((*rot)[0], (*rot)[1], (*rot)[2]);
    ((SoSFRotation *)f)->setValue(SbRotation(axis, (*rot)[3]));
    return TRUE;
  }
  return FALSE;
}

static SbBool SFString_jsval2field(JSContext * COIN_UNUSED_ARG(cx), const jsval v, SoField * f)
{
  if (JSVAL_IS_STRING(v)) {
    const char * str = spidermonkey()->JS_GetStringBytes(JSVAL_TO_STRING(v));
    ((SoSFString *)f)->setValue(str);
    return TRUE;
  }
  return FALSE;
}

static SbBool SFTime_jsval2field(JSContext * cx, const jsval v, SoField * f)
{
  double number;
  if (jsval2double(cx, v, number)) {
    spidermonkey()->JS_ValueToNumber(cx, v, &number);
    ((SoSFTime*)f)->setValue(SbTime(number));
    return TRUE;
  }
  return FALSE;
}

static SbBool SFVec2f_jsval2field(JSContext * cx, const jsval v, SoField * f)
{
  if (JSVAL_IS_SFVEC2F(cx, v)) {
    SbVec2f * vec = (SbVec2f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(v));
    assert(vec != NULL);
    ((SoSFVec2f *)f)->setValue(*vec);
    return TRUE;
  }
  return FALSE;
}

static SbBool SFVec3f_jsval2field(JSContext * cx, const jsval v, SoField * f)
{
  if (JSVAL_IS_SFVEC3F(cx, v)) {
    SbVec3f * vec = (SbVec3f *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(v));
    assert(vec != NULL);
    ((SoSFVec3f *)f)->setValue(*vec);
    return TRUE;
  }
  return FALSE;
}

static SbBool SFVec3d_jsval2field(JSContext * cx, const jsval v, SoField * f)
{
  if (JSVAL_IS_SFVEC3D(cx, v)) {
    SbVec3d * vec = (SbVec3d *)spidermonkey()->JS_GetPrivate(cx, JSVAL_TO_OBJECT(v));
    assert(vec != NULL);
    ((SoSFVec3d *)f)->setValue(*vec);
    return TRUE;
  }
  return FALSE;
}

// *************************************************************************
// field2jsval

static void SFBool_field2jsval(JSContext * COIN_UNUSED_ARG(cx), const SoField * f, jsval * v)
{
  const SbBool val = ((SoSFBool *)f)->getValue();
  *v = BOOLEAN_TO_JSVAL(val);
}

static void SFColor_field2jsval(JSContext * cx, const SoField * f, jsval *v)
{
  const SbColor & val = ((SoSFColor *)f)->getValue();
  JSObject * obj = SFColorFactory(cx, val);
  *v = OBJECT_TO_JSVAL(obj);
}

static void SFFloat_field2jsval(JSContext * COIN_UNUSED_ARG(cx), const SoField * COIN_UNUSED_ARG(f), jsval * COIN_UNUSED_ARG(v))
{
  //const float val = ((SoSFFloat *)f)->getValue();
  //JSBool ok = spidermonkey()->JS_NewDoubleValue(cx, val, v);
}

static void SFInt32_field2jsval(JSContext * COIN_UNUSED_ARG(cx), const SoField * f, jsval * v)
{
  const int32_t val = ((SoSFInt32 *)f)->getValue();
  *v = INT_TO_JSVAL(val);
}

static void SFEnum_field2jsval(JSContext * COIN_UNUSED_ARG(cx), const SoField * f, jsval * v)
{
  const int32_t val = ((SoSFInt32 *)f)->getValue();
  *v = INT_TO_JSVAL(val);
}

static void SFNode_field2jsval(JSContext * cx, const SoField * f, jsval * v)
{
  SoNode * node = ((SoSFNode *)f)->getValue();
  if (node == NULL)
    *v = JSVAL_NULL;
  else {
    JSObject * obj = SFNodeFactory(cx, node);
    *v = OBJECT_TO_JSVAL(obj);
  }
}

static void SFRotation_field2jsval(JSContext * cx, const SoField * f, jsval *v)
{
  const SbRotation & val = ((SoSFRotation *)f)->getValue();
  JSObject * obj = SFRotationFactory(cx, val);
  *v = OBJECT_TO_JSVAL(obj);
}

static void SFString_field2jsval(JSContext * cx, const SoField * f, jsval * v)
{
  const SbString & val = ((SoSFString *)f)->getValue();
  JSString * str = spidermonkey()->JS_NewStringCopyZ(cx, val.getString());
  *v = STRING_TO_JSVAL(str);
}

static void SFTime_field2jsval(JSContext * cx, const SoField * f, jsval * v)
{
  const SbTime & time = ((SoSFTime *)f)->getValue();
  spidermonkey()->JS_NewDoubleValue(cx, time.getValue(), v);
}

static void SFVec2f_field2jsval(JSContext * cx, const SoField * f, jsval *v)
{
  const SbVec2f & val = ((SoSFVec2f *)f)->getValue();
  JSObject * obj = SFVec2fFactory(cx, val);
  *v = OBJECT_TO_JSVAL(obj);
}

static void SFVec3f_field2jsval(JSContext * cx, const SoField * f, jsval *v)
{
  const SbVec3f & val = ((SoSFVec3f *)f)->getValue();
  JSObject * obj = SFVec3fFactory(cx, val);
  *v = OBJECT_TO_JSVAL(obj);
}

static void SFVec3d_field2jsval(JSContext * cx, const SoField * f, jsval *v)
{
  const SbVec3d & val = ((SoSFVec3d *)f)->getValue();
  JSObject * obj = SFVec3dFactory(cx, val);
  *v = OBJECT_TO_JSVAL(obj);
}

// *************************************************************************
// classes

CoinVrmlJs::ClassDescriptor CoinVrmlJs::SFColor = {
  {
    "SFColor", JSCLASS_HAS_PRIVATE, NULL, NULL,
    SFColorHandler::get, SFColorHandler::set,
    NULL, NULL, NULL,
    SFColorHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  SFColorFunctions
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::SFNode = {
  {
    "SFNode", JSCLASS_HAS_PRIVATE, NULL, NULL,
    SFNode_get, SFNode_set,
    NULL, NULL, NULL,
    SFNodeDestructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  NULL
};


CoinVrmlJs::ClassDescriptor CoinVrmlJs::SFRotation = {
  {
    "SFRotation", JSCLASS_HAS_PRIVATE, NULL, NULL,
    SFRotationHandler::get, SFRotationHandler::set,
    NULL, NULL, NULL,
    SFRotationHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  SFRotationFunctions
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::SFVec2f = {
  {
    "SFVec2f", JSCLASS_HAS_PRIVATE, NULL, NULL,
    SFVec2fHandler::get, SFVec2fHandler::set,
    NULL, NULL, NULL,
    SFVec2fHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  SFVec2fFunctions
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::SFVec3f = {
  {
    "SFVec3f", JSCLASS_HAS_PRIVATE, NULL, NULL,
    SFVec3fHandler::get, SFVec3fHandler::set,
    NULL, NULL, NULL,
    SFVec3fHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  SFVec3fFunctions
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::SFVec3d = {
  {
    "SFVec3d", JSCLASS_HAS_PRIVATE, NULL, NULL,
    SFVec3dHandler::get, SFVec3dHandler::set,
    NULL, NULL, NULL,
    SFVec3dHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  SFVec3dFunctions
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::MFColor = {
  {
    "MFColor", JSCLASS_HAS_PRIVATE, NULL, NULL,
    MFColorHandler::get, MFColorHandler::set,
    NULL, NULL, NULL,
    MFColorHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  MFFunctions,
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::MFFloat = {
  {
    "MFFloat", JSCLASS_HAS_PRIVATE, NULL, NULL,
    MFFloatHandler::get, MFFloatHandler::set,
    NULL, NULL, NULL,
    MFFloatHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  MFFunctions,
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::MFInt32 = {
  {
    "MFInt32", JSCLASS_HAS_PRIVATE, NULL, NULL,
    MFInt32Handler::get, MFInt32Handler::set,
    NULL, NULL, NULL,
    MFInt32Handler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  MFFunctions,
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::MFNode = {
  {
    "MFNode", JSCLASS_HAS_PRIVATE, NULL, NULL,
    MFNodeHandler::get, MFNodeHandler::set,
    NULL, NULL, NULL,
    MFNodeHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  MFFunctions,
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::MFRotation = {
  {
    "MFRotation", JSCLASS_HAS_PRIVATE, NULL, NULL,
    MFRotationHandler::get, MFRotationHandler::set,
    NULL, NULL, NULL,
    MFRotationHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  MFFunctions,
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::MFString = {
  {
    "MFString", JSCLASS_HAS_PRIVATE, NULL, NULL,
    MFStringHandler::get, MFStringHandler::set,
    NULL, NULL, NULL,
    MFStringHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  MFFunctions,
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::MFTime = {
  {
    "MFTime", JSCLASS_HAS_PRIVATE, NULL, NULL,
    MFTimeHandler::get, MFTimeHandler::set,
    NULL, NULL, NULL,
    MFTimeHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  MFFunctions,
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::MFVec2f = {
  {
    "MFVec2f", JSCLASS_HAS_PRIVATE, NULL, NULL,
    MFVec2fHandler::get, MFVec2fHandler::set,
    NULL, NULL, NULL,
    MFVec2fHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  MFFunctions,
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::MFVec3f = {
  {
    "MFVec3f", JSCLASS_HAS_PRIVATE, NULL, NULL,
    MFVec3fHandler::get, MFVec3fHandler::set,
    NULL, NULL, NULL,
    MFVec3fHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  MFFunctions,
};

CoinVrmlJs::ClassDescriptor CoinVrmlJs::MFVec3d = {
  {
    "MFVec3d", JSCLASS_HAS_PRIVATE, NULL, NULL,
    MFVec3dHandler::get, MFVec3dHandler::set,
    NULL, NULL, NULL,
    MFVec3dHandler::destructor,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0
  },
  MFFunctions,
};


CoinVrmlJs::ClassDescriptor * CLASSDESCRIPTORS[] = {
  &CoinVrmlJs::SFColor, &CoinVrmlJs::SFNode, &CoinVrmlJs::SFRotation,
  &CoinVrmlJs::SFVec2f, &CoinVrmlJs::SFVec3f, &CoinVrmlJs::SFVec3d,
  &CoinVrmlJs::MFColor,
  &CoinVrmlJs::MFFloat, &CoinVrmlJs::MFInt32, &CoinVrmlJs::MFNode,
  &CoinVrmlJs::MFRotation, &CoinVrmlJs::MFString, &CoinVrmlJs::MFTime,
  &CoinVrmlJs::MFVec2f, &CoinVrmlJs::MFVec3f, &CoinVrmlJs::MFVec3d
};

// *************************************************************************

// cleans up static / one-off resource allocations
static void
js_vrmlclasses_cleanup(void)
{
  delete garbagecollectedobjects;
  delete nodesensorstobedeleted;

  garbagecollectedobjects = NULL;
  nodesensorstobedeleted = NULL;
}

// *************************************************************************
// helper function to add all classes to engine

void
JS_addVRMLclasses(SoJavaScriptEngine * engine)
{
  // init static data
  if (garbagecollectedobjects == NULL) {
    garbagecollectedobjects = new SbList <JSObject *>;
    nodesensorstobedeleted = new SbList <SoNodeSensor *>;
    coin_atexit((coin_atexit_f *)js_vrmlclasses_cleanup, CC_ATEXIT_NORMAL);

    // set up default function stubs for Spidermonkey classes we
    // make. must be done at run-time to avoid calling spidermonkey()
    // early (i.e. not on demand).
    const size_t NRELEMENTS = sizeof(CLASSDESCRIPTORS) / sizeof(CLASSDESCRIPTORS[0]);
    for (size_t i=0; i < NRELEMENTS; i++) {
      CoinVrmlJs::ClassDescriptor * desc = CLASSDESCRIPTORS[i];
      desc->cls.addProperty = spidermonkey()->JS_PropertyStub;
      desc->cls.delProperty = spidermonkey()->JS_PropertyStub;
      desc->cls.enumerate = spidermonkey()->JS_EnumerateStub;
      desc->cls.resolve = spidermonkey()->JS_ResolveStub;
      desc->cls.convert = spidermonkey()->JS_ConvertStub;
    }
  }

  // Bool
  engine->addHandler(
    SoSFBool::getClassTypeId(), NULL,
    SFBool_field2jsval, SFBool_jsval2field);

  // Color
  engine->addHandler(
    SoSFColor::getClassTypeId(), SFColor_init,
    SFColor_field2jsval, SFColor_jsval2field);
  engine->addHandler(
    SoMFColor::getClassTypeId(),
    MFColorHandler::init,
    MFColorHandler::field2jsval,
    MFColorHandler::jsval2field);

  // Float
  engine->addHandler(
    SoSFFloat::getClassTypeId(), NULL,
    SFFloat_field2jsval, SFFloat_jsval2field);
  engine->addHandler(
    SoMFFloat::getClassTypeId(),
    MFFloatHandler::init,
    MFFloatHandler::field2jsval,
    MFFloatHandler::jsval2field);

  // Int32
  engine->addHandler(
    SoSFInt32::getClassTypeId(), NULL,
    SFInt32_field2jsval, SFInt32_jsval2field);
  engine->addHandler(
    SoMFInt32::getClassTypeId(),
    MFInt32Handler::init,
    MFInt32Handler::field2jsval,
    MFInt32Handler::jsval2field);

  // Enum
  engine->addHandler(
    SoSFEnum::getClassTypeId(), NULL,
    SFEnum_field2jsval, SFEnum_jsval2field);

  // Node
  engine->addHandler(
    SoSFNode::getClassTypeId(), SFNode_init,
    SFNode_field2jsval, SFNode_jsval2field);
  engine->addHandler(
    SoMFNode::getClassTypeId(),
    MFNodeHandler::init,
    MFNodeHandler::field2jsval,
    MFNodeHandler::jsval2field);

  // Rotation
  engine->addHandler(
    SoSFRotation::getClassTypeId(), SFRotation_init,
    SFRotation_field2jsval, SFRotation_jsval2field);
  engine->addHandler(
    SoMFRotation::getClassTypeId(),
    MFRotationHandler::init,
    MFRotationHandler::field2jsval,
    MFRotationHandler::jsval2field);

  // String
  engine->addHandler(
    SoSFString::getClassTypeId(), NULL,
    SFString_field2jsval, SFString_jsval2field);
  engine->addHandler(
    SoMFString::getClassTypeId(),
    MFStringHandler::init,
    MFStringHandler::field2jsval,
    MFStringHandler::jsval2field);

  // Time
  engine->addHandler(
    SoSFTime::getClassTypeId(), NULL,
    SFTime_field2jsval, SFTime_jsval2field);
  engine->addHandler(
    SoMFTime::getClassTypeId(),
    MFTimeHandler::init,
    MFTimeHandler::field2jsval,
    MFTimeHandler::jsval2field);

  // Vec2f
  engine->addHandler(
    SoSFVec2f::getClassTypeId(), SFVec2f_init,
    SFVec2f_field2jsval, SFVec2f_jsval2field);
  engine->addHandler(
    SoMFVec2f::getClassTypeId(),
    MFVec2fHandler::init,
    MFVec2fHandler::field2jsval,
    MFVec2fHandler::jsval2field);

  // Vec3f
  engine->addHandler(
    SoSFVec3f::getClassTypeId(), SFVec3f_init,
    SFVec3f_field2jsval, SFVec3f_jsval2field);
  engine->addHandler(
    SoMFVec3f::getClassTypeId(),
    MFVec3fHandler::init,
    MFVec3fHandler::field2jsval,
    MFVec3fHandler::jsval2field);

  // Vec3d
  engine->addHandler(
    SoSFVec3d::getClassTypeId(), SFVec3d_init,
    SFVec3d_field2jsval, SFVec3d_jsval2field);
  engine->addHandler(
    SoMFVec3d::getClassTypeId(),
    MFVec3dHandler::init,
    MFVec3dHandler::field2jsval,
    MFVec3dHandler::jsval2field);
}

#endif // HAVE_VRML97 && COIN_HAVE_JAVASCRIPT