Skip to content

Commit b20ef72

Browse files
committed
Reduce diff in intern.h
1 parent fcc2360 commit b20ef72

File tree

6 files changed

+121
-25
lines changed

6 files changed

+121
-25
lines changed

lib/cext/include/ruby/intern.h

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ RUBY_SYMBOL_EXPORT_BEGIN
5353
#define UNLIMITED_ARGUMENTS (-1)
5454

5555
/* array.c */
56+
#ifdef TRUFFLERUBY
5657
void rb_mem_clear(VALUE*, long);
57-
#define rb_assoc_new(a, b) rb_ary_new3(2, a, b)
58+
#else
59+
void rb_mem_clear(register VALUE*, register long);
60+
#endif
61+
VALUE rb_assoc_new(VALUE, VALUE);
5862
VALUE rb_check_array_type(VALUE);
5963
VALUE rb_ary_new(void);
6064
VALUE rb_ary_new_capa(long capa);
@@ -63,12 +67,12 @@ VALUE rb_ary_new_from_values(long n, const VALUE *elts);
6367
VALUE rb_ary_tmp_new(long);
6468
void rb_ary_free(VALUE);
6569
void rb_ary_modify(VALUE);
66-
#define rb_ary_freeze(array) rb_obj_freeze(array)
70+
VALUE rb_ary_freeze(VALUE);
6771
VALUE rb_ary_shared_with_p(VALUE, VALUE);
6872
VALUE rb_ary_aref(int, const VALUE*, VALUE);
6973
VALUE rb_ary_subseq(VALUE, long, long);
7074
void rb_ary_store(VALUE, long, VALUE);
71-
#define rb_ary_dup(array) rb_obj_dup(array)
75+
VALUE rb_ary_dup(VALUE);
7276
VALUE rb_ary_resurrect(VALUE ary);
7377
VALUE rb_ary_to_ary(VALUE);
7478
VALUE rb_ary_to_s(VALUE);
@@ -291,6 +295,25 @@ void rb_check_trusted(VALUE);
291295
} \
292296
} while (0)
293297
#define rb_check_trusted_internal(obj) ((void) 0)
298+
#ifndef TRUFFLERUBY
299+
#ifdef __GNUC__
300+
#define rb_check_frozen(obj) __extension__({rb_check_frozen_internal(obj);})
301+
#define rb_check_trusted(obj) __extension__({rb_check_trusted_internal(obj);})
302+
#else
303+
static inline void
304+
rb_check_frozen_inline(VALUE obj)
305+
{
306+
rb_check_frozen_internal(obj);
307+
}
308+
#define rb_check_frozen(obj) rb_check_frozen_inline(obj)
309+
static inline void
310+
rb_check_trusted_inline(VALUE obj)
311+
{
312+
rb_check_trusted_internal(obj);
313+
}
314+
#define rb_check_trusted(obj) rb_check_trusted_inline(obj)
315+
#endif
316+
#endif
294317
void rb_check_copyable(VALUE obj, VALUE orig);
295318

296319
#define RB_OBJ_INIT_COPY(obj, orig) \
@@ -302,8 +325,14 @@ int rb_sourceline(void);
302325
const char *rb_sourcefile(void);
303326
VALUE rb_check_funcall(VALUE, ID, int, const VALUE*);
304327

305-
NORETURN(void rb_error_arity(int, int, int));
306-
int rb_check_arity(int argc, int min, int max);
328+
NORETURN(MJIT_STATIC void rb_error_arity(int, int, int));
329+
static inline int
330+
rb_check_arity(int argc, int min, int max)
331+
{
332+
if ((argc < min) || (max != UNLIMITED_ARGUMENTS && argc > max))
333+
rb_error_arity(argc, min, max);
334+
return argc;
335+
}
307336
#define rb_check_arity rb_check_arity /* for ifdef */
308337

309338
#if defined(NFDBITS) && defined(HAVE_RB_FD_INIT)
@@ -509,7 +538,7 @@ void rb_hash_foreach(VALUE, int (*)(ANYARGS), VALUE);
509538
VALUE rb_hash(VALUE);
510539
VALUE rb_hash_new(void);
511540
VALUE rb_hash_dup(VALUE);
512-
#define rb_hash_freeze(array) rb_obj_freeze(array)
541+
VALUE rb_hash_freeze(VALUE);
513542
VALUE rb_hash_aref(VALUE, VALUE);
514543
VALUE rb_hash_lookup(VALUE, VALUE);
515544
VALUE rb_hash_lookup2(VALUE, VALUE, VALUE);
@@ -733,11 +762,11 @@ VALUE rb_str_buf_append(VALUE, VALUE);
733762
VALUE rb_str_buf_cat(VALUE, const char*, long);
734763
VALUE rb_str_buf_cat2(VALUE, const char*);
735764
VALUE rb_str_buf_cat_ascii(VALUE, const char*);
736-
VALUE rb_obj_as_string(VALUE object);
765+
VALUE rb_obj_as_string(VALUE);
737766
VALUE rb_check_string_type(VALUE);
738767
void rb_must_asciicompat(VALUE);
739-
#define rb_str_dup(string) rb_obj_dup(string)
740-
#define rb_str_resurrect(string) rb_obj_dup(string)
768+
VALUE rb_str_dup(VALUE);
769+
VALUE rb_str_resurrect(VALUE str);
741770
VALUE rb_str_locktmp(VALUE);
742771
VALUE rb_str_unlocktmp(VALUE);
743772
VALUE rb_str_dup_frozen(VALUE);
@@ -750,7 +779,7 @@ VALUE rb_str_subseq(VALUE, long, long);
750779
char *rb_str_subpos(VALUE, long, long*);
751780
void rb_str_modify(VALUE);
752781
void rb_str_modify_expand(VALUE, long);
753-
#define rb_str_freeze(string) rb_obj_freeze(string)
782+
VALUE rb_str_freeze(VALUE);
754783
void rb_str_set_len(VALUE, long);
755784
VALUE rb_str_resize(VALUE, long);
756785
VALUE rb_str_cat(VALUE, const char*, long);
@@ -774,7 +803,7 @@ VALUE rb_str_equal(VALUE str1, VALUE str2);
774803
VALUE rb_str_drop_bytes(VALUE, long);
775804
void rb_str_update(VALUE, long, long, VALUE);
776805
VALUE rb_str_replace(VALUE, VALUE);
777-
#define rb_str_inspect(string) rb_inspect(string)
806+
VALUE rb_str_inspect(VALUE);
778807
VALUE rb_str_dump(VALUE);
779808
VALUE rb_str_split(VALUE, const char*);
780809
void rb_str_setter(VALUE, ID, VALUE*);
@@ -789,23 +818,70 @@ VALUE rb_str_scrub(VALUE, VALUE);
789818
/* symbol.c */
790819
VALUE rb_sym_all_symbols(void);
791820

821+
#ifndef TRUFFLERUBY
792822
#ifdef HAVE_BUILTIN___BUILTIN_CONSTANT_P
793-
VALUE rb_str_new_cstr(const char *string);
823+
#define rb_str_new(str, len) RB_GNUC_EXTENSION_BLOCK( \
824+
(__builtin_constant_p(str) && __builtin_constant_p(len)) ? \
825+
rb_str_new_static((str), (len)) : \
826+
rb_str_new((str), (len)) \
827+
)
828+
#define rb_str_new_cstr(str) RB_GNUC_EXTENSION_BLOCK( \
829+
(__builtin_constant_p(str)) ? \
830+
rb_str_new_static((str), (long)strlen(str)) : \
831+
rb_str_new_cstr(str) \
832+
)
833+
#define rb_usascii_str_new(str, len) RB_GNUC_EXTENSION_BLOCK( \
834+
(__builtin_constant_p(str) && __builtin_constant_p(len)) ? \
835+
rb_usascii_str_new_static((str), (len)) : \
836+
rb_usascii_str_new((str), (len)) \
837+
)
794838
#define rb_utf8_str_new(str, len) RB_GNUC_EXTENSION_BLOCK( \
795839
(__builtin_constant_p(str) && __builtin_constant_p(len)) ? \
796840
rb_utf8_str_new_static((str), (len)) : \
797841
rb_utf8_str_new((str), (len)) \
798842
)
843+
#define rb_tainted_str_new_cstr(str) RB_GNUC_EXTENSION_BLOCK( \
844+
(__builtin_constant_p(str)) ? \
845+
rb_tainted_str_new((str), (long)strlen(str)) : \
846+
rb_tainted_str_new_cstr(str) \
847+
)
848+
#define rb_usascii_str_new_cstr(str) RB_GNUC_EXTENSION_BLOCK( \
849+
(__builtin_constant_p(str)) ? \
850+
rb_usascii_str_new_static((str), (long)strlen(str)) : \
851+
rb_usascii_str_new_cstr(str) \
852+
)
799853
#define rb_utf8_str_new_cstr(str) RB_GNUC_EXTENSION_BLOCK( \
800854
(__builtin_constant_p(str)) ? \
801855
rb_utf8_str_new_static((str), (long)strlen(str)) : \
802856
rb_utf8_str_new_cstr(str) \
803857
)
858+
#define rb_external_str_new_cstr(str) RB_GNUC_EXTENSION_BLOCK( \
859+
(__builtin_constant_p(str)) ? \
860+
rb_external_str_new((str), (long)strlen(str)) : \
861+
rb_external_str_new_cstr(str) \
862+
)
863+
#define rb_locale_str_new_cstr(str) RB_GNUC_EXTENSION_BLOCK( \
864+
(__builtin_constant_p(str)) ? \
865+
rb_locale_str_new((str), (long)strlen(str)) : \
866+
rb_locale_str_new_cstr(str) \
867+
)
868+
#define rb_str_buf_new_cstr(str) RB_GNUC_EXTENSION_BLOCK( \
869+
(__builtin_constant_p(str)) ? \
870+
rb_str_buf_cat(rb_str_buf_new((long)strlen(str)), \
871+
(str), (long)strlen(str)) : \
872+
rb_str_buf_new_cstr(str) \
873+
)
804874
#define rb_str_cat_cstr(str, ptr) RB_GNUC_EXTENSION_BLOCK( \
805875
(__builtin_constant_p(ptr)) ? \
806876
rb_str_cat((str), (ptr), (long)strlen(ptr)) : \
807877
rb_str_cat_cstr((str), (ptr)) \
808878
)
879+
#define rb_exc_new_cstr(klass, ptr) RB_GNUC_EXTENSION_BLOCK( \
880+
(__builtin_constant_p(ptr)) ? \
881+
rb_exc_new((klass), (ptr), (long)strlen(ptr)) : \
882+
rb_exc_new_cstr((klass), (ptr)) \
883+
)
884+
#endif
809885
#endif
810886
#define rb_str_new2 rb_str_new_cstr
811887
#define rb_str_new3 rb_str_new_shared
@@ -814,6 +890,7 @@ VALUE rb_str_new_cstr(const char *string);
814890
#define rb_tainted_str_new2 rb_tainted_str_new_cstr
815891
#define rb_str_buf_new2 rb_str_buf_new_cstr
816892
#define rb_usascii_str_new2 rb_usascii_str_new_cstr
893+
#define rb_str_buf_cat rb_str_cat
817894
#define rb_str_buf_cat2 rb_str_cat_cstr
818895
#define rb_str_cat2 rb_str_cat_cstr
819896
#define rb_strlen_lit(str) (sizeof(str "") - 1)

lib/truffle/truffle/cext.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,10 +1057,6 @@ def rb_errinfo
10571057
$!
10581058
end
10591059

1060-
def rb_check_arity(arg_count, min, max)
1061-
Truffle::Type.check_arity arg_count, min, max
1062-
end
1063-
10641060
def rb_arity_error_string(arg_count, min, max)
10651061
Truffle::Type.arity_error_string(arg_count, min, max)
10661062
end

src/main/c/cext/array.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ VALUE rb_ary_new_from_values(long n, const VALUE *values) {
4949
return array;
5050
}
5151

52+
VALUE rb_assoc_new(VALUE a, VALUE b) {
53+
return rb_ary_new3(2, a, b);
54+
}
55+
5256
VALUE rb_ary_push(VALUE array, VALUE value) {
5357
RUBY_INVOKE_NO_WRAP(array, "push", value);
5458
return array;
@@ -147,3 +151,11 @@ VALUE rb_ary_rotate(VALUE array, long n) {
147151
VALUE rb_ary_tmp_new(long capa) {
148152
return rb_ary_new_capa(capa);
149153
}
154+
155+
VALUE rb_ary_freeze(VALUE array) {
156+
return rb_obj_freeze(array);
157+
}
158+
159+
VALUE rb_ary_dup(VALUE array) {
160+
return rb_obj_dup(array);
161+
}

src/main/c/cext/call.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22

33
// Calling Ruby methods and blocks from C
44

5-
int rb_check_arity(int argc, int min, int max) {
6-
polyglot_invoke(RUBY_CEXT, "rb_check_arity", argc, min, max);
7-
return argc;
8-
}
9-
105
NORETURN(void rb_error_arity(int argc, int min, int max)) {
116
rb_exc_raise(rb_exc_new3(rb_eArgError, rb_tr_wrap(polyglot_invoke(RUBY_CEXT, "rb_arity_error_string", argc, min, max))));
127
}

src/main/c/cext/hash.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,7 @@ size_t rb_hash_size_num(VALUE hash) {
101101
st_index_t rb_hash_start(st_index_t h) {
102102
return (st_index_t) polyglot_as_i64(polyglot_invoke(RUBY_CEXT, "rb_hash_start", h));
103103
}
104+
105+
VALUE rb_hash_freeze(VALUE hash) {
106+
return rb_obj_freeze(hash);
107+
}

src/main/c/cext/string.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ VALUE rb_tainted_str_new_cstr(const char *ptr) {
7777
return str;
7878
}
7979

80+
VALUE rb_str_dup(VALUE string) {
81+
return rb_obj_dup(string);
82+
}
83+
84+
VALUE rb_str_resurrect(VALUE string) {
85+
return rb_obj_dup(string);
86+
}
87+
88+
VALUE rb_str_freeze(VALUE string) {
89+
return rb_obj_freeze(string);
90+
}
91+
92+
VALUE rb_str_inspect(VALUE string) {
93+
return rb_inspect(string);
94+
}
95+
8096
ID rb_intern_str(VALUE string) {
8197
return SYM2ID(RUBY_CEXT_INVOKE("rb_intern_str", string));
8298
}
@@ -164,10 +180,6 @@ int rb_str_cmp(VALUE a, VALUE b) {
164180
return polyglot_as_i32(RUBY_INVOKE_NO_WRAP(a, "<=>", b));
165181
}
166182

167-
VALUE rb_str_buf_cat(VALUE string, const char *to_concat, long length) {
168-
return rb_str_cat(string, to_concat, length);
169-
}
170-
171183
VALUE rb_str_conv_enc(VALUE string, rb_encoding *from, rb_encoding *to) {
172184
return rb_str_conv_enc_opts(string, from, to, 0, Qnil);
173185
}

0 commit comments

Comments
 (0)