Skip to content

Commit

Permalink
Change Mirror API to explore SOMns object model and metaclass system
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Marr <[email protected]>
  • Loading branch information
smarr committed Aug 5, 2018
1 parent fd0057c commit eb9fe83
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
5 changes: 3 additions & 2 deletions core-lib/Mirrors.ns
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ class Mirrors usingVmMirror: vmMirror = Value (
(*:TODO: Not happy with the naming of the mirror methods yet,
they are not unambiguous, about whether they apply to the object,
or to the object class *)
public name = ( ^ vmMirror mirrorAClassesName: obj )
public name = ( ^ vmMirror mirrorClassName: obj )
public classObject= ( ^ vmMirror objClass: obj )
public classMirror= ( ^ ClassMirror reflecting: classObject )
public superclass = ( ^ vmMirror mirrorSuperclass: obj )
public superclass = ( ^ vmMirror mirrorSuperclass: classObject )
public superclassName = ( ^ vmMirror mirrorAClassesName: (vmMirror mirrorSuperclass: classObject) )
public slots = ( ^ vmMirror mirrorSlots: obj )

public classDefinition = (
Expand Down
60 changes: 60 additions & 0 deletions core-lib/ObjectModel.som
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class ObjectModel usingPlatform: platform = Value (
| private ObjectMirror = platform mirrors ObjectMirror.
private ClassMirror = platform mirrors ClassMirror.
private Thing = platform kernel Thing. |
)(
public class Snake = ()()

private printInstanceAndSuperclassOf: classMirror = (
classMirror name print. ' is instance of ' print.
classMirror classMirror name println.
classMirror name print. ' is subclass of ' print.
classMirror superclassName println.
)

public main: args = (
| snake snakeMirror obj objMirror thing thingMirror |
snake := Snake new.
snakeMirror := (ObjectMirror reflecting: snake).
'snake is instance of ' print.
snakeMirror className println.

printInstanceAndSuperclassOf: snakeMirror classMirror.
printInstanceAndSuperclassOf: snakeMirror classMirror classMirror.
printInstanceAndSuperclassOf: snakeMirror classMirror classMirror classMirror.
printInstanceAndSuperclassOf: snakeMirror classMirror classMirror classMirror classMirror.
printInstanceAndSuperclassOf: snakeMirror classMirror classMirror classMirror classMirror classMirror.

'' println. '' println.
'Superclass hierarchy:' println.
'' println.

obj := Object new.
objMirror := (ObjectMirror reflecting: obj).
'obj is instance of ' print.
objMirror className println.

printInstanceAndSuperclassOf: objMirror classMirror.
printInstanceAndSuperclassOf: objMirror classMirror classMirror.
printInstanceAndSuperclassOf: objMirror classMirror classMirror classMirror.
printInstanceAndSuperclassOf: objMirror classMirror classMirror classMirror classMirror.
printInstanceAndSuperclassOf: objMirror classMirror classMirror classMirror classMirror classMirror.

'' println. '' println.
'In SOMns there is still Thing:' println.
'' println.

thing := Thing new.
thingMirror := (ObjectMirror reflecting: thing).
'thing is instance of ' print.
thingMirror className println.

printInstanceAndSuperclassOf: thingMirror classMirror.
printInstanceAndSuperclassOf: thingMirror classMirror classMirror.
printInstanceAndSuperclassOf: thingMirror classMirror classMirror classMirror.
printInstanceAndSuperclassOf: thingMirror classMirror classMirror classMirror classMirror.
printInstanceAndSuperclassOf: thingMirror classMirror classMirror classMirror classMirror classMirror.

^ 0
)
)
1 change: 1 addition & 0 deletions src/som/primitives/ClassPrims.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public final SAbstractObject doSClass(final Object receiver) {
}

@GenerateNodeFactory
@Primitive(primitive = "mirrorSuperclass:")
public abstract static class SuperClassPrim extends UnaryExpressionNode {
@Specialization
public final SAbstractObject doSClass(final SClass receiver) {
Expand Down

0 comments on commit eb9fe83

Please sign in to comment.