1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-11-13 11:04:27 -05:00

mruby: create stack of mruby state pointers

This commit is contained in:
Alex Kotov 2022-02-01 09:27:25 +05:00
parent 2a510851e4
commit f39833b1b8
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08

View file

@ -5,37 +5,46 @@
#include <mruby.h>
#include <mruby/presym.h>
#define MRB_STACK_SIZE 100
// TODO: implement locking
// TODO: turn into stack to support nested callbacks
static mrb_state *current_mrb = NULL;
// TODO: replace assertions with actual abort
static size_t mrb_stack_count = 0;
static mrb_state *mrb_stack[MRB_STACK_SIZE];
void mrb_mruby_kernaux_gem_final(mrb_state *const mrb) {}
void mrb_mruby_kernaux_gem_init(mrb_state *const mrb)
{
for (size_t index = mrb_stack_count; index < MRB_STACK_SIZE; ++index) {
mrb_stack[index] = NULL;
}
mrb_define_module_id(mrb, MRB_SYM(KernAux));
init_assert(mrb);
}
void current_mrb_start(mrb_state *mrb)
{
mrb_assert(current_mrb == NULL);
mrb_assert(mrb_stack_count < MRB_STACK_SIZE - 1);
mrb_assert(mrb_stack[mrb_stack_count] == NULL);
mrb_assert(mrb != NULL);
current_mrb = mrb;
mrb_stack[mrb_stack_count++] = mrb;
}
void current_mrb_finish(mrb_state *mrb)
{
mrb_assert(current_mrb != NULL);
mrb_assert(current_mrb == mrb);
mrb_assert(mrb_stack_count > 0);
mrb_assert(mrb_stack[mrb_stack_count - 1] != NULL);
mrb_assert(mrb_stack[mrb_stack_count - 1] == mrb);
current_mrb = NULL;
mrb_stack[--mrb_stack_count] = NULL;
}
mrb_state *current_mrb_get()
{
mrb_assert(current_mrb != NULL);
mrb_assert(mrb_stack[mrb_stack_count - 1] != NULL);
return current_mrb;
return mrb_stack[mrb_stack_count - 1];
}