This article is out of date. See iodb.org's recent version.

The Parenscript Object System (PSOS) is a facility for object-oriented programming in Parenscript using the same style and techniques as the Common Lisp Object System. It consists of a set of macros in lisp and a runtime environment in Parenscript.

In the Javascript world, the Parenscript Object System is probably the most sophisticated method for object-oriented programming. It is different from object-oriented systems that use the message-passing paradigm (like Java, C++) in a few ways. If you are not lisp-aware but ambitious, you can read chapters 15 and 16 of Practical Common Lisp, which discusses the Common Lisp Object system.

Contents show]

Features

PSOS is designed to be similar CLOS. However, PSOS should not be everything that CLOS is. It should be a fairly small library (and currently compiles to about 10k in Javascript), for example. There are also differences between Javascript and Lisp, especially Javascript's lack of Lisp-like symbols.

Multiple inheritance, generic functions, and basic method combination are currently implemented.

Examples

1. Different classes of people

The following is a simple example that demonstrates how PSOS can be used to obtain simple class inheritence. This works in much the same way as other javascript class systems that are out there already.

(defclass person () () (:documentation "Any old person.")) (defclass citizen (person) () (:documentation "A person who votes.")) (defclass politician (citizen) () (:documentation "A person in public office.")) (defgeneric discuss-environment (individual)) (defmethod discuss-environment ((individual citizen)) (alert "I don't know anything about the environment")) (defmethod discuss-enviornment ((individual politician)) (alert "The environment is an important issue, but so is the War on Terror.")) (dicuss-environment (new politician)) ; alerts "the environment is..."

2. An event-handling system

This is a practical example that demonstrates a way to use generic functions and classes to implement an event-handling system.

;;; Event Handling ;;; (defvar global-event-table (create)) (defclass event () ((origin) (description)) (:documentation "Primitive event class for event observation. Custom event classes are created that subclass the abstract event class. These are dispatched by calling (make-instance some-event-class (create :origin origin ...)).")) (defclass event-emitter () () (:documentation "Subclasses of this can emit events.")) (defun event-handler-fits? (handler ev) (return (is-subclass-of handler.event-class (class-of ev)))) (defmethod initialize-instance :after ((ev event)) (event-fired (get-origin ev) ev)) (defmethod register-event-handler ((on-obj event-emitter) event-class handler-func) (let ((obj-observers (defaultf on-obj.event-handlers (array)))) (obj-observers.push (create :func handler-func :event-class event-class)))) (defgeneric event-fired (within-obj ev) (:documentation "Called when an event was fired within the given object. For example, if a button is clicked within a page, then the event will eventually fire for the page object with the given event.")) (defmethod event-fired ((within-obj event-emitter) ev) (let ((handlers (and within-obj.event-handlers (within-obj.event-handlers.filter (lambda (handler) (return (event-handler-fits? handler ev))))))) (when handlers (dolist (handler handlers) (handler.func ev)))) (call-next-method)) (defmethod event-fired ((within-obj general-sail) ev) (call-next-method) (let ((parent (parent-sail within-obj))) (when parent (event-fired parent ev))))

Download

The current source may be downloaded via SVN:

svn checkout http://suavescript.googlecode.com/svn/trunk/paren-psos paren-psos

The project is maintained by Red Daly (reddaly at gmail) and licensed under the GPL.

Outstanding issues

PSOS works well as a class system now, but certain ideas have not been hammered out yet. In particular, dealing with slot definitions is currently done via a compile-time expansion rather than letting the Javascript runtime environment initiate slots. This is primarily due to what I do know about scoping in Javascript. Suggestions or comments about slot definitions, or PSOS in general, are welcome. -red 10:39, 16 January 2007 (UTC)

the suavescript project - the current home of PSOS