Skip to content

Commit 89fe630

Browse files
committed
Use embeddable types
Ref: https://bugs.ruby-lang.org/issues/21853 Introduced in Ruby 3.3, will officially be public API in 4.1, but can be used sooner by checking the existence of RUBY_TYPED_EMBEDDABLE
1 parent c5eb124 commit 89fe630

5 files changed

Lines changed: 23 additions & 19 deletions

File tree

ext/json/ext/generator/extconf.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
File.write('Makefile', dummy_makefile("").join)
66
else
77
append_cflags("-std=c99")
8+
have_const("RUBY_TYPED_EMBEDDABLE", "ruby.h") # RUBY_VERSION >= 3.3
9+
810
$defs << "-DJSON_GENERATOR"
911
$defs << "-DJSON_DEBUG" if ENV.fetch("JSON_DEBUG", "0") != "0"
1012

ext/json/ext/generator/generator.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -722,27 +722,20 @@ static void State_compact(void *ptr)
722722
state->as_json = rb_gc_location(state->as_json);
723723
}
724724

725-
static void State_free(void *ptr)
726-
{
727-
JSON_Generator_State *state = ptr;
728-
ruby_xfree(state);
729-
}
730-
731725
static size_t State_memsize(const void *ptr)
732726
{
733727
return sizeof(JSON_Generator_State);
734728
}
735729

736730
static const rb_data_type_t JSON_Generator_State_type = {
737-
"JSON/Generator/State",
738-
{
731+
.wrap_struct_name = "JSON/Generator/State",
732+
.function = {
739733
.dmark = State_mark,
740-
.dfree = State_free,
734+
.dfree = RUBY_DEFAULT_FREE,
741735
.dsize = State_memsize,
742736
.dcompact = State_compact,
743737
},
744-
0, 0,
745-
RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE,
738+
.flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_EMBEDDABLE,
746739
};
747740

748741
static void state_init(JSON_Generator_State *state)

ext/json/ext/json.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ typedef unsigned char _Bool;
5454
# define RUBY_TYPED_FROZEN_SHAREABLE 0
5555
#endif
5656

57+
#ifndef HAVE_RUBY_TYPED_EMBEDDABLE
58+
# define RUBY_TYPED_EMBEDDABLE 0
59+
#endif
60+
5761
#ifndef NORETURN
5862
#if defined(__has_attribute) && __has_attribute(noreturn)
5963
#define NORETURN(x) __attribute__((noreturn)) x
@@ -102,4 +106,6 @@ typedef unsigned char _Bool;
102106
#define JSON_CPU_LITTLE_ENDIAN_64BITS 0
103107
#endif
104108

109+
110+
105111
#endif // _JSON_H_

ext/json/ext/parser/extconf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
have_func("rb_str_to_interned_str", "ruby.h") # RUBY_VERSION >= 3.0
77
have_func("rb_hash_new_capa", "ruby.h") # RUBY_VERSION >= 3.2
88
have_func("rb_hash_bulk_insert", "ruby.h") # Missing on TruffleRuby
9+
have_const("RUBY_TYPED_EMBEDDABLE", "ruby.h") # RUBY_VERSION >= 3.3
910

1011
append_cflags("-std=c99")
1112

ext/json/ext/parser/parser.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ static void rvalue_stack_free(void *ptr)
251251
rvalue_stack *stack = (rvalue_stack *)ptr;
252252
if (stack) {
253253
ruby_xfree(stack->ptr);
254+
#ifndef HAVE_RUBY_TYPED_EMBEDDABLE
254255
ruby_xfree(stack);
256+
#endif
255257
}
256258
}
257259

@@ -262,14 +264,13 @@ static size_t rvalue_stack_memsize(const void *ptr)
262264
}
263265

264266
static const rb_data_type_t JSON_Parser_rvalue_stack_type = {
265-
"JSON::Ext::Parser/rvalue_stack",
266-
{
267+
.wrap_struct_name = "JSON::Ext::Parser/rvalue_stack",
268+
.function = {
267269
.dmark = rvalue_stack_mark,
268270
.dfree = rvalue_stack_free,
269271
.dsize = rvalue_stack_memsize,
270272
},
271-
0, 0,
272-
RUBY_TYPED_FREE_IMMEDIATELY,
273+
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE,
273274
};
274275

275276
static rvalue_stack *rvalue_stack_spill(rvalue_stack *old_stack, VALUE *handle, rvalue_stack **stack_ref)
@@ -1608,7 +1609,9 @@ static void JSON_ParserConfig_mark(void *ptr)
16081609
static void JSON_ParserConfig_free(void *ptr)
16091610
{
16101611
JSON_ParserConfig *config = ptr;
1612+
#ifndef HAVE_RUBY_TYPED_EMBEDDABLE
16111613
ruby_xfree(config);
1614+
#endif
16121615
}
16131616

16141617
static size_t JSON_ParserConfig_memsize(const void *ptr)
@@ -1617,14 +1620,13 @@ static size_t JSON_ParserConfig_memsize(const void *ptr)
16171620
}
16181621

16191622
static const rb_data_type_t JSON_ParserConfig_type = {
1620-
"JSON::Ext::Parser/ParserConfig",
1621-
{
1623+
.wrap_struct_name = "JSON::Ext::Parser/ParserConfig",
1624+
.function = {
16221625
JSON_ParserConfig_mark,
16231626
JSON_ParserConfig_free,
16241627
JSON_ParserConfig_memsize,
16251628
},
1626-
0, 0,
1627-
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE,
1629+
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_EMBEDDABLE,
16281630
};
16291631

16301632
static VALUE cJSON_parser_s_allocate(VALUE klass)

0 commit comments

Comments
 (0)