@JRubyMethod public IRubyObject oneof(ThreadContext context, IRubyObject name, Block block) { RubyOneofDescriptor oneofdef = (RubyOneofDescriptor) cOneofDescriptor.newInstance(context, Block.NULL_BLOCK); RubyOneofBuilderContext ctx = (RubyOneofBuilderContext) cOneofBuilderContext.newInstance(context, oneofdef, Block.NULL_BLOCK); oneofdef.setName(context, name); Binding binding = block.getBinding(); binding.setSelf(ctx); block.yieldSpecific(context); descriptor.addOneof(context, oneofdef); return context.runtime.getNil(); }
/** * Clone the binding. The frame will be duplicated, and eval context will * point to the new binding, but other fields will be copied as-is. * * @return a new cloned Binding */ public Binding clone() { return new Binding(this); }
@Override public boolean equals(Object other) { if ( this == other ) return true; if ( ! ( other instanceof Block ) ) return false; final Block that = (Block) other; return this.binding.equals(that.binding) && this.body == that.body; }
Binding newBinding = new Binding( oldBinding.getSelf(), oldBinding.getFrame().duplicate(), oldBinding.getVisibility(), oldBinding.getDynamicScope(), oldBinding.getMethod(), oldBinding.getFile(), oldBinding.getLine()); block = new Block(procBlock.getBody(), newBinding); block.getBinding().setFile(block.getBody().getFile()); block.getBinding().setLine(block.getBody().getLine()); block.getBinding().getDummyScope(block.getBody().getStaticScope());
Binding newBinding = new Binding( oldBinding.getSelf(), oldBinding.getFrame().duplicate(), oldBinding.getVisibility(), oldBinding.getKlass(), oldBinding.getDynamicScope(), oldBinding.getBacktrace().clone()); block = new Block(procBlock.getBody(), newBinding); block.getBinding().setFile(block.getBody().getFile()); block.getBinding().setLine(block.getBody().getLine()); block.getBinding().getDummyScope(block.getBody().getStaticScope());
/** * Evaluate the given string under the specified binding object. If the binding is not a Proc or Binding object * (RubyProc or RubyBinding) throw an appropriate type error. * @param context the thread context for the current thread * @param self the self against which eval was called; used as self in the eval in 1.9 mode * @param src The string containing the text to be evaluated * @param binding The binding object under which to perform the evaluation * @return An IRubyObject result from the evaluation */ public static IRubyObject evalWithBinding(ThreadContext context, IRubyObject self, IRubyObject src, Binding binding) { Ruby runtime = context.runtime; DynamicScope evalScope = binding.getEvalScope(runtime); evalScope.getStaticScope().determineModule(); // FIXME: It would be nice to just set this or remove it from staticScope altogether Frame lastFrame = context.preEvalWithBinding(binding); try { return evalCommon(context, evalScope, self, src, binding.getFile(), binding.getLine(), binding.getMethod(), binding.getFrame().getBlock(), EvalType.BINDING_EVAL); } finally { context.postEvalWithBinding(binding, lastFrame); } }
public Block cloneBlockAndFrame() { Binding oldBinding = binding; Binding binding = new Binding( oldBinding.getSelf(), oldBinding.getFrame().duplicate(), oldBinding.getVisibility(), oldBinding.getDynamicScope(), oldBinding.getMethod(), oldBinding.getFile(), oldBinding.getLine()); Block newBlock = new Block(body, binding); newBlock.type = type; newBlock.escapeBlock = this; return newBlock; }
public Block cloneBlockAndFrame() { Binding oldBinding = binding; Binding binding = new Binding( oldBinding.getSelf(), oldBinding.getFrame().duplicate(), oldBinding.getVisibility(), oldBinding.getKlass(), oldBinding.getDynamicScope(), oldBinding.getBacktrace()); Block newBlock = new Block(body, binding); newBlock.type = type; newBlock.escapeBlock = this; return newBlock; }
@JRubyMethod public IRubyObject source_location(ThreadContext context) { Ruby runtime = context.runtime; if (file != null) { return runtime.newArray(runtime.newString(file), runtime.newFixnum(line + 1 /*zero-based*/)); } if (block != null) { Binding binding = block.getBinding(); // block+binding may exist for a core method, which will have a null filename if (binding.getFile() != null) { return runtime.newArray( runtime.newString(binding.getFile()), runtime.newFixnum(binding.getLine() + 1 /*zero-based*/)); } } return context.nil; }
protected IRubyObject commonYieldPath(ThreadContext context, IRubyObject[] args, IRubyObject self, RubyModule klass, Binding binding, Type type, Block block) { // SSS: Important! Use getStaticScope() to use a copy of the static-scope stored in the block-body. // Do not use 'closure.getStaticScope()' -- that returns the original copy of the static scope. // This matters because blocks created for Thread bodies modify the static-scope field of the block-body // that records additional state about the block body. // // FIXME: Rather than modify static-scope, it seems we ought to set a field in block-body which is then // used to tell dynamic-scope that it is a dynamic scope for a thread body. Anyway, to be revisited later! Visibility oldVis = binding.getFrame().getVisibility(); RubyModule currentModule = getStaticScope().getModule(); Frame prevFrame = context.preYieldNoScope(binding, klass); if (klass == null) self = prepareSelf(binding); try { DynamicScope prevScope = binding.getDynamicScope(); DynamicScope newScope = closure.isForLoopBody() ? prevScope : DynamicScope.newDynamicScope(getStaticScope(), prevScope); context.pushScope(newScope); return Interpreter.INTERPRET_BLOCK(context, self, closure, args, binding.getMethod(), block, type); } finally { binding.getFrame().setVisibility(oldVis); context.postYield(binding, prevFrame); } }
public Block cloneBlockAndBinding() { Block newBlock = new Block(body, binding.clone()); newBlock.type = type; newBlock.escapeBlock = this; return newBlock; }
@JRubyMethod public IRubyObject local_variable_set(ThreadContext context, IRubyObject symbol, IRubyObject value) { String name = symbol.asJavaString().intern(); DynamicScope evalScope = binding.getEvalScope(context.runtime); int slot = evalScope.getStaticScope().isDefined(name); if (slot == -1) { // Yay! New variable associated with this binding slot = evalScope.getStaticScope().addVariable(name.intern()); evalScope.growIfNeeded(); } return evalScope.setValue(slot & 0xffff, value, slot >> 16); }
private static DynamicScope getNewBlockScope(Block block, boolean pushNewDynScope, boolean reuseParentDynScope) { DynamicScope newScope = block.getBinding().getDynamicScope(); if (pushNewDynScope) return block.allocScope(newScope); // Reuse! We can avoid the push only if surrounding vars aren't referenced! if (reuseParentDynScope) return newScope; // No change return null; }
@JRubyMethod(name = "receiver") public IRubyObject receiver(ThreadContext context) { return binding.getSelf(); } }
private static Binding getBindingForEval(ThreadContext context, IRubyObject scope) { if (scope instanceof RubyBinding) { return ((RubyBinding) scope).getBinding().cloneForEval(); } throw context.runtime.newTypeError("wrong argument type " + scope.getMetaClass() + " (expected binding)"); }
@Override protected Frame pre(ThreadContext context, RubyModule klass, Binding binding) { return context.preYieldLightBlock(binding, binding.getDummyScope(scope), klass); }
Binding newBinding = new Binding( oldBinding.getSelf(), oldBinding.getFrame().duplicate(), oldBinding.getVisibility(), oldBinding.getDynamicScope(), oldBinding.getMethod(), oldBinding.getFile(), oldBinding.getLine()); block = new Block(procBlock.getBody(), newBinding); block.getBinding().setFile(block.getBody().getFile()); block.getBinding().setLine(block.getBody().getLine()); block.getBinding().getDummyScope(block.getBody().getStaticScope());
Binding newBinding = new Binding( oldBinding.getSelf(), oldBinding.getFrame().duplicate(), oldBinding.getVisibility(), oldBinding.getKlass(), oldBinding.getDynamicScope(), oldBinding.getBacktrace().clone()); block = new Block(procBlock.getBody(), newBinding); block.getBinding().setFile(block.getBody().getFile()); block.getBinding().setLine(block.getBody().getLine()); block.getBinding().getDummyScope(block.getBody().getStaticScope());