forked from mozilla/rhino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContext.java
More file actions
2798 lines (2589 loc) · 102 KB
/
Copy pathContext.java
File metadata and controls
2798 lines (2589 loc) · 102 KB
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
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// API class
package org.mozilla.javascript;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import org.mozilla.classfile.ClassFileWriter.ClassFileFormatException;
import org.mozilla.javascript.ast.AstRoot;
import org.mozilla.javascript.ast.ScriptNode;
import org.mozilla.javascript.debug.DebuggableScript;
import org.mozilla.javascript.debug.Debugger;
import org.mozilla.javascript.xml.XMLLib;
/**
* This class represents the runtime context of an executing script.
*
* Before executing a script, an instance of Context must be created
* and associated with the thread that will be executing the script.
* The Context will be used to store information about the executing
* of the script such as the call stack. Contexts are associated with
* the current thread using the {@link #call(ContextAction)}
* or {@link #enter()} methods.<p>
*
* Different forms of script execution are supported. Scripts may be
* evaluated from the source directly, or first compiled and then later
* executed. Interactive execution is also supported.<p>
*
* Some aspects of script execution, such as type conversions and
* object creation, may be accessed directly through methods of
* Context.
*
* @see Scriptable
* @author Norris Boyd
* @author Brendan Eich
*/
public class Context
{
/**
* Language versions.
*
* All integral values are reserved for future version numbers.
*/
/**
* The unknown version.
* <p>Be aware, this version will not support many of the newer
* language features and will not change in the future.</p>
* <p>Please use one of the other constants like VERSION_ES6 to
* get support for recent language features.</p>
*/
public static final int VERSION_UNKNOWN = -1;
/**
* The default version.
*/
public static final int VERSION_DEFAULT = 0;
/**
* JavaScript 1.0
*/
public static final int VERSION_1_0 = 100;
/**
* JavaScript 1.1
*/
public static final int VERSION_1_1 = 110;
/**
* JavaScript 1.2
*/
public static final int VERSION_1_2 = 120;
/**
* JavaScript 1.3
*/
public static final int VERSION_1_3 = 130;
/**
* JavaScript 1.4
*/
public static final int VERSION_1_4 = 140;
/**
* JavaScript 1.5
*/
public static final int VERSION_1_5 = 150;
/**
* JavaScript 1.6
*/
public static final int VERSION_1_6 = 160;
/**
* JavaScript 1.7
*/
public static final int VERSION_1_7 = 170;
/**
* JavaScript 1.8
*/
public static final int VERSION_1_8 = 180;
/**
* ECMAScript 6.
*/
public static final int VERSION_ES6 = 200;
/**
* Controls behaviour of <tt>Date.prototype.getYear()</tt>.
* If <tt>hasFeature(FEATURE_NON_ECMA_GET_YEAR)</tt> returns true,
* Date.prototype.getYear subtructs 1900 only if 1900 <= date < 2000.
* The default behavior of {@link #hasFeature(int)} is always to subtruct
* 1900 as rquired by ECMAScript B.2.4.
*/
public static final int FEATURE_NON_ECMA_GET_YEAR = 1;
/**
* Control if member expression as function name extension is available.
* If <tt>hasFeature(FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME)</tt> returns
* true, allow <tt>function memberExpression(args) { body }</tt> to be
* syntax sugar for <tt>memberExpression = function(args) { body }</tt>,
* when memberExpression is not a simple identifier.
* See ECMAScript-262, section 11.2 for definition of memberExpression.
* By default {@link #hasFeature(int)} returns false.
*/
public static final int FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME = 2;
/**
* Control if reserved keywords are treated as identifiers.
* If <tt>hasFeature(RESERVED_KEYWORD_AS_IDENTIFIER)</tt> returns true,
* treat future reserved keyword (see Ecma-262, section 7.5.3) as ordinary
* identifiers but warn about this usage.
*
* By default {@link #hasFeature(int)} returns false.
*/
public static final int FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER = 3;
/**
* Control if <tt>toString()</tt> should returns the same result
* as <tt>toSource()</tt> when applied to objects and arrays.
* If <tt>hasFeature(FEATURE_TO_STRING_AS_SOURCE)</tt> returns true,
* calling <tt>toString()</tt> on JS objects gives the same result as
* calling <tt>toSource()</tt>. That is it returns JS source with code
* to create an object with all enumeratable fields of the original object
* instead of printing <tt>[object <i>result of
* {@link Scriptable#getClassName()}</i>]</tt>.
* <p>
* By default {@link #hasFeature(int)} returns true only if
* the current JS version is set to {@link #VERSION_1_2}.
*/
public static final int FEATURE_TO_STRING_AS_SOURCE = 4;
/**
* Control if properties <tt>__proto__</tt> and <tt>__parent__</tt>
* are treated specially.
* If <tt>hasFeature(FEATURE_PARENT_PROTO_PROPERTIES)</tt> returns true,
* treat <tt>__parent__</tt> and <tt>__proto__</tt> as special properties.
* <p>
* The properties allow to query and set scope and prototype chains for the
* objects. The special meaning of the properties is available
* only when they are used as the right hand side of the dot operator.
* For example, while <tt>x.__proto__ = y</tt> changes the prototype
* chain of the object <tt>x</tt> to point to <tt>y</tt>,
* <tt>x["__proto__"] = y</tt> simply assigns a new value to the property
* <tt>__proto__</tt> in <tt>x</tt> even when the feature is on.
*
* By default {@link #hasFeature(int)} returns true.
*/
public static final int FEATURE_PARENT_PROTO_PROPERTIES = 5;
/**
* @deprecated In previous releases, this name was given to
* FEATURE_PARENT_PROTO_PROPERTIES.
*/
@Deprecated
public static final int FEATURE_PARENT_PROTO_PROPRTIES = 5;
/**
* Control if support for E4X(ECMAScript for XML) extension is available.
* If hasFeature(FEATURE_E4X) returns true, the XML syntax is available.
* <p>
* By default {@link #hasFeature(int)} returns true if
* the current JS version is set to {@link #VERSION_DEFAULT}
* or is at least {@link #VERSION_1_6}.
* @since 1.6 Release 1
*/
public static final int FEATURE_E4X = 6;
/**
* Control if dynamic scope should be used for name access.
* If hasFeature(FEATURE_DYNAMIC_SCOPE) returns true, then the name lookup
* during name resolution will use the top scope of the script or function
* which is at the top of JS execution stack instead of the top scope of the
* script or function from the current stack frame if the top scope of
* the top stack frame contains the top scope of the current stack frame
* on its prototype chain.
* <p>
* This is useful to define shared scope containing functions that can
* be called from scripts and functions using private scopes.
* <p>
* By default {@link #hasFeature(int)} returns false.
* @since 1.6 Release 1
*/
public static final int FEATURE_DYNAMIC_SCOPE = 7;
/**
* Control if strict variable mode is enabled.
* When the feature is on Rhino reports runtime errors if assignment
* to a global variable that does not exist is executed. When the feature
* is off such assignments create a new variable in the global scope as
* required by ECMA 262.
* <p>
* By default {@link #hasFeature(int)} returns false.
* @since 1.6 Release 1
*/
public static final int FEATURE_STRICT_VARS = 8;
/**
* Control if strict eval mode is enabled.
* When the feature is on Rhino reports runtime errors if non-string
* argument is passed to the eval function. When the feature is off
* eval simply return non-string argument as is without performing any
* evaluation as required by ECMA 262.
* <p>
* By default {@link #hasFeature(int)} returns false.
* @since 1.6 Release 1
*/
public static final int FEATURE_STRICT_EVAL = 9;
/**
* When the feature is on Rhino will add a "fileName" and "lineNumber"
* properties to Error objects automatically. When the feature is off, you
* have to explicitly pass them as the second and third argument to the
* Error constructor. Note that neither behavior is fully ECMA 262
* compliant (as 262 doesn't specify a three-arg constructor), but keeping
* the feature off results in Error objects that don't have
* additional non-ECMA properties when constructed using the ECMA-defined
* single-arg constructor and is thus desirable if a stricter ECMA
* compliance is desired, specifically adherence to the point 15.11.5. of
* the standard.
* <p>
* By default {@link #hasFeature(int)} returns false.
* @since 1.6 Release 6
*/
public static final int FEATURE_LOCATION_INFORMATION_IN_ERROR = 10;
/**
* Controls whether JS 1.5 'strict mode' is enabled.
* When the feature is on, Rhino reports more than a dozen different
* warnings. When the feature is off, these warnings are not generated.
* FEATURE_STRICT_MODE implies FEATURE_STRICT_VARS and FEATURE_STRICT_EVAL.
* <p>
* By default {@link #hasFeature(int)} returns false.
* @since 1.6 Release 6
*/
public static final int FEATURE_STRICT_MODE = 11;
/**
* Controls whether a warning should be treated as an error.
* @since 1.6 Release 6
*/
public static final int FEATURE_WARNING_AS_ERROR = 12;
/**
* Enables enhanced access to Java.
* Specifically, controls whether private and protected members can be
* accessed, and whether scripts can catch all Java exceptions.
* <p>
* Note that this feature should only be enabled for trusted scripts.
* <p>
* By default {@link #hasFeature(int)} returns false.
* @since 1.7 Release 1
*/
public static final int FEATURE_ENHANCED_JAVA_ACCESS = 13;
/**
* Enables access to JavaScript features from ECMAscript 6 that are present in
* JavaScript engines that do not yet support version 6, such as V8.
* This includes support for typed arrays. Default is true.
* @since 1.7 Release 3
*/
public static final int FEATURE_V8_EXTENSIONS = 14;
/**
* Defines how an undefined "this" parameter is handled in certain calls. Previously Rhino
* would convert an undefined "this" to null, whereas recent specs call for it to be treated
* differently. Default is to be set if language version <= 1.7.
* @since 1.7.7
*/
public static final int FEATURE_OLD_UNDEF_NULL_THIS = 15;
/**
* If set, then the order of property key enumeration will be first numeric keys in numeric order,
* followed by string keys in order of creation, and finally Symbol keys, as specified in ES6.
* Default is true for language version >= "ES6" and false otherwise.
* @since 1.7.7.1
*/
public static final int FEATURE_ENUMERATE_IDS_FIRST = 16;
/**
* If set, then all objects will have a thread-safe property map. (Note that this doesn't make
* everything else that they do thread-safe -- that depends on the specific implementation.
* If not set, users should not share Rhino objects between threads, unless the "sync"
* function is used to wrap them with an explicit synchronizer. The default
* is false, which means that by default, individual objects are not thread-safe.
* @since 1.7.8
*/
public static final int FEATURE_THREAD_SAFE_OBJECTS = 17;
/**
* If set, then all integer numbers will be returned without decimal place. For instance
* assume there is a function like this:
* <code>function foo() {return 5;}</code>
* 5 will be returned if feature is set, 5.0 otherwise.
*/
public static final int FEATURE_INTEGER_WITHOUT_DECIMAL_PLACE = 18;
/**
* TypedArray buffer uses little/big endian depending on the platform.
* The default is big endian for Rhino.
* @since 1.7 Release 11
*/
public static final int FEATURE_LITTLE_ENDIAN = 19;
/**
* Configure the XMLProcessor to parse XML with security features or not.
* Security features include not fetching remote entity references and disabling XIncludes
* @since 1.7 Release 12
*/
public static final int FEATURE_ENABLE_XML_SECURE_PARSING = 20;
public static final String languageVersionProperty = "language version";
public static final String errorReporterProperty = "error reporter";
/**
* Convenient value to use as zero-length array of objects.
*/
public static final Object[] emptyArgs = ScriptRuntime.emptyArgs;
/**
* Creates a new Context. The context will be associated with the {@link
* ContextFactory#getGlobal() global context factory}.
*
* Note that the Context must be associated with a thread before
* it can be used to execute a script.
* @deprecated this constructor is deprecated because it creates a
* dependency on a static singleton context factory. Use
* {@link ContextFactory#enter()} or
* {@link ContextFactory#call(ContextAction)} instead. If you subclass
* this class, consider using {@link #Context(ContextFactory)} constructor
* instead in the subclasses' constructors.
*/
@Deprecated
public Context()
{
this(ContextFactory.getGlobal());
}
/**
* Creates a new context. Provided as a preferred super constructor for
* subclasses in place of the deprecated default public constructor.
* @param factory the context factory associated with this context (most
* likely, the one that created the context). Can not be null. The context
* features are inherited from the factory, and the context will also
* otherwise use its factory's services.
* @throws IllegalArgumentException if factory parameter is null.
*/
protected Context(ContextFactory factory)
{
if(factory == null) {
throw new IllegalArgumentException("factory == null");
}
this.factory = factory;
version = VERSION_DEFAULT;
optimizationLevel = codegenClass != null ? 0 : -1;
maximumInterpreterStackDepth = Integer.MAX_VALUE;
}
/**
* Get the current Context.
*
* The current Context is per-thread; this method looks up
* the Context associated with the current thread. <p>
*
* @return the Context associated with the current thread, or
* null if no context is associated with the current
* thread.
* @see ContextFactory#enterContext()
* @see ContextFactory#call(ContextAction)
*/
public static Context getCurrentContext()
{
Object helper = VMBridge.instance.getThreadContextHelper();
return VMBridge.instance.getContext(helper);
}
/**
* Same as calling {@link ContextFactory#enterContext()} on the global
* ContextFactory instance.
* @return a Context associated with the current thread
* @see #getCurrentContext()
* @see #exit()
* @see #call(ContextAction)
*/
public static Context enter()
{
return enter(null);
}
/**
* Get a Context associated with the current thread, using
* the given Context if need be.
* <p>
* The same as <code>enter()</code> except that <code>cx</code>
* is associated with the current thread and returned if
* the current thread has no associated context and <code>cx</code>
* is not associated with any other thread.
* @param cx a Context to associate with the thread if possible
* @return a Context associated with the current thread
* @deprecated use {@link ContextFactory#enterContext(Context)} instead as
* this method relies on usage of a static singleton "global" ContextFactory.
* @see ContextFactory#enterContext(Context)
* @see ContextFactory#call(ContextAction)
*/
@Deprecated
public static Context enter(Context cx)
{
return enter(cx, ContextFactory.getGlobal());
}
static final Context enter(Context cx, ContextFactory factory)
{
Object helper = VMBridge.instance.getThreadContextHelper();
Context old = VMBridge.instance.getContext(helper);
if (old != null) {
cx = old;
} else {
if (cx == null) {
cx = factory.makeContext();
if (cx.enterCount != 0) {
throw new IllegalStateException("factory.makeContext() returned Context instance already associated with some thread");
}
factory.onContextCreated(cx);
if (factory.isSealed() && !cx.isSealed()) {
cx.seal(null);
}
} else {
if (cx.enterCount != 0) {
throw new IllegalStateException("can not use Context instance already associated with some thread");
}
}
VMBridge.instance.setContext(helper, cx);
}
++cx.enterCount;
return cx;
}
/**
* Exit a block of code requiring a Context.
*
* Calling <code>exit()</code> will remove the association between
* the current thread and a Context if the prior call to
* {@link ContextFactory#enterContext()} on this thread newly associated a
* Context with this thread. Once the current thread no longer has an
* associated Context, it cannot be used to execute JavaScript until it is
* again associated with a Context.
* @see ContextFactory#enterContext()
*/
public static void exit()
{
Object helper = VMBridge.instance.getThreadContextHelper();
Context cx = VMBridge.instance.getContext(helper);
if (cx == null) {
throw new IllegalStateException(
"Calling Context.exit without previous Context.enter");
}
if (cx.enterCount < 1) Kit.codeBug();
if (--cx.enterCount == 0) {
VMBridge.instance.setContext(helper, null);
cx.factory.onContextReleased(cx);
}
}
/**
* Call {@link ContextAction#run(Context cx)}
* using the Context instance associated with the current thread.
* If no Context is associated with the thread, then
* <tt>ContextFactory.getGlobal().makeContext()</tt> will be called to
* construct new Context instance. The instance will be temporary
* associated with the thread during call to
* {@link ContextAction#run(Context)}.
* @deprecated use {@link ContextFactory#call(ContextAction)} instead as
* this method relies on usage of a static singleton "global"
* ContextFactory.
* @return The result of {@link ContextAction#run(Context)}.
*/
@Deprecated
public static <T> T call(ContextAction<T> action)
{
return call(ContextFactory.getGlobal(), action);
}
/**
* Call {@link
* Callable#call(Context cx, Scriptable scope, Scriptable thisObj,
* Object[] args)}
* using the Context instance associated with the current thread.
* If no Context is associated with the thread, then
* {@link ContextFactory#makeContext()} will be called to construct
* new Context instance. The instance will be temporary associated
* with the thread during call to {@link ContextAction#run(Context)}.
* <p>
* It is allowed but not advisable to use null for <tt>factory</tt>
* argument in which case the global static singleton ContextFactory
* instance will be used to create new context instances.
* @see ContextFactory#call(ContextAction)
*/
public static Object call(ContextFactory factory, final Callable callable,
final Scriptable scope, final Scriptable thisObj,
final Object[] args)
{
if(factory == null) {
factory = ContextFactory.getGlobal();
}
return call(factory, cx -> callable.call(cx, scope, thisObj, args));
}
/**
* The method implements {@link ContextFactory#call(ContextAction)} logic.
*/
static <T> T call(ContextFactory factory, ContextAction<T> action) {
Context cx = enter(null, factory);
try {
return action.run(cx);
}
finally {
exit();
}
}
/**
* @deprecated
* @see ContextFactory#addListener(org.mozilla.javascript.ContextFactory.Listener)
* @see ContextFactory#getGlobal()
*/
@Deprecated
public static void addContextListener(ContextListener listener)
{
// Special workaround for the debugger
String DBG = "org.mozilla.javascript.tools.debugger.Main";
if (DBG.equals(listener.getClass().getName())) {
Class<?> cl = listener.getClass();
Class<?> factoryClass = Kit.classOrNull(
"org.mozilla.javascript.ContextFactory");
Class<?>[] sig = { factoryClass };
Object[] args = { ContextFactory.getGlobal() };
try {
Method m = cl.getMethod("attachTo", sig);
m.invoke(listener, args);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
return;
}
ContextFactory.getGlobal().addListener(listener);
}
/**
* @deprecated
* @see ContextFactory#removeListener(org.mozilla.javascript.ContextFactory.Listener)
* @see ContextFactory#getGlobal()
*/
@Deprecated
public static void removeContextListener(ContextListener listener)
{
ContextFactory.getGlobal().addListener(listener);
}
/**
* Return {@link ContextFactory} instance used to create this Context.
*/
public final ContextFactory getFactory()
{
return factory;
}
/**
* Checks if this is a sealed Context. A sealed Context instance does not
* allow to modify any of its properties and will throw an exception
* on any such attempt.
* @see #seal(Object sealKey)
*/
public final boolean isSealed()
{
return sealed;
}
/**
* Seal this Context object so any attempt to modify any of its properties
* including calling {@link #enter()} and {@link #exit()} methods will
* throw an exception.
* <p>
* If <tt>sealKey</tt> is not null, calling
* {@link #unseal(Object sealKey)} with the same key unseals
* the object. If <tt>sealKey</tt> is null, unsealing is no longer possible.
*
* @see #isSealed()
* @see #unseal(Object)
*/
public final void seal(Object sealKey)
{
if (sealed) onSealedMutation();
sealed = true;
this.sealKey = sealKey;
}
/**
* Unseal previously sealed Context object.
* The <tt>sealKey</tt> argument should not be null and should match
* <tt>sealKey</tt> suplied with the last call to
* {@link #seal(Object)} or an exception will be thrown.
*
* @see #isSealed()
* @see #seal(Object sealKey)
*/
public final void unseal(Object sealKey)
{
if (sealKey == null) throw new IllegalArgumentException();
if (this.sealKey != sealKey) throw new IllegalArgumentException();
if (!sealed) throw new IllegalStateException();
sealed = false;
this.sealKey = null;
}
static void onSealedMutation()
{
throw new IllegalStateException();
}
/**
* Get the current language version.
* <p>
* The language version number affects JavaScript semantics as detailed
* in the overview documentation.
*
* @return an integer that is one of VERSION_1_0, VERSION_1_1, etc.
*/
public final int getLanguageVersion()
{
return version;
}
/**
* Set the language version.
*
* <p>
* Setting the language version will affect functions and scripts compiled
* subsequently. See the overview documentation for version-specific
* behavior.
*
* @param version the version as specified by VERSION_1_0, VERSION_1_1, etc.
*/
public void setLanguageVersion(int version)
{
if (sealed) onSealedMutation();
checkLanguageVersion(version);
Object listeners = propertyListeners;
if (listeners != null && version != this.version) {
firePropertyChangeImpl(listeners, languageVersionProperty,
Integer.valueOf(this.version),
Integer.valueOf(version));
}
this.version = version;
}
public static boolean isValidLanguageVersion(int version)
{
switch (version) {
case VERSION_DEFAULT:
case VERSION_1_0:
case VERSION_1_1:
case VERSION_1_2:
case VERSION_1_3:
case VERSION_1_4:
case VERSION_1_5:
case VERSION_1_6:
case VERSION_1_7:
case VERSION_1_8:
case VERSION_ES6:
return true;
}
return false;
}
public static void checkLanguageVersion(int version)
{
if (isValidLanguageVersion(version)) {
return;
}
throw new IllegalArgumentException("Bad language version: "+version);
}
/**
* Get the implementation version.
*
* <p>
* The implementation version is of the form
* <pre>
* "<i>name langVer</i> <code>release</code> <i>relNum date</i>"
* </pre>
* where <i>name</i> is the name of the product, <i>langVer</i> is
* the language version, <i>relNum</i> is the release number, and
* <i>date</i> is the release date for that specific
* release in the form "yyyy mm dd".
*
* @return a string that encodes the product, language version, release
* number, and date.
*/
public final String getImplementationVersion()
{
if (implementationVersion == null) {
Enumeration<URL> urls;
try {
urls = Context.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
} catch (IOException ioe) {
return null;
}
// There will be many manifests in the world -- enumerate all of them until we find the right one.
while (urls.hasMoreElements()) {
URL metaUrl = urls.nextElement();
InputStream is = null;
try {
is = metaUrl.openStream();
Manifest mf = new Manifest(is);
Attributes attrs = mf.getMainAttributes();
if ("Mozilla Rhino".equals(attrs.getValue("Implementation-Title"))) {
implementationVersion =
"Rhino " + attrs.getValue("Implementation-Version") + " " + attrs.getValue("Built-Date").replaceAll("-", " ");
return implementationVersion;
}
} catch (IOException e) {
// Ignore this unlikely event
} finally {
try {
if (is != null) is.close();
} catch (IOException e) {
// Ignore this even unlikelier event
}
}
}
}
return implementationVersion;
}
/**
* Get the current error reporter.
*
* @see org.mozilla.javascript.ErrorReporter
*/
public final ErrorReporter getErrorReporter()
{
if (errorReporter == null) {
return DefaultErrorReporter.instance;
}
return errorReporter;
}
/**
* Change the current error reporter.
*
* @return the previous error reporter
* @see org.mozilla.javascript.ErrorReporter
*/
public final ErrorReporter setErrorReporter(ErrorReporter reporter)
{
if (sealed) onSealedMutation();
if (reporter == null) throw new IllegalArgumentException();
ErrorReporter old = getErrorReporter();
if (reporter == old) {
return old;
}
Object listeners = propertyListeners;
if (listeners != null) {
firePropertyChangeImpl(listeners, errorReporterProperty,
old, reporter);
}
this.errorReporter = reporter;
return old;
}
/**
* Get the current locale. Returns the default locale if none has
* been set.
*
* @see java.util.Locale
*/
public final Locale getLocale()
{
if (locale == null)
locale = Locale.getDefault();
return locale;
}
/**
* Set the current locale.
*
* @see java.util.Locale
*/
public final Locale setLocale(Locale loc)
{
if (sealed) onSealedMutation();
Locale result = locale;
locale = loc;
return result;
}
/**
* Register an object to receive notifications when a bound property
* has changed
* @see java.beans.PropertyChangeEvent
* @see #removePropertyChangeListener(java.beans.PropertyChangeListener)
* @param l the listener
*/
public final void addPropertyChangeListener(PropertyChangeListener l)
{
if (sealed) onSealedMutation();
propertyListeners = Kit.addListener(propertyListeners, l);
}
/**
* Remove an object from the list of objects registered to receive
* notification of changes to a bounded property
* @see java.beans.PropertyChangeEvent
* @see #addPropertyChangeListener(java.beans.PropertyChangeListener)
* @param l the listener
*/
public final void removePropertyChangeListener(PropertyChangeListener l)
{
if (sealed) onSealedMutation();
propertyListeners = Kit.removeListener(propertyListeners, l);
}
/**
* Notify any registered listeners that a bounded property has changed
* @see #addPropertyChangeListener(java.beans.PropertyChangeListener)
* @see #removePropertyChangeListener(java.beans.PropertyChangeListener)
* @see java.beans.PropertyChangeListener
* @see java.beans.PropertyChangeEvent
* @param property the bound property
* @param oldValue the old value
* @param newValue the new value
*/
final void firePropertyChange(String property, Object oldValue,
Object newValue)
{
Object listeners = propertyListeners;
if (listeners != null) {
firePropertyChangeImpl(listeners, property, oldValue, newValue);
}
}
private void firePropertyChangeImpl(Object listeners, String property,
Object oldValue, Object newValue)
{
for (int i = 0; ; ++i) {
Object l = Kit.getListener(listeners, i);
if (l == null)
break;
if (l instanceof PropertyChangeListener) {
PropertyChangeListener pcl = (PropertyChangeListener)l;
pcl.propertyChange(new PropertyChangeEvent(
this, property, oldValue, newValue));
}
}
}
/**
* Report a warning using the error reporter for the current thread.
*
* @param message the warning message to report
* @param sourceName a string describing the source, such as a filename
* @param lineno the starting line number
* @param lineSource the text of the line (may be null)
* @param lineOffset the offset into lineSource where problem was detected
* @see org.mozilla.javascript.ErrorReporter
*/
public static void reportWarning(String message, String sourceName,
int lineno, String lineSource,
int lineOffset)
{
Context cx = Context.getContext();
if (cx.hasFeature(FEATURE_WARNING_AS_ERROR))
reportError(message, sourceName, lineno, lineSource, lineOffset);
else
cx.getErrorReporter().warning(message, sourceName, lineno,
lineSource, lineOffset);
}
/**
* Report a warning using the error reporter for the current thread.
*
* @param message the warning message to report
* @see org.mozilla.javascript.ErrorReporter
*/
public static void reportWarning(String message)
{
int[] linep = { 0 };
String filename = getSourcePositionFromStack(linep);
Context.reportWarning(message, filename, linep[0], null, 0);
}
public static void reportWarning(String message, Throwable t)
{
int[] linep = { 0 };
String filename = getSourcePositionFromStack(linep);
Writer sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println(message);
t.printStackTrace(pw);
pw.flush();
Context.reportWarning(sw.toString(), filename, linep[0], null, 0);
}
/**
* Report an error using the error reporter for the current thread.
*
* @param message the error message to report
* @param sourceName a string describing the source, such as a filename
* @param lineno the starting line number
* @param lineSource the text of the line (may be null)
* @param lineOffset the offset into lineSource where problem was detected
* @see org.mozilla.javascript.ErrorReporter
*/
public static void reportError(String message, String sourceName,
int lineno, String lineSource,
int lineOffset)
{
Context cx = getCurrentContext();
if (cx != null) {
cx.getErrorReporter().error(message, sourceName, lineno,
lineSource, lineOffset);
} else {
throw new EvaluatorException(message, sourceName, lineno,
lineSource, lineOffset);
}
}
/**
* Report an error using the error reporter for the current thread.
*
* @param message the error message to report
* @see org.mozilla.javascript.ErrorReporter
*/
public static void reportError(String message)
{
int[] linep = { 0 };
String filename = getSourcePositionFromStack(linep);
Context.reportError(message, filename, linep[0], null, 0);
}
/**
* Report a runtime error using the error reporter for the current thread.
*
* @param message the error message to report
* @param sourceName a string describing the source, such as a filename
* @param lineno the starting line number
* @param lineSource the text of the line (may be null)
* @param lineOffset the offset into lineSource where problem was detected
* @return a runtime exception that will be thrown to terminate the
* execution of the script
* @see org.mozilla.javascript.ErrorReporter