diff modules/bytearray.tp @ 294:d1dc2d70bdfd

Move OS module out of backend Javascript into a proper module file. Add normal bytearray to bytearray module. Add read:to method to os module for reading into a bytearray.
author Michael Pavone <pavone@retrodev.com>
date Thu, 24 Jul 2014 21:58:26 -0700
parents fb922651db29
children 2a0a88799737
line wrap: on
line diff
--- a/modules/bytearray.tp	Thu Jul 24 09:43:18 2014 -0700
+++ b/modules/bytearray.tp	Thu Jul 24 21:58:26 2014 -0700
@@ -2,6 +2,87 @@
 	includeSystemHeader: "unistd.h"
 	includeSystemHeader: "sys/mman.h"
 
+	normal <- :size {
+		#{
+			llProperty: bytes withType: uint32_t
+			llProperty: buffer withType: (void ptr)
+			llMessage: _init_buf withVars: {
+				sz <_ obj_int32 ptr
+			} andCode: :sz {
+				bytes <- sz num
+				buffer <- GC_MALLOC_ATOMIC: bytes
+				self
+			}
+
+			llMessage: _buf_ptr withVArs: {
+				ptrret <- cpointer ptr
+			} andCode: {
+				ptrret <- make_object: (addr_of: cpointer_meta) NULL 0
+				ptrret val!: buffer
+				ptrret
+			}
+
+			llMessage: length withVars: {
+				intret <- obj_int32 ptr
+			} andCode: {
+				intret <- make_object: (addr_of: obj_int32_meta) NULL 0
+				intret num!: bytes
+				intret
+			}
+
+			llMessage: set withVars: {
+				offset <- obj_int32 ptr
+				newval <- obj_uint8 ptr
+			} andCode: :offset newval {
+				(buffer castTo: (uint8_t ptr)) set: (offset num) (newval num)
+				self
+			}
+			llMessage: get withVars: {
+				offset <- obj_int32 ptr
+				ret <- obj_uint8 ptr
+			} andCode: :offset {
+				ret <- make_object: (addr_of: obj_uint8_meta) NULL 0
+				ret num!: ((buffer castTo: (uint8_t ptr)) get: (offset num))
+				ret
+			}
+
+			llMessage: stringFrom:to withVars:{
+				from <- obj_int32 ptr
+				to <- obj_int32 ptr
+				str <- string ptr
+			} andCode: :from :to {
+				//probably should do some UTF-8 validation at some point
+				str <- make_object: (addr_of: string_meta) NULL 0
+				str bytes!: (to num) - (from num)
+				str len!: (str bytes)
+				str data!: (GC_MALLOC_ATOMIC: (str bytes) + 1)
+				memcpy: (str data) buffer (str bytes)
+				(str data) set: (str bytes) 0
+				str
+			}
+
+			string <- {
+				stringFrom: 0 to:
+			}
+
+			findChar:else <- :char found :else {
+				notfound <- true
+				n <- length
+				i <- 0
+				while: { notFound && i < n } do: {
+					if: (get: i) = char {
+						notfound <- false
+					} else: {
+						i <- i + 1
+					}
+				}
+				if: notfound else else: {
+					found: i
+				}
+			}
+		} _init_buf: size
+	}
+
 	executable <- :size {
 		buf <- #{
 			llProperty: bytes withType: uint32_t