001: /***** BEGIN LICENSE BLOCK *****
002: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
003: *
004: * The contents of this file are subject to the Common Public
005: * License Version 1.0 (the "License"); you may not use this file
006: * except in compliance with the License. You may obtain a copy of
007: * the License at http://www.eclipse.org/legal/cpl-v10.html
008: *
009: * Software distributed under the License is distributed on an "AS
010: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * rights and limitations under the License.
013: *
014: * Copyright (C) 2006 Ola Bini <ola@ologix.com>
015: *
016: * Alternatively, the contents of this file may be used under the terms of
017: * either of the GNU General Public License Version 2 or later (the "GPL"),
018: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
019: * in which case the provisions of the GPL or the LGPL are applicable instead
020: * of those above. If you wish to allow use of your version of this file only
021: * under the terms of either the GPL or the LGPL, and not to allow others to
022: * use your version of this file under the terms of the CPL, indicate your
023: * decision by deleting the provisions above and replace them with the notice
024: * and other provisions required by the GPL or the LGPL. If you do not delete
025: * the provisions above, a recipient may use your version of this file under
026: * the terms of any one of the CPL, the GPL or the LGPL.
027: ***** END LICENSE BLOCK *****/package org.jruby.runtime;
028:
029: import org.jruby.RubyModule;
030: import org.jruby.ast.util.ArgsUtil;
031: import org.jruby.runtime.builtin.IRubyObject;
032: import org.jruby.util.collections.SinglyLinkedList;
033:
034: /**
035: * A Block implemented using a Java-based BlockCallback implementation
036: * rather than with an ICallable. For lightweight block logic within
037: * Java code.
038: */
039: public class CompiledBlock extends Block {
040: private CompiledBlockCallback callback;
041:
042: public CompiledBlock(ThreadContext context, IRubyObject self,
043: Arity arity, DynamicScope dynamicScope,
044: CompiledBlockCallback callback) {
045: this (self, context.getCurrentFrame(), context.peekCRef(),
046: Visibility.PUBLIC, context.getRubyClass(),
047: dynamicScope, arity, callback);
048: }
049:
050: private CompiledBlock(IRubyObject self, Frame frame,
051: SinglyLinkedList cref, Visibility visibility,
052: RubyModule klass, DynamicScope dynamicScope, Arity arity,
053: CompiledBlockCallback callback) {
054: super (null, self, frame, cref, visibility, klass, dynamicScope);
055: this .arity = arity;
056: this .callback = callback;
057: }
058:
059: protected void pre(ThreadContext context, RubyModule klass) {
060: context.preYieldSpecificBlock(this , klass);
061: }
062:
063: protected void post(ThreadContext context) {
064: context.postYield();
065: }
066:
067: public IRubyObject call(ThreadContext context, IRubyObject[] args) {
068: return yield(context,
069: context.getRuntime().newArrayNoCopy(args), null, null,
070: true);
071: }
072:
073: public IRubyObject yield(ThreadContext context, IRubyObject args,
074: IRubyObject self, RubyModule klass, boolean aValue) {
075: if (klass == null) {
076: self = this .self;
077: frame.setSelf(self);
078: }
079: IRubyObject[] realArgs = null;
080: if (!aValue) {
081: // handle as though it's just an array coming in...i.e. it should be multiassigned or just assigned as is to var 0.
082: // FIXME for now, since masgn isn't supported, this just wraps args in an IRubyObject[], since single vars will want that anyway
083: realArgs = new IRubyObject[] { args };
084: } else {
085: realArgs = ArgsUtil.convertToJavaArray(args);
086: }
087: try {
088: pre(context, klass);
089: return callback.call(context, self, realArgs);
090: } finally {
091: post(context);
092: }
093: }
094:
095: public Block cloneBlock() {
096: return new CompiledBlock(self, frame.duplicate(), cref,
097: visibility, klass, dynamicScope.cloneScope(), arity,
098: callback);
099: }
100:
101: public Arity arity() {
102: return arity;
103: }
104: }
|