Skip to content

Commit

Permalink
Use Ruby C API directly
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 13, 2024
1 parent 35bb8d5 commit 8e59241
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 14 deletions.
11 changes: 6 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ name: build
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
- ruby: 3.3
gemfile: gemfiles/rails80.gemfile
- ruby: 3.3
gemfile: Gemfile
os: ubuntu-latest
- ruby: 3.2
gemfile: gemfiles/rails71.gemfile
gemfile: Gemfile
os: macos-latest
- ruby: 3.1
gemfile: gemfiles/rails70.gemfile
gemfile: Gemfile
os: windows-latest
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
steps:
Expand Down
64 changes: 57 additions & 7 deletions ext/field_test/ext.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,66 @@
#include <rice/rice.hpp>
#include <rice/stl.hpp>
#include <ruby.h>

#include "bayestest.hpp"

using bayestest::BinaryTest;

void binary_test_free(void* data)
{
free(data);
}

size_t binary_test_size(const void* data)
{
// does not currently include size of variants
return sizeof(BinaryTest);
}

static const rb_data_type_t binary_test_type = {
.wrap_struct_name = "binary_test",
.function = {
.dmark = NULL,
.dfree = binary_test_free,
.dsize = binary_test_size,
},
.data = NULL,
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
};

VALUE binary_test_alloc(VALUE self)
{
BinaryTest* data = new BinaryTest();

return TypedData_Wrap_Struct(self, &binary_test_type, data);
}

VALUE binary_test_add(VALUE self, VALUE participants, VALUE conversions)
{
BinaryTest* data;
TypedData_Get_Struct(self, BinaryTest, &binary_test_type, data);

data->add(NUM2INT(participants), NUM2INT(conversions));

return Qnil;
}

VALUE binary_test_probabilities(VALUE self)
{
BinaryTest* data;
TypedData_Get_Struct(self, BinaryTest, &binary_test_type, data);

VALUE arr = rb_ary_new();
for (auto &v : data->probabilities()) {
rb_ary_push(arr, DBL2NUM(v));
}
return arr;
}

extern "C"
void Init_ext() {
auto rb_mFieldTest = Rice::define_module("FieldTest");
auto mFieldTest = rb_define_module("FieldTest");

Rice::define_class_under<BinaryTest>(rb_mFieldTest, "BinaryTest")
.define_constructor(Rice::Constructor<BinaryTest>())
.define_method("add", &BinaryTest::add)
.define_method("probabilities", &BinaryTest::probabilities);
auto cBinaryTest = rb_define_class_under(mFieldTest, "BinaryTest", rb_cObject);
rb_define_alloc_func(cBinaryTest, binary_test_alloc);
rb_define_method(cBinaryTest, "add", binary_test_add, 2);
rb_define_method(cBinaryTest, "probabilities", binary_test_probabilities, 0);
}
2 changes: 1 addition & 1 deletion ext/field_test/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "mkmf-rice"
require "mkmf"

$CXXFLAGS << " -std=c++17 $(optflags)"

Expand Down
1 change: 0 additions & 1 deletion field_test.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ Gem::Specification.new do |spec|
spec.add_dependency "railties", ">= 7"
spec.add_dependency "activerecord", ">= 7"
spec.add_dependency "browser", ">= 2"
spec.add_dependency "rice", ">= 4.0.2"
end

0 comments on commit 8e59241

Please sign in to comment.