@@ -18,7 +18,7 @@ def update(request_id, string_data, response_type, finished)
1818 return self
1919 end
2020
21- response_hash = TonBinding . read_string_to_hash ( string_data )
21+ response_hash = TonBinding . read_string_to_hash ( TonBinding . read_string_data ( string_data ) )
2222 @finished = finished
2323 @request_id = request_id
2424 @response_type = response_type
@@ -48,15 +48,15 @@ module TonClient
4848
4949 module TonBinding
5050 extend FFI ::Library
51- # ffi_lib FFI::Library::LIBC
52- # ffi_lib 'ruby'
51+ ffi_lib FFI ::Library ::LIBC
52+ ffi_lib 'ruby'
5353
5454 # memory allocators
55- # attach_function :malloc, [:size_t], :pointer
55+ attach_function :malloc , [ :size_t ] , :pointer
5656 # attach_function :calloc, [:size_t], :pointer
5757 # attach_function :valloc, [:size_t], :pointer
5858 # attach_function :realloc, [:pointer, :size_t], :pointer
59- # attach_function :free, [:pointer], :void
59+ attach_function :free , [ :pointer ] , :void
6060
6161 # # memory movers
6262 # attach_function :memcpy, [:pointer, :pointer, :size_t], :pointer
@@ -65,7 +65,7 @@ module TonBinding
6565 def self . setup_bindings
6666
6767 # tc_string_handle_t* tc_create_context(tc_string_data_t config);
68- # attach_function :tc_create_context, [TcStringDataT.by_value], TcStringHandleT .by_ref
68+ # attach_function :tc_create_context, [TcStringDataT.by_value], TcStringDataT .by_ref
6969 attach_function :tc_create_context , [ TcStringDataT . by_value ] , :pointer
7070
7171 # fn tc_destroy_context(context: InteropContext)
@@ -75,7 +75,7 @@ def self.setup_bindings
7575 # uint32_t context,
7676 # tc_string_data_t function_name,
7777 # tc_string_data_t function_params_json);
78- # attach_function :tc_request_sync, [:uint32, TcStringDataT.by_value, TcStringDataT.by_value], TcStringHandleT .by_ref
78+ # attach_function :tc_request_sync, [:uint32, TcStringDataT.by_value, TcStringDataT.by_value], TcStringDataT .by_ref
7979 attach_function :tc_request_sync , [ :uint32 , TcStringDataT . by_value , TcStringDataT . by_value ] , :pointer
8080
8181 # enum tc_response_types_t {
@@ -100,43 +100,50 @@ def self.setup_bindings
100100 attach_function :tc_request , [ :uint32 , TcStringDataT . by_value , TcStringDataT . by_value , :uint32 , :tc_response_handler_t ] , :void
101101
102102 # tc_string_data_t tc_read_string(const tc_string_handle_t* handle);
103- # attach_function :tc_read_string, [TcStringHandleT .by_ref], TcStringDataT.by_value
103+ # attach_function :tc_read_string, [TcStringDataT .by_ref], TcStringDataT.by_value
104104 attach_function :tc_read_string , [ :pointer ] , TcStringDataT . by_value
105105
106106 # void tc_destroy_string(const tc_string_handle_t* handle)
107- # attach_function :tc_destroy_string, [TcStringHandleT .by_ref], :void
107+ # attach_function :tc_destroy_string, [TcStringDataT .by_ref], :void
108108 attach_function :tc_destroy_string , [ :pointer ] , :void
109109 end
110110
111111 def self . make_string ( string )
112+ string = string . encode ( "utf-8" ) . freeze
112113 result = TonBinding ::TcStringDataT . new
113114 result [ :content ] = FFI ::MemoryPointer . from_string ( string )
114115 result [ :len ] = string . bytesize
115116 result
116117 end
117118
118- def self . read_string ( tc_string_handle )
119- is_ref = tc_string_handle . class == FFI ::Pointer
120- if is_ref
121- string = tc_read_string ( tc_string_handle )
122- else
123- string = tc_string_handle
124- end
119+ def self . read_string_data_ref ( tc_string_handle_ref )
120+ string = tc_read_string ( tc_string_handle_ref )
125121
126122 if string [ :len ] > 0
127- result = string [ :content ] . read_string ( string [ :len ] )
128- if is_ref
129- tc_destroy_string ( tc_string_handle )
130- # free(tc_string_handle)
123+ result = string [ :content ] . read_string ( string [ :len ] ) . freeze
124+ if tc_string_handle_ref
125+ tc_destroy_string ( tc_string_handle_ref )
126+ else
127+ p "NOOOOOOOOOOOO"
131128 end
129+ # free(tc_string_handle_ref)
130+ result
131+ else
132+ nil
133+ end
134+ end
135+
136+ def self . read_string_data ( tc_string_data )
137+ if tc_string_data [ :len ] > 0
138+ result = tc_string_data [ :content ] . read_string ( tc_string_data [ :len ] ) . freeze
139+ # free(tc_string_data)
132140 result
133141 else
134142 nil
135143 end
136144 end
137145
138- def self . read_string_to_hash ( tc_string_handle_t_ref )
139- json_string = read_string ( tc_string_handle_t_ref )
146+ def self . read_string_to_hash ( json_string )
140147 JSON . parse ( json_string , { max_nesting : false } ) if json_string
141148 end
142149
@@ -148,10 +155,9 @@ def self.send_request_sync(context: 1, method_name: '', payload: {})
148155 payload_string = make_string ( payload . to_json )
149156
150157 sdk_json_response = tc_request_sync ( context , method_name_string , payload_string )
151- response = read_string_to_hash ( sdk_json_response )
152-
153- return response [ 'result' ] if response [ 'result' ]
154- return response [ 'error' ] if response [ 'error' ]
158+ response = read_string_to_hash ( read_string_data_ref ( sdk_json_response ) )
159+
160+ response
155161 end
156162
157163 # block = { |response| }
0 commit comments