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) 2001 Alan Moore <alan_moore@gmx.net>
015: * Copyright (C) 2001-2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
016: * Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se>
017: * Copyright (C) 2004 Thomas E Enebo <enebo@acm.org>
018: * Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de>
019: *
020: * Alternatively, the contents of this file may be used under the terms of
021: * either of the GNU General Public License Version 2 or later (the "GPL"),
022: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
023: * in which case the provisions of the GPL or the LGPL are applicable instead
024: * of those above. If you wish to allow use of your version of this file only
025: * under the terms of either the GPL or the LGPL, and not to allow others to
026: * use your version of this file under the terms of the CPL, indicate your
027: * decision by deleting the provisions above and replace them with the notice
028: * and other provisions required by the GPL or the LGPL. If you do not delete
029: * the provisions above, a recipient may use your version of this file under
030: * the terms of any one of the CPL, the GPL or the LGPL.
031: ***** END LICENSE BLOCK *****/package org.jruby.ast.util;
032:
033: import org.jruby.Ruby;
034: import org.jruby.RubyArray;
035: import org.jruby.runtime.builtin.IRubyObject;
036: import org.jruby.runtime.MethodIndex;
037:
038: /**
039: *
040: * @author jpetersen
041: */
042: public final class ArgsUtil {
043:
044: public static IRubyObject[] convertToJavaArray(IRubyObject value) {
045: if (value == null) {
046: return IRubyObject.NULL_ARRAY;
047: }
048:
049: if (value instanceof RubyArray) {
050: return ((RubyArray) value).toJavaArrayMaybeUnsafe();
051: }
052:
053: return new IRubyObject[] { value };
054: }
055:
056: /**
057: * This name may be a bit misleading, since this also attempts to coerce
058: * array behavior using to_ary.
059: *
060: * @param runtime The JRuby runtime
061: * @param value The value to convert
062: * @param coerce Whether to coerce using to_ary or just wrap with an array
063: */
064: public static RubyArray convertToRubyArray(Ruby runtime,
065: IRubyObject value, boolean coerce) {
066: if (value == null) {
067: return RubyArray.newArrayLight(runtime, 0);
068: }
069:
070: if (!coerce) {
071: // don't attempt to coerce to array, just wrap and return
072: return RubyArray.newArrayNoCopyLight(runtime,
073: new IRubyObject[] { value });
074: }
075:
076: IRubyObject newValue = value.convertToType(runtime.getArray(),
077: MethodIndex.TO_ARY, "to_ary", false);
078:
079: if (newValue.isNil()) {
080: return RubyArray.newArrayNoCopyLight(runtime,
081: new IRubyObject[] { value });
082: }
083:
084: // empirically it appears that to_ary coersions always return array or nil, so this
085: // should always be an array by now.
086: return (RubyArray) newValue;
087: }
088:
089: /**
090: * Remove first element from array
091: *
092: * @param array to have first element "popped" off
093: * @return all but first element of the supplied array
094: */
095: public static IRubyObject[] popArray(IRubyObject[] array) {
096: if (array == null || array.length == 0) {
097: return IRubyObject.NULL_ARRAY;
098: }
099:
100: IRubyObject[] newArray = new IRubyObject[array.length - 1];
101: System.arraycopy(array, 1, newArray, 0, array.length - 1);
102:
103: return newArray;
104: }
105: }
|