[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

api[node-ffi] 01 S8Ref

NativeObject
 subclass: #S8Ref
 category: #S8Ref!
S8Ref comment: '
	Implement native object access to pointers using Node.JS buffer instances
	@2016 Alejandro Reimondo - [email protected]
	See https://github.com/TooTallNate/ref
'!
NativeObject subclass: #S8RefType category: #S8Ref!
S8Ref class instanceVariableNames: #current!
S8Ref class buildAccessors: #(
	#(current initialize 'self new')
)!
! S8Ref class methodsFor: #accessing !
currentOrNil
	" Returns the current instance (or nil). "

	^current! !

! S8Ref methodsFor: #private !
initializeHandle
	" Private - Initialize the handle of the receiver. "

	[	handle := self require: #Ref
	] on: Error do: [
		handle := self require: #S8Ref
	]! !

S8Ref buildGetters: #(
	endianness
)!
S8Ref buildFunctions: #(
	#(address: #(#(buffer json)))
	#(address:offset: #(#(buffer json) offset))
	#(hexAddress:offset: #(#(buffer json) offset))
	#(hexAddress: #(#(buffer json)))
	#(isNull:  #(#(buffer json)) Boolean)
	#(readObject:offset: #(#(buffer json) offset))
	#(readPointer:offset: #(#(buffer json) offset) NodeBuffer)
	#(readCString:offset: #(#(buffer json) offset))
	#(readCString: #(#(buffer json)))
	#(readInt64BE:offset: #(#(buffer json) offset))
	#(readInt64LE:offset: #(#(buffer json) offset))
	#(readUInt64BE:offset: #(#(buffer json) offset))
	#(readUInt64LE:offset: #(#(buffer json) offset))
	#(writeInt64BE:offset:value: #(#(buffer json) offset value))
	#(writeInt64LE:offset:value: #(#(buffer json) offset value))
	#(writeUInt64BE:offset:value: #(#(buffer json) offset value))
	#(writeUInt64LE:offset:value: #(#(buffer json) offset value))
	#(refType: #(#(type json)) S8RefType)
	#(derefType: #(#(type json)) S8RefType)
	#(coerceType: #(#(type json)) S8RefType)
	#(getType: #(#(buffer json)) S8RefType)
	#(get: #(#(buffer json)))
	#(get:offset: #(#(buffer json) offset))
	#(get:offset:type: #(#(buffer json) offset #(type json)))
	#(set:offset:value: #(#(buffer json) offset #(value json)))
	#(set:offset:value:type: #(#(buffer json) offset #(value json) #(type json)))
	#(alloc:value: #(#(type json) #(value json)) NodeBuffer)
	#(alloc: #(#(type json)) NodeBuffer)
	#(allocCString:encoding: #(aString encoding) NodeBuffer)
	#(allocCString: #(aString) NodeBuffer)
	#(writeCString:value: #(#(buffer json) #(value json)))
	#(writeCString:offset:value:encoding: #(#(buffer json) offset #(value json) encoding))
	#(writeCString:offset:value: #(#(buffer json) offset #(value json)))
	#(readInt64:offset: #(#(buffer json) offset))
	#(readUInt64:offset: #(#(buffer json) offset))
	#(writeInt64:offset:value: #(#(buffer json) offset value))
	#(writeUInt64:offset:value: #(#(buffer json) offset value))
	#(ref: #(#(buffer json)) NodeBuffer) #(ref NodeBuffer)
	#(deref: #(#(buffer json)) NodeBuffer) #(deref NodeBuffer)
	#(writeObject:offset:value:persistent: #(#(buffer json) offset #(value json) persistent))
	#(writeObject:offset:value: #(#(buffer json) offset #(value json)))
	#(writePointer:offset:value: #(#(buffer json) offset #(ptr json)))
	#(writePointer:value: #(#(buffer json) #(ptr json)))
	#(reinterpret:size:offset: #(#(buffer json) size offset) NodeBuffer)
	#(reinterpret:size: #(#(buffer json) size) NodeBuffer)
	#(reinterpretUntilZeros:size: #(#(buffer json) numOf0Bytes) NodeBuffer)
	#(reinterpretUntilZeros:size:offset: #(#(buffer json) numOf0Bytes offset) NodeBuffer)
)!

! S8Ref methodsFor: #accessing !
types
	" Returns the types array. "

	| js pool |
	js := JS @ (self handle basicAt: #types).
	pool := PoolDictionary new.
	js propertiesDo: [:key :value| pool at: key put: S8RefType @ value ].
	^pool! !

! S8Ref class methodsFor: #accessing !
type: aTypeName ifAbsent: aBlock
	" Returns the type at aTypeName. "

	| types |
	aTypeName = #pointer ifTrue: [
		^self current coerceType: #pointer
	].
	types := self current handle basicAt: #types.
	^S8RefType @ (types basicAt: aTypeName ifAbsent: aBlock)! !

! S8Ref class methodsFor: #accessing !
endianness
	" Returns the native endianness of the machine's processor. "

	^self current endianness! !

! S8Ref class methodsFor: #accessing !
type: aTypeName
	" Returns the type at aTypeName. "

	^self type: aTypeName ifAbsent: [
		self error: 'Type not found ',aTypeName
	]! !

! S8Ref class methodsFor: #instantiation !
@ aNameOrHandle
 " Returns aninstance of the receiver. "

 ((self isObject: aNameOrHandle) and: [aNameOrHandle isString]) ifTrue: [
  ^self type: aNameOrHandle
 ].
 ^super @ aNameOrHandle! !

! S8Ref class methodsFor: #accessing !
sizeOf: aTypeName
	" Returns the size of type at aTypeName. "

	^(self type: aTypeName) size! !

! S8Ref class methodsFor: #accessing !
alignOf: aTypeName
	" Returns the alignment of type at aTypeName. "

	^(self type: aTypeName) alignment! !

! S8Ref class methodsFor: #accessing !
types
	" Returns the types array. "

	^self current types! !

! S8Ref methodsFor: #accessing !
null
	" Returns a Buffer that references the C NULL pointer. "

	^NodeBuffer @ (self handle basicAt: #NULL)! !

! S8Ref methodsFor: #accessing !
nullPointer
	" Returns a pointer-sized Buffer instance pointing to NULL. "

	^NodeBuffer @ (self handle basicAt: #NULL_POINTER)! !

! S8Ref class methodsFor: #accessing !
null
	" Returns a Buffer that references the C NULL pointer. "

	^self current null! !

! S8Ref class methodsFor: #accessing !
nullPointer
	" Returns a pointer-sized Buffer instance pointing to NULL. "

	^self current nullPointer! !

! S8Ref class methodsFor: #accessing !
voidPointer
	" Returns the void* type. "

	^self refType: (self type: #void)! !

! S8Ref class methodsFor: #accessing !
voidPtr
	" Returns the void* type. "

	^self refType: (self type: #void)! !

! S8Ref class methodsFor: #alloc !
alloc: type
	" Allocate a buffer of specified type. "

	^self current alloc: type! !

! S8Ref class methodsFor: #types !
refType: type

	^self current refType: type! !

S8RefType buildGetters: #(
	indirection
	size
	alignment
	name
)!
S8RefType buildFunctions: #(
	#(get:offset: #(#(buffer json) offset))
	#(set:offset:value: #(#(buffer json) offset #(value json)))
)!

! S8RefType methodsFor: #accessing !
refType
	" Returns the type of reference to the receiver. "

	^S8Ref refType: self! !

! NodeBuffer methodsFor: #S8Ref !
address
	^S8Ref current address: self ! !

! NodeBuffer methodsFor: #S8Ref !
hexAddress
	^S8Ref current hexAddress: self ! !

! NodeBuffer methodsFor: #S8Ref !
endianness
	^S8Ref current endianness! !

! NodeBuffer methodsFor: #S8Ref !
isNull
	^S8Ref current isNull: self ! !

! NodeBuffer methodsFor: #S8Ref !
readObject: offset
	^S8Ref current readObject: self offset: offset ! !

! NodeBuffer methodsFor: #S8Ref !
readPointer: offset
	^S8Ref current readPointer: self offset: offset ! !

! NodeBuffer methodsFor: #S8Ref !
readCString
	^S8Ref current readCString: self ! !

! NodeBuffer methodsFor: #S8Ref !
readCString: offset
	^S8Ref current readCString: self offset: offset ! !

! NodeBuffer methodsFor: #S8Ref !
readInt64BE: offset
	^S8Ref current readInt64BE: self offset: offset ! !

! NodeBuffer methodsFor: #S8Ref !
readInt64LE: offset
	^S8Ref current readInt64LE: self offset: offset ! !

! NodeBuffer methodsFor: #S8Ref !
readUInt64BE: offset
	^S8Ref current readUInt64BE: self offset: offset ! !

! NodeBuffer methodsFor: #S8Ref !
readUInt64LE: offset
	^S8Ref current readUInt64LE: self offset: offset ! !

! NodeBuffer methodsFor: #S8Ref !
writeInt64BE: offset value: value
	^S8Ref current writeInt64BE: self offset: offset value: value! !

! NodeBuffer methodsFor: #S8Ref !
writeInt64LE: offset value: value
	^S8Ref current writeInt64LE: self offset: offset value: value! !

! NodeBuffer methodsFor: #S8Ref !
writeUInt64BE: offset value: value
	^S8Ref current writeUInt64BE: self offset: offset value: value! !

! NodeBuffer methodsFor: #S8Ref !
writeUInt64LE: offset value: value
	^S8Ref current writeUInt64LE: self offset: offset value: value! !

! NodeBuffer methodsFor: #S8Ref !
getType
	^S8Ref current getType: self! !

! NodeBuffer methodsFor: #S8Ref !
type
	^S8Ref current getType: self! !

! NodeBuffer methodsFor: #S8Ref !
get: offset type: type
	^S8Ref current get: self offset: offset type: type! !

! NodeBuffer methodsFor: #S8Ref !
get: offset
	^S8Ref current get: self offset: offset! !

! NodeBuffer methodsFor: #S8Ref !
get
	^S8Ref current get: self! !

! NodeBuffer methodsFor: #S8Ref !
set: offset value: value type: type
	^S8Ref current set: self offset: offset value: value type: type! !

! NodeBuffer methodsFor: #S8Ref !
set: offset value: value
	^S8Ref current set: self offset: offset value: value! !

! NodeBuffer methodsFor: #S8Ref !
writeCString: offset value: value encoding: encoding
	^S8Ref current
		writeCString: self offset: offset
		value: value encoding: encoding! !

! NodeBuffer methodsFor: #S8Ref !
writeCString: offset value: value
	^S8Ref current
		writeCString: self offset: offset
		value: value! !

! NodeBuffer methodsFor: #S8Ref !
writeCString: value
	^S8Ref current
		writeCString: self
		value: value! !

! NodeBuffer methodsFor: #S8Ref !
readInt64: offset
	^S8Ref current readInt64: self offset: offset! !

! NodeBuffer methodsFor: #S8Ref !
readUInt64: offset
	^S8Ref current readUInt64: self offset: offset! !

! NodeBuffer methodsFor: #S8Ref !
writeInt64: offset value: value
	^S8Ref current writeInt64: self offset: offset value: value! !

! NodeBuffer methodsFor: #S8Ref !
writeUInt64: offset value: value
	^S8Ref current writeUInt64: self offset: offset value: value! !

! NodeBuffer methodsFor: #S8Ref !
ref
	^S8Ref current ref: self! !

! NodeBuffer methodsFor: #S8Ref !
deref
	^S8Ref current deref: self! !

! NodeBuffer methodsFor: #S8Ref !
writeObject: offset value: value persistent: persistent
	^S8Ref current writeObject: self offset: offset value: value persistent: persistent! !

! NodeBuffer methodsFor: #S8Ref !
writeObject: offset value: value
	^S8Ref current writeObject: self offset: offset value: value! !

! NodeBuffer methodsFor: #S8Ref !
writePointer: value
	^S8Ref current writePointer: self value: value! !

! NodeBuffer methodsFor: #S8Ref !
writePointer: offset value: value
	^S8Ref current writePointer: self offset: offset value: value! !

! NodeBuffer methodsFor: #S8Ref !
reinterpret: size offset: offset
	^S8Ref current reinterpret: self size: size offset: offset! !

! NodeBuffer methodsFor: #S8Ref !
reinterpret: size
	^S8Ref current reinterpret: self size: size! !

! NodeBuffer methodsFor: #S8Ref !
reinterpretUntilZeros: size
	^S8Ref current reinterpretUntilZeros: self size: size! !

! NodeBuffer methodsFor: #S8Ref !
reinterpretUntilZeros: size offset: offset
	^S8Ref current reinterpretUntilZeros: self size: size offset: offset! !