A few notable Space Lua quirks.
Lua implementation notes
Space Lua is intended to be a more or less complete implementation of Lua 5.4. However, a few features are (still) missing:
- coroutines (not planned, not useful in the SilverBullet context)
- _ENV (planned)
- Hexadecimal numeric constants with a fractional part, or binary exponents (not supported by JavaScript number parser either)
Conversion of Strings to Numbers
Space Lua converts strings to numbers like standard Lua.
Syntax:
- Leading and trailing whitespace (space, tab, newline, carriage return, form feed and vertical tab) is trimmed.
- The entire string after trimming trailing whitespace must form a number.
- Optional
+or-signs are accepted before the number. - Decimal integers and decimal floats are supported with optional exponent (e.g.,
42,-3.5,.5,5.,1e3,-2.5E-2). - Hexadecimal integers and hexadecimal floats are also supported:
- integers (e.g.,
0x10,-0XFF), - floats require
porPexponent (e.g.,0x1.8p1,-0X10.3P-1).
- integers (e.g.,
Failure handling:
- In arithmetic and unary minus expressions invalid strings cause an Lua exception:
attempt to perform arithmetic on a non-number. tonumber(s)returnsnilon failure.tonumber(s, base)parses signed integers in bases 2..36 (without decimal points or exponents) and returnsnilon invalid input string or base.
Examples (with tonumber function):
| Code | Result | Expected |
|---|---|---|
| `tonumber(’ 42 ’)``````````````````(’ 42 ’)} | ```````````````````````````````````````````````````` | |
| `tonumber(‘-0xFf ’)````````````````````xFf ’)} | ```````````````````````````````````````````````````` | |
| `tonumber(‘0x1.8p1’)`````````````````````8p1’)} | ```````````````````````````````````````````````````` | |
| `tonumber(‘1e-2’)``````````````````(‘1e-2’)} | ```````````````````````````````````````````````````` | |
| `tonumber(‘abc’)`````````````````er(‘abc’)} | ```````````````````````````````````````````````````` | |
| `tonumber(‘1010’, 2)`````````````````````’, 2)} | ```````````````````````````````````````````````````` | |
| `tonumber(’ +fF’, 16)`````````````````````` 16)} | ```````````````````````````````````````````````````` | |
| `tonumber(‘8’, 8)``````````````````(‘8’, 8)} | ```````````````````````````````````````````````````` |
Examples (with arithmetic operations):
| Code | Result | Expected |
|---|---|---|
| `‘0xfFfFp1’````````````0xfFfFp1’} | string: `````````````````````````````````````````````````` | |
| `‘0xffffP-3’ + 0````````````````` + 0} | number: `````````````````````````````````````````````````` | |
| `-‘123E-12’````````````‘123E-12’} | number: `````````````````````````````````````````````````` |