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
- JS
- TS
- React
- Vue
- Svelte
- Solid
- Preact
- Web Components
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);
import { createPlayground, type EmbedOptions } from 'livecodes';
const options: EmbedOptions = {
"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);
import LiveCodes from 'livecodes/react';
export default function App() {
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"
}
}
};
return (<LiveCodes {...options} />);
}
<script setup>
import LiveCodes from "livecodes/vue";
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"
}
}
};
</script>
<template>
<LiveCodes v-bind="options" />
</template>
<script>
import LiveCodes from 'livecodes/svelte';
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"
}
}
};
</script>
<LiveCodes {...options} />
import LiveCodes from 'livecodes/solid';
export default function App() {
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"
}
}
};
return (<LiveCodes {...options} />);
}
import LiveCodes from 'livecodes/preact';
export default function App() {
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"
}
}
};
return (<LiveCodes {...options} />);
}
<live-codes></live-codes>
<script type="module">
import "livecodes/web-components";
const playground = document.querySelector("live-codes");
playground.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"
}
};
</script>
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
- JS
- TS
- React
- Vue
- Svelte
- Solid
- Preact
- Web Components
import { createPlayground } from 'livecodes';
const options = {
"template": "self",
"params": {
"activeEditor": "markup"
}
};
createPlayground('#container', options);
import { createPlayground, type EmbedOptions } from 'livecodes';
const options: EmbedOptions = {
"template": "self",
"params": {
"activeEditor": "markup"
}
};
createPlayground('#container', options);
import LiveCodes from 'livecodes/react';
export default function App() {
const options = {
"template": "self",
"params": {
"activeEditor": "markup"
}
};
return (<LiveCodes {...options} />);
}
<script setup>
import LiveCodes from "livecodes/vue";
const options = {
"template": "self",
"params": {
"activeEditor": "markup"
}
};
</script>
<template>
<LiveCodes v-bind="options" />
</template>
<script>
import LiveCodes from 'livecodes/svelte';
const options = {
"template": "self",
"params": {
"activeEditor": "markup"
}
};
</script>
<LiveCodes {...options} />
import LiveCodes from 'livecodes/solid';
export default function App() {
const options = {
"template": "self",
"params": {
"activeEditor": "markup"
}
};
return (<LiveCodes {...options} />);
}
import LiveCodes from 'livecodes/preact';
export default function App() {
const options = {
"template": "self",
"params": {
"activeEditor": "markup"
}
};
return (<LiveCodes {...options} />);
}
<live-codes
template="self"></live-codes>
<script type="module">
import "livecodes/web-components";
const playground = document.querySelector("live-codes");
playground.params = {
"activeEditor": "markup"
};
</script>
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.
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.
Links