Skip to content

Commit 7c92dd7

Browse files
committed
Cleanup
1 parent 203be6f commit 7c92dd7

File tree

10 files changed

+75
-83
lines changed

10 files changed

+75
-83
lines changed

java/herb_jni.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,17 @@ JNIEXPORT jstring JNICALL
104104
Java_org_herb_Herb_extractRuby(JNIEnv* env, jclass clazz, jstring source, jobject options) {
105105
const char* src = (*env)->GetStringUTFChars(env, source, 0);
106106

107+
hb_allocator_T allocator;
108+
if (!hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA)) {
109+
(*env)->ReleaseStringUTFChars(env, source, src);
110+
return NULL;
111+
}
112+
107113
hb_buffer_T output;
108114

109-
if (!hb_buffer_init(&output, strlen(src))) {
115+
if (!hb_buffer_init(&output, strlen(src), &allocator)) {
116+
hb_allocator_destroy(&allocator);
110117
(*env)->ReleaseStringUTFChars(env, source, src);
111-
112118
return NULL;
113119
}
114120

@@ -136,18 +142,12 @@ Java_org_herb_Herb_extractRuby(JNIEnv* env, jclass clazz, jstring source, jobjec
136142
}
137143
}
138144

139-
hb_allocator_T allocator;
140-
if (!hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA)) {
141-
(*env)->ReleaseStringUTFChars(env, source, src);
142-
return NULL;
143-
}
144-
145145
herb_extract_ruby_to_buffer_with_options(src, &output, &extract_options, &allocator);
146146

147147
jstring result = (*env)->NewStringUTF(env, output.value);
148148

149+
hb_buffer_free(&output);
149150
hb_allocator_destroy(&allocator);
150-
free(output.value);
151151
(*env)->ReleaseStringUTFChars(env, source, src);
152152

153153
return result;
@@ -157,17 +157,16 @@ JNIEXPORT jstring JNICALL
157157
Java_org_herb_Herb_extractHTML(JNIEnv* env, jclass clazz, jstring source) {
158158
const char* src = (*env)->GetStringUTFChars(env, source, 0);
159159

160-
hb_buffer_T output;
161-
162-
if (!hb_buffer_init(&output, strlen(src))) {
160+
hb_allocator_T allocator;
161+
if (!hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA)) {
163162
(*env)->ReleaseStringUTFChars(env, source, src);
164-
165163
return NULL;
166164
}
167165

168-
hb_allocator_T allocator;
169-
if (!hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA)) {
170-
free(output.value);
166+
hb_buffer_T output;
167+
168+
if (!hb_buffer_init(&output, strlen(src), &allocator)) {
169+
hb_allocator_destroy(&allocator);
171170
(*env)->ReleaseStringUTFChars(env, source, src);
172171
return NULL;
173172
}
@@ -176,8 +175,8 @@ Java_org_herb_Herb_extractHTML(JNIEnv* env, jclass clazz, jstring source) {
176175

177176
jstring result = (*env)->NewStringUTF(env, output.value);
178177

178+
hb_buffer_free(&output);
179179
hb_allocator_destroy(&allocator);
180-
free(output.value);
181180
(*env)->ReleaseStringUTFChars(env, source, src);
182181

183182
return result;

javascript/packages/node/extension/herb.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,16 @@ napi_value Herb_extract_ruby(napi_env env, napi_callback_info info) {
140140
char* string = CheckString(env, args[0]);
141141
if (!string) { return nullptr; }
142142

143+
hb_allocator_T allocator;
144+
if (!hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA)) {
145+
free(string);
146+
napi_throw_error(env, nullptr, "Failed to initialize allocator");
147+
return nullptr;
148+
}
149+
143150
hb_buffer_T output;
144-
if (!hb_buffer_init(&output, strlen(string))) {
151+
if (!hb_buffer_init(&output, strlen(string), &allocator)) {
152+
hb_allocator_destroy(&allocator);
145153
free(string);
146154
napi_throw_error(env, nullptr, "Failed to initialize buffer");
147155
return nullptr;
@@ -183,21 +191,13 @@ napi_value Herb_extract_ruby(napi_env env, napi_callback_info info) {
183191
}
184192
}
185193

186-
hb_allocator_T allocator;
187-
if (!hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA)) {
188-
free(output.value);
189-
free(string);
190-
napi_throw_error(env, nullptr, "Failed to initialize allocator");
191-
return nullptr;
192-
}
193-
194194
herb_extract_ruby_to_buffer_with_options(string, &output, &extract_options, &allocator);
195195

196196
napi_value result;
197197
napi_create_string_utf8(env, output.value, NAPI_AUTO_LENGTH, &result);
198198

199+
hb_buffer_free(&output);
199200
hb_allocator_destroy(&allocator);
200-
free(output.value);
201201
free(string);
202202
return result;
203203
}
@@ -215,18 +215,18 @@ napi_value Herb_extract_html(napi_env env, napi_callback_info info) {
215215
char* string = CheckString(env, args[0]);
216216
if (!string) { return nullptr; }
217217

218-
hb_buffer_T output;
219-
if (!hb_buffer_init(&output, strlen(string))) {
218+
hb_allocator_T allocator;
219+
if (!hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA)) {
220220
free(string);
221-
napi_throw_error(env, nullptr, "Failed to initialize buffer");
221+
napi_throw_error(env, nullptr, "Failed to initialize allocator");
222222
return nullptr;
223223
}
224224

225-
hb_allocator_T allocator;
226-
if (!hb_allocator_init(&allocator, HB_ALLOCATOR_ARENA)) {
227-
free(output.value);
225+
hb_buffer_T output;
226+
if (!hb_buffer_init(&output, strlen(string), &allocator)) {
227+
hb_allocator_destroy(&allocator);
228228
free(string);
229-
napi_throw_error(env, nullptr, "Failed to initialize allocator");
229+
napi_throw_error(env, nullptr, "Failed to initialize buffer");
230230
return nullptr;
231231
}
232232

@@ -235,8 +235,8 @@ napi_value Herb_extract_html(napi_env env, napi_callback_info info) {
235235
napi_value result;
236236
napi_create_string_utf8(env, output.value, NAPI_AUTO_LENGTH, &result);
237237

238+
hb_buffer_free(&output);
238239
hb_allocator_destroy(&allocator);
239-
free(output.value);
240240
free(string);
241241
return result;
242242
}

rust/src/ffi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub use crate::bindings::{
2-
ast_node_free, element_source_to_string, hb_allocator_T, hb_allocator_destroy, hb_allocator_init, hb_array_get, hb_array_size, hb_buffer_init,
3-
hb_buffer_value, hb_string_T, herb_extract, herb_extract_ruby_to_buffer_with_options, herb_free_tokens, herb_lex, herb_parse, herb_prism_version,
4-
herb_version, token_type_to_string, HB_ALLOCATOR_ARENA,
2+
ast_node_free, element_source_to_string, hb_allocator_T, hb_allocator_dealloc, hb_allocator_destroy, hb_allocator_init, hb_array_get, hb_array_size,
3+
hb_buffer_free, hb_buffer_init, hb_buffer_value, hb_string_T, herb_extract, herb_extract_ruby_to_buffer_with_options, herb_free_tokens, herb_lex, herb_parse,
4+
herb_prism_version, herb_version, token_type_to_string, HB_ALLOCATOR_ARENA,
55
};

rust/src/herb.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,19 @@ pub fn extract_ruby_with_options(source: &str, options: &ExtractRubyOptions) ->
123123
unsafe {
124124
let c_source = CString::new(source).map_err(|e| e.to_string())?;
125125

126-
let mut output: hb_buffer_T = std::mem::zeroed();
127-
128-
if !crate::ffi::hb_buffer_init(&mut output, source.len()) {
129-
return Err("Failed to initialize buffer".to_string());
130-
}
131-
132126
let mut allocator: crate::ffi::hb_allocator_T = std::mem::zeroed();
133127

134128
if !crate::ffi::hb_allocator_init(&mut allocator, crate::ffi::HB_ALLOCATOR_ARENA) {
135-
libc::free(output.value as *mut std::ffi::c_void);
136129
return Err("Failed to initialize allocator".to_string());
137130
}
138131

132+
let mut output: hb_buffer_T = std::mem::zeroed();
133+
134+
if !crate::ffi::hb_buffer_init(&mut output, source.len(), &mut allocator) {
135+
crate::ffi::hb_allocator_destroy(&mut allocator);
136+
return Err("Failed to initialize buffer".to_string());
137+
}
138+
139139
let c_options = crate::bindings::herb_extract_ruby_options_T {
140140
semicolons: options.semicolons,
141141
comments: options.comments,
@@ -147,8 +147,8 @@ pub fn extract_ruby_with_options(source: &str, options: &ExtractRubyOptions) ->
147147
let c_str = std::ffi::CStr::from_ptr(crate::ffi::hb_buffer_value(&output));
148148
let rust_str = c_str.to_string_lossy().into_owned();
149149

150+
crate::ffi::hb_buffer_free(&mut output);
150151
crate::ffi::hb_allocator_destroy(&mut allocator);
151-
libc::free(output.value as *mut std::ffi::c_void);
152152

153153
Ok(rust_str)
154154
}
@@ -174,8 +174,8 @@ pub fn extract_html(source: &str) -> Result<String, String> {
174174
let c_str = std::ffi::CStr::from_ptr(result);
175175
let rust_str = c_str.to_string_lossy().into_owned();
176176

177+
crate::ffi::hb_allocator_dealloc(&mut allocator, result as *mut std::ffi::c_void);
177178
crate::ffi::hb_allocator_destroy(&mut allocator);
178-
libc::free(result as *mut std::ffi::c_void);
179179

180180
Ok(rust_str)
181181
}

src/include/lex_helpers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ static inline void herb_lex_to_buffer(const char* source, hb_buffer_T* output, h
1616
for (size_t i = 0; i < hb_array_size(tokens); i++) {
1717
token_T* token = hb_array_get(tokens, i);
1818

19-
hb_string_T type = token_to_string(token);
19+
hb_string_T type = token_to_string(allocator, token);
2020
hb_buffer_append_string(output, type);
21-
free(type.data);
21+
hb_allocator_dealloc(allocator, type.data);
2222

2323
hb_buffer_append(output, "\n");
2424
}

src/include/token.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <stdarg.h>
1111

1212
token_T* token_init(hb_string_T value, token_type_T type, lexer_T* lexer);
13-
hb_string_T token_to_string(const token_T* token);
13+
hb_string_T token_to_string(hb_allocator_T* allocator, const token_T* token);
1414
hb_string_T token_type_to_string(token_type_T type);
1515
hb_string_T token_type_to_friendly_string(token_type_T type);
1616
char* token_types_to_friendly_string_va(hb_allocator_T* allocator, token_type_T first_token, ...);

src/include/util.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
#include <stdbool.h>
66
#include <stdlib.h>
77

8+
struct hb_allocator;
9+
810
int is_newline(int character);
911
int is_whitespace(int character);
10-
hb_string_T escape_newlines(hb_string_T input);
11-
hb_string_T quoted_string(hb_string_T input);
12-
char* herb_strdup(const char* s);
12+
hb_string_T escape_newlines(struct hb_allocator* allocator, hb_string_T input);
13+
hb_string_T quoted_string(struct hb_allocator* allocator, hb_string_T input);
1314

1415
#endif

src/pretty_print.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
# include "include/errors.h"
99
# include "include/token_struct.h"
1010
# include "include/util.h"
11+
# include "include/util/hb_allocator.h"
1112
# include "include/util/hb_buffer.h"
1213
# include "include/util/hb_string.h"
1314

@@ -55,9 +56,9 @@ void pretty_print_quoted_property(
5556
const bool last_property,
5657
hb_buffer_T* buffer
5758
) {
58-
hb_string_T quoted = quoted_string(value);
59+
hb_string_T quoted = quoted_string(buffer->allocator, value);
5960
pretty_print_property(name, quoted, indent, relative_indent, last_property, buffer);
60-
free(quoted.data);
61+
hb_allocator_dealloc(buffer->allocator, quoted.data);
6162
}
6263

6364
void pretty_print_boolean_property(
@@ -215,9 +216,9 @@ void pretty_print_token_property(
215216
pretty_print_label(name, indent, relative_indent, last_property, buffer);
216217

217218
if (token != NULL && !hb_string_is_empty(token->value)) {
218-
hb_string_T quoted = quoted_string(token->value);
219+
hb_string_T quoted = quoted_string(buffer->allocator, token->value);
219220
hb_buffer_append_string(buffer, quoted);
220-
free(quoted.data);
221+
hb_allocator_dealloc(buffer->allocator, quoted.data);
221222

222223
hb_buffer_append(buffer, " ");
223224
pretty_print_location(token->location, buffer);
@@ -241,16 +242,16 @@ void pretty_print_string_property(
241242
hb_string_T quoted;
242243

243244
if (!hb_string_is_empty(string)) {
244-
escaped = escape_newlines(string);
245-
quoted = quoted_string(escaped);
245+
escaped = escape_newlines(buffer->allocator, string);
246+
quoted = quoted_string(buffer->allocator, escaped);
246247
value = quoted;
247248
}
248249

249250
pretty_print_property(name, value, indent, relative_indent, last_property, buffer);
250251

251252
if (!hb_string_is_empty(string)) {
252-
if (!hb_string_is_empty(escaped)) { free(escaped.data); }
253-
if (!hb_string_is_empty(quoted)) { free(quoted.data); }
253+
if (!hb_string_is_empty(escaped)) { hb_allocator_dealloc(buffer->allocator, escaped.data); }
254+
if (!hb_string_is_empty(quoted)) { hb_allocator_dealloc(buffer->allocator, quoted.data); }
254255
}
255256
}
256257

src/token.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,21 +161,23 @@ char* token_types_to_friendly_string_va(hb_allocator_T* allocator, token_type_T
161161
return result;
162162
}
163163

164-
hb_string_T token_to_string(const token_T* token) {
164+
hb_string_T token_to_string(hb_allocator_T* allocator, const token_T* token) {
165165
hb_string_T type_string = token_type_to_string(token->type);
166166
hb_string_T template =
167167
hb_string("#<Herb::Token type=\"%.*s\" value=\"%.*s\" range=[%u, %u] start=(%u:%u) end=(%u:%u)>");
168168

169-
char* string = calloc(template.length + type_string.length + token->value.length + 16, sizeof(char));
169+
char* string = hb_allocator_alloc(allocator, template.length + type_string.length + token->value.length + 16);
170170

171171
if (!string) { return HB_STRING_EMPTY; }
172172

173+
memset(string, 0, template.length + type_string.length + token->value.length + 16);
174+
173175
hb_string_T escaped;
174176

175177
if (token->type == TOKEN_EOF) {
176-
escaped = hb_string(herb_strdup("<EOF>"));
178+
escaped = hb_string(hb_allocator_strdup(allocator, "<EOF>"));
177179
} else {
178-
escaped = escape_newlines(token_value(token));
180+
escaped = escape_newlines(allocator, token_value(token));
179181
}
180182

181183
sprintf(
@@ -193,7 +195,7 @@ hb_string_T token_to_string(const token_T* token) {
193195
token->location.end.column
194196
);
195197

196-
free(escaped.data);
198+
hb_allocator_dealloc(allocator, escaped.data);
197199

198200
return hb_string(string);
199201
}

src/util.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ int is_whitespace(int character) {
1515
return character == ' ' || character == '\t' || character == '\n' || character == '\r';
1616
}
1717

18-
hb_string_T escape_newlines(hb_string_T input) {
19-
hb_allocator_T malloc_allocator = hb_allocator_with_malloc();
18+
hb_string_T escape_newlines(hb_allocator_T* allocator, hb_string_T input) {
2019
hb_buffer_T buffer;
2120

22-
hb_buffer_init(&buffer, input.length, &malloc_allocator);
21+
hb_buffer_init(&buffer, input.length, allocator);
2322

2423
for (size_t i = 0; i < input.length; ++i) {
2524
switch (input.data[i]) {
@@ -40,11 +39,10 @@ hb_string_T escape_newlines(hb_string_T input) {
4039
return hb_string(buffer.value);
4140
}
4241

43-
static hb_string_T wrap_string(hb_string_T input, char character) {
44-
hb_allocator_T malloc_allocator = hb_allocator_with_malloc();
42+
static hb_string_T wrap_string(hb_allocator_T* allocator, hb_string_T input, char character) {
4543
hb_buffer_T buffer;
4644

47-
hb_buffer_init(&buffer, input.length + 2, &malloc_allocator);
45+
hb_buffer_init(&buffer, input.length + 2, allocator);
4846

4947
hb_buffer_append_char(&buffer, character);
5048
hb_buffer_append_string(&buffer, input);
@@ -53,15 +51,6 @@ static hb_string_T wrap_string(hb_string_T input, char character) {
5351
return hb_string(buffer.value);
5452
}
5553

56-
hb_string_T quoted_string(hb_string_T input) {
57-
return wrap_string(input, '"');
58-
}
59-
60-
char* herb_strdup(const char* s) {
61-
size_t len = strlen(s) + 1;
62-
char* copy = malloc(len);
63-
64-
if (copy) { memcpy(copy, s, len); }
65-
66-
return copy;
54+
hb_string_T quoted_string(hb_allocator_T* allocator, hb_string_T input) {
55+
return wrap_string(allocator, input, '"');
6756
}

0 commit comments

Comments
 (0)