Skip to content

Commit 5fdb52b

Browse files
authored
Fix API for structured result storage (#492)
1 parent f43ebf3 commit 5fdb52b

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

doc/api.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ Parameters:
210210
* `params`: Parameter stack slots
211211
* `result`: Returned value stack slots
212212

213-
Returned value: Pointer to the stack slot allocated for storing the returned value. For a returned value of a structured type, this stack slot already contains the pointer to the memory area sufficiently large to store the actual returned value. In this case, the user must fill this memory area, but not rewrite the pointer.
213+
Returned value: Pointer to the stack slot allocated for storing the returned value. Special rules apply to a returned value of a structured type or to multiple returned values (treated as a single implicit structure):
214+
215+
* Inside a C function called from Umka, this stack slot already contains the pointer to the memory area sufficiently large to store the actual returned value. The user must fill this memory area, but not rewrite the pointer
216+
217+
* Before calling an Umka function from C, the user must allocate a memory area needed for storing the actual returned value and put the area pointer to the stack slot returned by `umkaGetResult`
214218

215219
```
216220
static inline void *umkaGetInstance(UmkaStackSlot *result);

playground/umka.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/umka_vm.c

+7
Original file line numberDiff line numberDiff line change
@@ -3843,6 +3843,13 @@ void vmRun(VM *vm, FuncContext *fn)
38433843
{
38443844
const ParamLayout *paramLayout = (ParamLayout *)fn->params[-4].ptrVal; // For -4, see the stack layout diagram in umka_vm.c
38453845
numParamSlots = paramLayout->numParamSlots;
3846+
3847+
if (paramLayout->numResultParams > 0)
3848+
{
3849+
if (!fn->result || !fn->result->ptrVal)
3850+
vm->error->runtimeHandler(vm->error->context, ERR_RUNTIME, "Storage for structured result is not specified");
3851+
fn->params[paramLayout->firstSlotIndex[paramLayout->numParams - 1]].ptrVal = fn->result->ptrVal;
3852+
}
38463853
}
38473854
else
38483855
numParamSlots = sizeof(Interface) / sizeof(Slot); // Only upvalue

0 commit comments

Comments
 (0)