Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ext/json/ext/generator/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
File.write('Makefile', dummy_makefile("").join)
else
append_cflags("-std=c99")
have_const("RUBY_TYPED_EMBEDDABLE", "ruby.h") # RUBY_VERSION >= 3.3

$defs << "-DJSON_GENERATOR"
$defs << "-DJSON_DEBUG" if ENV.fetch("JSON_DEBUG", "0") != "0"

Expand Down
15 changes: 4 additions & 11 deletions ext/json/ext/generator/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,27 +722,20 @@ static void State_compact(void *ptr)
state->as_json = rb_gc_location(state->as_json);
}

static void State_free(void *ptr)
{
JSON_Generator_State *state = ptr;
ruby_xfree(state);
}

static size_t State_memsize(const void *ptr)
{
return sizeof(JSON_Generator_State);
}

static const rb_data_type_t JSON_Generator_State_type = {
"JSON/Generator/State",
{
.wrap_struct_name = "JSON/Generator/State",
.function = {
.dmark = State_mark,
.dfree = State_free,
.dfree = RUBY_DEFAULT_FREE,
.dsize = State_memsize,
.dcompact = State_compact,
},
0, 0,
RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE,
.flags = RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_EMBEDDABLE,
};

static void state_init(JSON_Generator_State *state)
Expand Down
6 changes: 6 additions & 0 deletions ext/json/ext/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ typedef unsigned char _Bool;
# define RUBY_TYPED_FROZEN_SHAREABLE 0
#endif

#ifndef HAVE_RUBY_TYPED_EMBEDDABLE
# define RUBY_TYPED_EMBEDDABLE 0
#endif

#ifndef NORETURN
#if defined(__has_attribute) && __has_attribute(noreturn)
#define NORETURN(x) __attribute__((noreturn)) x
Expand Down Expand Up @@ -102,4 +106,6 @@ typedef unsigned char _Bool;
#define JSON_CPU_LITTLE_ENDIAN_64BITS 0
#endif



#endif // _JSON_H_
1 change: 1 addition & 0 deletions ext/json/ext/parser/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
have_func("rb_str_to_interned_str", "ruby.h") # RUBY_VERSION >= 3.0
have_func("rb_hash_new_capa", "ruby.h") # RUBY_VERSION >= 3.2
have_func("rb_hash_bulk_insert", "ruby.h") # Missing on TruffleRuby
have_const("RUBY_TYPED_EMBEDDABLE", "ruby.h") # RUBY_VERSION >= 3.3

append_cflags("-std=c99")

Expand Down
18 changes: 10 additions & 8 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ static void rvalue_stack_free(void *ptr)
rvalue_stack *stack = (rvalue_stack *)ptr;
if (stack) {
ruby_xfree(stack->ptr);
#ifndef HAVE_RUBY_TYPED_EMBEDDABLE
ruby_xfree(stack);
#endif
}
}

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

static const rb_data_type_t JSON_Parser_rvalue_stack_type = {
"JSON::Ext::Parser/rvalue_stack",
{
.wrap_struct_name = "JSON::Ext::Parser/rvalue_stack",
.function = {
.dmark = rvalue_stack_mark,
.dfree = rvalue_stack_free,
.dsize = rvalue_stack_memsize,
},
0, 0,
RUBY_TYPED_FREE_IMMEDIATELY,
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_EMBEDDABLE,
};

static rvalue_stack *rvalue_stack_spill(rvalue_stack *old_stack, VALUE *handle, rvalue_stack **stack_ref)
Expand Down Expand Up @@ -1608,7 +1609,9 @@ static void JSON_ParserConfig_mark(void *ptr)
static void JSON_ParserConfig_free(void *ptr)
{
JSON_ParserConfig *config = ptr;
#ifndef HAVE_RUBY_TYPED_EMBEDDABLE
ruby_xfree(config);
#endif
}

static size_t JSON_ParserConfig_memsize(const void *ptr)
Expand All @@ -1617,14 +1620,13 @@ static size_t JSON_ParserConfig_memsize(const void *ptr)
}

static const rb_data_type_t JSON_ParserConfig_type = {
"JSON::Ext::Parser/ParserConfig",
{
.wrap_struct_name = "JSON::Ext::Parser/ParserConfig",
.function = {
JSON_ParserConfig_mark,
JSON_ParserConfig_free,
JSON_ParserConfig_memsize,
},
0, 0,
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE,
.flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE | RUBY_TYPED_EMBEDDABLE,
};

static VALUE cJSON_parser_s_allocate(VALUE klass)
Expand Down