Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
Rewrote the loading mechanism to use JRuby's BasicLibraryService
Browse files Browse the repository at this point in the history
  • Loading branch information
iconara committed Apr 26, 2012
1 parent a678b08 commit 1f0b418
Show file tree
Hide file tree
Showing 17 changed files with 100 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,4 +1,4 @@
*.gem
.bundle
pkg/*
/benchmark
/benchmark
10 changes: 8 additions & 2 deletions Rakefile
Expand Up @@ -4,11 +4,17 @@ require 'bundler'
Bundler::GemHelper.install_tasks

task :clean do
rm Dir['lib/ext/**/*.class']
rm Dir['ext/java/**/*.class']
end

task :compile do
exec %(javac -source 1.6 -target 1.6 -cp lib/ext/msgpack-0.6.5-SNAPSHOT.jar:$MY_RUBY_HOME/lib/jruby.jar -d lib/ext ext/java/org/msgpack/**/*.java)
classpath = (Dir["lib/ext/*.jar"] + ["#{ENV['MY_RUBY_HOME']}/lib/jruby.jar"]).join(':')
system %(javac -Xlint:-options -source 1.6 -target 1.6 -cp #{classpath} ext/java/*.java ext/java/org/msgpack/jruby/*.java)
end

task :package => :compile do
class_files = Dir['ext/java/**/*.class'].map { |path| path = path.sub('ext/java/', ''); "-C ext/java '#{path}'" }
system %(jar cf lib/ext/msgpack_jruby.jar #{class_files.join(' ')})
end

namespace :benchmark do
Expand Down
1 change: 1 addition & 0 deletions ext/.gitignore
@@ -0,0 +1 @@
*.class
15 changes: 15 additions & 0 deletions ext/java/MsgpackJrubyService.java
@@ -0,0 +1,15 @@
import java.io.IOException;

import org.jruby.Ruby;
import org.jruby.runtime.load.BasicLibraryService;

import org.msgpack.jruby.MessagePackLibrary;


public class MsgpackJrubyService implements BasicLibraryService {
public boolean basicLoad(final Ruby runtime) throws IOException {
new MessagePackLibrary().load(runtime, false);
return true;
}
}

68 changes: 68 additions & 0 deletions ext/java/org/msgpack/jruby/MessagePackLibrary.java
@@ -0,0 +1,68 @@
package org.msgpack.jruby;


import java.io.IOException;

import org.jruby.Ruby;
import org.jruby.RubyModule;
import org.jruby.RubyClass;
import org.jruby.RubyString;
import org.jruby.runtime.load.Library;
import org.jruby.runtime.callback.Callback;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;

import org.msgpack.MessagePack;
import org.msgpack.packer.BufferPacker;
import org.msgpack.packer.Packer;


public class MessagePackLibrary implements Library {
public void load(Ruby runtime, boolean wrap) throws IOException {
MessagePack msgPack = new MessagePack();
RubyModule msgpackModule = runtime.defineModule("MessagePack");
msgpackModule.defineModuleFunction("pack", new PackCallback(msgPack));
msgpackModule.defineModuleFunction("unpack", new UnpackCallback(msgPack));
}

private static class PackCallback implements Callback {
private MessagePack msgPack;

public PackCallback(MessagePack msgPack) {
this.msgPack = msgPack;
}

public IRubyObject execute(IRubyObject recv, IRubyObject[] args, Block block) {
try {
BufferPacker bufferedPacker = msgPack.createBufferPacker();
Packer packer = new RubyObjectPacker(msgPack, bufferedPacker).write(args[0]);
return RubyString.newString(recv.getRuntime(), bufferedPacker.toByteArray());
} catch (IOException ioe) {
// TODO: how to propagate these to the Ruby runtime?
return recv.getRuntime().getNil();
}
}

public Arity getArity() { return Arity.ONE_REQUIRED; }
}

private static class UnpackCallback implements Callback {
private RubyObjectUnpacker unpacker;

public UnpackCallback(MessagePack msgPack) {
this.unpacker = new RubyObjectUnpacker(msgPack);
}

public IRubyObject execute(IRubyObject recv, IRubyObject[] args, Block block) {
try {
return unpacker.unpack((RubyString) args[0]);
} catch (IOException ioe) {
// TODO: how to propagate these to the Ruby runtime?
return recv.getRuntime().getNil();
}
}

public Arity getArity() { return Arity.ONE_REQUIRED; }
}
}
37 changes: 0 additions & 37 deletions ext/java/org/msgpack/jruby/MessagePackModule.java

This file was deleted.

3 changes: 2 additions & 1 deletion ext/java/org/msgpack/jruby/RubyObjectPacker.java
Expand Up @@ -18,6 +18,7 @@
import org.jruby.RubySymbol;
import org.jruby.RubyArray;
import org.jruby.RubyHash;
import org.jruby.runtime.builtin.IRubyObject;


public class RubyObjectPacker extends MessagePackBufferPacker {
Expand All @@ -28,7 +29,7 @@ public RubyObjectPacker(MessagePack msgPack, Packer packer) {
this.packer = packer;
}

public Packer write(RubyObject o) throws IOException {
public Packer write(IRubyObject o) throws IOException {
if (o == null || o instanceof RubyNil) {
packer.writeNil();
return this;
Expand Down
Binary file added lib/ext/msgpack_jruby.jar
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed lib/ext/org/msgpack/jruby/MessagePackModule.class
Binary file not shown.
Binary file removed lib/ext/org/msgpack/jruby/RubyObjectPacker.class
Binary file not shown.
Binary file removed lib/ext/org/msgpack/jruby/RubyObjectUnpacker$1.class
Binary file not shown.
Binary file removed lib/ext/org/msgpack/jruby/RubyObjectUnpacker.class
Binary file not shown.
10 changes: 5 additions & 5 deletions lib/msgpack.rb
@@ -1,8 +1,8 @@
# encoding: utf-8

require 'java'

$CLASSPATH << File.expand_path('../ext', __FILE__)
Dir[File.expand_path('../ext/*.jar', __FILE__)].each { |path| $CLASSPATH << path }
$: << File.expand_path('../ext', __FILE__)

MessagePack = org.msgpack.jruby.MessagePackModule
require 'java'
require 'javassist-3.15.0-GA'
require 'msgpack-0.6.5-SNAPSHOT'
require 'msgpack_jruby'
32 changes: 0 additions & 32 deletions lib/msgpack/unpacker.rb

This file was deleted.

32 changes: 0 additions & 32 deletions spec/msgpack/unpacker_spec.rb

This file was deleted.

0 comments on commit 1f0b418

Please sign in to comment.