1 /* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package jdk.nashorn.internal.runtime; 27 28 import static jdk.nashorn.internal.codegen.CompilerConstants.CONSTANTS; 29 import static jdk.nashorn.internal.codegen.CompilerConstants.CREATE_PROGRAM_FUNCTION; 30 import static jdk.nashorn.internal.codegen.CompilerConstants.SOURCE; 31 import static jdk.nashorn.internal.codegen.CompilerConstants.STRICT_MODE; 32 import static jdk.nashorn.internal.runtime.CodeStore.newCodeStore; 33 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; 34 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; 35 import static jdk.nashorn.internal.runtime.Source.sourceFor; 36 37 import java.io.File; 38 import java.io.IOException; 39 import java.io.PrintWriter; 40 import java.lang.invoke.MethodHandle; 41 import java.lang.invoke.MethodHandles; 42 import java.lang.invoke.MethodType; 43 import java.lang.invoke.SwitchPoint; 44 import java.lang.ref.ReferenceQueue; 45 import java.lang.ref.SoftReference; 46 import java.lang.reflect.Field; 47 import java.lang.reflect.Modifier; 48 import java.net.MalformedURLException; 49 import java.net.URL; 50 import java.security.AccessControlContext; 51 import java.security.AccessController; 52 import java.security.CodeSigner; 53 import java.security.CodeSource; 54 import java.security.Permissions; 55 import java.security.PrivilegedAction; 56 import java.security.PrivilegedActionException; 57 import java.security.PrivilegedExceptionAction; 58 import java.security.ProtectionDomain; 59 import java.util.Collection; 60 import java.util.HashMap; 61 import java.util.LinkedHashMap; 62 import java.util.Map; 63 import java.util.Objects; 64 import java.util.concurrent.atomic.AtomicLong; 65 import java.util.concurrent.atomic.AtomicReference; 66 import java.util.function.Consumer; 67 import java.util.function.Supplier; 68 import java.util.logging.Level; 69 import javax.script.ScriptContext; 70 import javax.script.ScriptEngine; 71 import jdk.internal.org.objectweb.asm.ClassReader; 72 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter; 73 import jdk.nashorn.api.scripting.ClassFilter; 74 import jdk.nashorn.api.scripting.ScriptObjectMirror; 75 import jdk.nashorn.internal.codegen.Compiler; 76 import jdk.nashorn.internal.codegen.Compiler.CompilationPhases; 77 import jdk.nashorn.internal.codegen.ObjectClassGenerator; 78 import jdk.nashorn.internal.ir.FunctionNode; 79 import jdk.nashorn.internal.ir.debug.ASTWriter; 80 import jdk.nashorn.internal.ir.debug.PrintVisitor; 81 import jdk.nashorn.internal.lookup.MethodHandleFactory; 82 import jdk.nashorn.internal.objects.Global; 83 import jdk.nashorn.internal.parser.Parser; 84 import jdk.nashorn.internal.runtime.events.RuntimeEvent; 85 import jdk.nashorn.internal.runtime.logging.DebugLogger; 86 import jdk.nashorn.internal.runtime.logging.Loggable; 87 import jdk.nashorn.internal.runtime.logging.Logger; 88 import jdk.nashorn.internal.runtime.options.LoggingOption.LoggerInfo; 89 import jdk.nashorn.internal.runtime.options.Options; 90 91 /** 92 * This class manages the global state of execution. Context is immutable. 93 */ 94 public final class Context { 95 // nashorn specific security runtime access permission names 96 /** 97 * Permission needed to pass arbitrary nashorn command line options when creating Context. 98 */ 99 public static final String NASHORN_SET_CONFIG = "nashorn.setConfig"; 100 101 /** 102 * Permission needed to create Nashorn Context instance. 103 */ 104 public static final String NASHORN_CREATE_CONTEXT = "nashorn.createContext"; 105 106 /** 107 * Permission needed to create Nashorn Global instance. 108 */ 109 public static final String NASHORN_CREATE_GLOBAL = "nashorn.createGlobal"; 110 111 /** 112 * Permission to get current Nashorn Context from thread local storage. 113 */ 114 public static final String NASHORN_GET_CONTEXT = "nashorn.getContext"; 115 116 /** 117 * Permission to use Java reflection/jsr292 from script code. 118 */ 119 public static final String NASHORN_JAVA_REFLECTION = "nashorn.JavaReflection"; 120 121 /** 122 * Permission to enable nashorn debug mode. 123 */ 124 public static final String NASHORN_DEBUG_MODE = "nashorn.debugMode"; 125 126 // nashorn load psuedo URL prefixes 127 private static final String LOAD_CLASSPATH = "classpath:"; 128 private static final String LOAD_FX = "fx:"; 129 private static final String LOAD_NASHORN = "nashorn:"; 130 131 private static MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); 132 private static MethodType CREATE_PROGRAM_FUNCTION_TYPE = MethodType.methodType(ScriptFunction.class, ScriptObject.class); 133 134 /** 135 * Should scripts use only object slots for fields, or dual long/object slots? The default 136 * behaviour is to couple this to optimistic types, using dual representation if optimistic types are enabled 137 * and single field representation otherwise. This can be overridden by setting either the "nashorn.fields.objects" 138 * or "nashorn.fields.dual" system property. 139 */ 140 private final FieldMode fieldMode; 141 142 private static enum FieldMode { 143 /** Value for automatic field representation depending on optimistic types setting */ 144 AUTO, 145 /** Value for object field representation regardless of optimistic types setting */ 146 OBJECTS, 147 /** Value for dual primitive/object field representation regardless of optimistic types setting */ 148 DUAL 149 } 150 151 /** 152 * Keeps track of which builtin prototypes and properties have been relinked 153 * Currently we are conservative and associate the name of a builtin class with all 154 * its properties, so it's enough to invalidate a property to break all assumptions 155 * about a prototype. This can be changed to a more fine grained approach, but no one 156 * ever needs this, given the very rare occurrence of swapping out only parts of 157 * a builtin v.s. the entire builtin object 158 */ 159 private final Map<String, SwitchPoint> builtinSwitchPoints = new HashMap<>(); 160 161 /* Force DebuggerSupport to be loaded. */ 162 static { 163 DebuggerSupport.FORCELOAD = true; 164 } 165 166 /** 167 * ContextCodeInstaller that has the privilege of installing classes in the Context. 168 * Can only be instantiated from inside the context and is opaque to other classes 169 */ 170 public static class ContextCodeInstaller implements CodeInstaller { 171 private final Context context; 172 private final ScriptLoader loader; 173 private final CodeSource codeSource; 174 private int usageCount = 0; 175 private int bytesDefined = 0; 176 177 // We reuse this installer for 10 compilations or 200000 defined bytes. Usually the first condition 178 // will occur much earlier, the second is a safety measure for very large scripts/functions. 179 private final static int MAX_USAGES = 10; 180 private final static int MAX_BYTES_DEFINED = 200_000; 181 182 private ContextCodeInstaller(final Context context, final ScriptLoader loader, final CodeSource codeSource) { 183 this.context = context; 184 this.loader = loader; 185 this.codeSource = codeSource; 186 } 187 188 @Override 189 public Context getContext() { 190 return context; 191 } 192 193 @Override 194 public Class<?> install(final String className, final byte[] bytecode) { 195 usageCount++; 196 bytesDefined += bytecode.length; 197 final String binaryName = Compiler.binaryName(className); 198 return loader.installClass(binaryName, bytecode, codeSource); 199 } 200 201 @Override 202 public void initialize(final Collection<Class<?>> classes, final Source source, final Object[] constants) { 203 try { 204 AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() { 205 @Override 206 public Void run() throws Exception { 207 for (final Class<?> clazz : classes) { 208 //use reflection to write source and constants table to installed classes 209 final Field sourceField = clazz.getDeclaredField(SOURCE.symbolName()); 210 sourceField.setAccessible(true); 211 sourceField.set(null, source); 212 213 final Field constantsField = clazz.getDeclaredField(CONSTANTS.symbolName()); 214 constantsField.setAccessible(true); 215 constantsField.set(null, constants); 216 } 217 return null; 218 } 219 }); 220 } catch (final PrivilegedActionException e) { 221 throw new RuntimeException(e); 222 } 223 } 224 225 @Override 226 public void verify(final byte[] code) { 227 context.verify(code); 228 } 229 230 @Override 231 public long getUniqueScriptId() { 232 return context.getUniqueScriptId(); 233 } 234 235 @Override 236 public void storeScript(final String cacheKey, final Source source, final String mainClassName, 237 final Map<String,byte[]> classBytes, final Map<Integer, FunctionInitializer> initializers, 238 final Object[] constants, final int compilationId) { 239 if (context.codeStore != null) { 240 context.codeStore.store(cacheKey, source, mainClassName, classBytes, initializers, constants, compilationId); 241 } 242 } 243 244 @Override 245 public StoredScript loadScript(final Source source, final String functionKey) { 246 if (context.codeStore != null) { 247 return context.codeStore.load(source, functionKey); 248 } 249 return null; 250 } 251 252 @Override 253 public CodeInstaller withNewLoader() { 254 // Reuse this installer if we're within our limits. 255 if (usageCount < MAX_USAGES && bytesDefined < MAX_BYTES_DEFINED) { 256 return this; 257 } 258 return new ContextCodeInstaller(context, context.createNewLoader(), codeSource); 259 } 260 261 @Override 262 public boolean isCompatibleWith(final CodeInstaller other) { 263 if (other instanceof ContextCodeInstaller) { 264 final ContextCodeInstaller cci = (ContextCodeInstaller)other; 265 return cci.context == context && cci.codeSource == codeSource; 266 } 267 return false; 268 } 269 } 270 271 /** Is Context global debug mode enabled ? */ 272 public static final boolean DEBUG = Options.getBooleanProperty("nashorn.debug"); 273 274 private static final ThreadLocal<Global> currentGlobal = new ThreadLocal<>(); 275 276 // in-memory cache for loaded classes 277 private ClassCache classCache; 278 279 // persistent code store 280 private CodeStore codeStore; 281 282 // A factory for linking global properties as constant method handles. It is created when the first Global 283 // is created, and invalidated forever once the second global is created. 284 private final AtomicReference<GlobalConstants> globalConstantsRef = new AtomicReference<>(); 285 286 /** 287 * Get the current global scope 288 * @return the current global scope 289 */ 290 public static Global getGlobal() { 291 // This class in a package.access protected package. 292 // Trusted code only can call this method. 293 return currentGlobal.get(); 294 } 295 296 /** 297 * Set the current global scope 298 * @param global the global scope 299 */ 300 public static void setGlobal(final ScriptObject global) { 301 if (global != null && !(global instanceof Global)) { 302 throw new IllegalArgumentException("not a global!"); 303 } 304 setGlobal((Global)global); 305 } 306 307 /** 308 * Set the current global scope 309 * @param global the global scope 310 */ 311 public static void setGlobal(final Global global) { 312 // This class in a package.access protected package. 313 // Trusted code only can call this method. 314 assert getGlobal() != global; 315 //same code can be cached between globals, then we need to invalidate method handle constants 316 if (global != null) { 317 final GlobalConstants globalConstants = getContext(global).getGlobalConstants(); 318 if (globalConstants != null) { 319 globalConstants.invalidateAll(); 320 } 321 } 322 currentGlobal.set(global); 323 } 324 325 /** 326 * Get context of the current global 327 * @return current global scope's context. 328 */ 329 public static Context getContext() { 330 final SecurityManager sm = System.getSecurityManager(); 331 if (sm != null) { 332 sm.checkPermission(new RuntimePermission(NASHORN_GET_CONTEXT)); 333 } 334 return getContextTrusted(); 335 } 336 337 /** 338 * Get current context's error writer 339 * 340 * @return error writer of the current context 341 */ 342 public static PrintWriter getCurrentErr() { 343 final ScriptObject global = getGlobal(); 344 return (global != null)? global.getContext().getErr() : new PrintWriter(System.err); 345 } 346 347 /** 348 * Output text to this Context's error stream 349 * @param str text to write 350 */ 351 public static void err(final String str) { 352 err(str, true); 353 } 354 355 /** 356 * Output text to this Context's error stream, optionally with 357 * a newline afterwards 358 * 359 * @param str text to write 360 * @param crlf write a carriage return/new line after text 361 */ 362 public static void err(final String str, final boolean crlf) { 363 final PrintWriter err = Context.getCurrentErr(); 364 if (err != null) { 365 if (crlf) { 366 err.println(str); 367 } else { 368 err.print(str); 369 } 370 } 371 } 372 373 /** Current environment. */ 374 private final ScriptEnvironment env; 375 376 /** is this context in strict mode? Cached from env. as this is used heavily. */ 377 final boolean _strict; 378 379 /** class loader to resolve classes from script. */ 380 private final ClassLoader appLoader; 381 382 /*package-private*/ 383 ClassLoader getAppLoader() { 384 return appLoader; 385 } 386 387 /** Class loader to load classes compiled from scripts. */ 388 private final ScriptLoader scriptLoader; 389 390 /** Current error manager. */ 391 private final ErrorManager errors; 392 393 /** Unique id for script. Used only when --loader-per-compile=false */ 394 private final AtomicLong uniqueScriptId; 395 396 /** Optional class filter to use for Java classes. Can be null. */ 397 private final ClassFilter classFilter; 398 399 private static final ClassLoader myLoader = Context.class.getClassLoader(); 400 /** Process-wide singleton structure loader */ 401 private static final StructureLoader theStructLoader; 402 403 /*package-private*/ @SuppressWarnings("static-method") 404 ClassLoader getStructLoader() { 405 return theStructLoader; 406 } 407 408 private static AccessControlContext createNoPermAccCtxt() { 409 return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, new Permissions()) }); 410 } 411 412 private static AccessControlContext createPermAccCtxt(final String permName) { 413 final Permissions perms = new Permissions(); 414 perms.add(new RuntimePermission(permName)); 415 return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) }); 416 } 417 418 private static final AccessControlContext NO_PERMISSIONS_ACC_CTXT = createNoPermAccCtxt(); 419 private static final AccessControlContext CREATE_LOADER_ACC_CTXT = createPermAccCtxt("createClassLoader"); 420 private static final AccessControlContext CREATE_GLOBAL_ACC_CTXT = createPermAccCtxt(NASHORN_CREATE_GLOBAL); 421 422 static { 423 theStructLoader = AccessController.doPrivileged(new PrivilegedAction<StructureLoader>() { 424 @Override 425 public StructureLoader run() { 426 return new StructureLoader(myLoader); 427 } 428 }, CREATE_LOADER_ACC_CTXT); 429 } 430 431 /** 432 * ThrowErrorManager that throws ParserException upon error conditions. 433 */ 434 public static class ThrowErrorManager extends ErrorManager { 435 @Override 436 public void error(final String message) { 437 throw new ParserException(message); 438 } 439 440 @Override 441 public void error(final ParserException e) { 442 throw e; 443 } 444 } 445 446 /** 447 * Constructor 448 * 449 * @param options options from command line or Context creator 450 * @param errors error manger 451 * @param appLoader application class loader 452 */ 453 public Context(final Options options, final ErrorManager errors, final ClassLoader appLoader) { 454 this(options, errors, appLoader, null); 455 } 456 457 /** 458 * Constructor 459 * 460 * @param options options from command line or Context creator 461 * @param errors error manger 462 * @param appLoader application class loader 463 * @param classFilter class filter to use 464 */ 465 public Context(final Options options, final ErrorManager errors, final ClassLoader appLoader, final ClassFilter classFilter) { 466 this(options, errors, new PrintWriter(System.out, true), new PrintWriter(System.err, true), appLoader, classFilter); 467 } 468 469 /** 470 * Constructor 471 * 472 * @param options options from command line or Context creator 473 * @param errors error manger 474 * @param out output writer for this Context 475 * @param err error writer for this Context 476 * @param appLoader application class loader 477 */ 478 public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err, final ClassLoader appLoader) { 479 this(options, errors, out, err, appLoader, (ClassFilter)null); 480 } 481 482 /** 483 * Constructor 484 * 485 * @param options options from command line or Context creator 486 * @param errors error manger 487 * @param out output writer for this Context 488 * @param err error writer for this Context 489 * @param appLoader application class loader 490 * @param classFilter class filter to use 491 */ 492 public Context(final Options options, final ErrorManager errors, final PrintWriter out, final PrintWriter err, final ClassLoader appLoader, final ClassFilter classFilter) { 493 final SecurityManager sm = System.getSecurityManager(); 494 if (sm != null) { 495 sm.checkPermission(new RuntimePermission(NASHORN_CREATE_CONTEXT)); 496 } 497 498 this.classFilter = classFilter; 499 this.env = new ScriptEnvironment(options, out, err); 500 this._strict = env._strict; 501 if (env._loader_per_compile) { 502 this.scriptLoader = null; 503 this.uniqueScriptId = null; 504 } else { 505 this.scriptLoader = createNewLoader(); 506 this.uniqueScriptId = new AtomicLong(); 507 } 508 this.errors = errors; 509 510 // if user passed -classpath option, make a URLClassLoader with that and 511 // the app loader as the parent. 512 final String classPath = options.getString("classpath"); 513 if (!env._compile_only && classPath != null && !classPath.isEmpty()) { 514 // make sure that caller can create a class loader. 515 if (sm != null) { 516 sm.checkCreateClassLoader(); 517 } 518 this.appLoader = NashornLoader.createClassLoader(classPath, appLoader); 519 } else { 520 this.appLoader = appLoader; 521 } 522 523 final int cacheSize = env._class_cache_size; 524 if (cacheSize > 0) { 525 classCache = new ClassCache(cacheSize); 526 } 527 528 if (env._persistent_cache) { 529 codeStore = newCodeStore(this); 530 } 531 532 // print version info if asked. 533 if (env._version) { 534 getErr().println("nashorn " + Version.version()); 535 } 536 537 if (env._fullversion) { 538 getErr().println("nashorn full version " + Version.fullVersion()); 539 } 540 541 if (Options.getBooleanProperty("nashorn.fields.dual")) { 542 fieldMode = FieldMode.DUAL; 543 } else if (Options.getBooleanProperty("nashorn.fields.objects")) { 544 fieldMode = FieldMode.OBJECTS; 545 } else { 546 fieldMode = FieldMode.AUTO; 547 } 548 549 initLoggers(); 550 } 551 552 553 /** 554 * Get the class filter for this context 555 * @return class filter 556 */ 557 public ClassFilter getClassFilter() { 558 return classFilter; 559 } 560 561 /** 562 * Returns the factory for constant method handles for global properties. The returned factory can be 563 * invalidated if this Context has more than one Global. 564 * @return the factory for constant method handles for global properties. 565 */ 566 GlobalConstants getGlobalConstants() { 567 return globalConstantsRef.get(); 568 } 569 570 /** 571 * Get the error manager for this context 572 * @return error manger 573 */ 574 public ErrorManager getErrorManager() { 575 return errors; 576 } 577 578 /** 579 * Get the script environment for this context 580 * @return script environment 581 */ 582 public ScriptEnvironment getEnv() { 583 return env; 584 } 585 586 /** 587 * Get the output stream for this context 588 * @return output print writer 589 */ 590 public PrintWriter getOut() { 591 return env.getOut(); 592 } 593 594 /** 595 * Get the error stream for this context 596 * @return error print writer 597 */ 598 public PrintWriter getErr() { 599 return env.getErr(); 600 } 601 602 /** 603 * Should scripts compiled by this context use dual field representation? 604 * @return true if using dual fields, false for object-only fields 605 */ 606 public boolean useDualFields() { 607 return fieldMode == FieldMode.DUAL || (fieldMode == FieldMode.AUTO && env._optimistic_types); 608 } 609 610 /** 611 * Get the PropertyMap of the current global scope 612 * @return the property map of the current global scope 613 */ 614 public static PropertyMap getGlobalMap() { 615 return Context.getGlobal().getMap(); 616 } 617 618 /** 619 * Compile a top level script. 620 * 621 * @param source the source 622 * @param scope the scope 623 * 624 * @return top level function for script 625 */ 626 public ScriptFunction compileScript(final Source source, final ScriptObject scope) { 627 return compileScript(source, scope, this.errors); 628 } 629 630 /** 631 * Interface to represent compiled code that can be re-used across many 632 * global scope instances 633 */ 634 public static interface MultiGlobalCompiledScript { 635 /** 636 * Obtain script function object for a specific global scope object. 637 * 638 * @param newGlobal global scope for which function object is obtained 639 * @return script function for script level expressions 640 */ 641 public ScriptFunction getFunction(final Global newGlobal); 642 } 643 644 /** 645 * Compile a top level script. 646 * 647 * @param source the script source 648 * @return reusable compiled script across many global scopes. 649 */ 650 public MultiGlobalCompiledScript compileScript(final Source source) { 651 final Class<?> clazz = compile(source, this.errors, this._strict); 652 final MethodHandle createProgramFunctionHandle = getCreateProgramFunctionHandle(clazz); 653 654 return new MultiGlobalCompiledScript() { 655 @Override 656 public ScriptFunction getFunction(final Global newGlobal) { 657 return invokeCreateProgramFunctionHandle(createProgramFunctionHandle, newGlobal); 658 } 659 }; 660 } 661 662 /** 663 * Entry point for {@code eval} 664 * 665 * @param initialScope The scope of this eval call 666 * @param string Evaluated code as a String 667 * @param callThis "this" to be passed to the evaluated code 668 * @param location location of the eval call 669 * @return the return value of the {@code eval} 670 */ 671 public Object eval(final ScriptObject initialScope, final String string, 672 final Object callThis, final Object location) { 673 return eval(initialScope, string, callThis, location, false, false); 674 } 675 676 /** 677 * Entry point for {@code eval} 678 * 679 * @param initialScope The scope of this eval call 680 * @param string Evaluated code as a String 681 * @param callThis "this" to be passed to the evaluated code 682 * @param location location of the eval call 683 * @param strict is this {@code eval} call from a strict mode code? 684 * @param evalCall is this called from "eval" builtin? 685 * 686 * @return the return value of the {@code eval} 687 */ 688 public Object eval(final ScriptObject initialScope, final String string, 689 final Object callThis, final Object location, final boolean strict, final boolean evalCall) { 690 final String file = location == UNDEFINED || location == null ? "<eval>" : location.toString(); 691 final Source source = sourceFor(file, string, evalCall); 692 // is this direct 'eval' builtin call? 693 final boolean directEval = evalCall && (location != UNDEFINED); 694 final Global global = Context.getGlobal(); 695 ScriptObject scope = initialScope; 696 697 // ECMA section 10.1.1 point 2 says eval code is strict if it begins 698 // with "use strict" directive or eval direct call itself is made 699 // from from strict mode code. We are passed with caller's strict mode. 700 // Nashorn extension: any 'eval' is unconditionally strict when -strict is specified. 701 boolean strictFlag = strict || this._strict; 702 703 Class<?> clazz = null; 704 try { 705 clazz = compile(source, new ThrowErrorManager(), strictFlag); 706 } catch (final ParserException e) { 707 e.throwAsEcmaException(global); 708 return null; 709 } 710 711 if (!strictFlag) { 712 // We need to get strict mode flag from compiled class. This is 713 // because eval code may start with "use strict" directive. 714 try { 715 strictFlag = clazz.getField(STRICT_MODE.symbolName()).getBoolean(null); 716 } catch (final NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) { 717 //ignored 718 strictFlag = false; 719 } 720 } 721 722 // In strict mode, eval does not instantiate variables and functions 723 // in the caller's environment. A new environment is created! 724 if (strictFlag) { 725 // Create a new scope object with given scope as its prototype 726 scope = newScope(scope); 727 } 728 729 final ScriptFunction func = getProgramFunction(clazz, scope); 730 Object evalThis; 731 if (directEval) { 732 evalThis = (callThis != UNDEFINED && callThis != null) || strictFlag ? callThis : global; 733 } else { 734 // either indirect evalCall or non-eval (Function, engine.eval, ScriptObjectMirror.eval..) 735 evalThis = callThis; 736 } 737 738 return ScriptRuntime.apply(func, evalThis); 739 } 740 741 private static ScriptObject newScope(final ScriptObject callerScope) { 742 return new Scope(callerScope, PropertyMap.newMap(Scope.class)); 743 } 744 745 private static Source loadInternal(final String srcStr, final String prefix, final String resourcePath) { 746 if (srcStr.startsWith(prefix)) { 747 final String resource = resourcePath + srcStr.substring(prefix.length()); 748 // NOTE: even sandbox scripts should be able to load scripts in nashorn: scheme 749 // These scripts are always available and are loaded from nashorn.jar's resources. 750 return AccessController.doPrivileged( 751 new PrivilegedAction<Source>() { 752 @Override 753 public Source run() { 754 try { 755 final URL resURL = Context.class.getResource(resource); 756 return resURL != null ? sourceFor(srcStr, resURL) : null; 757 } catch (final IOException exp) { 758 return null; 759 } 760 } 761 }); 762 } 763 764 return null; 765 } 766 767 /** 768 * Implementation of {@code load} Nashorn extension. Load a script file from a source 769 * expression 770 * 771 * @param scope the scope 772 * @param from source expression for script 773 * 774 * @return return value for load call (undefined) 775 * 776 * @throws IOException if source cannot be found or loaded 777 */ 778 public Object load(final Object scope, final Object from) throws IOException { 779 final Object src = from instanceof ConsString ? from.toString() : from; 780 Source source = null; 781 782 // load accepts a String (which could be a URL or a file name), a File, a URL 783 // or a ScriptObject that has "name" and "source" (string valued) properties. 784 if (src instanceof String) { 785 final String srcStr = (String)src; 786 if (srcStr.startsWith(LOAD_CLASSPATH)) { 787 final URL url = getResourceURL(srcStr.substring(LOAD_CLASSPATH.length())); 788 source = url != null ? sourceFor(url.toString(), url) : null; 789 } else { 790 final File file = new File(srcStr); 791 if (srcStr.indexOf(':') != -1) { 792 if ((source = loadInternal(srcStr, LOAD_NASHORN, "resources/")) == null && 793 (source = loadInternal(srcStr, LOAD_FX, "resources/fx/")) == null) { 794 URL url; 795 try { 796 //check for malformed url. if malformed, it may still be a valid file 797 url = new URL(srcStr); 798 } catch (final MalformedURLException e) { 799 url = file.toURI().toURL(); 800 } 801 source = sourceFor(url.toString(), url); 802 } 803 } else if (file.isFile()) { 804 source = sourceFor(srcStr, file); 805 } 806 } 807 } else if (src instanceof File && ((File)src).isFile()) { 808 final File file = (File)src; 809 source = sourceFor(file.getName(), file); 810 } else if (src instanceof URL) { 811 final URL url = (URL)src; 812 source = sourceFor(url.toString(), url); 813 } else if (src instanceof ScriptObject) { 814 final ScriptObject sobj = (ScriptObject)src; 815 if (sobj.has("script") && sobj.has("name")) { 816 final String script = JSType.toString(sobj.get("script")); 817 final String name = JSType.toString(sobj.get("name")); 818 source = sourceFor(name, script); 819 } 820 } else if (src instanceof Map) { 821 final Map<?,?> map = (Map<?,?>)src; 822 if (map.containsKey("script") && map.containsKey("name")) { 823 final String script = JSType.toString(map.get("script")); 824 final String name = JSType.toString(map.get("name")); 825 source = sourceFor(name, script); 826 } 827 } 828 829 if (source != null) { 830 if (scope instanceof ScriptObject && ((ScriptObject)scope).isScope()) { 831 final ScriptObject sobj = (ScriptObject)scope; 832 // passed object is a script object 833 // Global is the only user accessible scope ScriptObject 834 assert sobj.isGlobal() : "non-Global scope object!!"; 835 return evaluateSource(source, sobj, sobj); 836 } else if (scope == null || scope == UNDEFINED) { 837 // undefined or null scope. Use current global instance. 838 final Global global = getGlobal(); 839 return evaluateSource(source, global, global); 840 } else { 841 /* 842 * Arbitrary object passed for scope. 843 * Indirect load that is equivalent to: 844 * 845 * (function(scope, source) { 846 * with (scope) { 847 * eval(<script_from_source>); 848 * } 849 * })(scope, source); 850 */ 851 final Global global = getGlobal(); 852 // Create a new object. This is where all declarations 853 // (var, function) from the evaluated code go. 854 // make global to be its __proto__ so that global 855 // definitions are accessible to the evaluated code. 856 final ScriptObject evalScope = newScope(global); 857 858 // finally, make a WithObject around user supplied scope object 859 // so that it's properties are accessible as variables. 860 final ScriptObject withObj = ScriptRuntime.openWith(evalScope, scope); 861 862 // evaluate given source with 'withObj' as scope 863 // but use global object as "this". 864 return evaluateSource(source, withObj, global); 865 } 866 } 867 868 throw typeError("cant.load.script", ScriptRuntime.safeToString(from)); 869 } 870 871 /** 872 * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source 873 * expression, after creating a new global scope. 874 * 875 * @param from source expression for script 876 * @param args (optional) arguments to be passed to the loaded script 877 * 878 * @return return value for load call (undefined) 879 * 880 * @throws IOException if source cannot be found or loaded 881 */ 882 public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException { 883 final Global oldGlobal = getGlobal(); 884 final Global newGlobal = AccessController.doPrivileged(new PrivilegedAction<Global>() { 885 @Override 886 public Global run() { 887 try { 888 return newGlobal(); 889 } catch (final RuntimeException e) { 890 if (Context.DEBUG) { 891 e.printStackTrace(); 892 } 893 throw e; 894 } 895 } 896 }, CREATE_GLOBAL_ACC_CTXT); 897 // initialize newly created Global instance 898 initGlobal(newGlobal); 899 setGlobal(newGlobal); 900 901 final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY : ScriptObjectMirror.wrapArray(args, oldGlobal); 902 newGlobal.put("arguments", newGlobal.wrapAsObject(wrapped), env._strict); 903 904 try { 905 // wrap objects from newGlobal's world as mirrors - but if result 906 // is from oldGlobal's world, unwrap it! 907 return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal); 908 } finally { 909 setGlobal(oldGlobal); 910 } 911 } 912 913 /** 914 * Load or get a structure class. Structure class names are based on the number of parameter fields 915 * and {@link AccessorProperty} fields in them. Structure classes are used to represent ScriptObjects 916 * 917 * @see ObjectClassGenerator 918 * @see AccessorProperty 919 * @see ScriptObject 920 * 921 * @param fullName full name of class, e.g. jdk.nashorn.internal.objects.JO2P1 contains 2 fields and 1 parameter. 922 * 923 * @return the {@code Class<?>} for this structure 924 * 925 * @throws ClassNotFoundException if structure class cannot be resolved 926 */ 927 @SuppressWarnings("unchecked") 928 public static Class<? extends ScriptObject> forStructureClass(final String fullName) throws ClassNotFoundException { 929 if (System.getSecurityManager() != null && !StructureLoader.isStructureClass(fullName)) { 930 throw new ClassNotFoundException(fullName); 931 } 932 return (Class<? extends ScriptObject>)Class.forName(fullName, true, theStructLoader); 933 } 934 935 /** 936 * Checks that the given Class can be accessed from no permissions context. 937 * 938 * @param clazz Class object 939 * @throws SecurityException if not accessible 940 */ 941 public static void checkPackageAccess(final Class<?> clazz) { 942 final SecurityManager sm = System.getSecurityManager(); 943 if (sm != null) { 944 Class<?> bottomClazz = clazz; 945 while (bottomClazz.isArray()) { 946 bottomClazz = bottomClazz.getComponentType(); 947 } 948 checkPackageAccess(sm, bottomClazz.getName()); 949 } 950 } 951 952 /** 953 * Checks that the given package name can be accessed from no permissions context. 954 * 955 * @param pkgName package name 956 * @throws SecurityException if not accessible 957 */ 958 public static void checkPackageAccess(final String pkgName) { 959 final SecurityManager sm = System.getSecurityManager(); 960 if (sm != null) { 961 checkPackageAccess(sm, pkgName.endsWith(".") ? pkgName : pkgName + "."); 962 } 963 } 964 965 /** 966 * Checks that the given package can be accessed from no permissions context. 967 * 968 * @param sm current security manager instance 969 * @param fullName fully qualified package name 970 * @throw SecurityException if not accessible 971 */ 972 private static void checkPackageAccess(final SecurityManager sm, final String fullName) { 973 Objects.requireNonNull(sm); 974 final int index = fullName.lastIndexOf('.'); 975 if (index != -1) { 976 final String pkgName = fullName.substring(0, index); 977 AccessController.doPrivileged(new PrivilegedAction<Void>() { 978 @Override 979 public Void run() { 980 sm.checkPackageAccess(pkgName); 981 return null; 982 } 983 }, NO_PERMISSIONS_ACC_CTXT); 984 } 985 } 986 987 /** 988 * Is {@code className} the name of a structure class? 989 * 990 * @param className a class name 991 * @return true if className is a structure class name 992 */ 993 public static boolean isStructureClass(final String className) { 994 return StructureLoader.isStructureClass(className); 995 } 996 997 /** 998 * Checks that the given Class can be accessed from no permissions context. 999 * 1000 * @param clazz Class object 1001 * @return true if package is accessible, false otherwise 1002 */ 1003 private static boolean isAccessiblePackage(final Class<?> clazz) { 1004 try { 1005 checkPackageAccess(clazz); 1006 return true; 1007 } catch (final SecurityException se) { 1008 return false; 1009 } 1010 } 1011 1012 /** 1013 * Checks that the given Class is public and it can be accessed from no permissions context. 1014 * 1015 * @param clazz Class object to check 1016 * @return true if Class is accessible, false otherwise 1017 */ 1018 public static boolean isAccessibleClass(final Class<?> clazz) { 1019 return Modifier.isPublic(clazz.getModifiers()) && Context.isAccessiblePackage(clazz); 1020 } 1021 1022 /** 1023 * Lookup a Java class. This is used for JSR-223 stuff linking in from 1024 * {@code jdk.nashorn.internal.objects.NativeJava} and {@code jdk.nashorn.internal.runtime.NativeJavaPackage} 1025 * 1026 * @param fullName full name of class to load 1027 * 1028 * @return the {@code Class<?>} for the name 1029 * 1030 * @throws ClassNotFoundException if class cannot be resolved 1031 */ 1032 public Class<?> findClass(final String fullName) throws ClassNotFoundException { 1033 if (fullName.indexOf('[') != -1 || fullName.indexOf('/') != -1) { 1034 // don't allow array class names or internal names. 1035 throw new ClassNotFoundException(fullName); 1036 } 1037 1038 // give chance to ClassFilter to filter out, if present 1039 if (classFilter != null && !classFilter.exposeToScripts(fullName)) { 1040 throw new ClassNotFoundException(fullName); 1041 } 1042 1043 // check package access as soon as possible! 1044 final SecurityManager sm = System.getSecurityManager(); 1045 if (sm != null) { 1046 checkPackageAccess(sm, fullName); 1047 } 1048 1049 // Try finding using the "app" loader. 1050 if (appLoader != null) { 1051 return Class.forName(fullName, true, appLoader); 1052 } else { 1053 final Class<?> cl = Class.forName(fullName); 1054 // return the Class only if it was loaded by boot loader 1055 if (cl.getClassLoader() == null) { 1056 return cl; 1057 } else { 1058 throw new ClassNotFoundException(fullName); 1059 } 1060 } 1061 } 1062 1063 /** 1064 * Hook to print stack trace for a {@link Throwable} that occurred during 1065 * execution 1066 * 1067 * @param t throwable for which to dump stack 1068 */ 1069 public static void printStackTrace(final Throwable t) { 1070 if (Context.DEBUG) { 1071 t.printStackTrace(Context.getCurrentErr()); 1072 } 1073 } 1074 1075 /** 1076 * Verify generated bytecode before emission. This is called back from the 1077 * {@link ObjectClassGenerator} or the {@link Compiler}. If the "--verify-code" parameter 1078 * hasn't been given, this is a nop 1079 * 1080 * Note that verification may load classes -- we don't want to do that unless 1081 * user specified verify option. We check it here even though caller 1082 * may have already checked that flag 1083 * 1084 * @param bytecode bytecode to verify 1085 */ 1086 public void verify(final byte[] bytecode) { 1087 if (env._verify_code) { 1088 // No verification when security manager is around as verifier 1089 // may load further classes - which should be avoided. 1090 if (System.getSecurityManager() == null) { 1091 CheckClassAdapter.verify(new ClassReader(bytecode), theStructLoader, false, new PrintWriter(System.err, true)); 1092 } 1093 } 1094 } 1095 1096 /** 1097 * Create and initialize a new global scope object. 1098 * 1099 * @return the initialized global scope object. 1100 */ 1101 public Global createGlobal() { 1102 return initGlobal(newGlobal()); 1103 } 1104 1105 /** 1106 * Create a new uninitialized global scope object 1107 * @return the global script object 1108 */ 1109 public Global newGlobal() { 1110 createOrInvalidateGlobalConstants(); 1111 return new Global(this); 1112 } 1113 1114 private void createOrInvalidateGlobalConstants() { 1115 for (;;) { 1116 final GlobalConstants currentGlobalConstants = getGlobalConstants(); 1117 if (currentGlobalConstants != null) { 1118 // Subsequent invocation; we're creating our second or later Global. GlobalConstants is not safe to use 1119 // with more than one Global, as the constant method handle linkages it creates create a coupling 1120 // between the Global and the call sites in the compiled code. 1121 currentGlobalConstants.invalidateForever(); 1122 return; 1123 } 1124 final GlobalConstants newGlobalConstants = new GlobalConstants(getLogger(GlobalConstants.class)); 1125 if (globalConstantsRef.compareAndSet(null, newGlobalConstants)) { 1126 // First invocation; we're creating the first Global in this Context. Create the GlobalConstants object 1127 // for this Context. 1128 return; 1129 } 1130 1131 // If we reach here, then we started out as the first invocation, but another concurrent invocation won the 1132 // CAS race. We'll just let the loop repeat and invalidate the CAS race winner. 1133 } 1134 } 1135 1136 /** 1137 * Initialize given global scope object. 1138 * 1139 * @param global the global 1140 * @param engine the associated ScriptEngine instance, can be null 1141 * @return the initialized global scope object. 1142 */ 1143 public Global initGlobal(final Global global, final ScriptEngine engine) { 1144 // Need only minimal global object, if we are just compiling. 1145 if (!env._compile_only) { 1146 final Global oldGlobal = Context.getGlobal(); 1147 try { 1148 Context.setGlobal(global); 1149 // initialize global scope with builtin global objects 1150 global.initBuiltinObjects(engine); 1151 } finally { 1152 Context.setGlobal(oldGlobal); 1153 } 1154 } 1155 1156 return global; 1157 } 1158 1159 /** 1160 * Initialize given global scope object. 1161 * 1162 * @param global the global 1163 * @return the initialized global scope object. 1164 */ 1165 public Global initGlobal(final Global global) { 1166 return initGlobal(global, null); 1167 } 1168 1169 /** 1170 * Return the current global's context 1171 * @return current global's context 1172 */ 1173 static Context getContextTrusted() { 1174 return getContext(getGlobal()); 1175 } 1176 1177 static Context getContextTrustedOrNull() { 1178 final Global global = Context.getGlobal(); 1179 return global == null ? null : getContext(global); 1180 } 1181 1182 private static Context getContext(final Global global) { 1183 // We can't invoke Global.getContext() directly, as it's a protected override, and Global isn't in our package. 1184 // In order to access the method, we must cast it to ScriptObject first (which is in our package) and then let 1185 // virtual invocation do its thing. 1186 return ((ScriptObject)global).getContext(); 1187 } 1188 1189 /** 1190 * Try to infer Context instance from the Class. If we cannot, 1191 * then get it from the thread local variable. 1192 * 1193 * @param clazz the class 1194 * @return context 1195 */ 1196 static Context fromClass(final Class<?> clazz) { 1197 final ClassLoader loader = clazz.getClassLoader(); 1198 1199 if (loader instanceof ScriptLoader) { 1200 return ((ScriptLoader)loader).getContext(); 1201 } 1202 1203 return Context.getContextTrusted(); 1204 } 1205 1206 private URL getResourceURL(final String resName) { 1207 if (appLoader != null) { 1208 return appLoader.getResource(resName); 1209 } 1210 return ClassLoader.getSystemResource(resName); 1211 } 1212 1213 private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) { 1214 ScriptFunction script = null; 1215 1216 try { 1217 script = compileScript(source, scope, new Context.ThrowErrorManager()); 1218 } catch (final ParserException e) { 1219 e.throwAsEcmaException(); 1220 } 1221 1222 return ScriptRuntime.apply(script, thiz); 1223 } 1224 1225 private static ScriptFunction getProgramFunction(final Class<?> script, final ScriptObject scope) { 1226 if (script == null) { 1227 return null; 1228 } 1229 return invokeCreateProgramFunctionHandle(getCreateProgramFunctionHandle(script), scope); 1230 } 1231 1232 private static MethodHandle getCreateProgramFunctionHandle(final Class<?> script) { 1233 try { 1234 return LOOKUP.findStatic(script, CREATE_PROGRAM_FUNCTION.symbolName(), CREATE_PROGRAM_FUNCTION_TYPE); 1235 } catch (NoSuchMethodException | IllegalAccessException e) { 1236 throw new AssertionError("Failed to retrieve a handle for the program function for " + script.getName(), e); 1237 } 1238 } 1239 1240 private static ScriptFunction invokeCreateProgramFunctionHandle(final MethodHandle createProgramFunctionHandle, final ScriptObject scope) { 1241 try { 1242 return (ScriptFunction)createProgramFunctionHandle.invokeExact(scope); 1243 } catch (final RuntimeException|Error e) { 1244 throw e; 1245 } catch (final Throwable t) { 1246 throw new AssertionError("Failed to create a program function", t); 1247 } 1248 } 1249 1250 private ScriptFunction compileScript(final Source source, final ScriptObject scope, final ErrorManager errMan) { 1251 return getProgramFunction(compile(source, errMan, this._strict), scope); 1252 } 1253 1254 private synchronized Class<?> compile(final Source source, final ErrorManager errMan, final boolean strict) { 1255 // start with no errors, no warnings. 1256 errMan.reset(); 1257 1258 Class<?> script = findCachedClass(source); 1259 if (script != null) { 1260 final DebugLogger log = getLogger(Compiler.class); 1261 if (log.isEnabled()) { 1262 log.fine(new RuntimeEvent<>(Level.INFO, source), "Code cache hit for ", source, " avoiding recompile."); 1263 } 1264 return script; 1265 } 1266 1267 StoredScript storedScript = null; 1268 FunctionNode functionNode = null; 1269 // Don't use code store if optimistic types is enabled but lazy compilation is not. 1270 // This would store a full script compilation with many wrong optimistic assumptions that would 1271 // do more harm than good on later runs with both optimistic types and lazy compilation enabled. 1272 final boolean useCodeStore = codeStore != null && !env._parse_only && (!env._optimistic_types || env._lazy_compilation); 1273 final String cacheKey = useCodeStore ? CodeStore.getCacheKey("script", null) : null; 1274 1275 if (useCodeStore) { 1276 storedScript = codeStore.load(source, cacheKey); 1277 } 1278 1279 if (storedScript == null) { 1280 if (env._dest_dir != null) { 1281 source.dump(env._dest_dir); 1282 } 1283 1284 functionNode = new Parser(env, source, errMan, strict, getLogger(Parser.class)).parse(); 1285 1286 if (errMan.hasErrors()) { 1287 return null; 1288 } 1289 1290 if (env._print_ast || functionNode.getFlag(FunctionNode.IS_PRINT_AST)) { 1291 getErr().println(new ASTWriter(functionNode)); 1292 } 1293 1294 if (env._print_parse || functionNode.getFlag(FunctionNode.IS_PRINT_PARSE)) { 1295 getErr().println(new PrintVisitor(functionNode, true, false)); 1296 } 1297 } 1298 1299 if (env._parse_only) { 1300 return null; 1301 } 1302 1303 final URL url = source.getURL(); 1304 final ScriptLoader loader = env._loader_per_compile ? createNewLoader() : scriptLoader; 1305 final CodeSource cs = new CodeSource(url, (CodeSigner[])null); 1306 final CodeInstaller installer = new ContextCodeInstaller(this, loader, cs); 1307 1308 if (storedScript == null) { 1309 final CompilationPhases phases = Compiler.CompilationPhases.COMPILE_ALL; 1310 1311 final Compiler compiler = Compiler.forInitialCompilation( 1312 installer, 1313 source, 1314 errMan, 1315 strict | functionNode.isStrict()); 1316 1317 final FunctionNode compiledFunction = compiler.compile(functionNode, phases); 1318 if (errMan.hasErrors()) { 1319 return null; 1320 } 1321 script = compiledFunction.getRootClass(); 1322 compiler.persistClassInfo(cacheKey, compiledFunction); 1323 } else { 1324 Compiler.updateCompilationId(storedScript.getCompilationId()); 1325 script = storedScript.installScript(source, installer); 1326 } 1327 1328 cacheClass(source, script); 1329 return script; 1330 } 1331 1332 private ScriptLoader createNewLoader() { 1333 return AccessController.doPrivileged( 1334 new PrivilegedAction<ScriptLoader>() { 1335 @Override 1336 public ScriptLoader run() { 1337 return new ScriptLoader(Context.this); 1338 } 1339 }, CREATE_LOADER_ACC_CTXT); 1340 } 1341 1342 private long getUniqueScriptId() { 1343 return uniqueScriptId.getAndIncrement(); 1344 } 1345 1346 /** 1347 * Cache for compiled script classes. 1348 */ 1349 @SuppressWarnings("serial") 1350 private static class ClassCache extends LinkedHashMap<Source, ClassReference> { 1351 private final int size; 1352 private final ReferenceQueue<Class<?>> queue; 1353 1354 ClassCache(final int size) { 1355 super(size, 0.75f, true); 1356 this.size = size; 1357 this.queue = new ReferenceQueue<>(); 1358 } 1359 1360 void cache(final Source source, final Class<?> clazz) { 1361 put(source, new ClassReference(clazz, queue, source)); 1362 } 1363 1364 @Override 1365 protected boolean removeEldestEntry(final Map.Entry<Source, ClassReference> eldest) { 1366 return size() > size; 1367 } 1368 1369 @Override 1370 public ClassReference get(final Object key) { 1371 for (ClassReference ref; (ref = (ClassReference)queue.poll()) != null; ) { 1372 remove(ref.source); 1373 } 1374 return super.get(key); 1375 } 1376 1377 } 1378 1379 private static class ClassReference extends SoftReference<Class<?>> { 1380 private final Source source; 1381 1382 ClassReference(final Class<?> clazz, final ReferenceQueue<Class<?>> queue, final Source source) { 1383 super(clazz, queue); 1384 this.source = source; 1385 } 1386 } 1387 1388 // Class cache management 1389 private Class<?> findCachedClass(final Source source) { 1390 final ClassReference ref = classCache == null ? null : classCache.get(source); 1391 return ref != null ? ref.get() : null; 1392 } 1393 1394 private void cacheClass(final Source source, final Class<?> clazz) { 1395 if (classCache != null) { 1396 classCache.cache(source, clazz); 1397 } 1398 } 1399 1400 // logging 1401 private final Map<String, DebugLogger> loggers = new HashMap<>(); 1402 1403 private void initLoggers() { 1404 ((Loggable)MethodHandleFactory.getFunctionality()).initLogger(this); 1405 } 1406 1407 /** 1408 * Get a logger, given a loggable class 1409 * @param clazz a Loggable class 1410 * @return debuglogger associated with that class 1411 */ 1412 public DebugLogger getLogger(final Class<? extends Loggable> clazz) { 1413 return getLogger(clazz, null); 1414 } 1415 1416 /** 1417 * Get a logger, given a loggable class 1418 * @param clazz a Loggable class 1419 * @param initHook an init hook - if this is the first time the logger is created in the context, run the init hook 1420 * @return debuglogger associated with that class 1421 */ 1422 public DebugLogger getLogger(final Class<? extends Loggable> clazz, final Consumer<DebugLogger> initHook) { 1423 final String name = getLoggerName(clazz); 1424 DebugLogger logger = loggers.get(name); 1425 if (logger == null) { 1426 if (!env.hasLogger(name)) { 1427 return DebugLogger.DISABLED_LOGGER; 1428 } 1429 final LoggerInfo info = env._loggers.get(name); 1430 logger = new DebugLogger(name, info.getLevel(), info.isQuiet()); 1431 if (initHook != null) { 1432 initHook.accept(logger); 1433 } 1434 loggers.put(name, logger); 1435 } 1436 return logger; 1437 } 1438 1439 /** 1440 * Given a Loggable class, weave debug info info a method handle for that logger. 1441 * Level.INFO is used 1442 * 1443 * @param clazz loggable 1444 * @param mh method handle 1445 * @param text debug printout to add 1446 * 1447 * @return instrumented method handle, or null if logger not enabled 1448 */ 1449 public MethodHandle addLoggingToHandle(final Class<? extends Loggable> clazz, final MethodHandle mh, final Supplier<String> text) { 1450 return addLoggingToHandle(clazz, Level.INFO, mh, Integer.MAX_VALUE, false, text); 1451 } 1452 1453 /** 1454 * Given a Loggable class, weave debug info info a method handle for that logger. 1455 * 1456 * @param clazz loggable 1457 * @param level log level 1458 * @param mh method handle 1459 * @param paramStart first parameter to print 1460 * @param printReturnValue should we print the return value? 1461 * @param text debug printout to add 1462 * 1463 * @return instrumented method handle, or null if logger not enabled 1464 */ 1465 public MethodHandle addLoggingToHandle(final Class<? extends Loggable> clazz, final Level level, final MethodHandle mh, final int paramStart, final boolean printReturnValue, final Supplier<String> text) { 1466 final DebugLogger log = getLogger(clazz); 1467 if (log.isEnabled()) { 1468 return MethodHandleFactory.addDebugPrintout(log, level, mh, paramStart, printReturnValue, text.get()); 1469 } 1470 return mh; 1471 } 1472 1473 private static String getLoggerName(final Class<?> clazz) { 1474 Class<?> current = clazz; 1475 while (current != null) { 1476 final Logger log = current.getAnnotation(Logger.class); 1477 if (log != null) { 1478 assert !"".equals(log.name()); 1479 return log.name(); 1480 } 1481 current = current.getSuperclass(); 1482 } 1483 assert false; 1484 return null; 1485 } 1486 1487 /** 1488 * This is a special kind of switchpoint used to guard builtin 1489 * properties and prototypes. In the future it might contain 1490 * logic to e.g. multiple switchpoint classes. 1491 */ 1492 public static final class BuiltinSwitchPoint extends SwitchPoint { 1493 //empty 1494 } 1495 1496 /** 1497 * Create a new builtin switchpoint and return it 1498 * @param name key name 1499 * @return new builtin switchpoint 1500 */ 1501 public SwitchPoint newBuiltinSwitchPoint(final String name) { 1502 assert builtinSwitchPoints.get(name) == null; 1503 final SwitchPoint sp = new BuiltinSwitchPoint(); 1504 builtinSwitchPoints.put(name, sp); 1505 return sp; 1506 } 1507 1508 /** 1509 * Return the builtin switchpoint for a particular key name 1510 * @param name key name 1511 * @return builtin switchpoint or null if none 1512 */ 1513 public SwitchPoint getBuiltinSwitchPoint(final String name) { 1514 return builtinSwitchPoints.get(name); 1515 } 1516 1517 }