Skip to main content

Self

Self is a prototype-based, dynamically typed, object-oriented programming language. It grew out of Smalltalk and uses message passing, blocks and slots instead of classes.

In LiveCodes, Self runs in the browser on a TypeScript implementation of its interpreter, entirely client-side. No server is involved.

Usage​

Demo:

show code
import { createPlayground } from 'livecodes';

const options = {
"config": {
"activeEditor": "script",
"script": {
"language": "self",
"content": "'Hello, World!' printLine.\n\n1 to: 5 Do: [|:n| n printLine]."
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
createPlayground('#container', options);

Output​

The program runs on the result page. Anything it writes with print or printLine is captured and shown in the console. When the final expression prints nothing, its value is shown as => value, as in a Self REPL.

'Hello, World!' printLine.
1 + 2.

Standard I/O​

The text passed as livecodes.self.input is available in the Self lobby as the stdin string slot. It can be parsed with asInteger or asFloat:

'Self' printLine.
(stdin asInteger + 1) printLine.

Set livecodes.self.input before the program runs, or pass the new input to livecodes.self.run(input).

Communication with JavaScript​

A few helper properties and methods are available in the browser global livecodes.self object:

  • livecodes.self.input: the standard input passed to the program.
  • livecodes.self.loaded: a promise that resolves when the interpreter has loaded and the program has run once. It rejects if the runtime fails to load. Other helpers should be used after it resolves.
  • livecodes.self.output: the program's output.
  • livecodes.self.error: the error message, if the program failed.
  • livecodes.self.exitCode: the evaluation status (0 ok, 1 Self parse or runtime error).
  • livecodes.self.run(input): runs the code again with new input, returning a promise that resolves to { output, error, exitCode }.

Example:

show code
import { createPlayground } from 'livecodes';

const options = {
"template": "self",
"params": {
"activeEditor": "markup"
}
};
createPlayground('#container', options);

Language Info​

Name​

self

Aliases / Extensions​

self

Editor​

script

Compiler​

A TypeScript port of the pinneyja/self-interpreter, packaged by @live-codes/self. The parser, object model, primitive methods and core Self world run as JavaScript; no WebAssembly or Pyodide is needed.

Version​

@live-codes/self 0.1.0.

Code Formatting​

Not currently supported.

Starter Template​

https://livecodes.io/?template=self

Limitations​

This is an educational implementation of the core language, not the official Self VM. The Morphic GUI, the process scheduler and snapshots are not included, and integers are JavaScript numbers.