Skip to content

Commit

Permalink
More Space Lua docs, and slight API tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
zefhemel committed Oct 15, 2024
1 parent 2842183 commit 413855c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
12 changes: 5 additions & 7 deletions common/space_lua_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ function exposeDefinitions(
system: System<any>,
scriptEnv: ScriptEnvironment,
) {
const defApi = new LuaTable();
env.set("def", defApi);
// Expose the command registration function to Lua via def.command({name="foo", function() ... end})
defApi.set(
"command",
// Expose the command registration function to Lua via define_command({name="foo", function() ... end})
env.set(
"define_command",
new LuaBuiltinFunction(
(def: LuaTable) => {
if (def.get(1) === undefined) {
Expand Down Expand Up @@ -79,8 +77,8 @@ function exposeDefinitions(
},
),
);
defApi.set(
"event_listener",
env.set(
"define_event_listener",
new LuaBuiltinFunction((def: LuaTable) => {
if (def.get(1) === undefined) {
throw new Error("Callback is required");
Expand Down
67 changes: 62 additions & 5 deletions website/Space Lua.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ And some [[Space Style]] to style it:

Now, let’s use it (put your cursor in there to see the code):
${marquee "Finally, marqeeeeeeee!"}
Hell yeah!
Oh boy, the times we live in!

## Commands
Custom commands can be defined using `def.command`:
Custom commands can be defined using `define_command`:

```space-lua
def.command {
define_command {
name = "Hello World";
function()
editor.flash_notification "Hello world!"
Expand All @@ -75,10 +75,10 @@ def.command {
Try it: {[Hello World]}

## Event listeners
You can listen to events using `def.event_listener`:
You can listen to events using `define_event_listener`:

```space-lua
def.event_listener {
define_event_listener {
event = "my-custom-event";
function(e)
editor.flash_notification("Custom triggered: " .. e.data.name)
Expand Down Expand Up @@ -106,6 +106,63 @@ Template:
Here's a greeting: {{greet_me("Pete")}}
```

# API
Lua APIs, which should be (roughly) implemented according to the Lua standard.
* `print`
* `assert`
* `ipairs`
* `pairs`
* `unpack`
* `type`
* `tostring`
* `tonumber`
* `error`
* `pcall`
* `xpcall`
* `setmetatable`
* `getmetatable`
* `rawset`
* `string`:
* `byte`
* `char`
* `find`
* `format`
* `gmatch`
* `gsub`
* `len`
* `lower`
* `upper`
* `match`
* `rep`
* `reverse`
* `sub`
* `split`
* `table`
* `concat`
* `insert`
* `remove`
* `sort`
* `os`
* `time`
* `date`
* `js` (Warning: this will be revised): JavaScript interop functions
* `new`: instantiate a JavaScript constructor
* `importModule`: import a JavaScript from a URL (`import` equivalent)
* `tolua`: convert a JS value to a Lua value
* `tojs`: convert a Lua value to a JS value
* `log`: console.log

In addition, [all SilverBullet syscalls](https://jsr.io/@silverbulletmd/silverbullet/doc/syscalls) are exposed. However since the Lua naming convention prefers using `snake_case` it is recommended you call them that way. For instance: `editor.flash_notification` is more Lua’y than `editor.flashNotification` (although both are supported at this time -- again, subject to change).

While in [[Space Script]] all syscalls are asynchronous and need to be called with `await`, this is happens transparently in Space Lua leading to cleaner code:

```space-lua
local function call_some_things()
local text = space.read_page(editor.get_current_page())
print("Current page text", text)
end
```

# Lua implementation notes
Space Lua is intended to be a more or less complete implementation of [Lua 5.4](https://www.lua.org/manual/5.4/). However, a few features are (still) missing:

Expand Down

0 comments on commit 413855c

Please sign in to comment.