Skip to content

Commit

Permalink
Extend interop with BigInteger, meta parents, and readBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Oct 22, 2024
1 parent 2747e4e commit 5ae9534
Show file tree
Hide file tree
Showing 38 changed files with 126 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*TruffleSqueak-Core-meta-object
interopGetMetaParents
^ {self superclass}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*TruffleSqueak-Core-meta-object
interopHasMetaParents
^ true
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
"class" : {
},
"instance" : {
"interopGetMetaParents" : "fn 10/22/2024 12:07",
"interopGetMetaQualifiedName" : "fn 3/9/2021 12:39",
"interopGetMetaSimpleName" : "fn 3/9/2021 12:38",
"interopHasMetaParents" : "fn 10/22/2024 12:07",
"interopInstantiate:" : "fn 3/9/2021 12:39",
"interopIsInstantiable" : "fn 3/9/2021 12:39",
"interopIsMetaInstance:" : "fn 3/9/2021 12:38",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ comparing
(Interop isBoolean: self) ifTrue: [ ^ anObject = (Interop asBoolean: self) ].
(Interop fitsInLong: self) ifTrue: [ ^ anObject = (Interop asLong: self) ].
(Interop fitsInDouble: self) ifTrue: [ ^ anObject = (Interop asDouble: self) ].
(Interop fitsInBigInteger: self) ifTrue: [ ^ anObject = (Interop asBigInteger: self) ].
(Interop isString: self) ifTrue: [ ^ anObject = (Interop asString: self) ].
((Interop isHostObject: self) and: [ Interop isHostObject: anObject ])
ifTrue: [ ^ self equals: anObject ].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ adaptToNumber: rcvr andSend: selector
"If I am involved in arithmetic with a number, convert me to a number."
(Interop fitsInLong: self) ifTrue: [ ^ rcvr perform: selector with: (Interop asLong: self) ].
(Interop fitsInDouble: self) ifTrue: [ ^ rcvr perform: selector with: (Interop asDouble: self) ].
(Interop fitsInBigInteger: self) ifTrue: [ ^ rcvr perform: selector with: (Interop asBigInteger: self) ].
self error: 'Cannot adapt foreign object to number'
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ converting
asNumber
(Interop fitsInLong: self) ifTrue: [ ^ Interop asLong: self ].
(Interop fitsInDouble: self) ifTrue: [ ^ Interop asDouble: self ].
(Interop fitsInBigInteger: self) ifTrue: [ ^ Interop asBigInteger: self ].
self error: 'Unable to interpret foreign value as number'
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ asSmalltalk
(Interop isString: self) ifTrue: [ ^ Interop asString: self ].
(Interop fitsInLong: self) ifTrue: [ ^ Interop asLong: self ].
(Interop fitsInDouble: self) ifTrue: [ ^ Interop asDouble: self ].
(Interop fitsInBigInteger: self) ifTrue: [ ^ Interop asBigInteger: self ].
self error: 'Unable to convert to Smalltalk object'
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ testing
isZero
(Interop fitsInLong: self) ifTrue: [ ^ (Interop asLong: self) isZero ].
(Interop fitsInDouble: self) ifTrue: [ ^ (Interop asDouble: self) isZero ].
(Interop fitsInBigInteger: self) ifTrue: [ ^ (Interop asBigInteger: self) isZero ].
self error: 'Unable to interpret foreign value as number'
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
"/" : "fn 8/2/2019 08:59",
"<" : "fn 8/2/2019 09:20",
"<=" : "fn 8/2/2019 09:14",
"=" : "fn 5/29/2020 15:15",
"=" : "fn 10/22/2024 10:48",
"==" : "fn 11/18/2020 15:45",
">" : "fn 8/2/2019 09:14",
">=" : "fn 8/2/2019 09:14",
"_dispatch:arguments:ifFail:" : "fn 5/25/2021 13:52",
"_dispatch:ifFail:" : "fn 8/2/2019 17:02",
"_redirect:" : "fn 8/2/2019 17:02",
"adaptToNumber:andSend:" : "fn 11/11/2019 22:07",
"adaptToNumber:andSend:" : "fn 10/22/2024 10:49",
"allInstVarNames" : "fn 6/25/2021 10:32",
"and:" : "fn 2/11/2021 10:35",
"asArray" : "fn 1/26/2021 15:51",
Expand All @@ -28,8 +28,8 @@
"asDate" : "fn 3/4/2021 16:48",
"asDateAndTime" : "fn 12/12/2019 23:05",
"asDuration" : "fn 12/12/2019 22:41",
"asNumber" : "fn 11/11/2019 22:07",
"asSmalltalk" : "fn 11/11/2019 22:06",
"asNumber" : "fn 10/22/2024 10:49",
"asSmalltalk" : "fn 10/22/2024 10:50",
"asString" : "fn 2/22/2021 12:40",
"asSymbol" : "fn 6/25/2021 13:26",
"asTime" : "fn 12/13/2019 12:28",
Expand Down Expand Up @@ -61,7 +61,7 @@
"isKindOf:" : "fn 6/25/2021 22:46",
"isNil" : "fn 11/11/2019 22:14",
"isString" : "fn 11/11/2019 22:14",
"isZero" : "fn 11/11/2019 22:07",
"isZero" : "fn 10/22/2024 10:50",
"longPrintOn:" : "fn 6/25/2021 09:39",
"notNil" : "fn 11/11/2019 19:51",
"or:" : "fn 2/11/2021 10:35",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
primitives numbers
asBigInteger: anObject
<primitive: 'primitiveAsBigInteger' module: 'PolyglotPlugin'>
^ (self fitsInBigInteger: anObject)
ifTrue: [ anObject interopAsBigInteger ]
ifFalse: [ self error: 'Object does not fit in BigInteger' ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
primitives numbers
fitsInBigInteger: anObject
<primitive: 'primitiveFitsInBigInteger' module: 'PolyglotPlugin'>
^ anObject interopFitsInBigInteger
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
primitives meta-data
getMetaParents: anObject
<primitive: 'primitiveGetMetaParents' module: 'PolyglotPlugin'>
^ (self hasMetaParents: anObject)
ifTrue: [ anObject interopGetMetaParents ]
ifFalse: [ self error: 'Object has no meta parents' ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
primitives meta-data
hasMetaParents: anObject
<primitive: 'primitiveHasMetaParents' module: 'PolyglotPlugin'>
^ (self isMetaObject: anObject)
ifTrue: [ anObject interopHasMetaParents ]
ifFalse: [ self error: 'Object is not a meta object' ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
primitives buffers
readBuffer: anObject byteOffset: byteOffset destination: destination destinationOffset: destinationOffset length: length
<primitive: 'primitiveReadBuffer' module: 'PolyglotPlugin'>
^ (self hasBufferElements: anObject)
ifTrue: [ anObject isForeignObject
ifTrue: [ PolyglotError signal ]
ifFalse: [ anObject interopReadBuffer: byteOffset - 1 destination: destination destinationOffset: destinationOffset - 1 length: length ] ]
ifFalse: [ self error: 'Object does not have buffer elements' ]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"class" : {
"asBigInteger:" : "fn 10/22/2024 09:46",
"asBoolean:" : "fn 3/5/2021 14:39",
"asDate:" : "fn 12/12/2019 20:42",
"asDouble:" : "fn 3/5/2021 14:41",
Expand All @@ -11,6 +12,7 @@
"asTime:" : "fn 12/12/2019 20:42",
"asTimeZone:" : "fn 12/12/2019 23:09",
"execute:with:" : "fn 6/26/2021 14:33",
"fitsInBigInteger:" : "fn 10/22/2024 11:54",
"fitsInDouble:" : "fn 3/5/2021 14:41",
"fitsInLong:" : "fn 3/5/2021 14:40",
"getArraySize:" : "fn 3/5/2021 14:35",
Expand All @@ -35,6 +37,7 @@
"getMembers:" : "fn 1/24/2020 14:50",
"getMembers:includeInternal:" : "fn 6/26/2021 14:35",
"getMetaObject:" : "fn 3/5/2021 14:48",
"getMetaParents:" : "fn 10/22/2024 12:38",
"getMetaQualifiedName:" : "fn 3/5/2021 14:49",
"getMetaSimpleName:" : "fn 3/5/2021 14:49",
"getScopeParent:" : "fn 3/9/2021 14:58",
Expand All @@ -56,6 +59,7 @@
"hasMemberWriteSideEffects:member:" : "fn 3/5/2021 14:26",
"hasMembers:" : "fn 3/5/2021 14:33",
"hasMetaObject:" : "fn 3/5/2021 14:43",
"hasMetaParents:" : "fn 10/22/2024 12:46",
"hasScopeParent:" : "fn 3/9/2021 14:57",
"hasSourceLocation:" : "fn 3/5/2021 14:43",
"hostIdentityHashCode:" : "fn 2/11/2021 12:04",
Expand Down Expand Up @@ -108,6 +112,7 @@
"memberToSelector:for:" : "fn 3/9/2021 12:49",
"memberToSelector:for:arguments:" : "fn 8/19/2021 14:49",
"readArrayElement:index:" : "fn 3/5/2021 14:52",
"readBuffer:byteOffset:destination:destinationOffset:length:" : "fn 10/22/2024 09:20",
"readBufferByte:index:" : "fn 6/26/2021 14:35",
"readBufferDouble:index:" : "fn 6/26/2021 14:35",
"readBufferFloat:index:" : "fn 6/26/2021 14:35",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
primitives internal
asBigInteger: aLargeInteger
<primitive: 'primitiveToJavaBigInteger' module: 'PolyglotPlugin'>
^ aLargeInteger
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
primitives internal
asHostObject: aGuestObject
<primitive: 'primitiveToHostObject' module: 'PolyglotPlugin'>
(self isHostObject: self)
(Interop isHostObject: aGuestObject)
ifFalse: [ self error: 'Object not a host object' ]
ifTrue: [ self error: 'Unexpected error' ]
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"class" : {
"addToHostClassPath:" : "fn 6/26/2021 14:36",
"asHostObject:" : "fn 3/5/2021 14:50",
"asBigInteger:" : "fn 10/22/2024 11:57",
"asHostObject:" : "fn 10/22/2024 10:07",
"asInteger:" : "fn 8/20/2022 11:11",
"asString:" : "fn 3/9/2021 13:59",
"asTriState:" : "fn 3/4/2021 11:55",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*TruffleSqueak-Core-number
interopAsBigInteger
^ Java asBigInteger: self
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*TruffleSqueak-Core-number
interopFitsInBigInteger
^ true
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"class" : {
},
"instance" : {
"interopAsBigInteger" : "fn 10/22/2024 11:58",
"interopFitsInBigInteger" : "fn 10/22/2024 09:13" } }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
"name" : "LargePositiveInteger" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*TruffleSqueak-Core-number
interopAsBigInteger
self subclassResponsibility
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*TruffleSqueak-Core-number
interopFitsInBigInteger
^ false
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*TruffleSqueak-Core-meta-object
interopGetMetaParents
^ self subclassResponsibility
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*TruffleSqueak-Core-meta-object
interopHasMetaParents
^ false
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*TruffleSqueak-Core-buffers
interopReadBuffer: byteOffset destination: destination destinationOffset: destinationOffset length: length
self subclassResponsibility
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
},
"instance" : {
"defaultLabelForPolyglotTools" : "fn 6/26/2021 14:43",
"interopAsBigInteger" : "fn 10/22/2024 09:58",
"interopAsBoolean" : "fn 3/4/2021 11:12",
"interopAsByte" : "fn 11/24/2020 12:35",
"interopAsDate" : "fn 11/20/2020 16:38",
Expand All @@ -17,6 +18,7 @@
"interopAsTime" : "fn 3/4/2021 16:26",
"interopAsTimeZone" : "fn 11/24/2020 14:52",
"interopExecute:" : "fn 11/24/2020 13:50",
"interopFitsInBigInteger" : "fn 10/22/2024 09:12",
"interopFitsInByte" : "fn 11/24/2020 12:34",
"interopFitsInDouble" : "fn 11/24/2020 12:34",
"interopFitsInFloat" : "fn 11/24/2020 12:34",
Expand All @@ -41,6 +43,7 @@
"interopGetLanguage" : "fn 3/4/2021 11:42",
"interopGetMembers:" : "fn 3/9/2021 12:44",
"interopGetMetaObject" : "fn 11/24/2020 16:29",
"interopGetMetaParents" : "fn 10/22/2024 09:23",
"interopGetMetaQualifiedName" : "fn 11/24/2020 12:54",
"interopGetMetaSimpleName" : "fn 11/24/2020 12:54",
"interopGetScopeParent" : "fn 3/10/2021 09:25",
Expand All @@ -60,6 +63,7 @@
"interopHasMemberWriteSideEffects:" : "fn 3/4/2021 11:11",
"interopHasMembers" : "fn 3/4/2021 11:11",
"interopHasMetaObject" : "fn 11/24/2020 16:29",
"interopHasMetaParents" : "fn 10/22/2024 09:23",
"interopHasScopeParent" : "fn 3/9/2021 14:19",
"interopHasSourceLocation" : "fn 3/4/2021 11:20",
"interopIdentityHashCode" : "fn 3/9/2021 13:59",
Expand Down Expand Up @@ -102,6 +106,7 @@
"interopIsTime" : "fn 11/20/2020 17:17",
"interopIsTimeZone" : "fn 11/20/2020 17:17",
"interopReadArrayElement:" : "fn 3/4/2021 11:10",
"interopReadBuffer:destination:destinationOffset:length:" : "fn 10/22/2024 09:20",
"interopReadBufferByte:" : "fn 3/31/2021 13:26",
"interopReadBufferDouble:" : "fn 3/31/2021 13:26",
"interopReadBufferFloat:" : "fn 3/31/2021 13:26",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ testJSMetadataAPIs
self assert: 'Object' equals: (Interop getMetaQualifiedName: (Interop getMetaObject: jsObject)) asString.
self assert: 'Object' equals: (Interop getMetaSimpleName: (Interop getMetaObject: jsObject)) asString.
self assert: jsCode equals: (Interop toDisplayString: jsObject) asString.
"GraalJS does not support meta parents yet"

self assert: (jsObject isKindOf: (Interop getMetaObject: jsObject)).
self assert: (jsObject respondsTo: #a).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ testJSObject
self should: [ Interop asLong: jsObject ] raise: Error.
self deny: (Interop fitsInDouble: jsObject).
self should: [ Interop asDouble: jsObject ] raise: Error.
self deny: (Interop fitsInBigInteger: jsObject).
self should: [ Interop asBigInteger: jsObject ] raise: Error.
self deny: (Interop hasArrayElements: jsObject).

self assert: 1 equals: (Interop getMemberSize: jsObject).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"testJSBasic" : "fn 5/29/2020 10:46",
"testJSDate" : "fn 5/29/2020 10:46",
"testJSExecutable" : "fn 5/29/2020 10:46",
"testJSMetadataAPIs" : "fn 6/25/2021 17:36",
"testJSObject" : "fn 1/26/2021 17:54",
"testJSMetadataAPIs" : "fn 10/22/2024 12:44",
"testJSObject" : "fn 10/22/2024 11:00",
"testSmalltalkFromJS" : "fn 3/9/2021 12:52" } }
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ testMetadataAPIs
"All boxed objects provide their class as meta object"
self assert: (Interop hasMetaObject: Smalltalk).
self assert: Smalltalk class equals: (Interop getMetaObject: Smalltalk).
self assert: {ProtoObject} equals: (Interop getMetaParents: Object).
self assert: {ClassDescription} equals: (Interop getMetaParents: Class).
self assert: {ClassDescription class} equals: (Interop getMetaParents: Class class).
self assert: Smalltalk class equals: (Interop getMetaObject: Smalltalk).
self assert: Smalltalk class asString equals: (Interop getMetaQualifiedName: (Interop getMetaObject: Smalltalk)) asString.
self assert: Smalltalk class asString equals: (Interop getMetaSimpleName: (Interop getMetaObject: Smalltalk)) asString.
self assert: ((Interop toDisplayString: Smalltalk) asString includesSubstring: Smalltalk asString)
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
testing
testNumbers
| li |
self deny: (Interop fitsInLong: Smalltalk).
self deny: (Interop fitsInDouble: Smalltalk).
self deny: (Interop fitsInBigInteger: Smalltalk).

self assert: (Interop fitsInLong: 42).
self assert: 42 == (Interop asLong: 42).
Expand All @@ -10,4 +12,9 @@ testNumbers

self deny: (Interop fitsInLong: 42.3).
self assert: (Interop fitsInDouble: 42.3).
self assert: 42.3 == (Interop asDouble: 42.3)
self assert: 42.3 == (Interop asDouble: 42.3).

li := SmallInteger maxVal + 1.
self deny: (Interop fitsInLong: li).
self assert: (Interop fitsInBigInteger: li).
self assert: li equals: (Interop asBigInteger: li)
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
"testInstantiables" : "fn 3/9/2021 16:52",
"testIterator" : "fn 3/31/2021 15:36",
"testMembers" : "fn 3/9/2021 16:52",
"testMetadataAPIs" : "fn 3/9/2021 16:52",
"testMetadataAPIs" : "fn 10/22/2024 12:12",
"testNulls" : "fn 3/9/2021 16:52",
"testNumbers" : "fn 3/9/2021 16:52",
"testNumbers" : "fn 10/22/2024 10:23",
"testScopeAPIs" : "fn 3/9/2021 16:38",
"testSmalltalkDNU" : "fn 6/30/2021 12:44",
"testStrings" : "fn 3/9/2021 16:53",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
testing
testJavaBuffers
| buffer |
| buffer byteArray |
buffer := (Java type: 'java.nio.ByteBuffer') allocate: 32.
self assert: (Interop hasBufferElements: buffer).
self assert: 32 equals: (Interop getBufferSize: buffer).
Expand All @@ -18,3 +18,7 @@ testJavaBuffers
self assert: 2.0999999046325684 equals: (Interop readBufferFloat: buffer index: 4).
self assert: 4.32198 equals: (Interop writeBufferDouble: buffer index: 5 value: 4.32198).
self assert: 4.32198 equals: (Interop readBufferDouble: buffer index: 5).

byteArray := ByteArray new: 17.
Interop readBuffer: buffer byteOffset: 0 destination: byteArray destinationOffset: 1 length: byteArray size - 1.
self assert: #[0 42 0 32 0 102 68 221 7 32 181 73 17 64 0 0 0] equals: byteArray
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"testJavaAddToHostClassPath" : "fn 11/11/2019 22:16",
"testJavaArray" : "fn 12/13/2019 11:43",
"testJavaBasic" : "fn 3/30/2021 16:52",
"testJavaBuffers" : "fn 3/31/2021 19:31",
"testJavaBuffers" : "fn 10/22/2024 09:38",
"testJavaDuration" : "fn 12/12/2019 23:15",
"testJavaException" : "fn 12/12/2019 23:15",
"testJavaHashes" : "fn 3/31/2021 19:09",
Expand Down
Loading

0 comments on commit 5ae9534

Please sign in to comment.