Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions templates/ext/herb/error_helpers.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%>
Expand Down
2 changes: 1 addition & 1 deletion templates/java/error_helpers.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 1 addition & 6 deletions templates/rust/src/ast/nodes.rs.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down
22 changes: 11 additions & 11 deletions templates/src/errors.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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| -%>
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 -%>
Expand Down
9 changes: 5 additions & 4 deletions templates/src/include/errors.h.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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| -%>
Expand All @@ -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| -%>
Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions templates/wasm/error_helpers.cpp.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%>
Expand Down
Loading