about summary refs log tree commit diff stats
path: root/src/ips
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-06-02 00:36:54 +0200
committerbptato <nincsnevem662@gmail.com>2023-06-05 03:58:21 +0200
commit8027e52cb221c432bed64517015ebf3182e6166d (patch)
tree18991f9e74c8dcfc0ed7439f3bc78a0cfec9b2d6 /src/ips
parentb3b97465805b7367df461a4b7b830fabaccf3a89 (diff)
downloadchawan-8027e52cb221c432bed64517015ebf3182e6166d.tar.gz
Add support for canvas and multipart
Quite incomplete canvas implementation. Crucially, the layout engine
can't do much with whatever is drawn because it doesn't support images
yet.

I've re-introduced multipart as well, with the FormData API. For the
append function I've also introduced a hack to the JS binding generator
that allows requesting the JSContext pointer in nim procs. Really I
should just fix the union generator thing and add support for overloading.

In conclusion, for now the only thing canvas can be used for is exporting
it as PNG and uploading it somewhere. Also, we now have PNG encoding and
decoding too. (Now if only we had sixels as well...)
Diffstat (limited to 'src/ips')
-rw-r--r--src/ips/serialize.nim89
1 files changed, 56 insertions, 33 deletions
diff --git a/src/ips/serialize.nim b/src/ips/serialize.nim
index 0636e2e9..c2a6f5a6 100644
--- a/src/ips/serialize.nim
+++ b/src/ips/serialize.nim
@@ -7,7 +7,9 @@ import tables
 
 import io/request
 import js/regex
+import types/blob
 import types/buffersource
+import types/formdata
 import types/url
 
 proc swrite*(stream: Stream, n: SomeNumber)
@@ -54,9 +56,13 @@ proc swrite*(stream: Stream, obj: ref object)
 proc sread*(stream: Stream, obj: var ref object)
 func slen*(obj: ref object): int
 
-proc swrite*(stream: Stream, part: MimePart)
-proc sread*(stream: Stream, part: var MimePart)
-func slen*(part: MimePart): int
+proc swrite*(stream: Stream, part: FormDataEntry)
+proc sread*(stream: Stream, part: var FormDataEntry)
+func slen*(part: FormDataEntry): int
+
+proc swrite*(stream: Stream, blob: Blob)
+proc sread*(stream: Stream, blob: var Blob)
+func slen*(blob: Blob): int
 
 proc swrite*[T](stream: Stream, o: Option[T])
 proc sread*[T](stream: Stream, o: var Option[T])
@@ -242,40 +248,57 @@ func slen*(obj: ref object): int =
   if obj != nil:
     result += slen(obj[])
 
-proc swrite*(stream: Stream, part: MimePart) =
-  stream.swrite(part.isFile)
+proc swrite*(stream: Stream, part: FormDataEntry) =
+  stream.swrite(part.isstr)
   stream.swrite(part.name)
-  stream.swrite(part.content)
-  if part.isFile:
-    stream.swrite(part.filename)
-    stream.swrite(part.contentType)
-    stream.swrite(part.fileSize)
-    stream.swrite(part.isStream)
-
-proc sread*(stream: Stream, part: var MimePart) =
-  var isFile: bool
-  stream.sread(isFile)
-  if isFile:
-    part = MimePart(isFile: true)
+  stream.swrite(part.filename)
+  if part.isstr:
+    stream.swrite(part.svalue)
+  else:
+    stream.swrite(part.value)
+
+proc sread*(stream: Stream, part: var FormDataEntry) =
+  var isstr: bool
+  stream.sread(isstr)
+  if isstr:
+    part = FormDataEntry(isstr: true)
   else:
-    part = MimePart(isFile: false)
+    part = FormDataEntry(isstr: false)
   stream.sread(part.name)
-  stream.sread(part.content)
-  if part.isFile:
-    stream.sread(part.filename)
-    stream.sread(part.contentType)
-    stream.sread(part.fileSize)
-    stream.sread(part.isStream)
-
-func slen*(part: MimePart): int =
-  result += slen(part.isFile)
+  stream.sread(part.filename)
+  if part.isstr:
+    stream.sread(part.svalue)
+  else:
+    stream.sread(part.value)
+
+func slen*(part: FormDataEntry): int =
+  result += slen(part.isstr)
   result += slen(part.name)
-  result += slen(part.content)
-  if part.isFile:
-    result += slen(part.filename)
-    result += slen(part.contentType)
-    result += slen(part.fileSize)
-    result += slen(part.isStream)
+  result += slen(part.filename)
+  if part.isstr:
+    result += slen(part.svalue)
+  else:
+    result += slen(part.value)
+
+proc swrite*(stream: Stream, blob: Blob) =
+  stream.swrite(blob.ctype)
+  stream.swrite(blob.size)
+  #TODO ??
+  stream.writeData(blob.buffer, int(blob.size))
+
+proc sread*(stream: Stream, blob: var Blob) =
+  new(blob)
+  stream.sread(blob.ctype)
+  stream.sread(blob.size)
+  blob.buffer = alloc(blob.size)
+  blob.deallocFun = dealloc
+  #TODO ??
+  assert stream.readData(blob.buffer, int(blob.size)) == int(blob.size)
+
+func slen*(blob: Blob): int =
+  result += slen(blob.ctype)
+  result += slen(blob.size)
+  result += int(blob.size) #TODO ??
 
 proc swrite*[T](stream: Stream, o: Option[T]) =
   stream.swrite(o.issome)