Skip to content

Commit

Permalink
ruby: re-do ruby extension
Browse files Browse the repository at this point in the history
Don't use SWIG, the project is easy enough.

Signed-off-by: Pierre-Alexandre Meyer <pierre@mouraf.org>
pierre committed Dec 7, 2011
1 parent cb804ea commit 42970a6
Showing 9 changed files with 99 additions and 48 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -42,8 +42,7 @@ test.sh
unsmile.dSYM/
php/libsmile.php
php/libsmile_wrap.c
ruby/build/Makefile
ruby/build/libsmile_wrap.c
ruby/Makefile
*.swp
php/Makefile.fragments
php/Makefile.global
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ clean: clean-php
-o -name $(UNSMILE) -o -name `basename $(LIBSMILE)` \) -type f -print | xargs rm -f && \
rm -rf $(UNSMILE).dSYM $(CURDIR)/.libs


#################
# PHP Extension #
#################
@@ -59,3 +60,27 @@ clean-php:
@cd $(PHP_DIR) && \
phpize --clean && \
rm -f config.h.in~


##################
# Ruby Extension #
##################

RUBY_LIBSMILE = $(RUBY_DIR)/libsmile.bundle

ruby: $(RUBY_LIBSMILE)

$(RUBY_LIBSMILE): $(LIBSMILE)
@cd $(RUBY_DIR) && \
$(RUBY) extconf.rb &&
$(MAKE)

check-ruby:
$(RUBY) $(TEST_DIR)/test.rb

install-ruby:

clean-ruby:
@cd $(RUBY_DIR) && \
$(MAKE) clean && \
rm -f Makefile
9 changes: 5 additions & 4 deletions libsmile.mk
Original file line number Diff line number Diff line change
@@ -41,10 +41,11 @@ LDFLAGS += -g
PHP = php
RUBY = ruby

API_DIR = $(CURDIR)/api
LIB_DIR = $(CURDIR)/lib
TEST_DIR = $(CURDIR)/test
PHP_DIR = $(CURDIR)/php
API_DIR = $(CURDIR)/api
LIB_DIR = $(CURDIR)/lib
TEST_DIR = $(CURDIR)/test
PHP_DIR = $(CURDIR)/php
RUBY_DIR = $(CURDIR)/ruby

# Common stanza to make gcc generate .*.d dependency files
MAKEDEPS = -Wp,-MT,$@,-MD,$(dir $@).$(notdir $@).d
19 changes: 0 additions & 19 deletions ruby/Makefile

This file was deleted.

11 changes: 0 additions & 11 deletions ruby/build/libsmile.i

This file was deleted.

3 changes: 0 additions & 3 deletions ruby/build/rubyio.h

This file was deleted.

5 changes: 1 addition & 4 deletions ruby/build/extconf.rb → ruby/extconf.rb
Original file line number Diff line number Diff line change
@@ -11,10 +11,7 @@
end
$LDFLAGS << ' -lsmile'

dir_config(extension_name, "../../include", "../../")
dir_config(extension_name, "../include", "../.libs")
find_header("smile.h")
find_library("libsmile", "smile_decode_block_init")
find_library("libsmile", "smile_decode_block")
find_library("libsmile", "smile_decode_block_exit")

create_makefile(extension_name)
37 changes: 37 additions & 0 deletions ruby/ruby_libsmile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "ruby.h"

#include "smile.h"

static VALUE rb_mSmile;
static VALUE rb_cParser;

static VALUE libsmile_init()
{
smile_decode_block_init();
return Qnil;
}

#define BUFFER_SIZE 65536

static VALUE libsmile_decode(VALUE self, VALUE input)
{
char result[BUFFER_SIZE] = {'\0'};

Check_Type(input, T_STRING);

// Reset for successive calls
smile_decode_block_reset();

smile_decode_block(result, BUFFER_SIZE, RSTRING_PTR(input), RSTRING_LEN(input));

return rb_str_new2(result);
}

void Init_libsmile()
{
rb_mSmile = rb_define_module("Smile");
rb_cParser = rb_define_class_under(rb_mSmile, "Parser", rb_cObject);

rb_define_method(rb_cParser, "initialize", libsmile_init, 0);
rb_define_method(rb_cParser, "decode", libsmile_decode, 1);
}
35 changes: 30 additions & 5 deletions test/test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
require '../ruby/build/libsmile'
require_relative '../ruby/libsmile'

smile_file = '../data/smile/test1.smile'
@parser = Smile::Parser.new()

smile = open(smile_file, "rb") { |io| io.read }
def read_file(path)
return open(path, "rb") { |io| io.read }
end

puts Libsmile.smile_decode_block_init
puts Libsmile.smile_decode_block_exit
def test_one_file(json_filename, smile_filename)
puts "*** Ruby *** Testing #{smile_filename}\n";

data_dir = "#{File.dirname(__FILE__)}/../data"

smile_file = "#{data_dir}/smile/#{smile_filename}"
smile = read_file(smile_file)
actual = @parser.decode(smile)

json_file = "#{data_dir}/json/#{json_filename}"
expected = read_file(json_file)

unless expected.eql? actual
puts("Expected: #{expected}");
puts("Actual: #{actual}");
exit(1);
end
end

files = ['json-org-sample1', 'json-org-sample2', 'json-org-sample3', 'json-org-sample4', 'json-org-sample5',
'numbers-int-4k', 'numbers-int-64k', 'test1', 'test2'];

files.each do |file|
test_one_file "#{file}.jsn", "#{file}.smile"
end

0 comments on commit 42970a6

Please sign in to comment.