comparison 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
comparison
equal deleted inserted replaced
293:2b045d5b673b 294:d1dc2d70bdfd
1 #{ 1 #{
2 includeSystemHeader: "unistd.h" 2 includeSystemHeader: "unistd.h"
3 includeSystemHeader: "sys/mman.h" 3 includeSystemHeader: "sys/mman.h"
4
5 normal <- :size {
6 #{
7 llProperty: bytes withType: uint32_t
8 llProperty: buffer withType: (void ptr)
9 llMessage: _init_buf withVars: {
10 sz <_ obj_int32 ptr
11 } andCode: :sz {
12 bytes <- sz num
13 buffer <- GC_MALLOC_ATOMIC: bytes
14 self
15 }
16
17 llMessage: _buf_ptr withVArs: {
18 ptrret <- cpointer ptr
19 } andCode: {
20 ptrret <- make_object: (addr_of: cpointer_meta) NULL 0
21 ptrret val!: buffer
22 ptrret
23 }
24
25 llMessage: length withVars: {
26 intret <- obj_int32 ptr
27 } andCode: {
28 intret <- make_object: (addr_of: obj_int32_meta) NULL 0
29 intret num!: bytes
30 intret
31 }
32
33 llMessage: set withVars: {
34 offset <- obj_int32 ptr
35 newval <- obj_uint8 ptr
36 } andCode: :offset newval {
37 (buffer castTo: (uint8_t ptr)) set: (offset num) (newval num)
38 self
39 }
40 llMessage: get withVars: {
41 offset <- obj_int32 ptr
42 ret <- obj_uint8 ptr
43 } andCode: :offset {
44 ret <- make_object: (addr_of: obj_uint8_meta) NULL 0
45 ret num!: ((buffer castTo: (uint8_t ptr)) get: (offset num))
46 ret
47 }
48
49 llMessage: stringFrom:to withVars:{
50 from <- obj_int32 ptr
51 to <- obj_int32 ptr
52 str <- string ptr
53 } andCode: :from :to {
54 //probably should do some UTF-8 validation at some point
55 str <- make_object: (addr_of: string_meta) NULL 0
56 str bytes!: (to num) - (from num)
57 str len!: (str bytes)
58 str data!: (GC_MALLOC_ATOMIC: (str bytes) + 1)
59 memcpy: (str data) buffer (str bytes)
60 (str data) set: (str bytes) 0
61 str
62 }
63
64 string <- {
65 stringFrom: 0 to:
66 }
67
68 findChar:else <- :char found :else {
69 notfound <- true
70 n <- length
71 i <- 0
72 while: { notFound && i < n } do: {
73 if: (get: i) = char {
74 notfound <- false
75 } else: {
76 i <- i + 1
77 }
78 }
79 if: notfound else else: {
80 found: i
81 }
82 }
83 } _init_buf: size
84 }
4 85
5 executable <- :size { 86 executable <- :size {
6 buf <- #{ 87 buf <- #{
7 llProperty: bytes withType: uint32_t 88 llProperty: bytes withType: uint32_t
8 llProperty: buffer withType: (void ptr) 89 llProperty: buffer withType: (void ptr)