Skip to content

Instantly share code, notes, and snippets.

@fowlmouth
Created June 30, 2015 16:52
Show Gist options
  • Save fowlmouth/b858e7cc41abf3264fee to your computer and use it in GitHub Desktop.
Save fowlmouth/b858e7cc41abf3264fee to your computer and use it in GitHub Desktop.
# new object format is
# (space,data)
# space is a pointer to an object that handles its identity
# space is another object so it has components and its own space
# needed to interpret itself (? maybe)
#
# some spaces can use the 4 or 8 byte int as raw data if its unique
# (native-int space, native-float space)
# AggregateType component can be used to fulfil a local data object
# Resource space holds identity of a resource object
#
# memory management is a task of the Space:
# objects in native-int space are their identity
# local spaces can use reference counting
# a resource space on the server side would hold a list of open clients
# on the client side it would probably use reference counting, an open
# resource is like an open file
#
import tables, typetraits
type
ObjectPtr* = ptr ObjectHandle
ObjectHandle* = object
space*: ObjectPtr
identity*: int
Object* = ObjectHandle
# define a local data space
type
LocalData = tuple
refCount: int
data: cstring
LocalDataSpace* = object
objects: seq[LocalData]
IntegerSpace* = object
# rest of local data components
type
NimType* = distinct int
ByteRequirement* = distinct int
MTable* = object
messages*: Table[string, ObjectHandle]
Aggr* = object
components*: seq[ComponentInstance]
ComponentInstance* = tuple
offs: int
co: Object
proc init
init()
var static_tys: seq[Object]
var id {.global.} = 0
proc nextID(): int =
result = id
id += 1
proc typeID* (ty:typedesc): NimType =
var id{.global.} = nextID().NimType
return id
type UC* {.unchecked.}[T] = array[1,T]
proc `==` (ci:ComponentInstance; oh:ObjectHandle): bool =
ci.co == oh
proc init =
## first create a local data space
## er first create the components for a local
## data space, as if the local data already
## exists..
## the way to do this is allocate things, pass them around
## to where they need to be and THEN set them up
##
## i also need to create a local data component holder that can hold itself
template static_co_sz: int =
sizeof(Aggr) +
sizeof(NimType) +
sizeof(ByteRequirement) +
sizeof(MTable)
var local_data_space = cast[ObjectPtr](
alloc0(sizeof(ObjectHandle)))
type ProtoCO = tuple
id: NimType
sz: int
name: string
dat: cstring
oh: ObjectHandle
var cos = newSeq[ProtoCO](6)
template declCO (ty): stmt =
let id = typeID(ty)
cos[id.int] = (
id, sizeof(ty),name(ty),
cast[cstring](alloc0(static_co_sz())),
ObjectHandle(
space: local_data_space,
identity: id.int )
)
template `cx ty`: ObjectHandle {.inject.}=
cos[id.int].oh
# let o = allocObj(static_co_sz)
declCO NimType
declCO ByteRequirement
declCO MTable
declCO Aggr
declCO LocalDataSpace
declCO IntegerSpace
var stdComp = Aggr(components: @[
(0, cxAggr),
(sizeof(Aggr), cxNimType),
(sizeof(Aggr)+sizeof(NimType), cxByterequirement),
(sizeof(Aggr)+sizeof(NimType)+sizeof(ByteRequirement), cxMTable)
])
template local_space_offset: int =
sizeof(Aggr)+sizeof(NimType)+sizeof(ByteRequirement)+
sizeof(MTable)
var localSpace = Aggr(components: @[
(0, cxAggr),
(sizeof(Aggr), cxNimType),
(sizeof(Aggr)+sizeof(NimType), cxByterequirement),
(sizeof(Aggr)+sizeof(NimType)+sizeof(ByteRequirement), cxMTable),
(sizeof(Aggr)+sizeof(NimType)+sizeof(ByteRequirement)+
sizeof(MTable), cxLocalDataSpace)
])
let localSpaceSize =
sizeof(Aggr)+sizeof(NimType)+sizeof(ByteRequirement)+
sizeof(MTable)+sizeof(LocalDataSpace)
let localSpaceDat =
cast[ptr UC[byte]](alloc0(localSpaceSize))
let localSpace_localSpace =
cast[ptr LocalDataSpace](localSpaceDat[local_space_offset].addr)
let localSpaceID = nextID()
assert localSpaceID == 6
localSpace_localSpace.objects.newSeq 7
local_data_space.space = local_data_space
local_data_space.identity = localSpaceID
template ptrToData (some:ObjectHandle; offs:int; asKind:typedesc): expr =
assert(some.space == local_data_space)
cast[ptr asKind](localSpace_localSpace.objects[some.identity].data[offs].addr)
template getLocal (some: ObjectHandle; ty: typedesc): expr =
let aggrty = some.ptrToData(0, Aggr)
let id = typeID(ty)
let oh = ObjectHandle(
space: local_data_space,
identity: id.int
)
let idx = aggrty.components.find(oh)
#if idx == -1: return
some.ptrToData(aggrty.components[idx].offs, ty)
for idx,c in cos.pairs:
assert idx == c.id.int
echo c.id.int
cast[ptr Aggr](c.dat)[] = stdComp
localSpace_localSpace.objects[c.id.int].data = c.dat
## now the standard components can finally be initialized
c.oh.getLocal(NimType)[] = c.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment