about summary refs log tree commit diff stats
path: root/src/types/blob.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-09-01 16:35:51 +0200
committerbptato <nincsnevem662@gmail.com>2024-09-01 17:07:05 +0200
commitd07187fc186681e262afd67bd748206e40aef346 (patch)
treebef16c305013311c01f4ee821d03dc2550b75a95 /src/types/blob.nim
parentd1ff49b0fc39171859cfa781f459a2e0242c2e79 (diff)
downloadchawan-d07187fc186681e262afd67bd748206e40aef346.tar.gz
canvas: make sure we don't link to QJS
Diffstat (limited to 'src/types/blob.nim')
-rw-r--r--src/types/blob.nim38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/types/blob.nim b/src/types/blob.nim
index c0278bde..cc69fd7c 100644
--- a/src/types/blob.nim
+++ b/src/types/blob.nim
@@ -2,6 +2,8 @@ import std/options
 import std/posix
 import std/strutils
 
+import io/bufreader
+import io/bufwriter
 import monoucha/fromjs
 import monoucha/javascript
 import monoucha/jstypes
@@ -25,6 +27,42 @@ type
 jsDestructor(Blob)
 jsDestructor(WebFile)
 
+# Forward declarations
+proc deallocBlob*(opaque, p: pointer) {.raises: [].}
+
+#TODO it would be nice if we had a separate fd type that does sendAux;
+# this solution fails when blob isn't swritten by some module that does
+# not import it (just as a transitive dependency).
+proc swrite*(writer: var BufferedWriter; blob: Blob) =
+  if blob.fd.isSome:
+    writer.sendAux.add(blob.fd.get)
+  writer.swrite(blob of WebFile)
+  if blob of WebFile:
+    writer.swrite(WebFile(blob).name)
+  writer.swrite(blob.fd.isSome)
+  writer.swrite(blob.ctype)
+  writer.swrite(blob.size)
+  if blob.size > 0:
+    writer.writeData(blob.buffer, int(blob.size))
+
+proc sread*(reader: var BufferedReader; blob: var Blob) =
+  var isWebFile: bool
+  reader.sread(isWebFile)
+  blob = if isWebFile: WebFile() else: Blob()
+  if isWebFile:
+    reader.sread(WebFile(blob).name)
+  var hasFd: bool
+  reader.sread(hasFd)
+  if hasFd:
+    blob.fd = some(reader.recvAux.pop())
+  reader.sread(blob.ctype)
+  reader.sread(blob.size)
+  if blob.size > 0:
+    let buffer = alloc(blob.size)
+    reader.readData(blob.buffer, int(blob.size))
+    blob.buffer = buffer
+    blob.deallocFun = deallocBlob
+
 proc newBlob*(buffer: pointer; size: int; ctype: string;
     deallocFun: DeallocFun; opaque: pointer = nil): Blob =
   return Blob(