<commit_before>#include <string.h>
#include <stdlib.h>

#include "shotgun/lib/shotgun.h"
#include "shotgun/lib/metaclass.h"
#include "shotgun/lib/hash.h"
#include "shotgun/lib/lookuptable.h"
#include "shotgun/lib/string.h"
#include "shotgun/lib/tuple.h"
#include "shotgun/lib/class.h"

OBJECT object_new(STATE) {
  return object_allocate(state);
}

OBJECT object_create_metaclass(STATE, OBJECT cls, OBJECT sup) {
  OBJECT meta;
  if(!sup) {
    sup = cls->klass;
  }
  
  meta = metaclass_s_attach(state, cls);
  class_set_superclass(meta, sup);
  
  return meta;
}

OBJECT object_metaclass(STATE, OBJECT obj) {
  if(REFERENCE_P(obj)) {
    if( metaclass_s_metaclass_p(state, obj->klass) ) {
      return obj->klass;
    }
    return object_create_metaclass(state, obj, 0);
  }
  
  if( NIL_P(obj) ) {
    return state->global->nil_class;
  } else if( TRUE_P(obj) ) {
    return state->global->true_class;
  } else if( FALSE_P(obj) ) { 
    return state->global->false_class;
  }
  
  return Qnil;
}

OBJECT object_make_weak_ref(STATE, OBJECT self) {
  OBJECT tup;
  
  tup = tuple_new2(state, 1, self);
  tup->RefsAreWeak = TRUE;
  
  return tup;
}

int object_kind_of_p(STATE, OBJECT self, OBJECT cls) {
  OBJECT found;
  
  found = object_class(state, self);
  if(found == cls) {
    return TRUE;
  }
  
  /* Normal class inheritence checking. */
  
  while(RTEST(found)) {
    found = class_get_superclass(found);
    if(found == cls) { return TRUE; }
    
    if(REFERENCE_P(found) && found->obj_type == IncModType) {
      if(included_module_get_module(found) == cls) { return TRUE; }
    }
    
  }
  
  return FALSE;
}

#define REMEMBER_FLAG 0x10

void object_propgate_gc_info(STATE, OBJECT self, OBJECT dest) {
  if(dest->gc_zone != MatureObjectZone || dest->Remember)
    return;
    
  if(self->gc_zone == MatureObjectZone) {
    if(self->Remember) {
      ptr_array_append(state->om->gc->remember_set, (xpointer)dest);
      dest->Remember = TRUE;
    }
  } else {
    int i;
    OBJECT tmp;
    for(i = 0; i < NUM_FIELDS(dest); i++) {
      tmp = NTH_FIELD(dest, i);
      if(!REFERENCE_P(tmp)) continue;
      
      if(tmp->gc_zone == YoungObjectZone) {
        ptr_array_append(state->om->gc->remember_set, (xpointer)dest);
        dest->Remember = TRUE;
        /* We can return because the only setting we have is now
           correct, no need to look through all the rest. */
        return;<commit_msg>Fixed kind_of? to check #extended Modules.<commit_after>#include <string.h>
#include <stdlib.h>

#include "shotgun/lib/shotgun.h"
#include "shotgun/lib/metaclass.h"
#include "shotgun/lib/hash.h"
#include "shotgun/lib/lookuptable.h"
#include "shotgun/lib/string.h"
#include "shotgun/lib/tuple.h"
#include "shotgun/lib/class.h"

OBJECT object_new(STATE) {
  return object_allocate(state);
}

OBJECT object_create_metaclass(STATE, OBJECT cls, OBJECT sup) {
  OBJECT meta;
  if(!sup) {
    sup = cls->klass;
  }
  
  meta = metaclass_s_attach(state, cls);
  class_set_superclass(meta, sup);
  
  return meta;
}

OBJECT object_metaclass(STATE, OBJECT obj) {
  if(REFERENCE_P(obj)) {
    if( metaclass_s_metaclass_p(state, obj->klass) ) {
      return obj->klass;
    }
    return object_create_metaclass(state, obj, 0);
  }
  
  if( NIL_P(obj) ) {
    return state->global->nil_class;
  } else if( TRUE_P(obj) ) {
    return state->global->true_class;
  } else if( FALSE_P(obj) ) { 
    return state->global->false_class;
  }
  
  return Qnil;
}

OBJECT object_make_weak_ref(STATE, OBJECT self) {
  OBJECT tup;
  
  tup = tuple_new2(state, 1, self);
  tup->RefsAreWeak = TRUE;
  
  return tup;
}

/*
 * Object#kind_of?
 *   True if given Module is obj's class or one of object's ancestors,
 *   a module included in class or in an ancestor or obj extended by
 *   the module. False otherwise.
 */
int object_kind_of_p(STATE, OBJECT self, OBJECT mod) {
  OBJECT found;

  if(REFERENCE_P(self)) {       /* object_class() skips metaclasses */
    found = self->klass;
  }
  else {
    found = state->global->special_classes[((uintptr_t)self) & SPECIAL_CLASS_MASK];
  }

  if(found == mod) {
    return TRUE;
  }

  while(RTEST(found)) {
    found = class_get_superclass(found);

    if(found == mod) {
      return TRUE;
    }

    /* IncludedModule is a wrapper around, well, an #included Module */
    if(REFERENCE_P(found) && found->obj_type == IncModType) {
      if(included_module_get_module(found) == mod) {
        return TRUE;
      }
    }
  }

  return FALSE;
}

#define REMEMBER_FLAG 0x10

void object_propgate_gc_info(STATE, OBJECT self, OBJECT dest) {
  if(dest->gc_zone != MatureObjectZone || dest->Remember)
    return;
    
  if(self->gc_zone == MatureObjectZone) {
    if(self->Remember) {
      ptr_array_append(state->om->gc->remember_set, (xpointer)dest);
      dest->Remember = TRUE;
    }
  } else {
    int i;
    OBJECT tmp;
    for(i = 0; i < NUM_FIELDS(dest); i++) {
      tmp = NTH_FIELD(dest, i);
      if(!REFERENCE_P(tmp)) continue;
      
      if(tmp->gc_zone == YoungObjectZone) {
        ptr_array_append(state->om->gc->remember_set, (xpointer)dest);
        dest->Remember = TRUE;
        /* We can return because the only setting we have is now
           correct, no need to look through all the rest. */
        return;
{'fraction_non_alphanumeric': 0.13386423966628744, 'fraction_numerical': 0.0026545316647705727, 'mean_word_length': 3.1666666666666665, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 58, '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': '2444957', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1943, 'n_words': 408}