diff --git a/.gitignore b/.gitignore index 5139e38..2577c55 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ jruby4max.jar out *.iml .idea +.project +.classpath diff --git a/Rakefile b/Rakefile index 9b77b30..9f3c847 100644 --- a/Rakefile +++ b/Rakefile @@ -2,9 +2,9 @@ require 'rake/clean' require 'tempfile' require 'rbconfig' -PROJECT_VERSION = '1.0.2' +PROJECT_VERSION = '1.0.3' BUILD_DATE = Time.now.utc.strftime '%B %d, %Y (%H:%M GMT)' -MANIFEST = +MANIFEST = "Library: JRuby for Max Version: #{PROJECT_VERSION} Built-Date: #{BUILD_DATE} @@ -31,7 +31,7 @@ TESTS = FileList['test/**/*.java'] CLASSPATH = FileList["#{LIB}/**/*.jar"].exclude(/^#{JAR}$/) TESTS_CLASSPATH = CLASSPATH.clone.add(BUILD) -WINDOWS = Config::CONFIG['host_os'] =~ /mswin/ +WINDOWS = RbConfig::CONFIG['host_os'] =~ /mswin/ CLASSPATH_SEPARATOR = if WINDOWS then ';' else ':' end ############################################################################## @@ -60,14 +60,14 @@ task :package => [:jar] do puts "Preparing distribution" package_lib = File.join PACKAGE,'lib' mkdir_p package_lib - + # Collect the files FileList['*.txt', '*.example'].each do |filename| cp filename, PACKAGE end FileList["#{LIB}/jruby.jar", JAR_FILE].each do |filename| cp filename, package_lib - end + end cp_r PATCHES, PACKAGE cp_r LICENSE, PACKAGE end @@ -125,7 +125,7 @@ end def javac classpath, src_files, dst_folder mkdir_p dst_folder - `javac -classpath #{classpath.join CLASSPATH_SEPARATOR} -d #{dst_folder} -g -source 1.5 -target 1.5 #{src_files}` + `javac -classpath #{classpath.join CLASSPATH_SEPARATOR} -d #{dst_folder} -g #{src_files} -Xlint:deprecation` end def jar filename, manifest, dst_folder @@ -144,8 +144,7 @@ class FileList File.open(filename, 'w') do |io| io.write contents end - end + end end end end - diff --git a/jruby_for_max.properties b/jruby_for_max.properties new file mode 100644 index 0000000..8fe9fd0 --- /dev/null +++ b/jruby_for_max.properties @@ -0,0 +1,31 @@ +############################# +# Options for JRuby for Max # +############################# + +# To use: +# - Rename this file to jruby_for_max.properties +# - Put it on the Max file search path +# - Restart Max (and restart again each time you change this file) + + +ruby.loadpaths = +# A semicolon-separated list of absolute paths to append to the default $LOAD_PATH. +# You can use this to add gems or other projects installed elsewhere. +# Default value is nothing. + +jruby.home = /Users/javier/.rvm/rubies/jruby-9.2.7.0 +# If you want to install a lot of gems: instead of adding a loadpath for each one, +# you can install them into a separate jruby installation and set jruby.home to +# run against that jruby instead of the built-in one. +# This variable is not set by default. + +gem.home = /Users/javier/.rvm/gems/jruby-9.2.7.0@global +# GEM locations where to install new gems + +gem.path = /Users/javier/.rvm/gems/jruby-9.2.7.0@global +# GEM locations where to find them to use them + +ruby.initializers = jruby_initialize.rb +# A semicolon-separated list of files. +# These files will be run in the order listed whenever a new jruby context is initialized. +# Default value is jruby_initialize.rb diff --git a/jruby_for_max.properties.example b/jruby_for_max.properties.example index 3fad93f..77b946e 100644 --- a/jruby_for_max.properties.example +++ b/jruby_for_max.properties.example @@ -15,14 +15,18 @@ ruby.loadpaths = # jruby.home = /Users/adam/lib/jruby-1.5.5 -# If you want to install a lot of gems: instead of adding a loadpath for each one, +# If you want to install a lot of gems: instead of adding a loadpath for each one, # you can install them into a separate jruby installation and set jruby.home to # run against that jruby instead of the built-in one. # This variable is not set by default. +# gem.home = /Users/javier/.rvm/gems/jruby-9.2.7.0@global +# GEM locations where to install new gems +# gem.path = /Users/javier/.rvm/gems/jruby-9.2.7.0@global +# GEM locations where to find them to use them ruby.initializers = jruby_initialize.rb -# A semicolon-separated list of files. +# A semicolon-separated list of files. # These files will be run in the order listed whenever a new jruby context is initialized. # Default value is jruby_initialize.rb diff --git a/jruby_for_max/lib/jruby_initialize.rb b/jruby_for_max/lib/jruby_initialize.rb index 97da3ca..7984cf8 100644 --- a/jruby_for_max/lib/jruby_initialize.rb +++ b/jruby_for_max/lib/jruby_initialize.rb @@ -89,7 +89,7 @@ def bang # Sets the tooltips for the inlets of this [mxj jruby] object def inlet_assist(*params) - $max_object.setInletAssist params.to_java(:string) + $max_object.setInletAssist params.to_java(:string) end # Sets the tooltips for the outlets of this [mxj jruby] object @@ -112,7 +112,7 @@ def max_object(namespace=nil) context = $max_object.context id = names[0] end - $max_object_map[context][id] + $max_object_map[context][id] end @@ -121,25 +121,25 @@ def max_object(namespace=nil) # Print the arguments to the Max console separated by newlines def puts(*params) - yield_atoms(*params) {|atom| java.lang.System.out.println(atom)} + yield_atoms(*params) {|atom| $max_object.getSystemOut.println(atom) } nil -end +end # Prints the arguments to the Max console def print(*params) - yield_atoms(*params) {|atom| java.lang.System.out.print(atom)} + yield_atoms(*params) {|atom| $max_object.getSystemOut.print(atom) } nil -end +end # Prints an error message to the Max console def error(*params) - yield_atoms(*params) {|atom| java.lang.System.err.println(atom)} + yield_atoms(*params) {|atom| $max_object.getSystemErr.println(atom) } nil end # Flush the Max console, use after print() def flush - java.lang.System.out.println + $max_object.getSystemOut.println nil end @@ -171,4 +171,3 @@ def yield_atoms( *params, &block ) end end end - diff --git a/lib/jruby.jar b/lib/jruby.jar index b844569..ac344c5 100644 Binary files a/lib/jruby.jar and b/lib/jruby.jar differ diff --git a/lib/max.jar b/lib/max.jar index 70561cb..0e1b8df 100644 Binary files a/lib/max.jar and b/lib/max.jar differ diff --git a/src/jruby4max/maxsupport/JRubyMaxObject.java b/src/jruby4max/maxsupport/JRubyMaxObject.java index 23a8dd2..e38ca05 100644 --- a/src/jruby4max/maxsupport/JRubyMaxObject.java +++ b/src/jruby4max/maxsupport/JRubyMaxObject.java @@ -1,5 +1,7 @@ package jruby4max.maxsupport; +import java.io.PrintStream; + /* Copyright (c) 2008, Adam Murray (adam@compusition.com). All rights reserved. @@ -31,14 +33,11 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING import com.cycling74.max.Executable; import com.cycling74.max.MaxObject; import com.cycling74.max.MaxQelem; + import jruby4max.rubysupport.IdInUseException; import jruby4max.rubysupport.MaxRubyAdapter; -import jruby4max.rubysupport.RubyProperties; import jruby4max.util.Logger; import jruby4max.util.Utils; -import org.jruby.CompatVersion; - -import java.io.PrintStream; /** * Superclass for objects that support Ruby scripting. @@ -51,9 +50,6 @@ public abstract class JRubyMaxObject extends MaxObject implements Logger { protected String id = defaultId(); private boolean autoinit = false; - protected Atom[] rubyVersionValue = Atom.newAtom( new String[]{ RubyProperties.DEFAULT_RUBY_VERSION_STRING } ); - protected CompatVersion rubyVersion = RubyProperties.DEFAULT_RUBY_VERSION; - protected MaxRubyAdapter ruby; protected JRubyMaxObject self = this; @@ -77,7 +73,6 @@ public JRubyMaxObject() { declareAttribute( "context", "getcontext", "context" ); declareAttribute( "id", "getid", "id" ); declareAttribute( "autoinit" ); - declareAttribute( "ruby_version", "getruby_version", "ruby_version" ); } private final MaxQelem getInitializerQelem() { @@ -101,12 +96,12 @@ protected class DefaultRubyInitializer implements Executable { public void execute() { initialized = true; try { - ruby = new MaxRubyAdapter( self, context, id, rubyVersion ); + ruby = new MaxRubyAdapter( self, context, id ); } catch(IdInUseException e) { String availableId = e.getMessage(); error( "id " + id + " not available. Using: " + availableId ); id = availableId; - ruby = new MaxRubyAdapter( self, context, id, rubyVersion ); + ruby = new MaxRubyAdapter( self, context, id ); } if( autoinit ) { ruby.init(); @@ -171,6 +166,14 @@ public void info( String message, boolean force ) { public void err( String message ) { error( this.getClass().getName() + ": " + message ); } + + public PrintStream getSystemOut() { + return System.out; + } + + public PrintStream getSystemErr() { + return System.err; + } public String toString() { return getClass().getName() + "#<" + Integer.toHexString( hashCode() ) + ">"; @@ -221,27 +224,4 @@ public void id( Atom[] params ) { private String defaultId() { return Integer.toHexString( hashCode() ); } - - public Atom[] getruby_version() { - return rubyVersionValue; - } - - // Using Atom[] is annoying but it avoids an annoying warning in the max menu "coerced float to String" when doing @ruby_version 1.9 - public void ruby_version(Atom[] rubyVersionValue) { - if (initialized) { - err("ruby_version cannot be changed. Use @ruby_version when creating the object."); - return; - } - if (rubyVersionValue == null || rubyVersionValue.length < 1) return; - - String rubyVersionString = rubyVersionValue[0].toString(); - // @ruby_version 1.9 comes through as "1.900000" so we have to chop off the trailing zeros - rubyVersionString = rubyVersionString.replaceFirst("(.*?)0+", "$1"); - - CompatVersion rubyVersion = RubyProperties.getRubyVersion(rubyVersionString); - if (rubyVersion != null) { - this.rubyVersion = rubyVersion; - this.rubyVersionValue = Atom.newAtom(new String[]{rubyVersionString}); - } else err("Invalid ruby_version '" + rubyVersionString + "'. Only 1_8 and 1_9 supported."); - } } diff --git a/src/jruby4max/rubysupport/IScriptEvaluator.java b/src/jruby4max/rubysupport/IScriptEvaluator.java index 7fa972f..29e9c84 100644 --- a/src/jruby4max/rubysupport/IScriptEvaluator.java +++ b/src/jruby4max/rubysupport/IScriptEvaluator.java @@ -49,6 +49,6 @@ public interface IScriptEvaluator { void setScriptFile( File scriptFile ); Object eval( CharSequence rubyCode ); - + void exit(); } diff --git a/src/jruby4max/rubysupport/MaxRubyAdapter.java b/src/jruby4max/rubysupport/MaxRubyAdapter.java index 0930f55..d851732 100644 --- a/src/jruby4max/rubysupport/MaxRubyAdapter.java +++ b/src/jruby4max/rubysupport/MaxRubyAdapter.java @@ -1,5 +1,13 @@ package jruby4max.rubysupport; +import java.io.File; +import java.io.IOException; +import java.math.BigInteger; + +import org.jruby.RubyArray; +import org.jruby.RubyHash; +import org.jruby.RubySymbol; + /* Copyright (c) 2008, Adam Murray (adam@compusition.com). All rights reserved. @@ -29,19 +37,12 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING import com.cycling74.max.Atom; import com.cycling74.max.MaxObject; + import jruby4max.maxsupport.Atomizer; import jruby4max.util.GlobalVariableStore; import jruby4max.util.LineBuilder; import jruby4max.util.Logger; import jruby4max.util.Utils; -import org.jruby.CompatVersion; -import org.jruby.RubyArray; -import org.jruby.RubyHash; -import org.jruby.RubySymbol; - -import java.io.File; -import java.io.IOException; -import java.math.BigInteger; /** * The bridge between Max and Ruby. @@ -66,20 +67,17 @@ public class MaxRubyAdapter { private String id; - private CompatVersion rubyVersion; - - public MaxRubyAdapter( MaxObject maxObject, String context, String id, CompatVersion rubyVersion ) { + public MaxRubyAdapter( MaxObject maxObject, String context, String id) { this.maxObject = maxObject; if( maxObject instanceof Logger ) { this.logger = (Logger)maxObject; } this.maxContext = context; this.id = id; - this.rubyVersion = rubyVersion; } private void getEvaluator() { - ruby = ScriptEvaluatorManager.getRubyEvaluator( maxContext, id, maxObject, rubyVersion ); + ruby = ScriptEvaluatorManager.getRubyEvaluator( maxContext, id, maxObject ); ruby.declareGlobal( "max_ruby_adapter", this ); ruby.declareGlobal( "global_variable_store", GlobalVariableStore.getInstance() ); } @@ -117,7 +115,7 @@ public void setId( String id ) { this.id = id; } } - + public void notifyDeleted() { ScriptEvaluatorManager.removeRubyEvaluator( maxObject ); } diff --git a/src/jruby4max/rubysupport/RubyProperties.java b/src/jruby4max/rubysupport/RubyProperties.java index 370580a..90809d9 100644 --- a/src/jruby4max/rubysupport/RubyProperties.java +++ b/src/jruby4max/rubysupport/RubyProperties.java @@ -1,12 +1,17 @@ package jruby4max.rubysupport; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Properties; + /* Copyright (c) 2008, Adam Murray (adam@compusition.com). All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: -1. Redistributions of source code must retain the above copyright notice, +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, @@ -28,13 +33,8 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ import com.cycling74.max.MaxSystem; -import jruby4max.util.Utils; -import org.jruby.CompatVersion; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.Properties; +import jruby4max.util.Utils; /** * Manages all global settings for Ruby evaluation. @@ -56,7 +56,7 @@ public class RubyProperties { try { String propsPath = MaxSystem.locateFile( "jruby_for_max.properties" ); if( propsPath == null ) { - //MaxSystem.error("jruby4max.ruby.properties not found! Maybe jruby4max objects was not installed correctly?"); + MaxSystem.error("jruby_for_max.properties not found! Maybe jruby4max objects was not installed correctly? Created a default instance."); properties = new Properties(); } else { @@ -116,19 +116,6 @@ public static String[] getLoadPaths() { return loadpaths; } - public static CompatVersion getRubyVersion( String version ) { - if( "1.9".equals( version ) ) { - return CompatVersion.RUBY1_9; - } - else if( "1.8".equals( version ) ) { - return CompatVersion.RUBY1_8; - } - else return null; - } - - public static String DEFAULT_RUBY_VERSION_STRING = "1.9"; - public static CompatVersion DEFAULT_RUBY_VERSION = CompatVersion.RUBY1_9; - public static String defaultGemHome() { return Utils.pathToContainingFolder(MaxSystem.locateFile("jruby.jar")) + File.separatorChar + "jruby_gems"; } diff --git a/src/jruby4max/rubysupport/ScriptEvaluator.java b/src/jruby4max/rubysupport/ScriptEvaluator.java index fbba4e8..894e5bf 100644 --- a/src/jruby4max/rubysupport/ScriptEvaluator.java +++ b/src/jruby4max/rubysupport/ScriptEvaluator.java @@ -1,53 +1,21 @@ package jruby4max.rubysupport; -/* -Copyright (c) 2008-2011, Adam Murray (adam@compusition.com). All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, -this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, -this list of conditions and the following disclaimer in the documentation -and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR -CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*/ - -import org.jruby.CompatVersion; -import org.jruby.embed.LocalContextScope; -import org.jruby.embed.LocalVariableBehavior; -import org.jruby.embed.ScriptingContainer; - import java.io.File; import java.util.HashMap; import java.util.Map; +import org.jruby.embed.LocalContextScope; +import org.jruby.embed.LocalVariableBehavior; +import org.jruby.embed.ScriptingContainer; + public class ScriptEvaluator implements IScriptEvaluator { private ScriptingContainer container; - private CompatVersion compatVersion; - private boolean initialized = false; private Map persitentGlobals = new HashMap(); - public ScriptEvaluator( CompatVersion rubyVersion ) { - // if compatVersion isn't set right away, it won't work (maybe because of the global var setup?) - compatVersion = rubyVersion; + public ScriptEvaluator() { resetEngineContext(); } @@ -92,7 +60,6 @@ public void undeclareGlobal( String variableName ) { protected void resetEngineContext() { container = new ScriptingContainer( LocalContextScope.SINGLETHREAD, LocalVariableBehavior.TRANSIENT ); - container.setCompatVersion( compatVersion ); container.setCurrentDirectory( System.getProperty( "user.home" ) ); // It seems we can't directly modify the current environment, so we need to make a copy first. @@ -127,6 +94,9 @@ public Object eval( CharSequence rubyCode ) { public void exit() { container.terminate(); container = null; - + } + + public String version() { + return container.getSupportedRubyVersion(); } } diff --git a/src/jruby4max/rubysupport/ScriptEvaluatorManager.java b/src/jruby4max/rubysupport/ScriptEvaluatorManager.java index b379191..ff21d8f 100644 --- a/src/jruby4max/rubysupport/ScriptEvaluatorManager.java +++ b/src/jruby4max/rubysupport/ScriptEvaluatorManager.java @@ -1,5 +1,9 @@ package jruby4max.rubysupport; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + /* Copyright (c) 2008, Adam Murray (adam@compusition.com). All rights reserved. @@ -28,11 +32,6 @@ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ import jruby4max.util.MappedSet; -import org.jruby.CompatVersion; - -import java.util.HashMap; -import java.util.Map; -import java.util.Set; /** * Factory for Ruby evaluators that manages shared contexts. @@ -81,7 +80,7 @@ private ScriptEvaluatorManager() { * * @return an implementation of IScriptEvaluator */ - public static IScriptEvaluator getRubyEvaluator( String maxContext, String id, Object maxObject, CompatVersion rubyVersion ) { + public static IScriptEvaluator getRubyEvaluator( String maxContext, String id, Object maxObject) { Map idMap = maxObjectMap.get( maxContext ); if( idMap == null ) { @@ -99,7 +98,7 @@ public static IScriptEvaluator getRubyEvaluator( String maxContext, String id, O IScriptEvaluator evaluator = evaluatorContexts.get( evaluatorContext ); if( evaluator == null ) { - evaluator = new ScriptEvaluator( rubyVersion ); + evaluator = new ScriptEvaluator(); evaluatorContexts.put( evaluatorContext, evaluator ); evaluatorContextCounter.put( evaluatorContext, 1 ); Set maxObjects = objectsUsingEvaluator.addValue( evaluatorContext, maxObject ); diff --git a/test/jruby4max/util/MappedSetTest.java b/test/jruby4max/util/MappedSetTest.java index 5421f8a..a3fcdef 100644 --- a/test/jruby4max/util/MappedSetTest.java +++ b/test/jruby4max/util/MappedSetTest.java @@ -4,10 +4,6 @@ import org.junit.Before; import org.junit.Test; -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - public class MappedSetTest { private MappedSet subject;