diff --git a/templates/ext/herb/error_helpers.c.erb b/templates/ext/herb/error_helpers.c.erb index a85bbf005..da80b05a1 100644 --- a/templates/ext/herb/error_helpers.c.erb +++ b/templates/ext/herb/error_helpers.c.erb @@ -31,9 +31,9 @@ static VALUE rb_<%= error.human %>_from_c_struct(<%= error.struct_type %>* <%= e ERROR_T* error = &<%= error.human %>->base; - VALUE type = rb_utf8_str_new_cstr(error_type_to_string(error)); + VALUE type = rb_string_from_hb_string(error_type_to_string(error)); VALUE location = rb_location_from_c_struct(error->location); - VALUE message = rb_utf8_str_new_cstr(error->message); + VALUE message = rb_string_from_hb_string(error->message); <%- error.fields.each do |field| -%> <%- case field -%> diff --git a/templates/java/error_helpers.c.erb b/templates/java/error_helpers.c.erb index e5ca9cca5..afd569bd2 100644 --- a/templates/java/error_helpers.c.erb +++ b/templates/java/error_helpers.c.erb @@ -18,7 +18,7 @@ jobject <%= error.name %>FromCStruct(JNIEnv* env, <%= error.struct_type %>* <%= jstring jtype = (*env)->NewStringUTF(env, "<%= error.name %>"); jobject location = CreateLocation(env, <%= error.human %>->base.location); - jstring jmessage = (*env)->NewStringUTF(env, <%= error.human %>->base.message); + jstring jmessage = CreateStringFromHbString(env, <%= error.human %>->base.message); return (*env)->NewObject(env, errorClass, constructor, jtype, location, jmessage); } diff --git a/templates/javascript/packages/node/extension/error_helpers.cpp.erb b/templates/javascript/packages/node/extension/error_helpers.cpp.erb index 4997b24b1..54a6e85b0 100644 --- a/templates/javascript/packages/node/extension/error_helpers.cpp.erb +++ b/templates/javascript/packages/node/extension/error_helpers.cpp.erb @@ -26,10 +26,10 @@ napi_value <%= error.name %>FromCStruct(napi_env env, <%= error.struct_type %>* napi_value result; napi_create_object(env, &result); - napi_value type = CreateString(env, error_type_to_string(&<%= error.human %>->base)); + napi_value type = CreateStringFromHbString(env, error_type_to_string(&<%= error.human %>->base)); napi_set_named_property(env, result, "type", type); - napi_value message = CreateString(env, <%= error.human %>->base.message); + napi_value message = CreateStringFromHbString(env, <%= error.human %>->base.message); napi_set_named_property(env, result, "message", message); napi_value location = CreateLocation(env, <%= error.human %>->base.location); diff --git a/templates/rust/src/ast/nodes.rs.erb b/templates/rust/src/ast/nodes.rs.erb index ad481c1a3..4786469c8 100644 --- a/templates/rust/src/ast/nodes.rs.erb +++ b/templates/rust/src/ast/nodes.rs.erb @@ -4,7 +4,6 @@ use crate::errors::*; use crate::nodes::*; use crate::union_types::*; use crate::{Location, Position}; -use std::ffi::CStr; use std::os::raw::c_void; unsafe fn convert_location(c_location: location_T) -> Location { @@ -22,11 +21,7 @@ unsafe fn convert_position(c_position: position_T) -> Position { <%- snake_name = error.name.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase -%> unsafe fn convert_<%= snake_name %>(error_ptr: *const <%= error.c_type %>) -> <%= error.name %> { let error_ref = &*error_ptr; - let message = if error_ref.base.message.is_null() { - String::new() - } else { - CStr::from_ptr(error_ref.base.message).to_string_lossy().into_owned() - }; + let message = string_from_hb_string(error_ref.base.message); let location = convert_location(error_ref.base.location); <%= error.name %>::new( diff --git a/templates/src/errors.c.erb b/templates/src/errors.c.erb index 74afc8e67..9e6514017 100644 --- a/templates/src/errors.c.erb +++ b/templates/src/errors.c.erb @@ -66,9 +66,9 @@ void error_init(ERROR_T* error, const error_type_T type, position_T start, posit if (truncated_c_string_<%= i %>) { free(truncated_c_string_<%= i %>); } <%- end -%> <%- end -%> - <%= error.human %>->base.message = hb_allocator_strdup(allocator, message); + <%= error.human %>->base.message = hb_string_copy(hb_string(message), allocator); <%- else -%> - <%= error.human %>->base.message = hb_allocator_strdup(allocator, "<%= error.message_template %>"); + <%= error.human %>->base.message = hb_string_copy(hb_string("<%= error.message_template %>"), allocator); <%- end -%> <%- error.fields.each do |field| -%> @@ -95,30 +95,30 @@ void append_<%= error.human %>(<%= (arguments + ["hb_array_T* errors"]).join(", } <%- end -%> -const char* error_type_to_string(ERROR_T* error) { +hb_string_T error_type_to_string(ERROR_T* error) { switch (error->type) { <%- errors.each do |error| -%> - case <%= error.type %>: return "<%= error.type %>"; + case <%= error.type %>: return hb_string("<%= error.type %>"); <%- end -%> } - return "Unknown error_type_T"; + return hb_string("Unknown error_type_T"); } -const char* error_human_type(ERROR_T* error) { +hb_string_T error_human_type(ERROR_T* error) { switch (error->type) { <%- errors.each do |error| -%> - case <%= error.type %>: return "<%= error.name %>"; + case <%= error.type %>: return hb_string("<%= error.name %>"); <%- end -%> } - return "Unknown error_type_T"; + return hb_string("Unknown error_type_T"); } void error_free_base_error(ERROR_T* error, hb_allocator_T* allocator) { if (error == NULL) { return; } - if (error->message != NULL) { hb_allocator_dealloc(allocator, error->message); } + if (!hb_string_is_null(error->message)) { hb_allocator_dealloc(allocator, error->message.data); } hb_allocator_dealloc(allocator, error); } @@ -210,13 +210,13 @@ static void error_pretty_print_<%= error.human %>(<%= error.struct_type %>* erro if (!error) { return; } hb_buffer_append(buffer, "@ "); - hb_buffer_append(buffer, error_human_type((ERROR_T*) error)); + hb_buffer_append_string(buffer, error_human_type((ERROR_T*) error)); hb_buffer_append(buffer, " "); pretty_print_location(error->base.location, buffer); hb_buffer_append(buffer, "\n"); - pretty_print_quoted_property(hb_string("message"), hb_string(error->base.message), indent, relative_indent, <%= error.fields.none? %>, buffer); + pretty_print_quoted_property(hb_string("message"), error->base.message, indent, relative_indent, <%= error.fields.none? %>, buffer); <%- error.fields.each_with_index do |field, index| -%> <%- case field -%> <%- when Herb::Template::PositionField -%> diff --git a/templates/src/include/errors.h.erb b/templates/src/include/errors.h.erb index f74b14a72..5b01db595 100644 --- a/templates/src/include/errors.h.erb +++ b/templates/src/include/errors.h.erb @@ -8,6 +8,7 @@ #include "util/hb_allocator.h" #include "util/hb_array.h" #include "util/hb_buffer.h" +#include "util/hb_string.h" typedef enum { <%- errors.each do |error| -%> @@ -18,7 +19,7 @@ typedef enum { typedef struct ERROR_STRUCT { error_type_T type; location_T location; - char* message; + hb_string_T message; } ERROR_T; <%- errors.each do |error| -%> @@ -42,10 +43,10 @@ void error_init(ERROR_T* error, error_type_T type, position_T start, position_T size_t error_sizeof(void); error_type_T error_type(ERROR_T* error); -char* error_message(ERROR_T* error); +hb_string_T error_message(ERROR_T* error); -const char* error_type_to_string(ERROR_T* error); -const char* error_human_type(ERROR_T* error); +hb_string_T error_type_to_string(ERROR_T* error); +hb_string_T error_human_type(ERROR_T* error); void error_free(ERROR_T* error, hb_allocator_T* allocator); diff --git a/templates/wasm/error_helpers.cpp.erb b/templates/wasm/error_helpers.cpp.erb index f9ca9c4e5..c7cf5f22b 100644 --- a/templates/wasm/error_helpers.cpp.erb +++ b/templates/wasm/error_helpers.cpp.erb @@ -34,8 +34,8 @@ val <%= error.name %>FromCStruct(<%= error.struct_type %>* <%= error.human %>) { val Object = val::global("Object"); val result = Object.new_(); - result.set("type", CreateString(error_type_to_string(&<%= error.human %>->base))); - result.set("message", CreateString(<%= error.human %>->base.message)); + result.set("type", CreateStringFromHbString(error_type_to_string(&<%= error.human %>->base))); + result.set("message", CreateStringFromHbString(<%= error.human %>->base.message)); result.set("location", CreateLocation(<%= error.human %>->base.location)); <%- error.fields.each do |field| -%> <%- case field -%>