⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jrubyinterpreter.java

📁 ZK 基础介绍 功能操作 模块 结合数据库操作
💻 JAVA
字号:
/* JRubyInterpreter.java{{IS_NOTE	Purpose:			Description:			History:		Sat Feb 10 19:56:11     2007, Created by tomyeh}}IS_NOTECopyright (C) 2007 Potix Corporation. All Rights Reserved.{{IS_RIGHT	This program is distributed under GPL Version 2.0 in the hope that	it will be useful, but WITHOUT ANY WARRANTY.}}IS_RIGHT*/package org.zkoss.zkmax.scripting.jruby;import java.util.Iterator;import org.jruby.Ruby;import org.jruby.javasupport.Java;import org.jruby.javasupport.JavaEmbedUtils;import org.jruby.javasupport.JavaObject;import org.jruby.javasupport.JavaUtil;import org.jruby.runtime.IAccessor;import org.jruby.runtime.GlobalVariable;import org.jruby.runtime.Block;import org.jruby.runtime.builtin.IRubyObject;import org.jruby.internal.runtime.GlobalVariables;import org.zkoss.xel.Function;import org.zkoss.zk.ui.Page;import org.zkoss.zk.ui.UiException;import org.zkoss.zk.scripting.util.GenericInterpreter;/** * JRuby interpreter. * * @author tomyeh */public class JRubyInterpreter extends GenericInterpreter {	private Ruby _runtime;	//GenericInterpreter//	protected void exec(String script) {		_runtime.evalScript(script);	}	protected boolean contains(String name) {		return _runtime.getGlobalVariables().isDefined(			GlobalVariable.variableName(name));	}	protected Object get(String name) {		IRubyObject ro = _runtime.getGlobalVariables().get(			GlobalVariable.variableName(name));		return rubyToJava(ro);	}	protected void set(String name, Object value) {		_runtime.getGlobalVariables().define(			GlobalVariable.variableName(name), new Variable(value));	}	protected void unset(String name) {		_runtime.getGlobalVariables().set(			GlobalVariable.variableName(name), _runtime.getNil());	}	//Interpreter//	public void init(Page owner, String zslang) {		super.init(owner, zslang);		_runtime = Ruby.getDefaultInstance();		_runtime.setGlobalVariables(new Variables(_runtime));	}	public void destroy() {		JavaEmbedUtils.terminate(_runtime);		_runtime = null;		super.destroy();	}	/**TODO: need to digg out a solution from jruby's manual	public Class getClass(String clsnm) {	}	public Function getFunction(String name, Class[] argTypes) {	}	*/	//utilities//	private IRubyObject javaToRuby(Object value) {		IRubyObject ro = JavaUtil.convertJavaToRuby(_runtime, value);		if (ro instanceof JavaObject)			return _runtime.getModule("JavaUtilities")				.callMethod(_runtime.getCurrentContext(), "wrap", ro);		return ro;	}	private Object rubyToJava(IRubyObject value) {		return JavaUtil.convertArgument(			Java.ruby_to_java(				_runtime.getObject(), value, Block.NULL_BLOCK),			Object.class);	}	/** The global scope. */	private class Variables extends GlobalVariables {		private Variables(Ruby runtime) {			super(runtime);			//we have to copy variables from the origin one to this			GlobalVariables vars = runtime.getGlobalVariables();			for (Iterator it = vars.getNames(); it.hasNext();) {				final String nm = (String)it.next();				set(nm, vars.get(nm));			}		}		public IRubyObject get(String name) {			IRubyObject ro = super.get(name);			if (ro == _runtime.getNil()) {				if (name.length() > 1 && name.charAt(0) == '$') //just in case					name = name.substring(1);				Object val = getFromNamespace(name);				if (val != UNDEFINED) 					return javaToRuby(val);			}			return ro;		}	}	/*ruby variable*/	private class Variable implements IAccessor {		private Object _value;			public Variable(Object value) {			_value = value;		}			public IRubyObject getValue() {			return javaToRuby(_value);		}			public IRubyObject setValue(IRubyObject value) {			_value = rubyToJava(value);			return value;		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -