1 45 47 49 package com.google.gwt.dev.js.rhino; 50 51 import java.beans.PropertyChangeEvent ; 52 import java.beans.PropertyChangeListener ; 53 import java.lang.reflect.Method ; 54 import java.text.MessageFormat ; 55 import java.util.Hashtable ; 56 import java.util.Locale ; 57 import java.util.ResourceBundle ; 58 59 84 85 public class Context { 86 public static final String languageVersionProperty = "language version"; 87 public static final String errorReporterProperty = "error reporter"; 88 89 97 public Context() { 98 setLanguageVersion(VERSION_DEFAULT); 99 } 100 101 128 public static Context enter() { 129 return enter(null); 130 } 131 132 143 public static Context enter(Context cx) { 144 145 Context old = getCurrentContext(); 146 147 if (cx == null) { 148 if (old != null) { 149 cx = old; 150 } else { 151 cx = new Context(); 152 setThreadContext(cx); 153 } 154 } else { 155 if (cx.enterCount != 0) { 156 if (cx != old) { 159 throw new RuntimeException 160 ("Cannot enter Context active on another thread"); 161 } 162 } else { 163 if (old != null) { 164 cx = old; 165 } else { 166 setThreadContext(cx); 167 } 168 } 169 } 170 171 ++cx.enterCount; 172 173 return cx; 174 } 175 176 189 public static void exit() { 190 boolean released = false; 191 Context cx = getCurrentContext(); 192 if (cx == null) { 193 throw new RuntimeException 194 ("Calling Context.exit without previous Context.enter"); 195 } 196 if (Context.check && cx.enterCount < 1) Context.codeBug(); 197 --cx.enterCount; 198 if (cx.enterCount == 0) { 199 released = true; 200 setThreadContext(null); 201 } 202 } 203 204 216 public static Context getCurrentContext() { 217 if (threadLocalCx != null) { 218 try { 219 return (Context)threadLocalGet.invoke(threadLocalCx, (Object []) null); 220 } catch (Exception ex) { } 221 } 222 Thread t = Thread.currentThread(); 223 return (Context) threadContexts.get(t); 224 } 225 226 private static void setThreadContext(Context cx) { 227 if (threadLocalCx != null) { 228 try { 229 threadLocalSet.invoke(threadLocalCx, new Object [] { cx }); 230 return; 231 } catch (Exception ex) { } 232 } 233 Thread t = Thread.currentThread(); 234 if (cx != null) { 235 threadContexts.put(t, cx); 236 } else { 237 threadContexts.remove(t); 238 } 239 } 240 241 246 247 250 public static final int VERSION_UNKNOWN = -1; 251 252 255 public static final int VERSION_DEFAULT = 0; 256 257 260 public static final int VERSION_1_0 = 100; 261 262 265 public static final int VERSION_1_1 = 110; 266 267 270 public static final int VERSION_1_2 = 120; 271 272 275 public static final int VERSION_1_3 = 130; 276 277 280 public static final int VERSION_1_4 = 140; 281 282 285 public static final int VERSION_1_5 = 150; 286 287 295 public int getLanguageVersion() { 296 return version; 297 } 298 299 309 public void setLanguageVersion(int version) { 310 this.version = version; 311 } 312 313 329 public String getImplementationVersion() { 330 return "Rhino 1.5 release 4.1 2003 04 21"; 331 } 332 333 338 public ErrorReporter getErrorReporter() { 339 return errorReporter; 340 } 341 342 348 public ErrorReporter setErrorReporter(ErrorReporter reporter) { 349 errorReporter = reporter; 350 return reporter; 351 } 352 353 359 360 public Locale getLocale() { 361 if (locale == null) 362 locale = Locale.getDefault(); 363 return locale; 364 } 365 366 371 public Locale setLocale(Locale loc) { 372 Locale result = locale; 373 locale = loc; 374 return result; 375 } 376 377 387 void firePropertyChange(String property, Object oldValue, 388 Object newValue) 389 { 390 Object [] array = listeners; 391 if (array != null) { 392 firePropertyChangeImpl(array, property, oldValue, newValue); 393 } 394 } 395 396 private void firePropertyChangeImpl(Object [] array, String property, 397 Object oldValue, Object newValue) 398 { 399 for (int i = array.length; i-- != 0;) { 400 Object obj = array[i]; 401 if (obj instanceof PropertyChangeListener ) { 402 PropertyChangeListener l = (PropertyChangeListener )obj; 403 l.propertyChange(new PropertyChangeEvent ( 404 this, property, oldValue, newValue)); 405 } 406 } 407 } 408 409 419 public static void reportWarning(String message, String sourceName, 420 int lineno, String lineSource, 421 int lineOffset) 422 { 423 Context cx = Context.getContext(); 424 cx.getErrorReporter().warning(message, sourceName, lineno, 425 lineSource, lineOffset); 426 } 427 428 434 441 442 452 public static void reportError(String message, String sourceName, 453 int lineno, String lineSource, 454 int lineOffset) 455 { 456 Context cx = getCurrentContext(); 457 if (cx != null) { 458 cx.errorCount++; 459 cx.getErrorReporter().error(message, sourceName, lineno, 460 lineSource, lineOffset); 461 } else { 462 throw new EvaluatorException(message); 463 } 464 } 465 466 472 479 480 492 532 533 539 546 547 562 public final Object getThreadLocal(Object key) { 563 if (hashtable == null) 564 return null; 565 return hashtable.get(key); 566 } 567 568 574 public void putThreadLocal(Object key, Object value) { 575 if (hashtable == null) 576 hashtable = new Hashtable (); 577 hashtable.put(key, value); 578 } 579 580 585 public void removeThreadLocal(Object key) { 586 if (hashtable == null) 587 return; 588 hashtable.remove(key); 589 } 590 591 600 public final boolean hasCompileFunctionsWithDynamicScope() { 601 return compileFunctionsWithDynamicScopeFlag; 602 } 603 604 611 public void setCompileFunctionsWithDynamicScope(boolean flag) { 612 compileFunctionsWithDynamicScopeFlag = flag; 613 } 614 615 620 public static final int FEATURE_NON_ECMA_GET_YEAR = 1; 621 622 629 public static final int FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME = 2; 630 631 636 public static final int FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER = 3; 637 638 646 public static final int FEATURE_TO_STRING_AS_SOURCE = 4; 647 648 658 public boolean hasFeature(int featureIndex) { 659 switch (featureIndex) { 660 case FEATURE_NON_ECMA_GET_YEAR: 661 672 return (version == Context.VERSION_1_0 673 || version == Context.VERSION_1_1 674 || version == Context.VERSION_1_2); 675 676 case FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME: 677 return false; 678 679 case FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER: 680 return false; 681 682 case FEATURE_TO_STRING_AS_SOURCE: 683 return version == VERSION_1_2; 684 } 685 throw new IllegalArgumentException (); 687 } 688 689 690 691 static String getMessage0(String messageId) { 692 return getMessage(messageId, null); 693 } 694 695 static String getMessage1(String messageId, Object arg1) { 696 Object [] arguments = {arg1}; 697 return getMessage(messageId, arguments); 698 } 699 700 static String getMessage2(String messageId, Object arg1, Object arg2) { 701 Object [] arguments = {arg1, arg2}; 702 return getMessage(messageId, arguments); 703 } 704 705 static String getMessage3 706 (String messageId, Object arg1, Object arg2, Object arg3) { 707 Object [] arguments = {arg1, arg2, arg3}; 708 return getMessage(messageId, arguments); 709 } 710 714 static Context getContext() { 715 Context cx = getCurrentContext(); 716 if (cx == null) { 717 throw new RuntimeException ( 718 "No Context associated with current Thread"); 719 } 720 return cx; 721 } 722 723 727 static final String defaultResource = 730 "com.google.gwt.dev.js.rhino.Messages"; 731 732 733 static String getMessage(String messageId, Object [] arguments) { 734 Context cx = getCurrentContext(); 735 Locale locale = cx != null ? cx.getLocale() : Locale.getDefault(); 736 737 ResourceBundle rb = ResourceBundle.getBundle(defaultResource, locale); 739 740 String formatString; 741 try { 742 formatString = rb.getString(messageId); 743 } catch (java.util.MissingResourceException mre) { 744 throw new RuntimeException 745 ("no message resource found for message property "+ messageId); 746 } 747 748 753 MessageFormat formatter = new MessageFormat (formatString); 755 return formatter.format(arguments); 756 } 757 758 static final boolean printTrees = true; 760 static final boolean printICode = true; 761 762 final boolean isVersionECMA1() { 763 return version == VERSION_DEFAULT || version >= VERSION_1_3; 764 } 765 766 767 static void codeBug() { 769 throw new RuntimeException ("FAILED ASSERTION"); 770 } 771 772 static final boolean check = true; 773 774 private static Hashtable threadContexts = new Hashtable (11); 775 private static Object threadLocalCx; 776 private static Method threadLocalGet; 777 private static Method threadLocalSet; 778 779 int version; 780 int errorCount; 781 782 private ErrorReporter errorReporter; 783 private Locale locale; 784 private boolean generatingDebug; 785 private boolean generatingDebugChanged; 786 private boolean generatingSource=true; 787 private boolean compileFunctionsWithDynamicScopeFlag; 788 private int enterCount; 789 private Object [] listeners; 790 private Hashtable hashtable; 791 private ClassLoader applicationClassLoader; 792 793 797 private Hashtable activationNames; 798 799 int instructionCount; 801 int instructionThreshold; 802 } 803 | Popular Tags |