Skip to main content

Nim (Wasm)

Nim is a statically typed, compiled systems programming language.

In LiveCodes, Nim runs in the browser using the Nim compiler compiled to WebAssembly. This target (Nim's c backend) compiles the program to C and then to WebAssembly using the Clang toolchain, the same one used for C (Wasm), entirely client-side.

For Nim's JavaScript backend, which compiles to JavaScript and allows DOM access for the result page, see Nim.

Usage​

Demo:

show code
import { createPlayground } from 'livecodes';

const options = {
"config": {
"activeEditor": "script",
"script": {
"language": "nim-wasm",
"content": "echo \"Hello, World!\""
},
"mode": "simple",
"editor": "auto",
"tools": {
"status": "full"
}
}
};
createPlayground('#container', options);

Communication with JavaScript​

The Nim code is compiled and run in a web worker. The following helpers are available in the browser global livecodes.nimWasm object:

  • livecodes.nimWasm.input: The standard input passed to the program.
  • livecodes.nimWasm.loaded: A promise that resolves when the compiler is loaded and the program has run once.
  • livecodes.nimWasm.output: The program's output.
  • livecodes.nimWasm.error: The compiler diagnostics, if the code failed to compile.
  • livecodes.nimWasm.exitCode: The program's exit code.
  • livecodes.nimWasm.run: A function that compiles and runs the code with new input. It takes a string as input and returns a promise that resolves with an object containing output, error and exitCode.

Example:

show code
import { createPlayground } from 'livecodes';

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

Language Info​

Name​

nim-wasm

Aliases / Extensions​

nimwasm, wasm.nim

Editor​

script

Compiler​

The Nim compiler (Nim's c backend) compiled to WebAssembly. The generated C is compiled and linked by the Clang toolchain of @live-codes/clang-wasm.

Version​

Nim v2.2.4, Clang v22.1.8.

Custom Settings​

The compiler can be configured using custom settings added to the property nim-wasm:

PropertyTypeDescription
compileArgsstring[]Extra Nim flags, e.g. ["--define:release2"].
argsstring[]Command-line arguments passed to the program.
Custom Settings
{
"nim-wasm": {
"compileArgs": ["--define:release2"],
"args": ["--name", "LiveCodes"]
}
}

Live Reload​

By default, new code changes are sent to the result page without a full page reload, avoiding the need to re-download the compiler. Adding the code comment # __livecodes_reload__ forces a full page reload.

Example Usage​

import std/strutils

echo "Hello, World!"

let name = stdin.readLine()
echo "Hello, ", name, "!"

This target supports standard input, command-line arguments and real exit codes. A program that reads standard input gets an end-of-input error when it is left blank.

Starter Template​

https://livecodes.io/?template=nim-wasm