added sync.Once; code cleanup
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 29 Nov 2022 12:01:29 +0000 (06:01 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 29 Nov 2022 12:01:29 +0000 (06:01 -0600)
compiler/src/wasm_emit.c
core/io/reader.onyx
core/std.onyx
core/string/string.onyx
core/sync/once.onyx [new file with mode: 0644]

index b8a046bc4d05a4cc3a3e6ba099678639b23d1c10..ecced22e275a7fb92bfb85aba567ed0b07c8b6f1 100644 (file)
@@ -4220,6 +4220,7 @@ static b32 emit_constexpr_(ConstExprContext *ctx, AstTyped *node, u32 offset) {
         patch.index = ctx->data_id;
         patch.location = offset;
         patch.offset = 0;
+        patch.data_id = 0;
 
         // Here, we cannot use the data_id property of the
         // memory reservation because there is no guarantee that
index f695029b2d5e3433d886ed39a113f5c9507b9605..bcd4663e0d9d332fa003fd42e34437d8a790eb08 100644 (file)
@@ -1,6 +1,6 @@
 package core.io
 
-use core {memory, math, array}
+use core {memory, math, array, iter}
 
 Reader :: struct {
     stream : ^Stream;
@@ -552,25 +552,16 @@ skip_bytes :: (use reader: ^Reader, bytes: u32) -> (skipped: i32, err: Error) {
     return 0, .None;
 }
 
-lines :: (r: ^Reader, inplace := false) -> Iterator(str) {
-    Context :: struct {
-        r: ^Reader;
-        inplace: bool;
-    }
-
-    c := new(Context, allocator=context.temp_allocator);
-    c.r = r;
-    c.inplace = inplace;
+lines :: (r: ^Reader, inplace := false) =>
+    iter.generator(^.{
+        r = r, inplace = inplace
 
-    next :: (use c: ^Context) -> (str, bool) {
-        line := r->read_line(consume_newline=true, inplace=inplace);
+    }, (ctx: $C) -> (str, bool) {
+        line := ctx.r->read_line(consume_newline=true, inplace=ctx.inplace);
 
         if line.count > 0 do return line, true;
         else do return "", false;
-    }
-
-    return .{ c, next };
-}
+    });
 
 
 #local reader_consume_error :: (use reader: ^Reader) -> Error {
index 7d79a1abd143f9d4dca3e62f5a059377e615943b..1537974f660b70dd57a95888fa719495e2ff250a 100644 (file)
@@ -92,6 +92,7 @@ package core
     #load "./sync/condition_variable"
     #load "./sync/semaphore"
     #load "./sync/barrier"
+    #load "./sync/once"
 
     #load "./threads/thread"
 }
index 668a020fa73a212e1d4e44fb9e63643772e562f9..ef901b8e1795a00fccffab48c4d204ebcc40deeb 100644 (file)
@@ -2,8 +2,20 @@ package core.string
 
 use core
 
+#doc "Generic procedure for turning something into a string."
 as_str :: #match {}
 
+#local HasAsStrMethod :: interface (t: $T) {
+    { T.as_str(t) } -> str;
+}
+
+#overload #precedence 10000
+as_str :: macro (t: $T/HasAsStrMethod) -> str {
+    return T.as_str(t);
+}
+
+
+
 free :: (s: str, allocator := context.allocator) do raw_free(allocator, s.data);
 
 alloc_copy :: (original: str, allocator := context.allocator) -> str {
diff --git a/core/sync/once.onyx b/core/sync/once.onyx
new file mode 100644 (file)
index 0000000..9c87fd0
--- /dev/null
@@ -0,0 +1,28 @@
+package core.sync
+
+Once :: struct {
+    done: bool;
+    mutex: Mutex;
+}
+
+#inject Once.exec :: #match #local {}
+
+#overload
+Once.exec :: (o: ^Once, f: () -> $R) {
+    if o.done do return;
+
+    mutex_lock(^o.mutex);
+    o.done = true;
+    f();
+    mutex_unlock(^o.mutex);
+}
+
+#overload
+Once.exec :: (o: ^Once, ctx: $Ctx, f: (Ctx) -> $R) {
+    if o.done do return;
+
+    mutex_lock(^o.mutex);
+    o.done = true;
+    f(ctx);
+    mutex_unlock(^o.mutex);
+}