<commit_before>#include <any/process.h>

#include <assert.h>
#include <any/loader.h>
#include <any/scheduler.h>
#include <any/gc.h>

#define GROW_FACTOR 2
#define INIT_STACK_SZ 64
#define INIT_HEAP_SZ 512

static AINLINE void* aalloc(aprocess_t* self, void* old, const int32_t sz)
{
    assert(self->alloc);
    return self->alloc(self->alloc_ud, old, sz);
}

static void call(aprocess_t* self, void* ud)
{
    int32_t* nargs = (int32_t*)ud;
    any_call(self, *nargs);
}

static AINLINE save_ctx(aprocess_t* p, aframe_t* frame, int32_t nargs)
{
    frame->prev = p->frame;
    p->frame = frame;
    frame->bp = p->sp;
    frame->nargs = nargs;
}

static AINLINE load_ctx(aprocess_t* p)
{
    int32_t nsp = p->frame->bp - p->frame->nargs;
    assert(p->sp > p->frame->bp && "return value missing");
    p->stack[nsp - 1] = p->stack[p->sp - 1];
    p->sp = nsp;
    p->frame = p->frame->prev;
}

static ASTDCALL entry(void* ud)
{
    aframe_t frame;
    aprocess_t* p = (aprocess_t*)ud;
    int32_t nargs = (int32_t)p->stack[--p->sp].v.integer;
    memset(&frame, 0, sizeof(aframe_t));
    frame.bp = 1; // start from stack[0] (nil)
    frame.nargs = 0;
    p->frame = &frame;
    any_pcall(p, nargs);
    p->flags |= APF_EXIT;
    while (TRUE) atask_yield(&p->task);
}

aerror_t aprocess_init(
    aprocess_t* self, ascheduler_t* owner, aalloc_t alloc, void* alloc_ud)
{
    self->owner = owner;
    self->alloc = alloc;
    self->alloc_ud = alloc_ud;
    self->flags = 0;
    self->stack = (avalue_t*)aalloc(
        self, NULL, sizeof(avalue_t)*INIT_STACK_SZ); // TODO: handle NULL
    self->stack_cap = INIT_STACK_SZ;
    self->sp = 0;
    any_push_nil(self); // stack[0] is nil
    return agc_init(&self->gc, INIT_HEAP_SZ, alloc, alloc_ud);
}

void aprocess_start(<commit_msg>handle out of memory on aprocess_init<commit_after>#include <any/process.h>

#include <assert.h>
#include <any/loader.h>
#include <any/scheduler.h>
#include <any/gc.h>

#define GROW_FACTOR 2
#define INIT_STACK_SZ 64
#define INIT_HEAP_SZ 512

static AINLINE void* aalloc(aprocess_t* self, void* old, const int32_t sz)
{
    assert(self->alloc);
    return self->alloc(self->alloc_ud, old, sz);
}

static void call(aprocess_t* self, void* ud)
{
    int32_t* nargs = (int32_t*)ud;
    any_call(self, *nargs);
}

static AINLINE save_ctx(aprocess_t* p, aframe_t* frame, int32_t nargs)
{
    frame->prev = p->frame;
    p->frame = frame;
    frame->bp = p->sp;
    frame->nargs = nargs;
}

static AINLINE load_ctx(aprocess_t* p)
{
    int32_t nsp = p->frame->bp - p->frame->nargs;
    assert(p->sp > p->frame->bp && "return value missing");
    p->stack[nsp - 1] = p->stack[p->sp - 1];
    p->sp = nsp;
    p->frame = p->frame->prev;
}

static ASTDCALL entry(void* ud)
{
    aframe_t frame;
    aprocess_t* p = (aprocess_t*)ud;
    int32_t nargs = (int32_t)p->stack[--p->sp].v.integer;
    memset(&frame, 0, sizeof(aframe_t));
    frame.bp = 1; // start from stack[0] (nil)
    frame.nargs = 0;
    p->frame = &frame;
    any_pcall(p, nargs);
    p->flags |= APF_EXIT;
    while (TRUE) atask_yield(&p->task);
}

aerror_t aprocess_init(
    aprocess_t* self, ascheduler_t* owner, aalloc_t alloc, void* alloc_ud)
{
    aerror_t ec;
    self->owner = owner;
    self->alloc = alloc;
    self->alloc_ud = alloc_ud;
    self->flags = 0;
    self->stack = (avalue_t*)aalloc(
        self, NULL, sizeof(avalue_t)*INIT_STACK_SZ);
    if (!self->stack) return AERR_FULL;
    self->stack_cap = INIT_STACK_SZ;
    self->sp = 0;
    any_push_nil(self); // stack[0] is nil
    ec = agc_init(&self->gc, INIT_HEAP_SZ, alloc, alloc_ud);
    if (ec != AERR_NONE) aalloc(self, self->stack, 0);
    return ec;
}

void aprocess_start(
{'fraction_non_alphanumeric': 0.16241865509761388, 'fraction_numerical': 0.015997830802603037, 'mean_word_length': 3.3708530805687205, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1740239', 'n_tokens_mistral': 1577, 'n_tokens_neox': 1522, 'n_words': 325}