Coin / src / vrml97 / Script.cpp

Peder Blekken a78b2c9 
Marius Kintel 3ba6b41 




























Peder Blekken a78b2c9 

Morten Eriksen 6869498 









Lars J. Aas 94c1d19 



Morten Eriksen d971301 


Lars J. Aas 94c1d19 

Morten Eriksen d971301 

Peder Blekken a78b2c9 


Peder Blekken b092bcb 
Peder Blekken 06a8e56 
Peder Blekken b092bcb 












Peder Blekken 06a8e56 
Peder Blekken b092bcb 












Morten Eriksen aa39f96 
Peder Blekken b092bcb 


Morten Eriksen aa39f96 
Peder Blekken b092bcb 









Peder Blekken 06a8e56 
Peder Blekken b092bcb 
Peder Blekken 06a8e56 
Peder Blekken b092bcb 







Peder Blekken 06a8e56 
Peder Blekken b092bcb 



Peder Blekken 06a8e56 
Peder Blekken b092bcb 


Morten Eriksen d971301 

Peder Blekken b092bcb 











Morten Eriksen d971301 


Peder Blekken b092bcb 
Morten Eriksen d971301 
Peder Blekken b092bcb 
Morten Eriksen d971301 
Peder Blekken b092bcb 









Peder Blekken a78b2c9 

Morten Eriksen d971301 

Tom Fredrik Blen… 87fd51d 
Peder Blekken a78b2c9 
Jostein 62cd825 
Morten Eriksen d971301 


Peder Blekken a78b2c9 


Peder Blekken 06a8e56 
Peder Blekken a78b2c9 

Peder Blekken 06a8e56 
Peder Blekken 92b98f0 
Morten Eriksen d971301 
Øystein Handegar… f2deb29 
erikgors 7d0bcae 
Morten Eriksen d971301 
Lars J. Aas 92263e4 


Morten Eriksen d971301 
Peder Blekken a78b2c9 
Peder Blekken 92b98f0 




Morten Eriksen d971301 



Peder Blekken 92b98f0 
Morten Eriksen d971301 
Øystein Handegar… f2deb29 
erikgors bbefa4e 
Øystein Handegar… f2deb29 
Peder Blekken 92b98f0 
Morten Eriksen d971301 


Peder Blekken 92b98f0 
erikgors 7d0bcae 
Øystein Handegar… f2deb29 
erikgors 7d0bcae 


Øystein Handegar… f2deb29 
Peder Blekken 92b98f0 

Øystein Handegar… f2deb29 
Morten Eriksen 54ba0c1 
Øystein Handegar… f2deb29 

erikgors bbefa4e 


Morten Eriksen d971301 

Øystein Handegar… f2deb29 
Morten Eriksen d971301 


Peder Blekken 92b98f0 
Morten Eriksen d971301 
erikgors bbefa4e 
Morten Eriksen d971301 

Øystein Handegar… f2deb29 
erikgors 7d0bcae 
Øystein Handegar… f2deb29 
Morten Eriksen d971301 
Morten Eriksen 54ba0c1 

Morten Eriksen d971301 

Peder Blekken 92b98f0 

Morten Eriksen d971301 


Morten Eriksen 54ba0c1 



Morten Eriksen d971301 


Morten Eriksen 54ba0c1 
erikgors 7d0bcae 




erikgors bbefa4e 

Peder Blekken 40ed6c0 
erikgors 7d0bcae 
erikgors bbefa4e 
Morten Eriksen 54ba0c1 



Karin (kyrah) Ko… b2a4b89 

Morten Eriksen d971301 










erikgors bbefa4e 
Morten Eriksen d971301 



Øystein Handegar… f2deb29 

Morten Eriksen d971301 


















erikgors 7d0bcae 
Morten Eriksen d971301 


Øystein Handegar… f2deb29 

Morten Eriksen d971301 
Peder Blekken 92b98f0 
Morten Eriksen 094edd2 
Peder Blekken a78b2c9 
Morten Eriksen d971301 
Peder Blekken a78b2c9 





Peder Blekken 8a99016 

Peder Blekken 1845d1f 
Peder Blekken a78b2c9 

Morten Eriksen d971301 

Peder Blekken a78b2c9 


Morten Eriksen d216776 
Morten Eriksen 54ba0c1 
Øystein Handegar… f2deb29 
Morten Eriksen 54ba0c1 







Morten Eriksen d971301 
Øystein Handegar… f2deb29 
Morten Eriksen d971301 
Peder Blekken 92b98f0 
Peder Blekken 1845d1f 
Peder Blekken 92b98f0 
Peder Blekken a78b2c9 





Morten Eriksen d971301 

erikgors bbefa4e 


Peder Blekken a78b2c9 

Morten Eriksen d971301 

erikgors bbefa4e 
Peder Blekken a78b2c9 




Morten Eriksen d971301 
Peder Blekken a78b2c9 
Peder Blekken 92b98f0 

Peder Blekken a78b2c9 









Morten Eriksen d971301 
Peder Blekken a78b2c9 
Morten Eriksen d971301 
Peder Blekken a78b2c9 





Morten Eriksen d971301 
Peder Blekken a78b2c9 





Morten Eriksen d971301 

Peder Blekken 92b98f0 











Morten Eriksen d971301 


Peder Blekken a78b2c9 
Jostein 62cd825 
Peder Blekken a78b2c9 


Morten Eriksen d971301 
Peder Blekken a78b2c9 


Peder Blekken 92b98f0 
Peder Blekken a78b2c9 

Morten Eriksen d971301 
Peder Blekken a78b2c9 


Peder Blekken 92b98f0 
Peder Blekken a78b2c9 

Morten Eriksen d971301 
Peder Blekken a78b2c9 


Peder Blekken 92b98f0 
Peder Blekken a78b2c9 

Morten Eriksen d971301 
Peder Blekken a78b2c9 


Peder Blekken 92b98f0 
Peder Blekken a78b2c9 

Morten Eriksen d971301 
Peder Blekken a78b2c9 


Peder Blekken 92b98f0 
Peder Blekken a78b2c9 

Morten Eriksen d971301 


Peder Blekken a78b2c9 


Peder Blekken 1845d1f 





Peder Blekken 06a8e56 

Morten Eriksen d971301 


Peder Blekken 1845d1f 











Peder Blekken 06a8e56 




Peder Blekken 1845d1f 
Peder Blekken 06a8e56 

























Peder Blekken 1845d1f 
Peder Blekken 06a8e56 
Peder Blekken 1845d1f 



Peder Blekken d672b1d 




Peder Blekken 06a8e56 

Peder Blekken bab5bb9 


Peder Blekken 06a8e56 




Peder Blekken a78b2c9 

Morten Eriksen d971301 


Peder Blekken a78b2c9 















Peder Blekken b185ddf 
Peder Blekken a78b2c9 






Morten Eriksen d971301 


Peder Blekken 92b98f0 
Morten Eriksen d971301 
Peder Blekken 92b98f0 
Morten Eriksen d971301 


Peder Blekken 92b98f0 





Morten Eriksen d971301 



erikgors bbefa4e 










Morten Eriksen d971301 
Peder Blekken 92b98f0 

Morten Eriksen d971301 
Øystein Handegar… f2deb29 
erikgors bbefa4e 


Øystein Handegar… f2deb29 
Morten Eriksen d971301 

Peder Blekken 92b98f0 

Morten Eriksen d971301 


Peder Blekken a78b2c9 


Morten Eriksen d971301 
Peder Blekken a78b2c9 

Morten Eriksen d971301 
Peder Blekken a78b2c9 





Morten Eriksen d971301 


Peder Blekken a78b2c9 
Jostein 62cd825 
Peder Blekken a78b2c9 
Peder Blekken 92b98f0 


Lars J. Aas 709baad 
Peder Blekken a78b2c9 













Peder Blekken 8a99016 

Peder Blekken a78b2c9 













erikgors bbefa4e 
Peder Blekken a78b2c9 


Morten Eriksen d971301 
Peder Blekken a78b2c9 






erikgors bbefa4e 
Peder Blekken a78b2c9 
Peder Blekken 378c0ef 



Peder Blekken a78b2c9 



Peder Blekken 378c0ef 



Peder Blekken a78b2c9 



Peder Blekken 378c0ef 
Peder Blekken a78b2c9 

Peder Blekken 378c0ef 


Peder Blekken a78b2c9 




Peder Blekken 8a99016 





Peder Blekken 378c0ef 



Peder Blekken 8a99016 
Peder Blekken a78b2c9 

Peder Blekken 92b98f0 

Peder Blekken a78b2c9 

Peder Blekken 92b98f0 

Peder Blekken a78b2c9 
Peder Blekken 92b98f0 
Peder Blekken a78b2c9 

Morten Eriksen d971301 

Peder Blekken a78b2c9 












Peder Blekken 92b98f0 
Morten Eriksen d971301 

Øystein Handegar… f2deb29 

Morten Eriksen d971301 
erikgors bbefa4e 
Morten Eriksen d971301 
erikgors bbefa4e 
Morten Eriksen 54ba0c1 



erikgors bbefa4e 

Øystein Handegar… f2deb29 
erikgors bbefa4e 
Morten Eriksen d971301 
Peder Blekken 40ed6c0 
erikgors bbefa4e 
Morten Eriksen d971301 
erikgors bbefa4e 

Peder Blekken 40ed6c0 
erikgors bbefa4e 
Peder Blekken 40ed6c0 



erikgors bbefa4e 



Morten Eriksen 54ba0c1 

erikgors bbefa4e 



erikgors 7d0bcae 
erikgors bbefa4e 

Morten Eriksen d971301 

erikgors bbefa4e 

Peder Blekken 3ac50f1 





Morten Eriksen 54ba0c1 
Morten Eriksen d971301 
erikgors bbefa4e 




erikgors 7d0bcae 


erikgors bbefa4e 



















erikgors 7d0bcae 






erikgors bbefa4e 
erikgors 7d0bcae 
erikgors bbefa4e 

























Morten Eriksen d971301 

Øystein Handegar… f2deb29 

Morten Eriksen d971301 












erikgors bbefa4e 
Morten Eriksen d971301 


Øystein Handegar… f2deb29 
erikgors bbefa4e 
Morten Eriksen d971301 

Øystein Handegar… f2deb29 
Morten Eriksen d971301 











Peder Blekken 92b98f0 



Morten Eriksen d971301 
















erikgors bbefa4e 



Morten Eriksen d971301 





Øystein Handegar… f2deb29 
erikgors 7d0bcae 




Øystein Handegar… f2deb29 
Morten Eriksen d971301 

erikgors bbefa4e 

Øystein Handegar… f2deb29 
erikgors bbefa4e 



Øystein Handegar… f2deb29 
Morten Eriksen d971301 




Øystein Handegar… f2deb29 
erikgors 7d0bcae 







Øystein Handegar… f2deb29 
erikgors bbefa4e 


Morten Eriksen d971301 

erikgors 7d0bcae 
Morten Eriksen d971301 





Øystein Handegar… f2deb29 
erikgors 7d0bcae 




Øystein Handegar… f2deb29 

erikgors 7d0bcae 


Morten Eriksen d971301 


Lars J. Aas 94c1d19 
Morten Eriksen d971301 
Peder Blekken 92b98f0 
Lars J. Aas 94c1d19 
  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
/**************************************************************************\
 * 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.
\**************************************************************************/

// FIXME: idea from thammer; make all the various operations connect
// through callback-plugins, for maximum flexibility. 20050606 mortene.

// FIXME: another idea; factor out all the internal Javascript
// handling, connecting up to fields, etc etc to a more generic
// internal interface -- as thammer's project would like to connect
// Javascript programs/routines to any scene graph field. 20050606 mortene.

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

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H

// FIXME: is this really necessary? Coin/src/Makefile.am stops
// traversal into src/vrml97/, so it seems
// superfluous. Investigate. 20050526 mortene.
#ifdef HAVE_VRML97

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

/*!
  \class SoVRMLScript SoVRMLScript.h Inventor/VRMLnodes/SoVRMLScript.h
  \brief The SoVRMLScript class is used to control the scene using scripts.
  \ingroup VRMLnodes

  \WEB3DCOPYRIGHT

  \verbatim
  Script {
    exposedField MFString url           []
    field        SFBool   directOutput  FALSE
    field        SFBool   mustEvaluate  FALSE
    # And any number of:
    eventIn      eventType eventName
    field        fieldType fieldName initialValue
    eventOut     eventType eventName
  }
  \endverbatim

  The Script node is used to program behaviour in a scene. Script nodes
  typically

  - signify a change or user action;
  - receive events from other nodes;
  - contain a program module that performs some computation;
  - effect change somewhere else in the scene by sending events.

  Each Script node has associated programming language code,
  referenced by the url field, that is executed to carry out the
  Script node's function. That code is referred to as the "script" in
  the rest of this description. Details on the url field can be found
  in 4.5, VRML and the World Wide Web
  (<http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-VRML97/part1/concepts.html#4.5>).

  Browsers are not required to support any specific language. Detailed
  information on scripting languages is described in 4.12, Scripting
  (<http://www.web3d.org/x3d/specifications/vrml/ISO-IEC-14772-VRML97/part1/concepts.html#4.12>).

  Browsers supporting a scripting language for which a language
  binding is specified shall adhere to that language binding.
  Sometime before a script receives the first event it shall be
  initialized (any language-dependent or user-defined initialize() is
  performed).

  The script is able to receive and process events that are sent to
  it. Each event that can be received shall be declared in the Script
  node using the same syntax as is used in a prototype definition:

  \verbatim
  eventIn type name
  \endverbatim

  The type can be any of the standard VRML fields
  (as defined in 5, Field and event reference). Name shall be an
  identifier that is unique for this Script node.

  The Script node is able to generate events in response to the
  incoming events. Each event that may be generated shall be declared
  in the Script node using the following syntax:

  \verbatim
  eventOut type name
  \endverbatim

  With the exception of the url field, exposedFields are not allowed
  in Script nodes.

  If the Script node's \e mustEvaluate field is \c FALSE, the browser
  may delay sending input events to the script until its outputs are
  needed by the browser. If the \e mustEvaluate field is TRUE, the
  browser shall send input events to the script as soon as possible,
  regardless of whether the outputs are needed. The \e mustEvaluate
  field shall be set to TRUE only if the Script node has effects that
  are not known to the browser (such as sending information across the
  network). Otherwise, poor performance may result.

  Once the script has access to a VRML node (via an SoSFNode or
  SoMFNode value either in one of the Script node's fields or passed
  in as an eventIn), the script is able to read the contents of that
  node's exposed fields.

  If the Script node's \e directOutput field is \c TRUE, the script
  may also send events directly to any node to which it has access,
  and may dynamically establish or break routes.

  If directOutput is \c FALSE (the default), the script may only
  affect the rest of the world via events sent through its
  eventOuts. The results are undefined if directOutput is \c FALSE and
  the script sends events directly to a node to which it has access.

  A script is able to communicate directly with the VRML browser to
  get information such as the current time and the current world
  URL. This is strictly defined by the API for the specific scripting
  language being used.  The location of the Script node in the scene
  graph has no affect on its operation. For example, if a parent of a
  Script node is a Switch node with whichChoice set to "-1" (i.e.,
  ignore its children), the Script node continues to operate as
  specified (i.e., it receives and sends events).
*/

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

/*! \file SoVRMLScript.h */
#include <Inventor/VRMLnodes/SoVRMLScript.h>
#include "coindefs.h"

#include <assert.h>

#include <Inventor/VRMLnodes/SoVRMLMacros.h>
#include <Inventor/SbName.h>
#include <Inventor/SoInput.h>
#include <Inventor/SoOutput.h>
#include <Inventor/misc/SoProto.h>
#include <Inventor/errors/SoReadError.h>
#include <Inventor/actions/SoWriteAction.h>
#include <Inventor/sensors/SoOneShotSensor.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/C/tidbits.h>
#include <Inventor/misc/SoJavaScriptEngine.h>

#include "nodes/SoSubNodeP.h"
#include "tidbitsp.h"

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

static SoVRMLScriptEvaluateCB * sovrmlscript_eval_cb = NULL;
static void * sovrmlscript_eval_closure = NULL;

class SoVRMLScriptP {
public:
  SoVRMLScriptP(SoVRMLScript * m)
  {
    this->master = m;
    this->oneshotsensor = new SoOneShotSensor(SoVRMLScript::eval_cb, master);
    this->isreading = FALSE;
    this->isevaluating = FALSE;
#ifdef COIN_HAVE_JAVASCRIPT
    this->engine = NULL;
#endif // !COIN_HAVE_JAVASCRIPT
  }

  ~SoVRMLScriptP()
  {
    delete this->oneshotsensor;

#ifdef COIN_HAVE_JAVASCRIPT
    // FIXME: this needs to be done in a nicer way. 20050720 erikgors.
    if (this->engine != NULL)
      this->shutdown();
#endif // !COIN_HAVE_JAVASCRIPT
  }

  static SbBool debug(void);
  static void cleanup(void);

#ifdef COIN_HAVE_JAVASCRIPT
  void initialize(void);
  void shutdown(void);

  static SbBool allowSpiderMonkey(void);
  static SbBool useSpiderMonkey(void);
#endif // !COIN_HAVE_JAVASCRIPT
  void evaluate(void);

  SbBool isreading, isevaluating;
  SoOneShotSensor * oneshotsensor;

  SbList<SbName> fieldnotifications, eventoutfields, eventinfields;
  void executeFunctions(void);

#ifdef COIN_HAVE_JAVASCRIPT
  SoJavaScriptEngine * engine;
#endif // !COIN_HAVE_JAVASCRIPT

  static SbBool spidermonkey_init_failed;

private:
  SoVRMLScript * master;
};

#define PUBLIC(p) ((p)->master)
#define PRIVATE(p) ((p)->pimpl)

SbBool SoVRMLScriptP::spidermonkey_init_failed = FALSE;

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

void
SoVRMLScriptP::cleanup(void)
{
#ifdef COIN_HAVE_JAVASCRIPT
  // FIXME: need to make sure this is added to atexit only after
  // the engine has started. 20050720 erikgors.
  if (SoJavaScriptEngine::getRuntime() == NULL)
    return;

  if (SoVRMLScriptP::useSpiderMonkey()) {
    // Destroy javascript runtime
    
    SoJavaScriptEngine::shutdown();
  }
#endif // !COIN_HAVE_JAVASCRIPT

  // reset static var
  SoVRMLScriptP::spidermonkey_init_failed = FALSE;
  sovrmlscript_eval_cb = NULL;
  sovrmlscript_eval_closure = NULL;
}

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

SbBool
SoVRMLScriptP::debug(void)
{
  static int d = -1;
  if (d == -1) {
    const char * env = coin_getenv("COIN_DEBUG_VRMLSCRIPT");
    d = (env && (atoi(env) > 0)) ? 1 : 0;

  }
  return d ? TRUE : FALSE;
}

#ifdef COIN_HAVE_JAVASCRIPT

// The Javascript support is far from being compliant with the VRML
// specification, and has so far been developed just for internal SIM
// use, so one needs to explicitly activate it for now.
SbBool
SoVRMLScriptP::allowSpiderMonkey(void)
{
  static int d = -1;
  if (d == -1) {
    const char * env = coin_getenv("COIN_ALLOW_SPIDERMONKEY");
    d = (env && (atoi(env) > 0)) ? 1 : 0;
  }
  return d ? TRUE : FALSE;
}

SbBool
SoVRMLScriptP::useSpiderMonkey(void)
{
  if (!SoVRMLScriptP::allowSpiderMonkey()) { return FALSE; }
  if (!spidermonkey()->available) { return FALSE; }
  if (SoJavaScriptEngine::getRuntime() == NULL) { return FALSE; }
  return TRUE;
}

#endif // !COIN_HAVE_JAVASCRIPT

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

SoType SoVRMLScript::classTypeId STATIC_SOTYPE_INIT;

// Doc in superclass
void
SoVRMLScript::initClass(void) // static
{
  SoVRMLScript::classTypeId =
    SoType::createType(SoNode::getClassTypeId(),
                       SbName("VRMLScript"),
                       SoVRMLScript::createInstance,
                       SoNode::nextActionMethodIndex++);
  SoNode::setCompatibilityTypes(SoVRMLScript::getClassTypeId(), SO_VRML97_NODE_TYPE);
}

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

SoVRMLScript::SoVRMLScript(void)
  : fielddata(NULL)
{
  coin_atexit((coin_atexit_f *)SoVRMLScriptP::cleanup, CC_ATEXIT_NORMAL);

#ifdef COIN_HAVE_JAVASCRIPT
  if (SoVRMLScriptP::allowSpiderMonkey() &&
      !SoVRMLScriptP::spidermonkey_init_failed &&
      // FIXME: next line is a hack-ish way of checking whether init()
      // has already been done on the SoJavaScriptEngine class.
      // 20060207 mortene.
      (SoJavaScriptEngine::getRuntime() == NULL)) {
    SbBool ok = SoJavaScriptEngine::init();
    if (!ok) { SoVRMLScriptP::spidermonkey_init_failed = TRUE; }
  }
#endif // !COIN_HAVE_JAVASCRIPT

  PRIVATE(this) = new SoVRMLScriptP(this);
  this->setNodeType(SoNode::VRML2);

  this->isBuiltIn = TRUE;
  assert(SoVRMLScript::classTypeId != SoType::badType());

  this->url.setNum(0);
  this->url.setContainer(this);

  // FIXME: directOutput should default be FALSE, according to the
  // VRML97 spec doc. 20050526 mortene.
  // Looks more like a hint than a rule to me. Something we can
  // safely ignore. 20050712 erikgors.
  this->directOutput.setValue(FALSE);
  this->directOutput.setContainer(this);

  // FIXME: shouldn't mustEvaluate be default FALSE? Seems like it
  // from the VRML97 spec doc. 20050526 mortene.
  this->mustEvaluate.setValue(FALSE);
  this->mustEvaluate.setContainer(this);

  this->initFieldData();
}

SoVRMLScript::~SoVRMLScript()
{
  delete PRIVATE(this);

  const int n = this->fielddata->getNumFields();
  for (int i = 0; i < n; i++) {
    SoField * f = this->fielddata->getField(this, i);
    if (f != &this->directOutput &&
        f != &this->url &&
        f != &this->mustEvaluate) delete f;
  }
  delete this->fielddata;
}

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

// Doc in superclass
SoType
SoVRMLScript::getClassTypeId(void)
{
  return SoVRMLScript::classTypeId;
}

// Doc in superclass
SoType
SoVRMLScript::getTypeId(void) const
{
  return SoVRMLScript::classTypeId;
}

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

/*!  
  Sets the callback that will be called when the script needs to be
  evaluated.  
*/
void 
SoVRMLScript::setScriptEvaluateCB(SoVRMLScriptEvaluateCB * cb,
                                  void * closure)
{
  sovrmlscript_eval_cb = cb;
  sovrmlscript_eval_closure = closure;
}

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

// Doc in superclass
void
SoVRMLScript::doAction(SoAction * COIN_UNUSED_ARG(action))
{
}

// Doc in superclass
void
SoVRMLScript::callback(SoCallbackAction * action)
{
  SoVRMLScript::doAction((SoAction*) action);
}

// Doc in superclass
void
SoVRMLScript::GLRender(SoGLRenderAction * action)
{
  SoVRMLScript::doAction((SoAction*) action);
}

// Doc in superclass
void
SoVRMLScript::getBoundingBox(SoGetBoundingBoxAction * action)
{
  SoVRMLScript::doAction((SoAction*) action);
}

// Doc in superclass
void
SoVRMLScript::pick(SoPickAction * action)
{
  SoVRMLScript::doAction((SoAction*) action);
}

// Doc in superclass
void
SoVRMLScript::handleEvent(SoHandleEventAction * action)
{
  SoVRMLScript::doAction((SoAction*) action);
}

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

// Doc in superclass
void
SoVRMLScript::write(SoWriteAction * action)
{
  int i;
  const SbName URL("url");
  const SbName DIRECTOUTPUT("directOutput");
  const SbName MUSTEVALUATE("mustEvaluate");
  const SoFieldData * fd = this->getFieldData();

  SoOutput * out = action->getOutput();
  if (out->getStage() == SoOutput::COUNT_REFS) {
    // We will always write NORMAL and EXPOSED fields, so do a
    // setDefault(FALSE) on them. We will not write other field types,
    // so do a setDefault(TRUE) on them.
    for (i = 0; i < fd->getNumFields(); i++) {
      const SoField * f = fd->getField(this, i);
      SbName fieldname = fd->getFieldName(i);
      if (fieldname != URL && fieldname != DIRECTOUTPUT &&
          fieldname != MUSTEVALUATE) {
        if ((f->getFieldType() == SoField::NORMAL_FIELD) ||
            (f->getFieldType() == SoField::EXPOSED_FIELD)) {
          ((SoField*)f)->setDefault(FALSE);
        }
        else ((SoField*)f)->setDefault(TRUE);
      }
    }
    inherited::write(action);
  }
  else if (out->getStage() == SoOutput::WRITE) {
    if (this->writeHeader(out, FALSE, FALSE))
      return;
    for (i = 0; i < fd->getNumFields(); i++) {
      const SoField * f = fd->getField(this, i);
      SbName fieldname = fd->getFieldName(i);
      SbBool writevalue = FALSE;
      if (fieldname != URL && fieldname != DIRECTOUTPUT &&
          fieldname != MUSTEVALUATE) {
        out->indent();
        switch (f->getFieldType()) {
        case SoField::NORMAL_FIELD:        
          out->write("field ");
          writevalue = TRUE;
          break;
        case SoField::EVENTIN_FIELD:
          out->write("eventIn ");
          break;
        case SoField::EVENTOUT_FIELD:
          out->write("eventOut ");
          break;
        case SoField::EXPOSED_FIELD:
          out->write("exposedField ");
          writevalue = TRUE;
          break;
        default:
          break;
        }
        out->write(f->getTypeId().getName().getString());
        out->write(' ');

        if (writevalue) {
          f->write(out, fieldname);
        }
        else {
          out->write(fieldname.getString());
          // the write() call below is needed to resolve
          // ROUTES. SoField::write() will not write anything since if
          // we get here, the field is either an EVENTIN or an
          // EVENTOUT field
          f->write(out, fieldname); 
        }
        out->write("\n");
      }
      else if (f->shouldWrite()){
        f->write(out, fieldname);
      }
    }
    this->writeFooter(out);
  }
  else assert(0 && "unknown stage");
}

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

// Doc in superclass
void
SoVRMLScript::copyContents(const SoFieldContainer * from,
                           SbBool copyConn)
{
  assert(from->isOfType(SoVRMLScript::getClassTypeId()));

  const SoVRMLScript * fromnode = (SoVRMLScript*) from;

  const SoFieldData * src = from->getFieldData();
  const int n = src->getNumFields();
  for (int i = 0; i < n; i++) {
    const SoField * f = src->getField(from, i);
    if (f != &fromnode->directOutput &&
        f != &fromnode->url &&
        f != &fromnode->mustEvaluate) {
      SoField * cp = (SoField*) f->getTypeId().createInstance();
      cp->setFieldType(f->getFieldType());
      cp->setContainer(this);
      this->fielddata->addField(this, src->getFieldName(i), cp);
    }
  }
  inherited::copyContents(from, copyConn);
}

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

// Doc in superclass
void 
SoVRMLScript::notify(SoNotList * l)
{
  const SoField * f = l->getLastField();

  if (!PRIVATE(this)->isreading && !PRIVATE(this)->isevaluating) {
    if (f == &this->mustEvaluate) {
      int pri = this->mustEvaluate.getValue() ? 0 : 
        SoDelayQueueSensor::getDefaultPriority();
      PRIVATE(this)->oneshotsensor->setPriority(pri);
    }
    else {
      SbName name;
      SbBool ok = this->getFieldName(f, name);
      assert(ok);

      // We silently ignore events for non-eventIn fields
      // FIXME: This will happen when we get a fieldnotification from a
      // reference. Should we post a warning? 20050712 erikgors.
      if (PRIVATE(this)->eventinfields.find(name) != -1) {
        if (PRIVATE(this)->fieldnotifications.find(name) == -1) {
          PRIVATE(this)->fieldnotifications.append(name);
        }

        if (!PRIVATE(this)->oneshotsensor->isScheduled()) {
          PRIVATE(this)->oneshotsensor->schedule();
        }
      }
    }
  }

#ifdef COIN_HAVE_JAVASCRIPT
  if (f == &this->url) {
    PRIVATE(this)->initialize();
  }
#endif // !COIN_HAVE_JAVASCRIPT

  inherited::notify(l);
}

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

// Doc in superclass
void *
SoVRMLScript::createInstance(void)
{
  return (void *)new SoVRMLScript;
}

// Doc in superclass
const SoFieldData *
SoVRMLScript::getFieldData(void) const
{
  return this->fielddata;
}

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

// Doc in superclass
SbBool
SoVRMLScript::readInstance(SoInput * in, unsigned short COIN_UNUSED_ARG(flags))
{
  // avoid triggering the eval cb while reading the file.
  PRIVATE(this)->isreading = TRUE;

  SbName name(SbName::empty());
  SbBool ok;

  ok = in->read(name, TRUE);

  const SbName URL("url");
  const SbName DIRECTOUTPUT("directOutput");
  const SbName MUSTEVALUATE("mustEvaluate");
  const SbName EVENTIN("eventIn");
  const SbName EVENTOUT("eventOut");
  const SbName FIELD("field");
  const SbName EXPOSEDFIELD("exposedField");

  SbBool err = FALSE;

  SoField * builtinfield;

  while (!err && ok) {
    if (name == EVENTIN ||
        name == EVENTOUT ||
        name == FIELD ||
        name == EXPOSEDFIELD) {
      SbName ftype, fname;
      err = ! (in->read(ftype, TRUE) && in->read(fname, TRUE));
      if (!err) {
        SoType type = SoType::fromName(ftype);
        if (type.isDerivedFrom(SoField::getClassTypeId()) && type.canCreateInstance()) {
          SoField * f = (SoField*) type.createInstance();

          if (name == EVENTIN) {
            f->setFieldType(SoField::EVENTIN_FIELD);
            PRIVATE(this)->eventinfields.append(fname);
          }
          else if (name == EVENTOUT) {
            f->setFieldType(SoField::EVENTOUT_FIELD);
            PRIVATE(this)->eventoutfields.append(fname);
          }
          else if (name == EXPOSEDFIELD) {
            f->setFieldType(SoField::EXPOSED_FIELD);
          }
          f->setContainer(this);
          this->fielddata->addField(this, fname, f);
          if (name == FIELD || name == EXPOSEDFIELD) { // only read field values for fields

            err = ! f->read(in, fname);
            if (err) {
              SoReadError::post(in, "Unable to read default value for '%s'.", 
                                fname.getString());
            }
          }
          else {
            (void) in->checkISReference(this, fname, err);
            err = !err;
            if (err) {
              SoReadError::post(in, "Error while parsing IS reference for '%s'.", 
                                fname.getString());
            }
          }
        }
        else {
          err = TRUE;
          SoReadError::post(in, "Unknown field type.");
        }
      }
      else {
        SoReadError::post(in, "Unable to read field name.");
      }
      if (!err) {
        name = "";
        ok = in->read(name, TRUE);
      }
    }
    else if ((builtinfield = this->getField(name)) != NULL) {
      err = !builtinfield->read(in, name);
      if (!err) {
        name = "";
        ok = in->read(name, TRUE);
      }
      else {
        SoReadError::post(in, "Error while reading field '%s'.", 
                          name.getString());
      }
    }
    else ok = FALSE;
  }
  PRIVATE(this)->isreading = FALSE;
  
  if (!err) {
    if (name != "") in->putBack(name.getString());
    // evaluate script
    PRIVATE(this)->oneshotsensor->schedule();
  }
  return !err;
}

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

//
// Private method that initializes the field data and adds the default
// fields.
//
void
SoVRMLScript::initFieldData(void)
{
  if (this->fielddata) delete this->fielddata;
  this->fielddata = new SoFieldData;
  this->fielddata->addField(this, "url", &this->url);
  this->fielddata->addField(this, "directOutput", &this->directOutput);
  this->fielddata->addField(this, "mustEvaluate", &this->mustEvaluate);
}

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

#ifdef COIN_HAVE_JAVASCRIPT

void
SoVRMLScriptP::initialize(void)
{
  if (this->engine != NULL) {
    if (SoVRMLScriptP::debug()) {
      SoDebugError::postInfo("SoVRMLScriptP::initialize",
                             "restarting script engine");
    }
    this->shutdown();
  }

  SbString script;

  for (int index = 0; index < PUBLIC(this)->url.getNum(); ++index) {
    SbString s(PUBLIC(this)->url[index].getString());

    // javascript support
    const char jsPrefix[] = "javascript:";
    const char jsPrefix2[] = "vrmlscript:";
    const size_t jsPrefixlen = sizeof(jsPrefix) - 1;
    const size_t jsPrefixlen2 = sizeof(jsPrefix2) - 1;
    if (s.getLength() > (int)jsPrefixlen && 
        ((s.getSubString(0, jsPrefixlen -1) == jsPrefix) ||
         (s.getSubString(0, jsPrefixlen2 -1) == jsPrefix2))) {
      // starting javascript engine
      if (!SoVRMLScriptP::useSpiderMonkey()) {
        if (SoVRMLScriptP::debug()) {
          SoDebugError::postInfo("SoVRMLScriptP::initialize",
                                 "Only the SpiderMonkey Javascript engine "
                                 "currently supported.");
        }
        continue;
      }
      assert(this->engine == NULL);
      this->engine = new SoJavaScriptEngine;
      script = s.getSubString(jsPrefixlen);
      break;
    }
  }

  if (this->engine == NULL) {
    static int first = 1;
    if (first) {
      SoDebugError::postWarning("SoVRMLScript::initialize",
                                "No script language evaluation engine available.");
      first = 0;
    }
    return;
  }

  // FIXME: should scriptFields be set before or after the script has been
  // executed? After script feels most correct from a languange independent
  // viewpoint.  200507011 erikgors.

  // FIXME: is getName() a sensiable identificator for script? 20050719 erikgors.
  SbName name = PUBLIC(this)->getName();
  this->engine->executeScript(name, script.getString());

  int numFields = PUBLIC(this)->fielddata->getNumFields();
  for (int i=0; i<numFields; ++i) {
    const SbName & name = PUBLIC(this)->fielddata->getFieldName(i);
    const SoField * f = PUBLIC(this)->fielddata->getField(PUBLIC(this), i);
    if (!(f->getFieldType() == SoField::NORMAL_FIELD ||
        f->getFieldType() == SoField::EVENTOUT_FIELD)) {
      if (SoVRMLScriptP::debug()) {
        SoDebugError::postInfo("SoVRMLScriptP::initialize",
                               "skipping scriptField %s", name.getString());
      }
      continue;
    }
    if (SoVRMLScriptP::debug()) {
      SoDebugError::postInfo("SoVRMLScriptP::initialize",
                             "setting scriptField %s", name.getString());
    }
    this->engine->setScriptField(name, f);
  }

  // Adding TRUE and FALSE to be bug-compatible. 20050719 erikgors.
  SoSFBool * boolean = (SoSFBool *)SoSFBool::createInstance();
  boolean->setValue(TRUE);
  this->engine->setScriptField(SbName("TRUE"), boolean);
  boolean->setValue(FALSE);
  this->engine->setScriptField(SbName("FALSE"), boolean);
  delete boolean;

  // Run initialize function if it exists
  SbName initialize("initialize");
  if (this->engine->hasScriptField(initialize)) {
    if (SoVRMLScriptP::debug()) {
      SoDebugError::postInfo("SoVRMLScriptP::initialize",
                             "executing script function \"%s\"", initialize.getString());
    }
    this->engine->executeFunction(initialize, 0, NULL);
  }
}

void
SoVRMLScriptP::shutdown(void)
{
  assert(this->engine != NULL);

  SbName shutdown("shutdown");
  if (this->engine->hasScriptField(shutdown)) {
    if (SoVRMLScriptP::debug()) {
      SoDebugError::postInfo("SoVRMLScriptP::initialize",
                             "executing script function \"%s\"", shutdown.getString());
    }
    this->engine->executeFunction(shutdown, 0, NULL);
  }

  delete this->engine;
  this->engine = NULL;
}

#endif // !COIN_HAVE_JAVASCRIPT

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

void 
SoVRMLScriptP::evaluate(void)
{
  if (SoVRMLScriptP::debug()) {
    SoDebugError::postInfo("SoVRMLScript::eval_cb", "invoked");
    for (int i = 0; i < this->fieldnotifications.getLength(); i++) {
      SoDebugError::postInfo("SoVRMLScriptP::evaluate",
                             "notification on field '%s'",
                             this->fieldnotifications[i].getString());
    }
  }
  
  if (sovrmlscript_eval_cb) {
    sovrmlscript_eval_cb(sovrmlscript_eval_closure, PUBLIC(this));
  }
#ifdef COIN_HAVE_JAVASCRIPT
  else if (this->engine != NULL) {
    this->executeFunctions();
  }
#endif // !COIN_HAVE_JAVASCRIPT
  else {
    // FIXME: improve on this to be of more informational value to
    // both the app programmer and end-user. 20050526 mortene.
    static SbBool first = TRUE;
    if (first) {
      first = FALSE;
      SoDebugError::postWarning("SoVRMLScript::eval_cb",
                                "No script language evaluation engine available.");
    }
  }
}

// callback for oneshotsensor
void 
SoVRMLScript::eval_cb(void * data, SoSensor *)
{
  SoVRMLScript * thisp = (SoVRMLScript *)data;

  // FIXME: this wrapping is too simple, we can loose important new
  // input events. What we need to do is ignoring any writes to the
  // output field(s) that are updated. 20050602 mortene.
  PRIVATE(thisp)->isevaluating = TRUE;
  PRIVATE(thisp)->evaluate();
  PRIVATE(thisp)->isevaluating = FALSE;
}

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

void
SoVRMLScriptP::executeFunctions(void)
{
  int i;

  if (this->fieldnotifications.getLength() == 0) {
    return;
  }
  
  // Execute all functions for input fields that have been changed.

  for (i = 0; i < this->fieldnotifications.getLength(); i++) {
    const SbName & n = this->fieldnotifications[i];
    const SoField * f = PUBLIC(this)->getField(n);
    assert(f);
#ifdef COIN_HAVE_JAVASCRIPT
    if (!this->engine->executeFunction(n, 1, f)) {
      SoDebugError::postWarning("SoVRMLScriptP::executeFunctions", 
                                "could not execute function %s",
                                n.getString());
    }
#endif // !COIN_HAVE_JAVASCRIPT
  }
  this->fieldnotifications.truncate(0);

  // Call eventsProcessed
#ifdef COIN_HAVE_JAVASCRIPT
  static SbName eventsProcessed("eventsProcessed");
  if (this->engine->hasScriptField(eventsProcessed)) {
    this->engine->executeFunction(eventsProcessed, 0, NULL);
  }
#endif // !COIN_HAVE_JAVASCRIPT

  // Then, pick up all new eventOut values.

  for (i = 0; i < this->eventoutfields.getLength(); i++) {
    const SbName & name = this->eventoutfields[i];
#ifdef COIN_HAVE_JAVASCRIPT
    if (!this->engine->hasScriptField(name)) {
      if (debug()) {
        SoDebugError::postInfo("SoVRMLScriptP::executeFunctions",
                               "No new value of eventOut '%s'...",
                               name.getString());
      }
      continue;
    }
#endif // !COIN_HAVE_JAVASCRIPT
    SoField * f = PUBLIC(this)->getEventOut(name);
    assert(f);

    if (debug()) {
      SoDebugError::postInfo("SoVRMLScriptP::executeFunctions",
                             "Picking up new value of eventOut '%s'...",
                             name.getString());
    }

    // FIXME: should probably rather compare new value with old before
    // actually pushing it. Perhaps there's something about this in
    // the spec. 20050606 mortene.
#ifdef COIN_HAVE_JAVASCRIPT
    if (!this->engine->getScriptField(name, f)) {
      SoDebugError::postWarning("SoVRMLScriptP::executeFunctions", 
                                "could not convert eventOut field %s",
                                name.getString());
    }
#endif // !COIN_HAVE_JAVASCRIPT

    // FIXME: We could simply unset the field, but then a lot of
    // "buggy" javascripts will start to complain. 20050721 erikgors.
    // this->engine->unsetScriptField(name);
  }
}

#undef PRIVATE
#undef PUBLIC

#endif // HAVE_VRML97
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.