about summary refs log tree commit diff stats
path: root/310copy-bytes.subx
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-10-25 18:45:11 -0700
committerKartik Agaram <vc@akkartik.com>2020-10-25 18:45:11 -0700
commita148b23a22709a45647fd07ce95db9147217a061 (patch)
treedb95e96fbb4a1da518f25bcda5cf863b34ecb4a1 /310copy-bytes.subx
parent8a6ad45d8d26c60b62a2a7ac8d594b3c4d1dbc45 (diff)
downloadmu-a148b23a22709a45647fd07ce95db9147217a061.tar.gz
7101 - tile: remove quotes when evaluating strings
This found several bugs due to me not checking for null strings.
Diffstat (limited to '310copy-bytes.subx')
-rw-r--r--310copy-bytes.subx41
1 files changed, 41 insertions, 0 deletions
diff --git a/310copy-bytes.subx b/310copy-bytes.subx
index 7a90495b..eed337c0 100644
--- a/310copy-bytes.subx
+++ b/310copy-bytes.subx
@@ -113,3 +113,44 @@ test-stream-to-string:
     89/<- %esp 5/r32/ebp
     5d/pop-to-ebp
     c3/return
+
+# like stream-to-string but ignore surrounding quotes
+# we might do other stuff here later
+unquote-stream-to-string:  # in: (addr stream _), out: (addr handle array _)
+    # . prologue
+    55/push-ebp
+    89/<- %ebp 4/r32/esp
+    # . save registers
+    50/push-eax
+    51/push-ecx
+    52/push-edx
+    56/push-esi
+    # esi = s
+    8b/-> *(ebp+8) 6/r32/esi
+    # var len/ecx: int = s->write - s->read - 2
+    8b/-> *esi 1/r32/ecx
+    2b/subtract *(esi+4) 1/r32/ecx
+    81 7/subop/compare %ecx 2/imm32
+    7c/jump-if-< $unquote-stream-to-string:end/disp8
+    81 5/subop/subtract %ecx 2/imm32
+    # allocate
+    (allocate-array Heap %ecx *(ebp+0xc))
+    # var in/edx: (addr byte) = s->data + s->read + 1
+    8b/-> *(esi+4) 2/r32/edx
+    8d/copy-address *(esi+edx+0xd) 2/r32/edx  # Stream-data + 1
+    # var dest/eax: (addr byte) = data for out
+    8b/-> *(ebp+0xc) 0/r32/eax
+    (lookup *eax *(eax+4))  # => eax
+    8d/copy-address *(eax+4) 0/r32/eax
+    #
+    (copy-bytes %edx %eax %ecx)
+$unquote-stream-to-string:end:
+    # . restore registers
+    5e/pop-to-esi
+    5a/pop-to-edx
+    59/pop-to-ecx
+    58/pop-to-eax
+    # . epilogue
+    89/<- %esp 5/r32/ebp
+    5d/pop-to-ebp
+    c3/return