modified C api for native functions

This commit is contained in:
cozis
2022-08-17 04:33:00 +02:00
parent 919bc6fcbd
commit 6ba7fd21ef
15 changed files with 171 additions and 497 deletions
+2 -2
View File
@@ -57,7 +57,7 @@ static void walk(Object *self, void (*callback)(Object **referer, void *userp),
callback(&func->closure, userp);
}
static int call(Object *self, Object **argv, unsigned int argc, Object **rets, unsigned int maxretc, Heap *heap, Error *error)
static int call(Object *self, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Heap *heap, Error *error)
{
assert(self != NULL && heap != NULL && error != NULL);
@@ -108,7 +108,7 @@ static int call(Object *self, Object **argv, unsigned int argc, Object **rets, u
// The right amount of arguments was provided.
argv2 = argv;
int retc = run(func->runtime, error, func->exe, func->index, func->closure, argv2, expected_argc, rets, maxretc);
int retc = run(func->runtime, error, func->exe, func->index, func->closure, argv2, expected_argc, rets);
// NOTE: Every object reference is invalidated from here.
+5 -5
View File
@@ -43,11 +43,11 @@
typedef struct {
Object base;
Runtime *runtime;
int (*callback)(Runtime *runtime, Object **argv, unsigned int argc, Object **rets, unsigned int maxretc, Error *error);
int (*callback)(Runtime *runtime, Object **argv, unsigned int argc, Object *rets[MAX_RETS], Error *error);
int argc;
} NativeFunctionObject;
static int call(Object *self, Object **argv, unsigned int argc, Object **rets, unsigned int maxretc, Heap *heap, Error *error)
static int call(Object *self, Object **argv, unsigned int argc, Object *rets[static MAX_RETS], Heap *heap, Error *error)
{
assert(self != NULL);
assert(heap != NULL);
@@ -112,8 +112,8 @@ static int call(Object *self, Object **argv, unsigned int argc, Object **rets, u
}
assert(func->callback != NULL);
int retc = func->callback(func->runtime, argv2, argc2, rets, maxretc, error);
int retc = func->callback(func->runtime, argv2, argc2, rets, error);
// NOTE: Since the callback may have executed some bytecode, a GC
// cycle may have been triggered, therefore we must assume
// every object reference that was locally saved is invalidated
@@ -156,7 +156,7 @@ static TypeObject t_nfunc = {
* The newly created object. If an error occurred, NULL is returned
* and information about the error is stored in the [error] argument.
*/
Object *Object_FromNativeFunction(Runtime *runtime, int (*callback)(Runtime*, Object**, unsigned int, Object**, unsigned int, Error*), int argc, Heap *heap, Error *error)
Object *Object_FromNativeFunction(Runtime *runtime, int (*callback)(Runtime*, Object**, unsigned int, Object*[static MAX_RETS], Error*), int argc, Heap *heap, Error *error)
{
assert(callback != NULL);
+5 -5
View File
@@ -916,9 +916,7 @@ static _Bool step(Runtime *runtime, Error *error)
ASSERT(error->occurred == 0);
Object *rets[8];
unsigned int maxrets = sizeof(rets)/sizeof(rets[0]);
int num_rets = Object_Call(callable, argv, argc, rets, maxrets, runtime->heap, error);
int num_rets = Object_Call(callable, argv, argc, rets, runtime->heap, error);
if(num_rets < 0)
return 0;
@@ -1278,6 +1276,7 @@ static _Bool step(Runtime *runtime, Error *error)
int retc = ops[0].as_int;
UNUSED(retc);
ASSERT(retc >= 0);
ASSERT(retc <= MAX_RETS);
ASSERT(retc == runtime->frame->used);
return 0;
}
@@ -1400,7 +1399,7 @@ int run(Runtime *runtime, Error *error,
Executable *exe, int index,
Object *closure,
Object **argv, int argc,
Object **rets, int maxretc)
Object *rets[static MAX_RETS])
{
ASSERT(runtime != NULL);
ASSERT(error != NULL);
@@ -1484,7 +1483,8 @@ int run(Runtime *runtime, Error *error,
// If an error occurred, we want to return NULL.
if(error->occurred == 0)
{
retc = MIN(frame.used, maxretc);
retc = frame.used;
ASSERT(retc <= MAX_RETS);
for(int i = 0; i < retc; i += 1)
{
+3 -3
View File
@@ -56,7 +56,7 @@ const char *Runtime_GetCurrentScriptAbsolutePath(Runtime *runtime);
Snapshot *Snapshot_New(Runtime *runtime);
void Snapshot_Free(Snapshot *snapshot);
void Snapshot_Print(Snapshot *snapshot, FILE *fp);
int run(Runtime *runtime, Error *error, Executable *exe, int index, Object *closure, Object **argv, int argc, Object **rets, int maxretc);
int run(Runtime *runtime, Error *error, Executable *exe, int index, Object *closure, Object **argv, int argc, Object *rets[static MAX_RETS]);
typedef enum {
SM_END,
@@ -81,7 +81,7 @@ struct StaticMapSlot {
_Bool as_bool;
long long int as_int;
double as_float;
int (*as_funct)(Runtime*, Object**, unsigned int, Object**, unsigned int, Error*);
int (*as_funct)(Runtime*, Object**, unsigned int, Object*[static MAX_RETS], Error*);
TypeObject *as_type;
};
union { int argc; int length; };
@@ -89,7 +89,7 @@ struct StaticMapSlot {
Object *Object_NewStaticMap(StaticMapSlot slots[], void (*initfn)(StaticMapSlot[]), Runtime *runt, Error *error);
Object *Object_FromNojaFunction(Runtime *runtime, Executable *exe, int index, int argc, Object *closure, Heap *heap, Error *error);
Object *Object_FromNativeFunction(Runtime *runtime, int (*callback)(Runtime*, Object**, unsigned int, Object**, unsigned int, Error*), int argc, Heap *heap, Error *error);
Object *Object_FromNativeFunction(Runtime *runtime, int (*callback)(Runtime*, Object**, unsigned int, Object*[static MAX_RETS], Error*), int argc, Heap *heap, Error *error);
typedef struct {
Error base;
Runtime *runtime;