5
5
VALUE rb_eFastJsonparserUnknownError, rb_eFastJsonparserParseError;
6
6
7
7
using namespace simdjson ;
8
- static dom::parser parser;
8
+
9
+ typedef struct {
10
+ dom::parser *parser;
11
+ } parser_t ;
12
+
13
+ static void Parser_delete (void *ptr) {
14
+ parser_t *data = (parser_t *) ptr;
15
+ delete data->parser ;
16
+ }
17
+
18
+ static size_t Parser_memsize (const void *parser) {
19
+ return sizeof (dom::parser); // TODO: low priority, figure the real size, e.g. internal buffers etc.
20
+ }
21
+
22
+ static const rb_data_type_t parser_data_type = {
23
+ " Parser" ,
24
+ { 0 , Parser_delete, Parser_memsize, },
25
+ 0 , 0 , RUBY_TYPED_FREE_IMMEDIATELY
26
+ };
27
+
28
+ static VALUE parser_allocate (VALUE klass) {
29
+ parser_t *data;
30
+ VALUE obj = TypedData_Make_Struct (klass, parser_t , &parser_data_type, data);
31
+ data->parser = new dom::parser;
32
+ return obj;
33
+ }
34
+
35
+ static inline dom::parser * get_parser (VALUE self) {
36
+ parser_t *data;
37
+ TypedData_Get_Struct (self, parser_t , &parser_data_type, data);
38
+ return data->parser ;
39
+ }
9
40
10
41
// Convert tape to Ruby's Object
11
42
static VALUE make_ruby_object (dom::element element, bool symbolize_keys)
@@ -71,8 +102,9 @@ static VALUE make_ruby_object(dom::element element, bool symbolize_keys)
71
102
static VALUE rb_fast_jsonparser_parse (VALUE self, VALUE arg, VALUE symbolize_keys)
72
103
{
73
104
Check_Type (arg, T_STRING);
105
+ dom::parser *parser = get_parser (self);
74
106
75
- auto [doc, error] = parser. parse (RSTRING_PTR (arg), RSTRING_LEN (arg));
107
+ auto [doc, error] = parser-> parse (RSTRING_PTR (arg), RSTRING_LEN (arg));
76
108
if (error != SUCCESS)
77
109
{
78
110
rb_raise (rb_eFastJsonparserParseError, " %s" , error_message (error));
@@ -83,8 +115,9 @@ static VALUE rb_fast_jsonparser_parse(VALUE self, VALUE arg, VALUE symbolize_key
83
115
static VALUE rb_fast_jsonparser_load (VALUE self, VALUE arg, VALUE symbolize_keys)
84
116
{
85
117
Check_Type (arg, T_STRING);
118
+ dom::parser *parser = get_parser (self);
86
119
87
- auto [doc, error] = parser. load (RSTRING_PTR (arg));
120
+ auto [doc, error] = parser-> load (RSTRING_PTR (arg));
88
121
if (error != SUCCESS)
89
122
{
90
123
rb_raise (rb_eFastJsonparserParseError, " %s" , error_message (error));
@@ -96,9 +129,10 @@ static VALUE rb_fast_jsonparser_load_many(VALUE self, VALUE arg, VALUE symbolize
96
129
{
97
130
Check_Type (arg, T_STRING);
98
131
Check_Type (batch_size, T_FIXNUM);
132
+ dom::parser *parser = get_parser (self);
99
133
100
134
try {
101
- auto [docs, error] = parser. load_many (RSTRING_PTR (arg), FIX2INT (batch_size));
135
+ auto [docs, error] = parser-> load_many (RSTRING_PTR (arg), FIX2INT (batch_size));
102
136
if (error != SUCCESS)
103
137
{
104
138
rb_raise (rb_eFastJsonparserParseError, " %s" , error_message (error));
@@ -123,10 +157,12 @@ extern "C"
123
157
void Init_fast_jsonparser (void )
124
158
{
125
159
VALUE rb_mFastJsonparser = rb_const_get (rb_cObject, rb_intern (" FastJsonparser" ));
160
+ VALUE rb_cFastJsonparserNative = rb_const_get (rb_mFastJsonparser, rb_intern (" Native" ));
126
161
127
- rb_define_module_function (rb_mFastJsonparser, " _parse" , reinterpret_cast <VALUE (*)(...)>(rb_fast_jsonparser_parse), 2 );
128
- rb_define_module_function (rb_mFastJsonparser, " _load" , reinterpret_cast <VALUE (*)(...)>(rb_fast_jsonparser_load), 2 );
129
- rb_define_module_function (rb_mFastJsonparser, " _load_many" , reinterpret_cast <VALUE (*)(...)>(rb_fast_jsonparser_load_many), 3 );
162
+ rb_define_alloc_func (rb_cFastJsonparserNative, parser_allocate);
163
+ rb_define_method (rb_cFastJsonparserNative, " _parse" , reinterpret_cast <VALUE (*)(...)>(rb_fast_jsonparser_parse), 2 );
164
+ rb_define_method (rb_cFastJsonparserNative, " _load" , reinterpret_cast <VALUE (*)(...)>(rb_fast_jsonparser_load), 2 );
165
+ rb_define_method (rb_cFastJsonparserNative, " _load_many" , reinterpret_cast <VALUE (*)(...)>(rb_fast_jsonparser_load_many), 3 );
130
166
131
167
rb_eFastJsonparserParseError = rb_const_get (rb_mFastJsonparser, rb_intern (" ParseError" ));
132
168
rb_global_variable (&rb_eFastJsonparserParseError);
0 commit comments