<commit_before>#include <terror/bootstrap.h>
#include <terror/darray.h>
#include <terror/bstrlib.h>
#include <terror/runtime.h>
#include <terror/primitives.h>
#include <terror/debugger.h>
#include <dirent.h>

VALUE Number_bp;
VALUE String_bp;
VALUE Vector_bp;
VALUE Map_bp;
VALUE Closure_bp;
int Debug;

static inline DArray*
kernel_files()
{
  DArray *entries = DArray_create(sizeof(bstring), 10);

  DIR *dirp = opendir("kernel"); // todo -- make it an absolute path
  struct dirent *dp;
  readdir(dirp); // .
  readdir(dirp); // ..

  while ((dp = readdir(dirp)) != NULL) {
    DArray_push(entries, bfromcstr(dp->d_name));
  }
  (void)closedir(dirp);

  return entries;
}

#define DEFPRIM(M, N, F) DArray_push((M), String_new(state, (N))); DArray_push((M), Closure_new(state, Function_native_new((F)), NULL))
#define DEFVALUE(M, N, V) DArray_push((M), String_new(state, (N))); DArray_push((M), (V));

static inline void
expose_VM(STATE, VALUE lobby)
{
  VALUE vm = Value_new(state, ObjectType);
  Value_set(state, lobby, "VM", vm);

  // VM.primitives map
  DArray *primitives = DArray_create(sizeof(VALUE), 10);

  // Object
  DEFPRIM(primitives, "to_s", Primitive_to_s);
  DEFPRIM(primitives, "prototype", Primitive_prototype);
  DEFPRIM(primitives, "or", Primitive_or);

  DEFPRIM(primitives, "equals", Primitive_equals);
  DEFPRIM(primitives, "is", Primitive_is);
  DEFPRIM(primitives, "print", Primitive_print);
  DEFPRIM(primitives, "puts", Primitive_puts);
  DEFPRIM(primitives, "require", Primitive_require);
  DEFPRIM(primitives, "clone", Primitive_clone);

  // Vector
  DEFPRIM(primitives, "vector_[]", Primitive_Vector_at);
  DEFPRIM(primitives, "vector_push", Primitive_Vector_push);
  DEFPRIM(primitives, "vector_to_map", Primitive_Vector_to_map);
  DEFPRIM(primitives, "vector_each", Primitive_Vector_each);
  DEFPRIM(primitives, "vector_each_with_index", Primitive_Vector_each_with_index);

  // Number
  DEFPRIM(primitives, "number_+", Primitive_Number_add);
  DEFPRIM(primitives, "number_-", Primitive_Number_sub);
  DEFPRIM(primitives, "number_*", Primitive_Number_mul);
  DEFPRIM(primitives, "number_/", Primitive_Number_div);
  DEFPRIM(primitives, "number_<", Primitive_Number_lt);
  DEFPRIM(primitives, "number_>", Primitive_Number_gt);

  // String
  DEFPRIM(primitives, "string_+", Primitive_String_concat);

  // Map
  DEFPRIM(primitives, "map_each", Primitive_Map_each);

  Value_set(state, vm, "primitives", Map_new(state, primitives));

  // VM.types map
  DArray *types = DArray_create(sizeof(VALUE), 10);

  DEFVALUE(types, "object", Object_bp);
  DEFVALUE(types, "number", Number_bp);
  DEFVALUE(types, "string", String_bp);
  DEFVALUE(types, "vector", Vector_bp);
  DEFVALUE(types, "map", Map_bp);
  DEFVALUE(types, "closure", Closure_bp);

  Value_set(state, vm, "types", Map_new(state, types));
}

void
State_bootstrap(STATE)
{
  DArray *filenames = kernel_files();
  int count = DArray_count(filenames);

  // Expose toplevel constants
  expose_VM(state, state->lobby);

  int reenable_debugger = 0;

  // Disable debugger while loading kernel files
  if(Debug) {
    reenable_debugger = 1;
    Debug = 0;
  }

  // Load all files.
  for(int i=0; i < count; i++) {
    bstring filename = (bstring)DArray_at(filenames, i);
    bstring path = bfromcstr("kernel/");
    bconcat(path, filename);
    debug("[BOOTSTRAP] Loading %s...", bdata(path));
    Primitive_require(state, String_new(state, bdata(path)), NULL, NULL);
  }<commit_msg>During bootstrap, assign values as soon as possible<commit_after>#include <terror/bootstrap.h>
#include <terror/darray.h>
#include <terror/bstrlib.h>
#include <terror/runtime.h>
#include <terror/primitives.h>
#include <terror/debugger.h>
#include <dirent.h>

VALUE Number_bp;
VALUE String_bp;
VALUE Vector_bp;
VALUE Map_bp;
VALUE Closure_bp;
int Debug;

static inline DArray*
kernel_files()
{
  DArray *entries = DArray_create(sizeof(bstring), 10);

  DIR *dirp = opendir("kernel"); // todo -- make it an absolute path
  struct dirent *dp;
  readdir(dirp); // .
  readdir(dirp); // ..

  while ((dp = readdir(dirp)) != NULL) {
    DArray_push(entries, bfromcstr(dp->d_name));
  }
  (void)closedir(dirp);

  return entries;
}

#define DEFPRIM(N, F) Value_set(state, primitives, (N), Closure_new(state, Function_native_new((F)), NULL))
#define DEFVALUE(N, V) Value_set(state, types, (N), (V))

static inline void
expose_VM(STATE, VALUE lobby)
{
  VALUE vm = Value_new(state, ObjectType);
  Value_set(state, lobby, "VM", vm);

  // VM.primitives map
  DArray *prims    = DArray_create(sizeof(VALUE), 10);
  VALUE primitives = Map_new(state, prims);
  Value_set(state, vm, "primitives", primitives);

  // Object
  DEFPRIM("to_s", Primitive_to_s);
  DEFPRIM("prototype", Primitive_prototype);
  DEFPRIM("or", Primitive_or);

  DEFPRIM("equals", Primitive_equals);
  DEFPRIM("is", Primitive_is);
  DEFPRIM("print", Primitive_print);
  DEFPRIM("puts", Primitive_puts);
  DEFPRIM("require", Primitive_require);
  DEFPRIM("clone", Primitive_clone);

  // Vector
  DEFPRIM("vector_[]", Primitive_Vector_at);
  DEFPRIM("vector_push", Primitive_Vector_push);
  DEFPRIM("vector_to_map", Primitive_Vector_to_map);
  DEFPRIM("vector_each", Primitive_Vector_each);
  DEFPRIM("vector_each_with_index", Primitive_Vector_each_with_index);

  // Number
  DEFPRIM("number_+", Primitive_Number_add);
  DEFPRIM("number_-", Primitive_Number_sub);
  DEFPRIM("number_*", Primitive_Number_mul);
  DEFPRIM("number_/", Primitive_Number_div);
  DEFPRIM("number_<", Primitive_Number_lt);
  DEFPRIM("number_>", Primitive_Number_gt);

  // String
  DEFPRIM("string_+", Primitive_String_concat);

  // Map
  DEFPRIM("map_each", Primitive_Map_each);

  // VM.types map
  DArray *ts = DArray_create(sizeof(VALUE), 10);
  VALUE types = Map_new(state, ts);
  Value_set(state, vm, "types", types);

  DEFVALUE("object", Object_bp);
  DEFVALUE("number", Number_bp);
  DEFVALUE("string", String_bp);
  DEFVALUE("vector", Vector_bp);
  DEFVALUE("map", Map_bp);
  DEFVALUE("closure", Closure_bp);
}

void
State_bootstrap(STATE)
{
  DArray *filenames = kernel_files();
  int count = DArray_count(filenames);

  // Expose toplevel constants
  expose_VM(state, state->lobby);

  int reenable_debugger = 0;

  // Disable debugger while loading kernel files
  if(Debug) {
    reenable_debugger = 1;
    Debug = 0;
  }

  // Load all files.
  for(int i=0; i < count; i++) {
    bstring filename = (bstring)DArray_at(filenames, i);
    bstring path = bfromcstr("kernel/");
    bconcat(path, filename);
    debug("[BOOTSTRAP] Loading %s...", bdata(path));
    Primitive_require(state, String_new(state, bdata(path)), NULL, NULL);
  }
{'fraction_non_alphanumeric': 0.15116453794139745, 'fraction_numerical': 0.003005259203606311, 'mean_word_length': 5.369377990430622, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, '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': '775160', 'n_tokens_mistral': 2532, 'n_tokens_neox': 2415, 'n_words': 491}