direct-bind 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 823f939b4c23d80452da0ac8eabfe556112d7002b646faac513db3c18f10eab0
4
+ data.tar.gz: d06d1b0a80243307520649694b8aa468946e72fb317a381a11e37093714667e1
5
+ SHA512:
6
+ metadata.gz: 5f480cf13dee00200a52dd648a8074895f204670fb1d066eaf7f514f2b9b93950bef9acf3bf74ec7a504ab80828b28ba6a18f5f785472a4f2c21b247bc704074
7
+ data.tar.gz: 365cf7562c83507b87c423baf5c1a3c5b95719fc1d4df2acf049b8ad1b05e1c9c5d02ca62c85c7547f73a8c417a3a71689c3b7e66ba0978c4004e42bcb7708d0
@@ -0,0 +1,134 @@
1
+
2
+ = Contributor Covenant Code of Conduct
3
+
4
+ == Our Pledge
5
+
6
+ We as members, contributors, and leaders pledge to make participation in our
7
+ community a harassment-free experience for everyone, regardless of age, body
8
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
9
+ identity and expression, level of experience, education, socio-economic status,
10
+ nationality, personal appearance, race, caste, color, religion, or sexual
11
+ identity and orientation.
12
+
13
+ We pledge to act and interact in ways that contribute to an open, welcoming,
14
+ diverse, inclusive, and healthy community.
15
+
16
+ == Our Standards
17
+
18
+ Examples of behavior that contributes to a positive environment for our
19
+ community include:
20
+
21
+ * Demonstrating empathy and kindness toward other people
22
+ * Being respectful of differing opinions, viewpoints, and experiences
23
+ * Giving and gracefully accepting constructive feedback
24
+ * Accepting responsibility and apologizing to those affected by our mistakes,
25
+ and learning from the experience
26
+ * Focusing on what is best not just for us as individuals, but for the overall
27
+ community
28
+
29
+ Examples of unacceptable behavior include:
30
+
31
+ * The use of sexualized language or imagery, and sexual attention or advances of
32
+ any kind
33
+ * Trolling, insulting or derogatory comments, and personal or political attacks
34
+ * Public or private harassment
35
+ * Publishing others' private information, such as a physical or email address,
36
+ without their explicit permission
37
+ * Other conduct which could reasonably be considered inappropriate in a
38
+ professional setting
39
+
40
+ == Enforcement Responsibilities
41
+
42
+ Community leaders are responsible for clarifying and enforcing our standards of
43
+ acceptable behavior and will take appropriate and fair corrective action in
44
+ response to any behavior that they deem inappropriate, threatening, offensive,
45
+ or harmful.
46
+
47
+ Community leaders have the right and responsibility to remove, edit, or reject
48
+ comments, commits, code, wiki edits, issues, and other contributions that are
49
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
50
+ decisions when appropriate.
51
+
52
+ == Scope
53
+
54
+ This Code of Conduct applies within all community spaces, and also applies when
55
+ an individual is officially representing the community in public spaces.
56
+ Examples of representing our community include using an official e-mail address,
57
+ posting via an official social media account, or acting as an appointed
58
+ representative at an online or offline event.
59
+
60
+ == Enforcement
61
+
62
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
63
+ reported to the community leaders responsible for enforcement at
64
+ ivo@ivoanjo.me.
65
+ All complaints will be reviewed and investigated promptly and fairly.
66
+
67
+ All community leaders are obligated to respect the privacy and security of the
68
+ reporter of any incident.
69
+
70
+ == Enforcement Guidelines
71
+
72
+ Community leaders will follow these Community Impact Guidelines in determining
73
+ the consequences for any action they deem in violation of this Code of Conduct:
74
+
75
+ === 1. Correction
76
+
77
+ **Community Impact**: Use of inappropriate language or other behavior deemed
78
+ unprofessional or unwelcome in the community.
79
+
80
+ **Consequence**: A private, written warning from community leaders, providing
81
+ clarity around the nature of the violation and an explanation of why the
82
+ behavior was inappropriate. A public apology may be requested.
83
+
84
+ === 2. Warning
85
+
86
+ **Community Impact**: A violation through a single incident or series of
87
+ actions.
88
+
89
+ **Consequence**: A warning with consequences for continued behavior. No
90
+ interaction with the people involved, including unsolicited interaction with
91
+ those enforcing the Code of Conduct, for a specified period of time. This
92
+ includes avoiding interactions in community spaces as well as external channels
93
+ like social media. Violating these terms may lead to a temporary or permanent
94
+ ban.
95
+
96
+ === 3. Temporary Ban
97
+
98
+ **Community Impact**: A serious violation of community standards, including
99
+ sustained inappropriate behavior.
100
+
101
+ **Consequence**: A temporary ban from any sort of interaction or public
102
+ communication with the community for a specified period of time. No public or
103
+ private interaction with the people involved, including unsolicited interaction
104
+ with those enforcing the Code of Conduct, is allowed during this period.
105
+ Violating these terms may lead to a permanent ban.
106
+
107
+ === 4. Permanent Ban
108
+
109
+ **Community Impact**: Demonstrating a pattern of violation of community
110
+ standards, including sustained inappropriate behavior, harassment of an
111
+ individual, or aggression toward or disparagement of classes of individuals.
112
+
113
+ **Consequence**: A permanent ban from any sort of public interaction within the
114
+ community.
115
+
116
+ == Attribution
117
+
118
+ This Code of Conduct is adapted from the Contributor Covenant,
119
+ version 2.1, available at
120
+ https://www.contributor-covenant.org/version/2/1/code_of_conduct.html.
121
+
122
+ Community Impact Guidelines were inspired by
123
+ Mozilla's code of conduct enforcement ladder.
124
+
125
+ For answers to common questions about this code of conduct, see the FAQ at
126
+ https://www.contributor-covenant.org/faq. Translations are available at
127
+ https://www.contributor-covenant.org/translations.
128
+
129
+
130
+
131
+
132
+
133
+
134
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ivo Anjo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.adoc ADDED
@@ -0,0 +1,54 @@
1
+ = `direct-bind`
2
+ :toc:
3
+ :toc-placement: macro
4
+ :toclevels: 4
5
+ :toc-title:
6
+
7
+ A ruby gem for getting direct access to function pointers. (TODO)
8
+
9
+ == Quickest start
10
+
11
+
12
+ == Installation
13
+
14
+ Install the gem and add to the application's `Gemfile` or `gems.rb` file by executing:
15
+
16
+ [source,bash]
17
+ ----
18
+ $ bundle add direct-bind
19
+ ----
20
+
21
+ If bundler is not being used to manage dependencies, install the gem by executing:
22
+
23
+ [source,bash]
24
+ ----
25
+ $ gem install direct-bind
26
+ ----
27
+
28
+ == Usage
29
+
30
+ Use `require "direct-bind"` to load the gem.
31
+
32
+ == Requirements
33
+
34
+ TODO
35
+
36
+ == Development
37
+
38
+ To use the gem locally, remember to run `bundle exec rake compile` to build the native code before you can use it. Afterwards, you can run the examples directly from the tree by running `bundle exec ruby [some example]`.
39
+
40
+ When adding new files to `ext/direct_bind_native_extension` you need to run `bundle exec rake clean compile`. This will ensure the new files get picked up and compiled correctly.
41
+
42
+ === Installation and deployment
43
+
44
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to https://rubygems.org[rubygems.org]. To run specs, run `bundle exec rake spec`.
45
+
46
+ To run all actions (build the extension, check linting, and run specs), run `bundle exec rake`.
47
+
48
+ == Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ivoanjo/direct-bind. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the https://github.com/ivoanjo/direct-bind/blob/master/CODE_OF_CONDUCT.adoc[code of conduct].
51
+
52
+ == Code of Conduct
53
+
54
+ Everyone interacting in the direct-bind project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the https://github.com/ivoanjo/direct-bind/blob/master/CODE_OF_CONDUCT.adoc[code of conduct].
@@ -0,0 +1,51 @@
1
+ # direct-bind: Ruby gem for getting direct access to function pointers
2
+ # Copyright (c) 2025 Ivo Anjo <ivo@ivoanjo.me>
3
+ #
4
+ # This file is part of direct-bind.
5
+ #
6
+ # MIT License
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files (the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be included in all
16
+ # copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ # SOFTWARE.
25
+
26
+ # frozen_string_literal: true
27
+
28
+ require_relative "lib/direct_bind/version"
29
+
30
+ Gem::Specification.new do |spec|
31
+ spec.name = "direct-bind"
32
+ spec.version = DirectBind::VERSION
33
+ spec.authors = ["Ivo Anjo"]
34
+ spec.email = ["ivo@ivoanjo.me"]
35
+
36
+ spec.summary = "Ruby gem for getting direct access to function pointers"
37
+ spec.homepage = "https://github.com/ivoanjo/direct-bind"
38
+ spec.license = "MIT"
39
+ spec.required_ruby_version = ">= 2.5.0"
40
+
41
+ # Specify which files should be added to the gem when it is released.
42
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
43
+ spec.files = Dir.chdir(__dir__) do
44
+ `git ls-files -z`.split("\x0").reject do |f|
45
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features|examples)/|\.(?:git|travis|circleci)|appveyor)}) ||
46
+ [".editorconfig", ".ruby-version", ".standard.yml", "gems.rb", "Rakefile"].include?(f)
47
+ end
48
+ end
49
+ spec.require_paths = ["lib", "ext"]
50
+ spec.extensions = ["ext/direct_bind_native_extension/extconf.rb"]
51
+ end
@@ -0,0 +1,148 @@
1
+ // direct-bind: Ruby gem for getting direct access to function pointers
2
+ // Copyright (c) 2025 Ivo Anjo <ivo@ivoanjo.me>
3
+ //
4
+ // This file is part of direct-bind.
5
+ //
6
+ // MIT License
7
+ //
8
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ // of this software and associated documentation files (the "Software"), to deal
10
+ // in the Software without restriction, including without limitation the rights
11
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ // copies of the Software, and to permit persons to whom the Software is
13
+ // furnished to do so, subject to the following conditions:
14
+ //
15
+ // The above copyright notice and this permission notice shall be included in all
16
+ // copies or substantial portions of the Software.
17
+ //
18
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ // SOFTWARE.
25
+
26
+ #include "direct_bind.h"
27
+
28
+ // # Self-test implementation
29
+
30
+ #define SELF_TEST_ARITY 3
31
+
32
+ static VALUE self_test_target_func(
33
+ __attribute__((unused)) VALUE _1,
34
+ __attribute__((unused)) VALUE _2,
35
+ __attribute__((unused)) VALUE _3,
36
+ __attribute__((unused)) VALUE _4
37
+ ) {
38
+ return Qnil;
39
+ }
40
+
41
+ bool direct_bind_self_test(bool raise_on_failure) {
42
+ VALUE anonymous_module = rb_module_new();
43
+ rb_define_method(anonymous_module, "direct_bind_self_test_target", self_test_target_func, SELF_TEST_ARITY);
44
+
45
+ ID self_test_id = rb_intern("direct_bind_self_test_target");
46
+ direct_bind_cfunc_result test_target = direct_bind_get_cfunc(anonymous_module, self_test_id, raise_on_failure);
47
+
48
+ if (!test_target.ok) return false;
49
+
50
+ return test_target.arity == SELF_TEST_ARITY && test_target.func == self_test_target_func;
51
+ }
52
+
53
+ // # Structure layouts and exported symbol definitions from Ruby
54
+
55
+ // ## From internal/gc.h
56
+ void rb_objspace_each_objects(int (*callback)(void *start, void *end, size_t stride, void *data), void *data);
57
+ int rb_objspace_internal_object_p(VALUE obj);
58
+
59
+ // ## From method.h
60
+ typedef struct rb_method_entry_struct {
61
+ VALUE flags;
62
+ VALUE defined_class;
63
+ struct rb_method_definition_struct * const def;
64
+ ID called_id;
65
+ VALUE owner;
66
+ } rb_method_entry_t;
67
+
68
+ // ### This was simplified/inlined vs the original structure
69
+ struct rb_method_definition_struct {
70
+ unsigned int type: 4;
71
+ int _ignored;
72
+ struct {
73
+ VALUE (*func)(ANYARGS);
74
+ void *_ignored;
75
+ int argc;
76
+ } cfunc;
77
+ };
78
+
79
+ // # This is where the magic happens: Using objectspace to find the method entry and retrieve the cfunc
80
+
81
+ typedef struct {
82
+ VALUE target_klass;
83
+ ID target_id;
84
+ direct_bind_cfunc_result result;
85
+ } find_data_t;
86
+
87
+ static bool valid_method_entry(VALUE object);
88
+ static bool found_target_method_entry(rb_method_entry_t *method_entry, find_data_t *find_data);
89
+ static int find_cfunc(void *start, void *end, size_t stride, void *data);
90
+
91
+ direct_bind_cfunc_result direct_bind_get_cfunc(VALUE klass, ID method_name, bool raise_on_failure) {
92
+ VALUE definition_not_found = rb_sprintf("method %"PRIsVALUE".%"PRIsVALUE" not found", klass, ID2SYM(method_name));
93
+
94
+ find_data_t find_data = {.target_klass = klass, .target_id = method_name, .result = {.ok = false, .failure_reason = definition_not_found}};
95
+ rb_objspace_each_objects(find_cfunc, &find_data);
96
+
97
+ if (raise_on_failure && find_data.result.ok == false) {
98
+ rb_raise(rb_eRuntimeError, "direct_bind_get_cfunc failed: %"PRIsVALUE, find_data.result.failure_reason);
99
+ }
100
+
101
+ return find_data.result;
102
+ }
103
+
104
+ // TODO: Maybe change this to use safe memory reads that can never segv (e.g. if structure layouts are off?)
105
+ static int find_cfunc(void *start, void *end, size_t stride, void *data) {
106
+ const int stop_iteration = 1;
107
+ const int continue_iteration = 0;
108
+ const int vm_method_type_cfunc = 1;
109
+
110
+ find_data_t *find_data = (find_data_t *) data;
111
+
112
+ for (VALUE v = (VALUE) start; v != (VALUE) end; v += stride) {
113
+ if (!valid_method_entry(v)) continue;
114
+
115
+ rb_method_entry_t *method_entry = (rb_method_entry_t*) v;
116
+ if (!found_target_method_entry(method_entry, find_data)) continue;
117
+
118
+ if (method_entry->def == NULL) {
119
+ find_data->result.failure_reason = rb_str_new_lit("method_entry->def is NULL");
120
+ } else if (method_entry->def->type != vm_method_type_cfunc) {
121
+ find_data->result.failure_reason = rb_str_new_lit("method_entry is not a cfunc");
122
+ } else {
123
+ find_data->result = (direct_bind_cfunc_result) {
124
+ .ok = true,
125
+ .failure_reason = Qnil,
126
+ .arity = method_entry->def->cfunc.argc,
127
+ .func = method_entry->def->cfunc.func,
128
+ };
129
+ }
130
+ return stop_iteration;
131
+ }
132
+
133
+ return continue_iteration;
134
+ }
135
+
136
+ static bool is_method_entry(VALUE imemo) {
137
+ const unsigned long method_entry_id = 6;
138
+ return ((RBASIC(imemo)->flags >> FL_USHIFT) & method_entry_id) == method_entry_id;
139
+ }
140
+
141
+ static bool valid_method_entry(VALUE object) {
142
+ return rb_objspace_internal_object_p(object) && RB_BUILTIN_TYPE(object) == RUBY_T_IMEMO && RB_TYPE_P(object, RUBY_T_IMEMO) && is_method_entry(object);
143
+ }
144
+
145
+ static bool found_target_method_entry(rb_method_entry_t *method_entry, find_data_t *find_data) {
146
+ VALUE method_klass = method_entry->defined_class ? method_entry->defined_class : method_entry->owner;
147
+ return method_klass == find_data->target_klass && method_entry->called_id == find_data->target_id;
148
+ }
@@ -0,0 +1,46 @@
1
+ // direct-bind: Ruby gem for getting direct access to function pointers
2
+ // Copyright (c) 2025 Ivo Anjo <ivo@ivoanjo.me>
3
+ //
4
+ // This file is part of direct-bind.
5
+ //
6
+ // MIT License
7
+ //
8
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ // of this software and associated documentation files (the "Software"), to deal
10
+ // in the Software without restriction, including without limitation the rights
11
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ // copies of the Software, and to permit persons to whom the Software is
13
+ // furnished to do so, subject to the following conditions:
14
+ //
15
+ // The above copyright notice and this permission notice shall be included in all
16
+ // copies or substantial portions of the Software.
17
+ //
18
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ // SOFTWARE.
25
+
26
+ #pragma once
27
+
28
+ #include <stdbool.h>
29
+ #include <ruby.h>
30
+
31
+ typedef struct {
32
+ bool ok;
33
+ VALUE failure_reason;
34
+ int arity;
35
+ VALUE (*func)(ANYARGS);
36
+ } direct_bind_cfunc_result;
37
+
38
+ // Recommended to call once during your gem's initialization, to validate that direct-bind's Ruby hacking is in good shape.
39
+ bool direct_bind_self_test(bool raise_on_failure);
40
+
41
+ // Provides the reverse of `rb_define_method`: Given a class and a method_name, retrieves the arity and func previously
42
+ // passed to `rb_define_method`.
43
+ //
44
+ // Performance note: As of this writing, this method scans objspace to find the definition of the method, so you
45
+ // most probably want to cache its result, rather than calling it very often.
46
+ direct_bind_cfunc_result direct_bind_get_cfunc(VALUE klass, ID method_name, bool raise_on_failure);
@@ -0,0 +1,41 @@
1
+ // direct-bind: Ruby gem for getting direct access to function pointers
2
+ // Copyright (c) 2025 Ivo Anjo <ivo@ivoanjo.me>
3
+ //
4
+ // This file is part of direct-bind.
5
+ //
6
+ // MIT License
7
+ //
8
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ // of this software and associated documentation files (the "Software"), to deal
10
+ // in the Software without restriction, including without limitation the rights
11
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ // copies of the Software, and to permit persons to whom the Software is
13
+ // furnished to do so, subject to the following conditions:
14
+ //
15
+ // The above copyright notice and this permission notice shall be included in all
16
+ // copies or substantial portions of the Software.
17
+ //
18
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ // SOFTWARE.
25
+
26
+ #include "direct_bind.h"
27
+
28
+ VALUE direct_bind_call(VALUE _self, VALUE klass, VALUE method, VALUE instance);
29
+
30
+ void Init_direct_bind_native_extension(void) {
31
+ direct_bind_self_test(true);
32
+
33
+ VALUE direct_bind_module = rb_define_module("DirectBind");
34
+ rb_define_singleton_method(direct_bind_module, "call", direct_bind_call, 3);
35
+ }
36
+
37
+ VALUE direct_bind_call(__attribute__((unused)) VALUE _self, VALUE klass, VALUE method, VALUE instance) {
38
+ direct_bind_cfunc_result result = direct_bind_get_cfunc(klass, SYM2ID(method), true);
39
+ if (result.arity != 0) rb_raise(rb_eArgError, "Unexpected arity on cfunc: %d", result.arity);
40
+ return ((VALUE (*)(VALUE)) result.func)(instance);
41
+ }
@@ -0,0 +1,46 @@
1
+ # direct-bind: Ruby gem for getting direct access to function pointers
2
+ # Copyright (c) 2025 Ivo Anjo <ivo@ivoanjo.me>
3
+ #
4
+ # This file is part of direct-bind.
5
+ #
6
+ # MIT License
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files (the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be included in all
16
+ # copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ # SOFTWARE.
25
+
26
+ if ["jruby", "truffleruby"].include?(RUBY_ENGINE)
27
+ raise \
28
+ "\n#{"-" * 80}\nSorry! This gem is unsupported on #{RUBY_ENGINE}. Since it relies on a lot of guts of MRI Ruby, " \
29
+ "it's impossible to make a direct port.\n" \
30
+ "Perhaps a #{RUBY_ENGINE} equivalent could be created -- help is welcome! :)\n#{"-" * 80}"
31
+ end
32
+
33
+ require "mkmf"
34
+
35
+ append_cflags("-std=gnu99")
36
+ append_cflags("-Wno-declaration-after-statement")
37
+ append_cflags("-Wno-compound-token-split-by-macro")
38
+ append_cflags("-Werror-implicit-function-declaration")
39
+ append_cflags("-Wunused-parameter")
40
+ append_cflags("-Wold-style-definition")
41
+ append_cflags("-Wall")
42
+ append_cflags("-Wextra")
43
+ append_cflags("-Werror") if ENV["ENABLE_WERROR"] == "true"
44
+
45
+ create_header
46
+ create_makefile "direct_bind_native_extension"
@@ -0,0 +1,33 @@
1
+ # direct-bind: Ruby gem for getting direct access to function pointers
2
+ # Copyright (c) 2025 Ivo Anjo <ivo@ivoanjo.me>
3
+ #
4
+ # This file is part of direct-bind.
5
+ #
6
+ # MIT License
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files (the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be included in all
16
+ # copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ # SOFTWARE.
25
+
26
+ # frozen_string_literal: true
27
+
28
+ require_relative "direct_bind/version"
29
+
30
+ require "direct_bind_native_extension"
31
+
32
+ module DirectBind
33
+ end
@@ -0,0 +1,30 @@
1
+ # direct-bind: Ruby gem for getting direct access to function pointers
2
+ # Copyright (c) 2025 Ivo Anjo <ivo@ivoanjo.me>
3
+ #
4
+ # This file is part of direct-bind.
5
+ #
6
+ # MIT License
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files (the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions:
14
+ #
15
+ # The above copyright notice and this permission notice shall be included in all
16
+ # copies or substantial portions of the Software.
17
+ #
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ # SOFTWARE.
25
+
26
+ # frozen_string_literal: true
27
+
28
+ module DirectBind
29
+ VERSION = "0.1.0"
30
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: direct-bind
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivo Anjo
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ email:
13
+ - ivo@ivoanjo.me
14
+ executables: []
15
+ extensions:
16
+ - ext/direct_bind_native_extension/extconf.rb
17
+ extra_rdoc_files: []
18
+ files:
19
+ - CODE_OF_CONDUCT.adoc
20
+ - LICENSE
21
+ - README.adoc
22
+ - direct-bind.gemspec
23
+ - ext/direct_bind_native_extension/direct_bind.c
24
+ - ext/direct_bind_native_extension/direct_bind.h
25
+ - ext/direct_bind_native_extension/direct_bind_native_extension.c
26
+ - ext/direct_bind_native_extension/extconf.rb
27
+ - lib/direct-bind.rb
28
+ - lib/direct_bind/version.rb
29
+ homepage: https://github.com/ivoanjo/direct-bind
30
+ licenses:
31
+ - MIT
32
+ metadata: {}
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ - ext
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 2.5.0
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.6.7
49
+ specification_version: 4
50
+ summary: Ruby gem for getting direct access to function pointers
51
+ test_files: []