public static ScriptingContainer makeContainer(RubyConfig config) { // Required global setting for when JRuby fakes up Kernel.system('ruby') // calls. // Since this is global, other JRuby servlets in this servlet container // are affected... // // TODO: move to a better location in the code? remove? if (config.getJrubyHome() != null) { System.setProperty("jruby.home", config.getJrubyHome()); } final ScriptingContainer container = new ScriptingContainer(config.getScope()); container.setCompileMode(config.getCompileMode()); if (config.getJrubyHome() != null) { container.setHomeDirectory(config.getJrubyHome()); } container.setCompatVersion(config.getCompatVersion()); container.setCurrentDirectory(config.getAppRoot()); // don't propagate ENV to global JVM level container.getProvider().getRubyInstanceConfig().setUpdateNativeENVEnabled(false); logger.info( "new ScriptingContainer scope={} compileMode={} jrubyHome={} compatVersion={}, pwd={}", new Object[] { config.getScope(), config.getCompileMode(), config.getJrubyHome(), config.getCompatVersion(), config.getAppRoot() }); return container; }
Engine(File root) { List<String> loadPaths = new ArrayList<String>(); loadPaths.add(new File(root, "lib").getAbsolutePath()); for (VirtualFile vf : Play.roots) { loadPaths.add(new File(vf.getRealFile(), "public/stylesheets").getAbsolutePath()); } scriptingContainer = new ScriptingContainer(); scriptingContainer.getProvider().setLoadPaths(loadPaths); scriptingContainer.setErrorWriter(errors); }
public IRubyObject getInstanceVariable(IRubyObject obj, String variableName) { BiVariableMap map = container.getVarMap(); synchronized (map) { if (map.containsKey(variableName)) { BiVariable bv = map.getVariable( (RubyObject) container.getProvider().getRuntime().getTopSelf(), variableName); return bv.getRubyObject(); } } return null; }
public IRubyObject setInstanceVariable(IRubyObject obj, String variableName, IRubyObject value) { BiVariableMap map = container.getVarMap(); synchronized (map) { if (map.containsKey(variableName)) { BiVariable bv = map.getVariable( (RubyObject) container.getProvider().getRuntime().getTopSelf(), variableName); bv.setRubyObject(value); } else { InstanceVariable iv = new InstanceVariable(obj, variableName, value); map.update(variableName, iv); } } return obj.getInstanceVariables().setInstanceVariable(variableName, value); }
private <T> T call( MethodType type, Class<T> returnType, Object receiver, String methodName, Block block, EmbedEvalUnit unit, Object... args) { if (methodName == null || methodName.length() == 0) { return null; } Ruby runtime = container.getProvider().getRuntime(); RubyObject rubyReceiver = getReceiverObject(runtime, receiver); boolean sharing_variables = true; Object obj = container.getAttribute(AttributeName.SHARING_VARIABLES); if (obj != null && obj instanceof Boolean && ((Boolean) obj) == false) { sharing_variables = false; } try { if (sharing_variables) { ManyVarsDynamicScope scope; if (unit != null && unit.getScope() != null) scope = unit.getScope(); else scope = EmbedRubyRuntimeAdapterImpl.getManyVarsDynamicScope(container, 0); container.getVarMap().inject(scope, 0, rubyReceiver); runtime.getCurrentContext().pushScope(scope); } IRubyObject result = callEachType(type, rubyReceiver, methodName, block, args); if (sharing_variables) { container.getVarMap().retrieve(rubyReceiver); } if (!(result instanceof RubyNil) && returnType != null) { Object ret = JavaEmbedUtils.rubyToJava(runtime, result, returnType); return ret != null ? returnType.cast(ret) : null; } return null; } catch (RaiseException e) { runtime.printError(e.getException()); throw new InvokeFailedException(e.getMessage(), e); } catch (Throwable e) { throw new InvokeFailedException(e); } finally { if (sharing_variables) { runtime.getCurrentContext().popScope(); } } }