Skip to content

Commit 716fb89

Browse files
authored
C: Use hb_string_T for ERROR_T.message and error functions (#1313)
Follow up on #687
1 parent 25e4140 commit 716fb89

File tree

7 files changed

+24
-28
lines changed

7 files changed

+24
-28
lines changed

templates/ext/herb/error_helpers.c.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ static VALUE rb_<%= error.human %>_from_c_struct(<%= error.struct_type %>* <%= e
3131

3232
ERROR_T* error = &<%= error.human %>->base;
3333

34-
VALUE type = rb_utf8_str_new_cstr(error_type_to_string(error));
34+
VALUE type = rb_string_from_hb_string(error_type_to_string(error));
3535
VALUE location = rb_location_from_c_struct(error->location);
36-
VALUE message = rb_utf8_str_new_cstr(error->message);
36+
VALUE message = rb_string_from_hb_string(error->message);
3737

3838
<%- error.fields.each do |field| -%>
3939
<%- case field -%>

templates/java/error_helpers.c.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobject <%= error.name %>FromCStruct(JNIEnv* env, <%= error.struct_type %>* <%=
1818

1919
jstring jtype = (*env)->NewStringUTF(env, "<%= error.name %>");
2020
jobject location = CreateLocation(env, <%= error.human %>->base.location);
21-
jstring jmessage = (*env)->NewStringUTF(env, <%= error.human %>->base.message);
21+
jstring jmessage = CreateStringFromHbString(env, <%= error.human %>->base.message);
2222

2323
return (*env)->NewObject(env, errorClass, constructor, jtype, location, jmessage);
2424
}

templates/javascript/packages/node/extension/error_helpers.cpp.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ napi_value <%= error.name %>FromCStruct(napi_env env, <%= error.struct_type %>*
2626
napi_value result;
2727
napi_create_object(env, &result);
2828

29-
napi_value type = CreateString(env, error_type_to_string(&<%= error.human %>->base));
29+
napi_value type = CreateStringFromHbString(env, error_type_to_string(&<%= error.human %>->base));
3030
napi_set_named_property(env, result, "type", type);
3131

32-
napi_value message = CreateString(env, <%= error.human %>->base.message);
32+
napi_value message = CreateStringFromHbString(env, <%= error.human %>->base.message);
3333
napi_set_named_property(env, result, "message", message);
3434

3535
napi_value location = CreateLocation(env, <%= error.human %>->base.location);

templates/rust/src/ast/nodes.rs.erb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::errors::*;
44
use crate::nodes::*;
55
use crate::union_types::*;
66
use crate::{Location, Position};
7-
use std::ffi::CStr;
87
use std::os::raw::c_void;
98

109
unsafe fn convert_location(c_location: location_T) -> Location {
@@ -22,11 +21,7 @@ unsafe fn convert_position(c_position: position_T) -> Position {
2221
<%- snake_name = error.name.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase -%>
2322
unsafe fn convert_<%= snake_name %>(error_ptr: *const <%= error.c_type %>) -> <%= error.name %> {
2423
let error_ref = &*error_ptr;
25-
let message = if error_ref.base.message.is_null() {
26-
String::new()
27-
} else {
28-
CStr::from_ptr(error_ref.base.message).to_string_lossy().into_owned()
29-
};
24+
let message = string_from_hb_string(error_ref.base.message);
3025
let location = convert_location(error_ref.base.location);
3126

3227
<%= error.name %>::new(

templates/src/errors.c.erb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ void error_init(ERROR_T* error, const error_type_T type, position_T start, posit
6666
if (truncated_c_string_<%= i %>) { free(truncated_c_string_<%= i %>); }
6767
<%- end -%>
6868
<%- end -%>
69-
<%= error.human %>->base.message = hb_allocator_strdup(allocator, message);
69+
<%= error.human %>->base.message = hb_string_copy(hb_string(message), allocator);
7070
<%- else -%>
71-
<%= error.human %>->base.message = hb_allocator_strdup(allocator, "<%= error.message_template %>");
71+
<%= error.human %>->base.message = hb_string_copy(hb_string("<%= error.message_template %>"), allocator);
7272
<%- end -%>
7373

7474
<%- error.fields.each do |field| -%>
@@ -95,30 +95,30 @@ void append_<%= error.human %>(<%= (arguments + ["hb_array_T* errors"]).join(",
9595
}
9696
<%- end -%>
9797

98-
const char* error_type_to_string(ERROR_T* error) {
98+
hb_string_T error_type_to_string(ERROR_T* error) {
9999
switch (error->type) {
100100
<%- errors.each do |error| -%>
101-
case <%= error.type %>: return "<%= error.type %>";
101+
case <%= error.type %>: return hb_string("<%= error.type %>");
102102
<%- end -%>
103103
}
104104

105-
return "Unknown error_type_T";
105+
return hb_string("Unknown error_type_T");
106106
}
107107

108-
const char* error_human_type(ERROR_T* error) {
108+
hb_string_T error_human_type(ERROR_T* error) {
109109
switch (error->type) {
110110
<%- errors.each do |error| -%>
111-
case <%= error.type %>: return "<%= error.name %>";
111+
case <%= error.type %>: return hb_string("<%= error.name %>");
112112
<%- end -%>
113113
}
114114

115-
return "Unknown error_type_T";
115+
return hb_string("Unknown error_type_T");
116116
}
117117

118118
void error_free_base_error(ERROR_T* error, hb_allocator_T* allocator) {
119119
if (error == NULL) { return; }
120120

121-
if (error->message != NULL) { hb_allocator_dealloc(allocator, error->message); }
121+
if (!hb_string_is_null(error->message)) { hb_allocator_dealloc(allocator, error->message.data); }
122122

123123
hb_allocator_dealloc(allocator, error);
124124
}
@@ -210,13 +210,13 @@ static void error_pretty_print_<%= error.human %>(<%= error.struct_type %>* erro
210210
if (!error) { return; }
211211

212212
hb_buffer_append(buffer, "@ ");
213-
hb_buffer_append(buffer, error_human_type((ERROR_T*) error));
213+
hb_buffer_append_string(buffer, error_human_type((ERROR_T*) error));
214214
hb_buffer_append(buffer, " ");
215215

216216
pretty_print_location(error->base.location, buffer);
217217
hb_buffer_append(buffer, "\n");
218218

219-
pretty_print_quoted_property(hb_string("message"), hb_string(error->base.message), indent, relative_indent, <%= error.fields.none? %>, buffer);
219+
pretty_print_quoted_property(hb_string("message"), error->base.message, indent, relative_indent, <%= error.fields.none? %>, buffer);
220220
<%- error.fields.each_with_index do |field, index| -%>
221221
<%- case field -%>
222222
<%- when Herb::Template::PositionField -%>

templates/src/include/errors.h.erb

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

1213
typedef enum {
1314
<%- errors.each do |error| -%>
@@ -18,7 +19,7 @@ typedef enum {
1819
typedef struct ERROR_STRUCT {
1920
error_type_T type;
2021
location_T location;
21-
char* message;
22+
hb_string_T message;
2223
} ERROR_T;
2324

2425
<%- errors.each do |error| -%>
@@ -42,10 +43,10 @@ void error_init(ERROR_T* error, error_type_T type, position_T start, position_T
4243
size_t error_sizeof(void);
4344
error_type_T error_type(ERROR_T* error);
4445

45-
char* error_message(ERROR_T* error);
46+
hb_string_T error_message(ERROR_T* error);
4647

47-
const char* error_type_to_string(ERROR_T* error);
48-
const char* error_human_type(ERROR_T* error);
48+
hb_string_T error_type_to_string(ERROR_T* error);
49+
hb_string_T error_human_type(ERROR_T* error);
4950

5051
void error_free(ERROR_T* error, hb_allocator_T* allocator);
5152

templates/wasm/error_helpers.cpp.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ val <%= error.name %>FromCStruct(<%= error.struct_type %>* <%= error.human %>) {
3434
val Object = val::global("Object");
3535
val result = Object.new_();
3636

37-
result.set("type", CreateString(error_type_to_string(&<%= error.human %>->base)));
38-
result.set("message", CreateString(<%= error.human %>->base.message));
37+
result.set("type", CreateStringFromHbString(error_type_to_string(&<%= error.human %>->base)));
38+
result.set("message", CreateStringFromHbString(<%= error.human %>->base.message));
3939
result.set("location", CreateLocation(<%= error.human %>->base.location));
4040
<%- error.fields.each do |field| -%>
4141
<%- case field -%>

0 commit comments

Comments
 (0)