annotate modules/bytearray.tp @ 263:98147071baf6

Add support for llMessage definitions in buildMethodTables
author Michael Pavone <pavone@retrodev.com>
date Mon, 14 Jul 2014 09:30:31 -0700
parents fb922651db29
children d1dc2d70bdfd
rev   line source
pavone@178 1 #{
pavone@178 2 includeSystemHeader: "unistd.h"
pavone@178 3 includeSystemHeader: "sys/mman.h"
pavone@178 4
pavone@178 5 executable <- :size {
pavone@178 6 buf <- #{
pavone@178 7 llProperty: bytes withType: uint32_t
pavone@178 8 llProperty: buffer withType: (void ptr)
pavone@178 9 llMessage: _init withVars: {
pavone@178 10 sz <- obj_int32 ptr
pavone@178 11 } andCode: :sz {
pavone@178 12 bytes <- sz num
pavone@178 13 buffer <- sbrk: bytes
pavone@178 14 mprotect: buffer bytes (PROT_READ or PROT_WRITE or PROT_EXEC)
pavone@178 15 self
pavone@178 16 }
pavone@178 17 llMessage: set withVars: {
pavone@178 18 offset <- obj_int32 ptr
pavone@178 19 newval <- obj_uint8 ptr
pavone@178 20 } andCode: :offset newval {
pavone@178 21 (buffer castTo: (uint8_t ptr)) set: (offset num) (newval num)
pavone@178 22 self
pavone@178 23 }
pavone@178 24 llMessage: get withVars: {
pavone@178 25 offset <- obj_int32 ptr
pavone@178 26 ret <- obj_uint8 ptr
pavone@178 27 } andCode: :offset {
pavone@178 28 ret <- make_object: (addr_of: obj_uint8_meta) NULL 0
pavone@178 29 ret num!: ((buffer castTo: (uint8_t ptr)) get: (offset num))
pavone@178 30 ret
pavone@178 31 }
pavone@178 32 llMessage: run withVars: {
pavone@178 33 fun <- uint64_t funptr
pavone@178 34 funret <- obj_uint64 ptr
pavone@178 35 } andCode: {
pavone@178 36 funret <- make_object: (addr_of: obj_uint64_meta) NULL 0
pavone@178 37 fun <- buffer
pavone@178 38 funret num!: ( fun: )
pavone@178 39 funret
pavone@178 40 }
pavone@178 41
pavone@178 42 llMessage: runWithArg withVars: {
pavone@178 43 fun <- uint64_t funptr: uint64_t
pavone@178 44 funret <- obj_uint64 ptr
pavone@178 45 arg <- obj_uint64 ptr
pavone@178 46 } andCode: :arg {
pavone@178 47 fun <- buffer
pavone@178 48 funret <- make_object: (addr_of: obj_uint64_meta) NULL 0
pavone@178 49 funret num!: ( fun: (arg num) )
pavone@178 50 funret
pavone@178 51 }
pavone@178 52 }
pavone@258 53 buf _init: size
pavone@178 54 }
pavone@178 55
pavone@178 56 executableFromBytes <- :bytes {
pavone@178 57 totalSize <- bytes fold: 0 with: :acc el {
pavone@178 58 acc + (el length)
pavone@178 59 }
pavone@178 60 ba <- executable: totalSize
pavone@178 61 bytes fold: 0 with: :idx el {
pavone@180 62 el flattenTo: ba at: idx
pavone@178 63 }
pavone@178 64 ba
pavone@178 65 }
pavone@178 66 }