---
title: "《搜索引擎零距离》第三章 IRS虚拟机及编译器实现原理(3)"
author: "roki"
date: "2009-06-23T08:59:28Z"
tags: []
slug: "irs-3"
draft: false
lastmod: "2009-06-23T08:59:28Z"
id: 413228
url: "https://roki.iteye.com/blog/413228"
---

**3.1.7	方法调用**
例子：

foo.bar() 
foo.bar 
bar() 
print "hello world\n" 
print 
语法：
 标识符 ] )] 
	若未指定被调用方法的对象，则调用 self 的方法。
	方法名中除了通常的标识符以外，还可以添加“?”或“!”等后缀。通常在布尔型（返回真或伪）方法名后添加“?”，在比同名（无“!”）方法更具破坏性的方法名（例：tr 和 tr!）后添加“!”。
	若最后一个参数前带“*”的话，将会先展开该参数的值，然后才传递。例如：
foo(1,*)   # 等同于 foo(1,2,3,4) 

**3.2	Java与JRuby的整合**
	当前版本的JRuby与Java的结合并不是非常完美，需要编写一些接口代码。笔者所使用的JRuby包是： jruby-complete-1.0.1.jar。
**3.2.1	Java中的Ruby运行库环境**
	首先，Java程序中需要进行如下定义:
	RubyInstanceConfig config = new RubyInstanceConfig();
	Ruby runtime = Ruby.newInstance(config);

	其中 RubyInstanceConfig config里可以配置JRuby运行环境的各种参数，比如
currentDirectory(当前目录)，environment（环境变量）等。config作为Ruby.newInstance的参数来构造runtime ，而runtime 是JRuby的运行库环境，各种JRuby语句的执行都需要在runtime的上下文中执行 。
	其次，需要在ParalleIRVirtualMachine进行如下定义：
	IRubyObject rt = JavaUtil.convertJavaToRuby(runtime, this);
这句代码把ParalleIRVirtualMachine对象转换成IRubyObject对象，以便在这个对象上进行其他操作。
	最后，我们需要把对象rt作为一个全局变量添加到runtime上去：
		IAccessor d = new ValueAccessor(rt);
		runtime.getGlobalVariables().define("$vm", d);
这步操作完成之后，runtime里就有了一个可供使用的全局对象$vm, 而$vm实际引用的是
IRubyObject对象rt， 由于rt是由ParalleIRVirtualMachine对象转化而来的，所以$vm实际引用的是ParalleIRVirtualMachine对象。
	为了在$vm对象上添加方法，需要在rt.getMetaClass()返回的对象上调用defineMethod方法。defineMethod的第一个参数是函数名，第二个参数是一个org.jruby.runtime.callback.Callback对象。

	例如，要在$vm上添加一个名为match的方法，可以这样写：
	rt.getMetaClass().defineMethod("match", processMatch);
	其中，第一个参数是函数名，第二个参数是一个org.jruby.runtime.callback.Callback对象。processMatch的定义是：
		Callback processMatch = new IRSReflectionCallback(
				ParalleIRVirtualMachine.class, "processMatchRuby",
				new Class { RubyString.class }, false,// if restArg
				false, // if staticArg
				Arity.singleArgument(), true);// if fast
上述new IRSReflectionCallback构造函数的第一个参数ParalleIRVirtualMachine.class
是目标Java对象的类型，processMatchRuby是对象上的函数名，new Class { RubyString.class }是参数类型数组，参数Arity.singleArgument()指明有几个参数，single是指一个参数，其他参数一般情况下不需修改。
	其中 IRSReflectionCallback类是一个自定义的类，它提供了Callback接口的实现。
Callback接口定义如下：
Callback.java：

public interface Callback {
    public IRubyObject execute(IRubyObject recv, IRubyObject args, Block block);
    public Arity getArity();
**}
3.2.2	IRSReflectionCallback类实现**


```java
IRSReflectionCallback.java:

package com.irs..ruby;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

import org.jruby.exceptions.JumpException;
import org.jruby.exceptions.MainExitException;
import org.jruby.exceptions.RaiseException;
import org.jruby.exceptions.ThreadKill;
import org.jruby.javasupport.JavaObject;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.callback.Callback;

/**
 * A wrapper for <code>java.lang.reflect.Method</code> objects which implement Ruby methods.
 */
public class IRSReflectionCallback implements Callback {
    private Method method;
    private Class type;
    private String methodName;
    private Class[] argumentTypes;
    private boolean isRestArgs;
    private Arity arity;
    private boolean isStaticMethod;
    private boolean fast;
    
/*构造函数*/
    public IRSReflectionCallback(Class type, String methodName, Class[] argumentTypes,
            boolean isRestArgs, boolean isStaticMethod, Arity arity, boolean fast) {
        this.type = type;
    	this.methodName = methodName;
    	this.argumentTypes = argumentTypes;
        this.isRestArgs = isRestArgs;
        this.isStaticMethod = isStaticMethod;
    	this.arity = arity;
        this.fast = fast;
    	
        assert type != null;
        assert methodName != null;
        assert arity != null;
        
        loadMethod(fast);
    }
    
/*
按照是否为静态方法来传递函数参数
静态方法不需要传递this对象，成员方法需要传递this对象
*/
    private void loadMethod(boolean fast) {
    	Class[] args;
    	
        if (isStaticMethod) {
            Class[] types = new Class[argumenttypes.length + 1];
            System.arraycopy(argumentTypes, 0, types, 1, argumentTypes.length);
            types[0] = IRubyObject.class;
            args = types;
        } else {
            args = argumentTypes;
        }
        
        // ENEBO: Perhaps slow but simple for now
        if (!fast) {
            Class[] types = new Class[args.length + 1];
            System.arraycopy(args, 0, types, 0, args.length);
            types[args.length] = Block.class;
            args = types;
        }
        
        try {
        	List<Class> tmpArg=Arrays.asList(args);
			//通过反射来从对象上获得方法，供Ruby引擎调用
            method = type.getMethod(methodName, args);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException("NoSuchMethodException: Cannot get method \"" + methodName
                    + "\" in class \"" + type.getName() + "\" by Reflection.");
        } catch (SecurityException e) {
            throw new RuntimeException("SecurityException: Cannot get method \"" + methodName
                    + "\" in class \"" + type.getName() + "\" by Reflection.");
        }
    }
    
    /**
     * Returns an object array that collects all rest arguments in its own object array which
     * is then put into the last slot of the first object array.  That is, assuming that this
     * callback expects one required argument and any number of rest arguments, an input of
     * <code>[1, 2, 3]</code> is transformed into <code>[1, [2, 3]]</code>.  
     */
    protected final Object[] packageRestArgumentsForReflection(final Object[] originalArgs) {
        IRubyObject[] restArray = new IRubyObject[originalargs.length - (argumenttypes.length - 1)];
        Object[] result = new Object[argumenttypes.length];
        try {
            System.arraycopy(originalArgs, argumentTypes.length - 1, restArray, 0, originalArgs.length - (argumentTypes.length - 1));
        } catch (ArrayIndexOutOfBoundsException e) {
            assert false : e;
        	return null;
        }
        System.arraycopy(originalArgs, 0, result, 0, argumentTypes.length - 1);
        result[argumenttypes.length - 1] = restArray;
        return result;
    }

    /**
	在IRubyObject recv对象上调用目标方法
     * Invokes the Ruby method. Actually, this methods delegates to an internal version
     * that may throw the usual Java reflection exceptions.  Ruby exceptions are rethrown, 
     * other exceptions throw an AssertError and abort the execution of the Ruby program.
     * They should never happen.
     */
	/**
     * Calls a wrapped Ruby method for the specified receiver with the specified arguments.
     */
    public IRubyObject execute(IRubyObject recv, IRubyObject[] oargs, Block block) {
        arity.checkArity(recv.getRuntime(), oargs);

        Object[] methodArgs = oargs;
        
    	if (isRestArgs) {
    		methodArgs = packageRestArgumentsForReflection(methodArgs);
    	}
        try {
        	JavaObject receiver = (JavaObject)recv;
        	Object target=receiver.getValue();
        	
            if (isStaticMethod) {
                Object[] args = new Object[methodargs.length + (fast ? 1 : 2)];
                System.arraycopy(methodArgs, 0, args, 1, methodArgs.length);
                args[0] = recv;
                if (!fast) args[methodargs.length + 1] = block;
                receiver = null;
                methodArgs = args;
            } else {
                Object[] args = new Object[methodargs.length + (fast ? 0 : 1)];
                System.arraycopy(methodArgs, 0, args, 0, methodArgs.length);
                if (!fast) args[methodargs.length] = block;
                methodArgs = args;
            }
            return (IRubyObject) method.invoke(target, methodArgs);
        } catch (InvocationTargetException e) {
            if (e.getTargetException() instanceof RaiseException) {
                throw (RaiseException) e.getTargetException();
            } else if (e.getTargetException() instanceof JumpException) {
                throw (JumpException) e.getTargetException();
            } else if (e.getTargetException() instanceof ThreadKill) {
            	// allow it to bubble up
            	throw (ThreadKill) e.getTargetException();
            } else if (e.getTargetException() instanceof Exception) {
                if(e.getTargetException() instanceof MainExitException) {
                    throw (RuntimeException)e.getTargetException();
                }
                recv.getRuntime().getJavaSupport().handleNativeException(e.getTargetException());
                return recv.getRuntime().getNil();
            } else {
                throw (Error) e.getTargetException();
            }
        } catch (IllegalAccessException e) {
            StringBuffer message = new StringBuffer();
            message.append(e.getMessage());
            message.append(':');
            message.append(" methodName=").append(methodName);
            message.append(" recv=").append(recv.toString());
            message.append(" type=").append(type.getName());
            message.append(" methodArgs=[");
            for (int i = 0; i < methodArgs.length; i++) {
                message.append(methodArgs[i]);
                message.append(' ');
            }
            message.append(']');
            assert false : message.toString();
            return null;
        } catch (final IllegalArgumentException e) {
/*            StringBuffer message = new StringBuffer();
            message.append(e.getMessage());
            message.append(':');
            message.append(" methodName=").append(methodName);
            message.append(" recv=").append(recv.toString());
            message.append(" type=").append(type.getName());
            message.append(" methodArgs=[");
            for (int i = 0; i < methodArgs.length; i++) {
                message.append(methodArgs[i]);
                message.append(' ');
            }
            message.append(']');*/
            assert false : e;
            return null;
        }
    }

    /**
     * Returns the arity of the wrapped Ruby method.
     */
    public Arity getArity() {
        return arity;
    }
}
```


**3.2.3	在Java中编译执行Ruby脚本**

```java
public IRubyObject compileAndRun(String scriptTxt) {

		// Node node
		try {

			//获得全局的runtime对象（Ruby运行库环境）
			Ruby ruby = runtime;

			Script script = null;
			StringReader reader = new StringReader(scriptTxt);

			Node parsedScriptNode = ruby.parse(reader, scriptTxt,
						ruby.getCurrentContext().getCurrentScope(), 0);

			// do the compile
			StandardASMCompiler compiler = new StandardASMCompiler(
						parsedScriptNode);

			NodeCompilerFactory.getCompiler(parsedScriptNode).compile(
						parsedScriptNode, compiler);
			//加载类
			Class scriptClass = compiler.loadClass(new JRubyClassLoader());
			//生成script对象
			script = (Script) scriptClass.newInstance();

			ThreadContext tc = ruby.getCurrentContext();
			//执行脚本
			return script.run(tc, tc.getFrameSelf(), IRubyObject.NULL_ARRAY,
					Block.NULL_BLOCK);

		} catch (NotCompilableException nce) {
			System.err.println("Error -- Not compileable: " + nce.getMessage());
			return null;
		} catch (JumpException je) {
			if (je.getJumpType() == JumpException.JumpType.ReturnJump) {
				return (IRubyObject) je.getValue();
			} else {
				throw je;
			}
		} catch (ClassNotFoundException e) {
			System.err.println("Error -- Not compileable: " + e.getMessage());
			return null;
		} catch (InstantiationException e) {
			System.err.println("Error -- Not compileable: " + e.getMessage());
			return null;
		} catch (IllegalAccessException e) {
			System.err.println("Error -- Not compileable: " + e.getMessage());
			return null;
		}
	}
```


**3.2.4	Java内嵌Ruby方法总结**
	结合以上各个步骤，我们就可以在Java中定义方法，然后在Ruby中调用它了，下面总结一下整个过程。
//示例Java类，其中的helloword方法将在Ruby中被调用

```java
Class JavaSampleCls{
	public void helloworld(RubyString param){
		System.out.println(“hello ”+param.toString());
	}
}


	//初始化Ruby运行环境
	RubyInstanceConfig config = new RubyInstanceConfig();
	Ruby runtime = Ruby.newInstance(config);

	//初始化目标Java对象
	JavaSampleCls javasample=new JavaSampleCls();

	//把目标的普通Java对象转化为Ruby可识别的IRubyObject对象
	IRubyObject targetObject = JavaUtil.convertJavaToRuby(runtime,javasample);

	//定义callback对象： 类JavaSampleCls上的helloworld方法，一个String参数
	Callback  helloCallback = new IRSReflectionCallback(
				JavaSampleCls.class, "helloworld",
				new Class[] { RubyString.class }, false,// if restArg
				false, // if staticArg
				Arity.singleArgument(), true);// if fast

		//把callback定义到targetObject上
		targetObject.getMetaClass().defineMethod("helloworld", helloCallback);

		IAccessor d = new ValueAccessor(targetObject);

		//把targetObject绑定到Ruby运行库环境中
		runtime.getGlobalVariables().define("$vm", d);

		//声明Ruby脚本
		String rubycode=”$vm.helloworld(“java”)”;
		//编译执行
		compileAndRun（rubycode）;
```