From a28d0919aefc44c1a53eb2484e1cc6a33eb265da Mon Sep 17 00:00:00 2001 From: Guille Polito Date: Thu, 26 Sep 2019 15:52:56 +0200 Subject: [PATCH] Resolution of constants --- .../FFIFunctionParserTest.class.st | 18 ++++ .../FFIFunctionResolutionTest.class.st | 86 ++++++++++++++++++- src/UnifiedFFI/FFIValueArgument.class.st | 10 ++- 3 files changed, 111 insertions(+), 3 deletions(-) diff --git a/src/UnifiedFFI-Tests/FFIFunctionParserTest.class.st b/src/UnifiedFFI-Tests/FFIFunctionParserTest.class.st index a8c83a0d977..2bc2ddee203 100644 --- a/src/UnifiedFFI-Tests/FFIFunctionParserTest.class.st +++ b/src/UnifiedFFI-Tests/FFIFunctionParserTest.class.st @@ -13,6 +13,12 @@ FFIFunctionParserTest >> assertIsFalse: anArgument [ self assert: anArgument value equals: 'false' ] +{ #category : #asserting } +FFIFunctionParserTest >> assertIsNULL: anArgument [ + + self assert: anArgument value equals: 'NULL' +] + { #category : #asserting } FFIFunctionParserTest >> assertIsNil: anArgument [ @@ -211,6 +217,18 @@ FFIFunctionParserTest >> testUntypedFalseArgumentIsFalse [ self assertIsFalse: argument ] +{ #category : #tests } +FFIFunctionParserTest >> testUntypedNilArgumentIsNULL [ + | parser argument | + + parser := self newParser + setStream: 'NULL' readStream; + yourself. + + argument := parser parseArgument. + self assertIsNULL: argument +] + { #category : #tests } FFIFunctionParserTest >> testUntypedNilArgumentIsNil [ | parser argument | diff --git a/src/UnifiedFFI-Tests/FFIFunctionResolutionTest.class.st b/src/UnifiedFFI-Tests/FFIFunctionResolutionTest.class.st index 5c192da4307..f6d2ac507b0 100644 --- a/src/UnifiedFFI-Tests/FFIFunctionResolutionTest.class.st +++ b/src/UnifiedFFI-Tests/FFIFunctionResolutionTest.class.st @@ -7,10 +7,22 @@ Class { #category : #'UnifiedFFI-Tests-Tests' } +{ #category : #helpers } +FFIFunctionResolutionTest >> pushInstVar: anInteger [ + + stack push: {#ivar . anInteger} +] + { #category : #helpers } FFIFunctionResolutionTest >> pushLiteral: aLiteral [ - stack push: aLiteral + stack push: { #literal . aLiteral } +] + +{ #category : #helpers } +FFIFunctionResolutionTest >> pushReceiver [ + + stack push: #( self ) ] { #category : #running } @@ -20,6 +32,20 @@ FFIFunctionResolutionTest >> setUp [ stack := Stack new. ] +{ #category : #tests } +FFIFunctionResolutionTest >> testResolveConstantFalseShouldSetConstantZeroLoader [ + + | argument | + argument := FFIValueArgument new + value: 'false'; + yourself. + + argument resolveUsing: FFICallout new. + argument loader emitArgument: self context: nil. + + self assert: stack pop equals: { #literal . 0 }. +] + { #category : #tests } FFIFunctionResolutionTest >> testResolveConstantIntegerShouldSetConstantLoader [ @@ -31,5 +57,61 @@ FFIFunctionResolutionTest >> testResolveConstantIntegerShouldSetConstantLoader [ argument resolveUsing: FFICallout new. argument loader emitArgument: self context: nil. - self assert: stack pop equals: 1. + self assert: stack pop equals: { #literal . 1}. +] + +{ #category : #tests } +FFIFunctionResolutionTest >> testResolveConstantNULLShouldSetConstantNullLoader [ + + | argument | + argument := FFIValueArgument new + value: 'NULL'; + yourself. + + argument resolveUsing: FFICallout new. + argument loader emitArgument: self context: nil. + + self assert: stack pop equals: { #literal . ExternalAddress null}. +] + +{ #category : #tests } +FFIFunctionResolutionTest >> testResolveConstantNilShouldSetConstantNullLoader [ + + | argument | + argument := FFIValueArgument new + value: 'nil'; + yourself. + + argument resolveUsing: FFICallout new. + argument loader emitArgument: self context: nil. + + self assert: stack pop equals: { #literal . ExternalAddress null }. +] + +{ #category : #tests } +FFIFunctionResolutionTest >> testResolveConstantSelfShouldSetConstantSelfLoader [ + + | argument | + argument := FFIValueArgument new + value: 'self'; + yourself. + + argument resolveUsing: FFICallout new. + argument loader emitArgument: self context: nil. + + self assert: stack pop equals: #(#self). +] + +{ #category : #tests } +FFIFunctionResolutionTest >> testResolveConstantTrueShouldSetConstantOneLoader [ + + | argument | + argument := FFIValueArgument new + value: 'true'; + yourself. + + argument resolveUsing: FFICallout new. + argument loader emitArgument: self context: nil. + + self assert: stack pop equals: { #literal . 1 }. ] diff --git a/src/UnifiedFFI/FFIValueArgument.class.st b/src/UnifiedFFI/FFIValueArgument.class.st index 253d25206e4..246704d0a32 100644 --- a/src/UnifiedFFI/FFIValueArgument.class.st +++ b/src/UnifiedFFI/FFIValueArgument.class.st @@ -40,7 +40,15 @@ FFIValueArgument >> loader [ { #category : #resolving } FFIValueArgument >> resolveUsing: aResolver [ - + + value = 'false' ifTrue: [ + ^ resolution := FFIConst value: 0 type: (aResolver resolveType: #bool) ]. + value = 'true' ifTrue: [ + ^ resolution := FFIConst value: 1 type: (aResolver resolveType: #bool) ]. + (value = 'nil' or: [ value = 'NULL' ]) ifTrue: [ + ^ resolution := FFIConst value: ExternalAddress null type: (aResolver resolveType: #'void *') ]. + value = 'self' ifTrue: [ + ^ resolution := FFISelfArgument new ]. resolution := FFIConst value: value ]