Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2010 Wayne Meissner Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 
 
 package org.jruby.cext;
 
 
 import org.jruby.Ruby;
 
 
 public class JRuby {
 
     public static long callRubyMethod(IRubyObject recvObject methodNameIRubyObject[] args) {
         IRubyObject retval = recv.callMethod(recv.getRuntime().getCurrentContext(),
                 methodName.toString(), args);
 
         return Handle.nativeHandle(retval);
     }
 
     public static long callRubyMethodB(IRubyObject recvObject methodNameIRubyObject[] argsIRubyObject blockProc) {
         IRubyObject retval = recv.callMethod(recv.getRuntime().getCurrentContext(),
                 methodName.toString(), args, ((RubyProc)blockProc).getBlock());
 
         return Handle.nativeHandle(retval);
     }
 
     public static long callRubyMethod0(IRubyObject recvObject methodName) {
         IRubyObject retval = RuntimeHelpers.invoke(recv.getRuntime().getCurrentContext(),
                 recvmethodName.toString());
 
         return Handle.nativeHandle(retval);
     }
 
     public static long callRubyMethod1(IRubyObject recvObject methodNameIRubyObject arg1) {
         IRubyObject retval = RuntimeHelpers.invoke(recv.getRuntime().getCurrentContext(),
                 recvmethodName.toString(), arg1);
 
         return Handle.nativeHandle(retval);
     }
 
     public static long callRubyMethod2(IRubyObject recvObject methodNameIRubyObject arg1IRubyObject arg2) {
         IRubyObject retval = RuntimeHelpers.invoke(recv.getRuntime().getCurrentContext(),
                 recvmethodName.toString(), arg1arg2);
 
         return Handle.nativeHandle(retval);
     }
 
     public static long callRubyMethod3(IRubyObject recvObject methodNameIRubyObject arg1,
             IRubyObject arg2IRubyObject arg3) {
         IRubyObject retval = RuntimeHelpers.invoke(recv.getRuntime().getCurrentContext(),
                recvmethodName.toString(), arg1arg2arg3);
        return Handle.nativeHandle(retval);
    }
    public static long callSuperMethod(Ruby runtimeIRubyObject[] args) {
        ThreadContext currentContext = runtime.getCurrentContext();
        IRubyObject retval = RuntimeHelpers.invokeSuper(currentContext,
                runtime.getCurrentContext().getFrameSelf(), args.);
        return Handle.nativeHandle(retval);
    }
    public static long instanceEval(IRubyObject selfIRubyObject[] args) {
        Ruby runtime = self.getRuntime();
        ThreadContext ctxt = runtime.getCurrentContext();
        Block block = ctxt.getFrameBlock();
        IRubyObject retval = self.callMethod(ctxt"instance_eval"argsblock);
        return Handle.nativeHandle(retval);
    }
    public static long newString(Ruby runtimebyte[] bytesint lengthboolean tainted) {
        IRubyObject retval = RubyString.newStringNoCopy(runtimebytes);
        if (tainted) {
            retval.setTaint(tainted);
        }
        ((RubyStringretval).getByteList().setRealSize(length);
        return Handle.nativeHandle(retval);
    }
    public static DynamicMethod newMethod(RubyModule modulelong fnint arity) {
        switch (arity) {
            case 0:
                return new NativeMethod0(modulearityfn);
            case 1:
                return new NativeMethod1(modulearityfn);
            case 2:
                return new NativeMethod2(modulearityfn);
            case 3:
                return new NativeMethod3(modulearityfn);
            default:
                return new NativeMethod(modulearityfn);
        }
    }
    public static RubyFloat newFloat(Ruby runtimelong handledouble value) {
        final RubyFloat f = RubyFloat.newFloat(runtimevalue);
        GC.register(f, Handle.newHandle(runtimefhandle));
        return f;
    }
    public static IRubyObject newThread(Ruby runtimelong fnIRubyObject args_ary) {
        RubyProc proc = (RubyProcnewProc(runtimefn);
        IRubyObject[] args = (args_ary instanceof RubyArray) ? ((RubyArray)args_ary).toJavaArray() : new IRubyObject[] {args_ary};
        return RubyThread.newInstance(runtime.getThread(), argsproc.getBlock());
    }
    public static IRubyObject newProc(Ruby runtimelong fn) {
        String name = System.currentTimeMillis() + "$block_jruby-cext";
        IRubyObject recv = runtime.getCurrentContext().getFrameSelf();
        RubyMethod method = RubyMethod.newMethod(recv.getMetaClass(), namerecv.getMetaClass(), name,
                new NativeProcMethod(recv.getMetaClass(), fn), recv);
        IRubyObject proc = method.to_proc(runtime.getCurrentContext(), .);
        return proc;
    }

    
rb_yield
    public static IRubyObject yield(Ruby runtimeRubyArray args) {
        return runtime.getCurrentContext().getFrameBlock().call(runtime.getCurrentContext(), args.toJavaArray());
    }

    
rb_block_given_p
    public static int blockGiven(Ruby runtime) {
        return runtime.getCurrentContext().getFrameBlock().isGiven() ? 1 : 0;
    }

    
rb_block_proc
    public static RubyProc getBlockProc(Ruby runtime) {
        Block block = runtime.getCurrentContext().getFrameBlock();
        RubyProc p = RubyProc.newProc(runtimeblockblock.type);
        return p;
    }
    public static long ll2inum(Ruby runtimelong l) {
        RubyFixnum n = RubyFixnum.newFixnum(runtimel);
        Handle h = GC.lookup(n);
        if (h != null) {
            return h.getAddress();
        }
        h = Handle.newHandle(runtimen, Native.getInstance(runtime).newFixnumHandle(nl));
        GC.register(nh);
        return h.getAddress();
    }
    private static final BigInteger UINT64_BASE = BigInteger.valueOf(.).add(.);
    public static long ull2inum(Ruby runtimelong l) {
        RubyObject n = l < 0
                    ? RubyBignum.newBignum(runtime, BigInteger.valueOf(l & 0x7fffffffffffffffL).add())
                    : runtime.newFixnum(l);
        Handle h = GC.lookup(n);
        if (h != null) {
            return h.getAddress();
        }
        // FIXME should create Bignum handle for Bignum values
        h = Handle.newHandle(runtimen, Native.getInstance(runtime).newFixnumHandle(nl));
        GC.register(nh);
        return h.getAddress();
    }
    public static long int2big(Ruby runtimelong l) {
        return Handle.nativeHandle(RubyBignum.newBignum(runtimel));
    }
    public static long uint2big(Ruby runtimelong l) {
        IRubyObject retval = l < 0
                    ? RubyBignum.newBignum(runtime, BigInteger.valueOf(l & 0x7fffffffffffffffL).add())
                    : RubyBignum.newBignum(runtimel);
        return Handle.nativeHandle(retval);
    }

    
rb_gv_set
    public static long gv_set(Ruby runtimeString nameIRubyObject value) {
        return Handle.nativeHandle(runtime.getGlobalVariables().set(namevalue));
    }

    
rb_gv_get
    public static long gv_get(Ruby runtimeString name) {
        return Handle.nativeHandle(runtime.getGlobalVariables().get(name));
    }
    public static void clearErrorInfo(Ruby runtime) {
        runtime.getCurrentContext().setErrorInfo(runtime.getNil());
    }

    
rb_sys_fail
    public static void sysFail(Ruby runtimeString message) {
        final int n = LastError.getLastError(jnr.ffi.Runtime.getSystemRuntime());
        sysFail(runtimemessagen);
    }
    public static void sysFail(Ruby runtimeString messageint n) {
        IRubyObject arg = (message != null) ? runtime.newString(message) : runtime.getNil();
        RubyClass instance = runtime.getErrno(n);
        if(instance == null) {
            instance = runtime.getSystemCallError();
            throw new RaiseException((RubyException)(instance.newInstance(runtime.getCurrentContext(), new IRubyObject[]{argruntime.newFixnum(n)}, .)));
        } else {
            throw new RaiseException((RubyException)(instance.newInstance(runtime.getCurrentContext(), new IRubyObject[]{arg}, .)));
        }
    }
    public static void threadSleep(Ruby runtimeint interval) {
        try {
            runtime.getCurrentContext().getThread().sleep(interval);
        } catch (InterruptedException e) {
            // Thread wakeup, do nothing
        }
    }
    
    public static long getMetaClass(IRubyObject object) {
        RubyClass metaClass = object.getMetaClass();
        return Handle.nativeHandle(metaClass);
    }
    public static final class NativeFunctionTask implements BlockingTask {
        private Native nativeInstance;
        private long runrun_datawakeupwakeup_data = 0;
        public long retval = 4; // 4 is VALUE Qnil
        public NativeFunctionTask(Native nativeInstancelong runlong run_data,
                long wakeuplong wakeup_data) {
            this. = nativeInstance;
            this. = run;
            this. = run_data;
            this. = wakeup;
            this. = wakeup_data;
        }
        public void run() throws InterruptedException {
             = .callFunction();
        }
        public void wakeup() {
            .callFunction();
        }
    }
    public static long nativeBlockingRegion(Ruby runtimelong blocking_funclong blocking_data,
            long unblocking_funclong unblocking_data) {
        RubyThread thread = runtime.getCurrentContext().getThread();
        NativeFunctionTask task = new NativeFunctionTask(Native.getInstance(runtime), blocking_func,
                blocking_dataunblocking_funcunblocking_data);
        GC.disable();
        int lockCount = GIL.releaseAllLocks();
        try {
            thread.executeBlockingTask(task);
        } catch (InterruptedException e) {
            // ignore
        } finally  {
            GIL.acquire(lockCount);
            GC.enable();
        }
        return task.retval;
    }
New to GrepCode? Check out our FAQ X