Skip to content

one value convention: nil is undefined, first result everywhere - #156

Closed
ceifa wants to merge 1 commit into
new-apis-2from
refactor/improve-project-ergonomics
Closed

ceifa wants to merge 1 commit into
new-apis-2from
refactor/improve-project-ergonomics

Conversation

@ceifa

@ceifa ceifa commented Nov 20, 2026

Copy link
Copy Markdown
Owner

One convention for the values that cross between Lua and JS. Breaking.

What changes

nil is undefined, everywhere. It used to read as null, except where it did not: a Lua function that returned nothing gave null when called directly and undefined through state.async.

state.set('greet', (name = 'world') => `hello ${name}`)
await state.doString('return greet(nil)') // "hello world" — was "hello null"
state.get('missing')                      // undefined — was null

null is now only what the injected null sentinel reads back as, so with inject the two stay apart in both directions. Without it a JS null still becomes nil, and reads back as undefined.

The first result by default, returns: 'all' for every one of them. doString, doFile, doStringSync, doFileSync, state.async and state.call all take it, and give back a LuaMultiReturn.

const [quotient, remainder] = await state.doString('return 7 // 2, 7 % 2', { returns: 'all' })
state.call(divmod, [7, 2], { returns: 'all' })      // [3, 1]
await state.async(divmod, { returns: 'all' })(7, 2) // [3, 1]

state.call(target, args, options) replaces call(name, ...args). It is the synchronous counterpart of state.async: the target is a global's name or a Lua function, and the options are those of a run plus returns.

Migrating

before after
value === null on something read from Lua value === undefined
state.call('f', a, b) → LuaMultiReturn state.call('f', [a, b], { returns: 'all' })
thread.call(...) on a thread other than the state thread.runSync(argCount)

The old variadic call throws a TypeError that says how to write it, rather than misreading its arguments.

Worth a look in review

  • call moved from Thread to LuaState, since a function target needs the state's function extension.
  • call now runs under limits.functionTimeout and limits.maxInstructions, like state.async does. Before, call(name) honoured neither.
  • From reading the old code, call left its results on the state's stack on every call; the new one rewinds. A test now holds it to that.
  • thread.run and thread.runSync still give back every value on the stack. They are the stack level API, and the README says so.
  • The version is not bumped.

Verification

npm run lint:nofix, npm test (444 passing, 429 before) and npm run luatests pass locally on a wasm built with emsdk 6.0.4. The new overloads were type checked from a consumer's side against dist/index.d.ts. The benchmarks were not run.

nil reads as undefined wherever it comes from, so a JS default parameter
applies to a nil argument and a Lua function that returns nothing gives
the same value sync and async. null is only what the injected sentinel
reads back as.

Every entry point gives back the first result, and returns: 'all' asks
for all of them as a LuaMultiReturn: doString, doFile, their Sync twins,
state.async and state.call.

state.call is now call(target, args, options): the synchronous
counterpart of state.async, taking a global's name or a Lua function.
It no longer leaves its results on the state's stack.
@ceifa
ceifa changed the base branch from main to new-apis-2 November 20, 2026 00:27
@ceifa ceifa closed this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant