Skip to content

Commit 5e20e74

Browse files
authored
Merge pull request #90 from sue445/rb_eval_string
Add RbEvalString
2 parents 53e9b28 + f9e0fb1 commit 5e20e74

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ ref. https://docs.ruby-lang.org/ja/latest/function/index.html
227227
* [ ] `rb_equal`
228228
* [ ] `rb_eval`
229229
* [ ] `rb_eval_cmd`
230-
* [ ] `rb_eval_string`
230+
* [x] `rb_eval_string`
231231
* [ ] `rb_eval_string_protect`
232232
* [ ] `rb_eval_string_wrap`
233233
* [ ] `rb_exc_fatal`

ruby-internal-eval.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,15 @@ func RbFuncallvPublic(recv VALUE, mid ID, argc int, argv []VALUE) VALUE {
3434
func RbFuncall3(recv VALUE, mid ID, argc int, argv []VALUE) VALUE {
3535
return RbFuncallvPublic(recv, mid, argc, argv)
3636
}
37+
38+
// RbEvalString calls `rb_eval_string` in C
39+
//
40+
// Original definition is following
41+
//
42+
// VALUE rb_eval_string(const char *str)
43+
func RbEvalString(str string) VALUE {
44+
char, clean := string2Char(str)
45+
defer clean()
46+
47+
return VALUE(C.rb_eval_string(char))
48+
}

testdata/dummy/ext/dummy/tests.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ VALUE rb_dummy_tests_rb_const_get_at(VALUE self, VALUE name);
1717
void rb_dummy_tests_rb_const_set(VALUE self, VALUE name, VALUE val);
1818
VALUE rb_dummy_tests_rb_const_defined(VALUE self, VALUE name);
1919
VALUE rb_dummy_tests_rb_const_defined_at(VALUE self, VALUE name);
20+
VALUE rb_dummy_tests_rb_eval_string(VALUE self, VALUE str);
2021
*/
2122
import "C"
2223

@@ -154,6 +155,14 @@ func rb_dummy_tests_rb_const_defined_at(klass C.VALUE, name C.VALUE) C.VALUE {
154155
return C.VALUE(ruby.Qfalse())
155156
}
156157

158+
//export rb_dummy_tests_rb_eval_string
159+
func rb_dummy_tests_rb_eval_string(_ C.VALUE, str C.VALUE) C.VALUE {
160+
goStr := ruby.Value2String(ruby.VALUE(str))
161+
ret := ruby.RbEvalString(goStr)
162+
163+
return C.VALUE(ret)
164+
}
165+
157166
// defineMethodsToDummyTests define methods in Dummy::Tests
158167
func defineMethodsToDummyTests(rb_mDummy ruby.VALUE) {
159168
rb_cTests := ruby.RbDefineClassUnder(rb_mDummy, "Tests", ruby.VALUE(C.rb_cObject))
@@ -173,4 +182,5 @@ func defineMethodsToDummyTests(rb_mDummy ruby.VALUE) {
173182
ruby.RbDefineSingletonMethod(rb_cTests, "rb_const_set", C.rb_dummy_tests_rb_const_set, 2)
174183
ruby.RbDefineSingletonMethod(rb_cTests, "rb_const_defined", C.rb_dummy_tests_rb_const_defined, 1)
175184
ruby.RbDefineSingletonMethod(rb_cTests, "rb_const_defined_at", C.rb_dummy_tests_rb_const_defined_at, 1)
185+
ruby.RbDefineSingletonMethod(rb_cTests, "rb_eval_string", C.rb_dummy_tests_rb_eval_string, 1)
176186
}

testdata/dummy/sig/dummy/tests.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ module Dummy
2424
def self.rb_const_set: (string name, untyped val) -> void
2525
def self.rb_const_defined: (string name) -> bool
2626
def self.rb_const_defined_at: (string name) -> bool
27+
def self.rb_eval_string: (string str) -> untyped
2728
end
2829
end

testdata/dummy/spec/dummy/tests_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,10 @@
182182
end
183183
end
184184
end
185+
186+
describe ".rb_eval_string" do
187+
it "works" do
188+
expect(Dummy::Tests.rb_eval_string("1 + 2")).to eq 3
189+
end
190+
end
185191
end

0 commit comments

Comments
 (0)