1283 static Context getContextTrustedOrNull() {
1284 final Global global = Context.getGlobal();
1285 return global == null ? null : getContext(global);
1286 }
1287
1288 private static Context getContext(final Global global) {
1289 // We can't invoke Global.getContext() directly, as it's a protected override, and Global isn't in our package.
1290 // In order to access the method, we must cast it to ScriptObject first (which is in our package) and then let
1291 // virtual invocation do its thing.
1292 return ((ScriptObject)global).getContext();
1293 }
1294
1295 /**
1296 * Try to infer Context instance from the Class. If we cannot,
1297 * then get it from the thread local variable.
1298 *
1299 * @param clazz the class
1300 * @return context
1301 */
1302 static Context fromClass(final Class<?> clazz) {
1303 final ClassLoader loader = clazz.getClassLoader();
1304
1305 if (loader instanceof ScriptLoader) {
1306 return ((ScriptLoader)loader).getContext();
1307 }
1308
1309 return Context.getContextTrusted();
1310 }
1311
1312 private URL getResourceURL(final String resName) {
1313 if (appLoader != null) {
1314 return appLoader.getResource(resName);
1315 }
1316 return ClassLoader.getSystemResource(resName);
1317 }
1318
1319 private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
1320 ScriptFunction script = null;
1321
1322 try {
1323 script = compileScript(source, scope, new Context.ThrowErrorManager());
|
1283 static Context getContextTrustedOrNull() {
1284 final Global global = Context.getGlobal();
1285 return global == null ? null : getContext(global);
1286 }
1287
1288 private static Context getContext(final Global global) {
1289 // We can't invoke Global.getContext() directly, as it's a protected override, and Global isn't in our package.
1290 // In order to access the method, we must cast it to ScriptObject first (which is in our package) and then let
1291 // virtual invocation do its thing.
1292 return ((ScriptObject)global).getContext();
1293 }
1294
1295 /**
1296 * Try to infer Context instance from the Class. If we cannot,
1297 * then get it from the thread local variable.
1298 *
1299 * @param clazz the class
1300 * @return context
1301 */
1302 static Context fromClass(final Class<?> clazz) {
1303 ClassLoader loader = null;
1304 try {
1305 loader = clazz.getClassLoader();
1306 } catch (SecurityException ignored) {
1307 // This could fail because of anonymous classes being used.
1308 // Accessing loader of anonymous class fails (for extension
1309 // loader class too?). In any case, for us fetching Context
1310 // from class loader is just an optimization. We can always
1311 // get Context from thread local storage (below).
1312 }
1313
1314 if (loader instanceof ScriptLoader) {
1315 return ((ScriptLoader)loader).getContext();
1316 }
1317
1318 return Context.getContextTrusted();
1319 }
1320
1321 private URL getResourceURL(final String resName) {
1322 if (appLoader != null) {
1323 return appLoader.getResource(resName);
1324 }
1325 return ClassLoader.getSystemResource(resName);
1326 }
1327
1328 private Object evaluateSource(final Source source, final ScriptObject scope, final ScriptObject thiz) {
1329 ScriptFunction script = null;
1330
1331 try {
1332 script = compileScript(source, scope, new Context.ThrowErrorManager());
|