1 32 package org.jruby.internal.runtime; 33 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 38 import org.jruby.Ruby; 39 import org.jruby.exceptions.RaiseException; 40 import org.jruby.runtime.IAccessor; 41 import org.jruby.runtime.builtin.IRubyObject; 42 43 47 public class GlobalVariables { 48 private Ruby runtime; 49 private Map globalVariables = new HashMap (); 50 51 public GlobalVariables(Ruby runtime) { 52 this.runtime = runtime; 53 } 54 55 public void define(String name, IAccessor accessor) { 56 assert name != null; 57 assert accessor != null; 58 assert name.startsWith("$"); 59 60 globalVariables.put(name, new GlobalVariable(accessor)); 61 } 62 63 public void defineReadonly(String name, IAccessor accessor) { 64 assert name != null; 65 assert accessor != null; 66 assert name.startsWith("$"); 67 68 globalVariables.put(name, new GlobalVariable(new ReadonlyAccessor(name, accessor))); 69 } 70 71 public boolean isDefined(String name) { 72 assert name != null; 73 assert name.startsWith("$"); 74 75 GlobalVariable variable = (GlobalVariable)globalVariables.get(name); 76 return variable != null && !(variable.getAccessor() instanceof UndefinedAccessor); 77 } 78 79 84 public void alias(String name, String oldName) { 85 assert name != null; 86 assert oldName != null; 87 assert name.startsWith("$"); 88 assert oldName.startsWith("$"); 89 90 if (runtime.getSafeLevel() >= 4) { 91 throw runtime.newSecurityError("Insecure: can't alias global variable"); 92 } 93 94 GlobalVariable oldVariable = createIfNotDefined(oldName); 95 GlobalVariable variable = (GlobalVariable)globalVariables.get(name); 96 97 if (variable != null && oldVariable != variable && variable.isTracing()) { 98 throw new RaiseException(runtime, runtime.getClass("RuntimeError"), "can't alias in tracer", false); 99 } 100 101 globalVariables.put(name, oldVariable); 102 } 103 104 public IRubyObject get(String name) { 105 assert name != null; 106 assert name.startsWith("$"); 107 108 GlobalVariable variable = (GlobalVariable)globalVariables.get(name); 109 if (variable != null) { 110 return variable.getAccessor().getValue(); 111 } 112 runtime.getWarnings().warning("global variable `" + name + "' not initialized"); 113 return runtime.getNil(); 114 } 115 116 public IRubyObject set(String name, IRubyObject value) { 117 assert name != null; 118 assert name.startsWith("$"); 119 120 if (runtime.getSafeLevel() >= 4) { 121 throw runtime.newSecurityError("Insecure: can't change global variable value"); 122 } 123 124 GlobalVariable variable = createIfNotDefined(name); 125 IRubyObject result = variable.getAccessor().setValue(value); 126 return result; 128 } 129 130 public Iterator getNames() { 131 return globalVariables.keySet().iterator(); 132 } 133 134 private GlobalVariable createIfNotDefined(String name) { 135 GlobalVariable variable = (GlobalVariable)globalVariables.get(name); 136 if (variable == null) { 137 variable = GlobalVariable.newUndefined(runtime, name); 138 globalVariables.put(name, variable); 139 } 140 return variable; 141 } 142 } 143 | Popular Tags |