summaryrefslogtreecommitdiff
path: root/spec/ruby/optional/capi/ext
diff options
context:
space:
mode:
authorBenoit Daloze <eregontp@gmail.com>2021-07-29 22:11:21 +0200
committerBenoit Daloze <eregontp@gmail.com>2021-07-29 22:11:21 +0200
commit6998d758248d778fa95b008c78d05473e48b8428 (patch)
tree8abc6926f647ea5f374a5b34c3a4820c5861e32e /spec/ruby/optional/capi/ext
parent15d05f8120745a121b93fab9fd2addf5f094e8d2 (diff)
Update to ruby/spec@b65d01f
Diffstat (limited to 'spec/ruby/optional/capi/ext')
-rw-r--r--spec/ruby/optional/capi/ext/fiber_spec.c58
-rw-r--r--spec/ruby/optional/capi/ext/regexp_spec.c6
-rw-r--r--spec/ruby/optional/capi/ext/rubyspec.h1
-rw-r--r--spec/ruby/optional/capi/ext/string_spec.c40
4 files changed, 105 insertions, 0 deletions
diff --git a/spec/ruby/optional/capi/ext/fiber_spec.c b/spec/ruby/optional/capi/ext/fiber_spec.c
new file mode 100644
index 0000000000..7912e878f3
--- /dev/null
+++ b/spec/ruby/optional/capi/ext/fiber_spec.c
@@ -0,0 +1,58 @@
+#include "ruby.h"
+#include "rubyspec.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+VALUE fiber_spec_rb_fiber_current(VALUE self) {
+ return rb_fiber_current();
+}
+
+VALUE fiber_spec_rb_fiber_alive_p(VALUE self, VALUE fiber) {
+ return rb_fiber_alive_p(fiber);
+}
+
+VALUE fiber_spec_rb_fiber_resume(VALUE self, VALUE fiber, VALUE ary) {
+ long argc = RARRAY_LEN(ary);
+ VALUE *argv = (VALUE*) alloca(sizeof(VALUE) * argc);
+ int i;
+
+ for (i = 0; i < argc; i++) {
+ argv[i] = rb_ary_entry(ary, i);
+ }
+
+ return rb_fiber_resume(fiber, (int)argc, argv);
+}
+
+VALUE fiber_spec_rb_fiber_yield(VALUE self, VALUE ary) {
+ long argc = RARRAY_LEN(ary);
+ VALUE *argv = (VALUE*) alloca(sizeof(VALUE) * argc);
+ int i;
+
+ for (i = 0; i < argc; i++) {
+ argv[i] = rb_ary_entry(ary, i);
+ }
+ return rb_fiber_yield((int)argc, argv);
+}
+
+VALUE fiber_spec_rb_fiber_new_function(RB_BLOCK_CALL_FUNC_ARGLIST(args, dummy)) {
+ return rb_funcall(args, rb_intern("inspect"), 0);
+}
+
+VALUE fiber_spec_rb_fiber_new(VALUE self) {
+ return rb_fiber_new(fiber_spec_rb_fiber_new_function, Qnil);
+}
+
+void Init_fiber_spec(void) {
+ VALUE cls = rb_define_class("CApiFiberSpecs", rb_cObject);
+ rb_define_method(cls, "rb_fiber_current", fiber_spec_rb_fiber_current, 0);
+ rb_define_method(cls, "rb_fiber_alive_p", fiber_spec_rb_fiber_alive_p, 1);
+ rb_define_method(cls, "rb_fiber_resume", fiber_spec_rb_fiber_resume, 2);
+ rb_define_method(cls, "rb_fiber_yield", fiber_spec_rb_fiber_yield, 1);
+ rb_define_method(cls, "rb_fiber_new", fiber_spec_rb_fiber_new, 0);
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/spec/ruby/optional/capi/ext/regexp_spec.c b/spec/ruby/optional/capi/ext/regexp_spec.c
index 70780bae2a..0a62616f33 100644
--- a/spec/ruby/optional/capi/ext/regexp_spec.c
+++ b/spec/ruby/optional/capi/ext/regexp_spec.c
@@ -35,6 +35,11 @@ VALUE regexp_spec_backref_get(VALUE self) {
return rb_backref_get();
}
+static VALUE regexp_spec_backref_set(VALUE self, VALUE backref) {
+ rb_backref_set(backref);
+ return Qnil;
+}
+
VALUE regexp_spec_reg_match_backref_get(VALUE self, VALUE re, VALUE str) {
rb_reg_match(re, str);
return rb_backref_get();
@@ -51,6 +56,7 @@ void Init_regexp_spec(void) {
rb_define_method(cls, "a_re_1st_match", regexp_spec_reg_1st_match, 1);
rb_define_method(cls, "rb_reg_match", regexp_spec_reg_match, 2);
rb_define_method(cls, "rb_backref_get", regexp_spec_backref_get, 0);
+ rb_define_method(cls, "rb_backref_set", regexp_spec_backref_set, 1);
rb_define_method(cls, "rb_reg_match_backref_get", regexp_spec_reg_match_backref_get, 2);
rb_define_method(cls, "rb_reg_options", regexp_spec_rb_reg_options, 1);
rb_define_method(cls, "rb_reg_regcomp", regexp_spec_rb_reg_regcomp, 1);
diff --git a/spec/ruby/optional/capi/ext/rubyspec.h b/spec/ruby/optional/capi/ext/rubyspec.h
index a10ab49f7c..7e4a252b38 100644
--- a/spec/ruby/optional/capi/ext/rubyspec.h
+++ b/spec/ruby/optional/capi/ext/rubyspec.h
@@ -55,6 +55,7 @@
#define rb_catch(tag, func, data) rb_catch(tag, RUBY_METHOD_FUNC(func), data)
#define rb_catch_obj(tag, func, data) rb_catch_obj(tag, RUBY_METHOD_FUNC(func), data)
#define rb_proc_new(fn, arg) rb_proc_new(RUBY_METHOD_FUNC(fn), arg)
+#define rb_fiber_new(fn, arg) rb_fiber_new(RUBY_METHOD_FUNC(fn), arg)
#define rb_thread_create(fn, arg) rb_thread_create(RUBY_METHOD_FUNC(fn), arg)
#define rb_define_hooked_variable(name, var, getter, setter) rb_define_hooked_variable(name, var, RUBY_METHOD_FUNC(getter), (void (*)(...))setter)
#endif
diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c
index dd7bc397cd..f2245be0ef 100644
--- a/spec/ruby/optional/capi/ext/string_spec.c
+++ b/spec/ruby/optional/capi/ext/string_spec.c
@@ -1,6 +1,7 @@
#include "ruby.h"
#include "rubyspec.h"
+#include <fcntl.h>
#include <string.h>
#include <stdarg.h>
@@ -279,6 +280,16 @@ VALUE string_spec_rb_str_resize_RSTRING_LEN(VALUE self, VALUE str, VALUE size) {
return INT2FIX(RSTRING_LEN(modified));
}
+VALUE string_spec_rb_str_resize_copy(VALUE self, VALUE str) {
+ rb_str_modify_expand(str, 5);
+ char *buffer = RSTRING_PTR(str);
+ buffer[1] = 'e';
+ buffer[2] = 's';
+ buffer[3] = 't';
+ rb_str_resize(str, 4);
+ return str;
+}
+
VALUE string_spec_rb_str_split(VALUE self, VALUE str) {
return rb_str_split(str, ",");
}
@@ -374,6 +385,20 @@ VALUE string_spec_RSTRING_PTR_after_yield(VALUE self, VALUE str) {
return from_rstring_ptr;
}
+VALUE string_spec_RSTRING_PTR_read(VALUE self, VALUE str, VALUE path) {
+ char *cpath = StringValueCStr(path);
+ int fd = open(cpath, O_RDONLY);
+ rb_str_modify_expand(str, 10);
+ char *buffer = RSTRING_PTR(str);
+ read(fd, buffer, 10);
+ rb_str_modify_expand(str, 21);
+ char *buffer2 = RSTRING_PTR(str);
+ read(fd, buffer2 + 10, 11);
+ rb_str_set_len(str, 21);
+ close(fd);
+ return str;
+}
+
VALUE string_spec_StringValue(VALUE self, VALUE str) {
return StringValue(str);
}
@@ -476,6 +501,18 @@ static VALUE string_spec_rb_utf8_str_new_cstr(VALUE self) {
return rb_utf8_str_new_cstr("nokogiri");
}
+static VALUE call_rb_str_vcatf(VALUE mesg, const char *fmt, ...){
+ va_list ap;
+ va_start(ap, fmt);
+ VALUE result = rb_str_vcatf(mesg, fmt, ap);
+ va_end(ap);
+ return result;
+}
+
+static VALUE string_spec_rb_str_vcatf(VALUE self, VALUE mesg) {
+ return call_rb_str_vcatf(mesg, "fmt %d %d number", 42, 7);
+}
+
void Init_string_spec(void) {
VALUE cls = rb_define_class("CApiStringSpecs", rb_cObject);
rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2);
@@ -527,6 +564,7 @@ void Init_string_spec(void) {
rb_define_method(cls, "rb_str_modify_expand", string_spec_rb_str_modify_expand, 2);
rb_define_method(cls, "rb_str_resize", string_spec_rb_str_resize, 2);
rb_define_method(cls, "rb_str_resize_RSTRING_LEN", string_spec_rb_str_resize_RSTRING_LEN, 2);
+ rb_define_method(cls, "rb_str_resize_copy", string_spec_rb_str_resize_copy, 1);
rb_define_method(cls, "rb_str_set_len", string_spec_rb_str_set_len, 2);
rb_define_method(cls, "rb_str_set_len_RSTRING_LEN", string_spec_rb_str_set_len_RSTRING_LEN, 2);
rb_define_method(cls, "rb_str_split", string_spec_rb_str_split, 1);
@@ -542,6 +580,7 @@ void Init_string_spec(void) {
rb_define_method(cls, "RSTRING_PTR_set", string_spec_RSTRING_PTR_set, 3);
rb_define_method(cls, "RSTRING_PTR_after_funcall", string_spec_RSTRING_PTR_after_funcall, 2);
rb_define_method(cls, "RSTRING_PTR_after_yield", string_spec_RSTRING_PTR_after_yield, 1);
+ rb_define_method(cls, "RSTRING_PTR_read", string_spec_RSTRING_PTR_read, 2);
rb_define_method(cls, "StringValue", string_spec_StringValue, 1);
rb_define_method(cls, "SafeStringValue", string_spec_SafeStringValue, 1);
rb_define_method(cls, "rb_str_hash", string_spec_rb_str_hash, 1);
@@ -563,6 +602,7 @@ void Init_string_spec(void) {
rb_define_method(cls, "rb_utf8_str_new_static", string_spec_rb_utf8_str_new_static, 0);
rb_define_method(cls, "rb_utf8_str_new", string_spec_rb_utf8_str_new, 0);
rb_define_method(cls, "rb_utf8_str_new_cstr", string_spec_rb_utf8_str_new_cstr, 0);
+ rb_define_method(cls, "rb_str_vcatf", string_spec_rb_str_vcatf, 1);
}
#ifdef __cplusplus