smalltalk: reflection accessors (Object>>class, methodDict, selectors)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-25 08:18:32 +00:00
parent ae94a24de5
commit 9954a234ae
3 changed files with 121 additions and 1 deletions

View File

@@ -523,7 +523,12 @@
(fn
(receiver selector args)
(let ((cls (st-class-of receiver)))
;; Universal Object messages — work on any receiver type.
(cond
((= selector "class")
(cond
((st-class-ref? receiver) (st-class-ref "Metaclass"))
(else (st-class-ref cls))))
((or (= cls "SmallInteger") (= cls "Float"))
(st-num-send receiver selector args))
((or (= cls "String") (= cls "Symbol"))
@@ -778,6 +783,32 @@
((= selector "superclass")
(let ((s (st-class-superclass name)))
(cond ((= s nil) nil) (else (st-class-ref s)))))
((= selector "methodDict")
;; The class's own method dictionary (instance side).
(get (st-class-get name) :methods))
((= selector "classMethodDict")
(get (st-class-get name) :class-methods))
((= selector "selectors")
;; Own instance-side selectors as an Array of symbols.
(let ((out (list)))
(begin
(for-each
(fn (k) (append! out (make-symbol k)))
(keys (get (st-class-get name) :methods)))
out)))
((= selector "classSelectors")
(let ((out (list)))
(begin
(for-each
(fn (k) (append! out (make-symbol k)))
(keys (get (st-class-get name) :class-methods)))
out)))
((= selector "instanceVariableNames")
;; Own ivars as an Array of strings (matches Pharo).
(get (st-class-get name) :ivars))
((= selector "allInstVarNames")
;; Inherited + own ivars in declaration order (root first).
(st-class-all-ivars name))
;; Class definition: `Object subclass: #Foo instanceVariableNames: 'x y'`.
;; Supports the short `subclass:` and the full
;; `subclass:instanceVariableNames:classVariableNames:package:` form.