/** * Injects a global value to a parsed Ruby script. This method is * invoked during EvalUnit#run() is executed. */ @Override public void inject() { synchronized (getRuntime()) { String gName = (name.startsWith("$") ? name : "$" + name); getRuntime().getGlobalVariables().set( gName.intern(), irubyObject ); } }
/** Defines a global variable */ public void defineVariable(final GlobalVariable variable, org.jruby.internal.runtime.GlobalVariable.Scope scope) { globalVariables.define(variable.name(), new IAccessor() { @Override public IRubyObject getValue() { return variable.get(); } @Override public IRubyObject setValue(IRubyObject newValue) { return variable.set(newValue); } }, scope); }
@Override public void initialize(BSFManager manager, String language, Vector someDeclaredBeans) throws BSFException { super.initialize(manager, language, someDeclaredBeans); runtime = JavaEmbedUtils.initialize(getClassPath(manager)); for (int i = 0, size = someDeclaredBeans.size(); i < size; i++) { BSFDeclaredBean bean = (BSFDeclaredBean) someDeclaredBeans.elementAt(i); runtime.getGlobalVariables().define( GlobalVariable.variableName(bean.name), new BeanGlobalVariable(runtime, bean), org.jruby.internal.runtime.GlobalVariable.Scope.GLOBAL); } runtime.getGlobalVariables().defineReadonly("$bsf", new FunctionsGlobalVariable(runtime, new BSFFunctions(manager, this)), org.jruby.internal.runtime.GlobalVariable.Scope.GLOBAL); }
public boolean untraceVar(String name, IRubyObject command) { assert name != null; assert name.startsWith("$"); if (isDefined(name)) { GlobalVariable variable = globalVariables.get(name); return variable.removeTrace(command); } return false; }
public PrintStream getErrorStream() { // FIXME: We can't guarantee this will always be a RubyIO...so the old code here is not safe /*java.io.OutputStream os = ((RubyIO) getGlobalVariables().getService("$stderr")).getOutStream(); if(null != os) { return new PrintStream(os); } else { return new PrintStream(new org.jruby.util.SwallowingOutputStream()); }*/ return new PrintStream(new IOOutputStream(getGlobalVariables().get("$stderr"))); }
@JRubyMethod(name = {"exists?", "exist?"}, meta = true, compat = RUBY1_9) public static IRubyObject exist(ThreadContext context, IRubyObject recv, IRubyObject arg) { // Capture previous exception if any. IRubyObject exception = context.runtime.getGlobalVariables().get("$!"); try { return context.runtime.newFileStat(RubyFile.get_path(context, arg).asJavaString(), false).directory_p(); } catch (Exception e) { // Restore $! context.runtime.getGlobalVariables().set("$!", exception); return context.runtime.newBoolean(false); } }
@JRubyMethod(name = "chomp!") public IRubyObject chomp_bang19(ThreadContext context) { modifyCheck(); Ruby runtime = context.runtime; if (value.getRealSize() == 0) return context.nil; IRubyObject rsObj = runtime.getGlobalVariables().get("$/"); if (rsObj == runtime.getGlobalVariables().getDefaultSeparator()) return smartChopBangCommon(runtime); return chompBangCommon(runtime, rsObj); }
@JRubyMethod(name = "global_variables", module = true, visibility = PRIVATE, compat = RUBY1_9) public static RubyArray global_variables19(ThreadContext context, IRubyObject recv) { Ruby runtime = context.runtime; RubyArray globalVariables = runtime.newArray(); for (String globalVariableName : runtime.getGlobalVariables().getNames()) { globalVariables.append(runtime.newSymbol(globalVariableName)); } return globalVariables; }
@Override public Object interpret(ThreadContext context, DynamicScope currDynScope, IRubyObject self, Object[] temp, Block block) { String newNameString = newName.retrieve(context, self, currDynScope, temp).toString(); String oldNameString = oldName.retrieve(context, self, currDynScope, temp).toString(); context.runtime.getGlobalVariables().alias(newNameString, oldNameString); return null; }
/** * Removes this object from {@link BiVariableMap}. * * @param runtime environment where a variable is removed. */ @Override public void remove() { synchronized (getRuntime()) { String gName = (name.startsWith("$") ? name : "$" + name); getRuntime().getGlobalVariables().clear( gName.intern() ); } } }
public GlobalVariable getVariable(String name) { assert name != null; assert name.startsWith("$"); GlobalVariable variable = globalVariables.get(name); if (variable != null) return variable; return createIfNotDefined(name); }
public static IRubyObject puts1(ThreadContext context, IRubyObject maybeIO, IRubyObject arg0) { Ruby runtime = context.runtime; assert runtime.getGlobalVariables().getDefaultSeparator() instanceof RubyString; RubyString separator = (RubyString) runtime.getGlobalVariables().getDefaultSeparator(); putsSingle(context, runtime, maybeIO, arg0, separator); return context.nil; }
/** defines a readonly global variable * */ public void defineReadonlyVariable(String name, IRubyObject value, org.jruby.internal.runtime.GlobalVariable.Scope scope) { globalVariables.defineReadonly(name, new ValueAccessor(value), scope); }
@Override public void initialize(BSFManager manager, String language, Vector someDeclaredBeans) throws BSFException { super.initialize(manager, language, someDeclaredBeans); runtime = JavaEmbedUtils.initialize(getClassPath(manager)); for (int i = 0, size = someDeclaredBeans.size(); i < size; i++) { BSFDeclaredBean bean = (BSFDeclaredBean) someDeclaredBeans.elementAt(i); runtime.getGlobalVariables().define( GlobalVariable.variableName(bean.name), new BeanGlobalVariable(runtime, bean), org.jruby.internal.runtime.GlobalVariable.Scope.GLOBAL); } runtime.getGlobalVariables().defineReadonly("$bsf", new FunctionsGlobalVariable(runtime, new BSFFunctions(manager, this)), org.jruby.internal.runtime.GlobalVariable.Scope.GLOBAL); }
public void untraceVar(String name) { assert name != null; assert name.startsWith("$"); if (isDefined(name)) { GlobalVariable variable = globalVariables.get(name); variable.removeTraces(); } }
@JRubyMethod(rest = true, module = true, visibility = PRIVATE, reads = LASTLINE) public static IRubyObject print(ThreadContext context, IRubyObject recv, IRubyObject[] args) { RubyIO.print(context, context.runtime.getGlobalVariables().get("$>"), args); return context.nil; }
@JRubyMethod(name = {"exists?", "exist?"}, meta = true, compat = RUBY1_9) public static IRubyObject exist(ThreadContext context, IRubyObject recv, IRubyObject arg) { // Capture previous exception if any. IRubyObject exception = context.runtime.getGlobalVariables().get("$!"); try { return context.runtime.newFileStat(RubyFile.get_path(context, arg).asJavaString(), false).directory_p(); } catch (Exception e) { // Restore $! context.runtime.getGlobalVariables().set("$!", exception); return context.runtime.newBoolean(false); } }
@JRubyMethod(name = "chomp!", compat = RUBY1_9) public IRubyObject chomp_bang19(ThreadContext context) { Ruby runtime = context.runtime; if (value.getRealSize() == 0) return runtime.getNil(); IRubyObject rsObj = runtime.getGlobalVariables().get("$/"); if (rsObj == runtime.getGlobalVariables().getDefaultSeparator()) return smartChopBangCommon19(runtime); return chompBangCommon19(runtime, rsObj); }
/** Returns an Array with the names of all global variables. * */ @JRubyMethod(name = "global_variables", module = true, visibility = PRIVATE) public static RubyArray global_variables(ThreadContext context, IRubyObject recv) { Ruby runtime = context.runtime; RubyArray globalVariables = runtime.newArray(); for (String globalVariableName : runtime.getGlobalVariables().getNames()) { globalVariables.append(runtime.newString(globalVariableName)); } return globalVariables; }