From 405b9bc4246fa5b69ea2497f10c79dd21db2e497 Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Wed, 17 May 2023 17:47:48 -0500 Subject: [PATCH] bugfixes and additions See changes in CHANGELOG --- CHANGELOG | 6 +++++- compiler/src/checker.c | 14 -------------- core/container/iter.onyx | 8 ++++++-- core/encoding/json/encoder.onyx | 2 +- core/io/writer.onyx | 1 + core/runtime/platform/js/platform.onyx | 5 ++++- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 065676e4..3caae898 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -11,9 +11,13 @@ Additions: Removals: Changes: +* `iter.single` can take a `dispose` function, which is called on close of the + iterator, with the single value yielded. +* `io.write_escaped_str` supports escaping "\\" now. -Bugfixes: +Bugfixes: +* `json` encoder was wrongly not encoding strings when using `encode` on an `any`. diff --git a/compiler/src/checker.c b/compiler/src/checker.c index 2f6be84a..e59cf451 100644 --- a/compiler/src/checker.c +++ b/compiler/src/checker.c @@ -2912,20 +2912,6 @@ CheckStatus check_function_header(AstFunction* func) { ERROR(param->local->token->pos, "Compound types are not allowed as parameter types. Try splitting this into multiple parameters."); } - // NOTE: I decided to make parameter default values not type checked against - // the actual parameter type. The actual type checking will happen in check_call - // when the default value is used as an argument and then has to be checked against - // the parameter type - brendanfh 2021/01/06 - // if (param->default_value != NULL) { - // if (!unify_node_and_type(¶m->default_value, param->local->type)) { - // onyx_report_error(param->local->token->pos, - // "Expected default value of type '%s', was of type '%s'.", - // type_get_name(param->local->type), - // type_get_name(param->default_value->type)); - // return Check_Error; - // } - // } - if (param->vararg_kind != VA_Kind_Not_VA) has_had_varargs = 1; if (local->type->kind != Type_Kind_Array && type_size_of(local->type) == 0) { diff --git a/core/container/iter.onyx b/core/container/iter.onyx index 5e37c928..e4fbe304 100644 --- a/core/container/iter.onyx +++ b/core/container/iter.onyx @@ -451,14 +451,18 @@ const :: (value: $T) -> Iterator(T) { } #doc "Yields a single value, then stops." -single :: (value: $T) -> Iterator(T) { - return generator(&.{ v = value, yielded = false }, c => { +single :: (value: $T, dispose: (T) -> void = null_proc) -> Iterator(T) { + return generator(&.{ v = value, yielded = false, dispose = dispose }, c => { if !c.yielded { c.yielded = true; return c.v, true; } return .{}, false; + }, c => { + if c.dispose != null_proc { + c.dispose(c.v); + } }); } diff --git a/core/encoding/json/encoder.onyx b/core/encoding/json/encoder.onyx index c2d981a0..f0522c47 100644 --- a/core/encoding/json/encoder.onyx +++ b/core/encoding/json/encoder.onyx @@ -174,7 +174,7 @@ encode :: (w: ^io.Writer, data: any) -> Encoding_Error { case .Slice, .Dynamic_Array { if data.type == str { - io.write_format_va(w, "{\"}", .[data]); + io.write_escaped_str(w, *misc.any_as(data, str)); break; } diff --git a/core/io/writer.onyx b/core/io/writer.onyx index f01b57e4..ad94c566 100644 --- a/core/io/writer.onyx +++ b/core/io/writer.onyx @@ -175,6 +175,7 @@ write_escaped_str :: (use writer: &Writer, s: str) { case #char "\f" { write_byte(writer, #char "\\"); write_byte(writer, #char "f"); } case #char "\0" { write_byte(writer, #char "\\"); write_byte(writer, #char "0"); } case #char "\"" { write_byte(writer, #char "\\"); write_byte(writer, #char "\""); } + case #char "\\" { write_byte(writer, #char "\\"); write_byte(writer, #char "\\"); } case #default { // @Speed diff --git a/core/runtime/platform/js/platform.onyx b/core/runtime/platform/js/platform.onyx index fe162d8e..fe919cfa 100644 --- a/core/runtime/platform/js/platform.onyx +++ b/core/runtime/platform/js/platform.onyx @@ -24,7 +24,10 @@ __output_string :: (s: str) -> u32 #foreign "host" "print_str" --- __output_error :: (s: str) -> u32 #foreign "host" "print_str" --- __exit :: (status: i32) -> void #foreign "host" "exit" --- __time :: () -> i64 #foreign "host" "time" --- -__read_from_input :: (buf: [] u8) -> u32 do return 0; + +#if !#defined(__read_from_input) { + __read_from_input :: (buf: [] u8) -> u32 do return 0; +} // Sets up everything needed for execution. __start :: () { -- 2.25.1