<commit_before>#include "defs.hpp"
#include "utils.hpp"
#include "report.hpp"
#include "mmc_image.hpp"
#include "core_image.hpp"
#include "mmobj.hpp"
#include "vm.hpp"
#include "process.hpp"
#include <assert.h>

using namespace std;

word MMCImage::HEADER_SIZE = 5 * WSIZE;
word MMCImage::MAGIC_NUMBER = 0x420;

MMCImage::MMCImage(VM* vm, CoreImage* core_image, const std::string& name_or_path)
  : _vm(vm), _mmobj(vm->mmobj()), _core_image(core_image), _name_or_path(name_or_path) {
}

void MMCImage::load_header() {
  word magic_number = unpack_word(_data, 0 * WSIZE);
  _ot_size = unpack_word(_data,  1 * WSIZE);
  _er_size = unpack_word(_data, 2 * WSIZE);
  _es_size = unpack_word(_data, 3 * WSIZE);
  _names_size = unpack_word(_data,  4 * WSIZE);

  debug() << " ============ Module: " << _name_or_path << " ===========" << endl;
  debug() << "Header:magic: " << magic_number << " =?= " << MMCImage::MAGIC_NUMBER << endl;
  debug() << "Header:ot_size: " << _ot_size << endl;
  debug() << "Header:er_size: " << _er_size << endl;
  debug() << "Header:es_size: " << _es_size << endl;
  debug() << "Header:names_size: " << _names_size << endl;
}

void MMCImage::link_external_references() {
  const char* base = _data;
  int start_external_refs = HEADER_SIZE + _names_size + _ot_size;

  for (int i = 0; i < _er_size; i += (2 * WSIZE)) {
    word name_offset = unpack_word(_data, start_external_refs + i);
    char* name = (char*) (base + name_offset);
    word obj_offset = unpack_word(_data, start_external_refs + i + WSIZE);
    // word* obj = (word*) (base + obj_offset);
    // debug() << obj_offset << " - " << *obj << " [" << name << "] -> " << _core_image->get_prime(name) << endl;
    * (word*) (base + obj_offset) = (word) _core_image->get_prime(name);
    // debug() << "External refs " << obj_offset << " - " << (oop) *obj << " [" << name << "] -> " << _core_image->get_prime(name) << endl;
  }
}

oop MMCImage::instantiate_class(oop class_name, oop cclass, oop cclass_dict, std::map<std::string, oop>& mod_classes, oop imodule) {
  char* cname = _mmobj->mm_string_cstr(class_name);
  debug() << "Instantiating class " << cname << endl;
  oop super_name = _mmobj->mm_compiled_class_super_name(cclass);
  char* super_name_str = _mmobj->mm_string_cstr(super_name);
  debug() << "Super " << super_name_str << endl;

  oop cmod_params_list =   _mmobj->mm_compiled_module_params(_compiled_module);
  oop cmod_aliases_dict = _mmobj->mm_compiled_module_aliases(_compiled_module);
  // number num_params = _mmobj->mm_list_size(
  //   _mmobj->mm_compiled_module_params(_compiled_module));

  oop super_class = NULL;
  if (mod_classes.find(super_name_str) != mod_classes.end()) {
    debug() << "Super class already instantiated" << endl;
    super_class = mod_classes.at(super_name_str);
  } else if (_mmobj->mm_dictionary_has_key(cclass_dict, _vm->new_symbol(super_name_str))) {
    debug() << "Super class not instantiated. recursively instantiate it" << endl;
    super_class = instantiate_class(super_name, _mmobj->mm_dictionary_get(cclass_dict, _vm->new_symbol(super_name_str)), cclass_dict, mod_classes, imodule);
  } else if(_mmobj->mm_dictionary_has_key(cmod_aliases_dict, _vm->new_symbol(super_name_str))) {
    //when loading the aliases, have a _alias_idx_map<oop[string], int> to indicate where it will be in the imod
    super_class = _mmobj->mm_module_get_param(
      imodule, _alias_idx_map[super_name]); //_mmobj->mm_dictionary_index_of(cmod_aliases_dict, super_name) + num_params
    debug() << "Super class is module alias: " << super_class << endl;
  } else  if (_mmobj->mm_list_index_of(cmod_params_list, super_name) != -1) {
    super_class = _mmobj->mm_module_get_param(
      imodule, _mmobj->mm_list_index_of(cmod_params_list, super_name));
    debug() << "Super class is module parameter: " << super_class << endl;
  }
  else if (_core_image->has_class(super_name_str)) {
    debug() << "Super class got from super module (core)" << endl;
    super_class = _core_image->get_prime(super_name_str);
  } else {
    bail("Super class not found");
  }

  oop class_funs_dict = _mmobj->mm_cfuns_to_funs_dict(_mmobj->mm_compiled_class_own_methods(cclass), imodule);
  oop class_behavior = _mmobj->mm_class_behavior_new(super_class, class_funs_dict);

  oop funs_dict = _mmobj->mm_cfuns_to_funs_dict(_mmobj->mm_compiled_class_methods(cclass), imodule);
  number num_fields = _mmobj->mm_compiled_class_num_fields(cclass);
  oop klass = _mmobj->mm_class_new(class_behavior, super_class, funs_dict, cclass, num_fields);
  mod_classes[cname] = klass;<commit_msg>Fixing type bug: should be a symbol instead of string<commit_after>#include "defs.hpp"
#include "utils.hpp"
#include "report.hpp"
#include "mmc_image.hpp"
#include "core_image.hpp"
#include "mmobj.hpp"
#include "vm.hpp"
#include "process.hpp"
#include <assert.h>

using namespace std;

word MMCImage::HEADER_SIZE = 5 * WSIZE;
word MMCImage::MAGIC_NUMBER = 0x420;

MMCImage::MMCImage(VM* vm, CoreImage* core_image, const std::string& name_or_path)
  : _vm(vm), _mmobj(vm->mmobj()), _core_image(core_image), _name_or_path(name_or_path) {
}

void MMCImage::load_header() {
  word magic_number = unpack_word(_data, 0 * WSIZE);
  _ot_size = unpack_word(_data,  1 * WSIZE);
  _er_size = unpack_word(_data, 2 * WSIZE);
  _es_size = unpack_word(_data, 3 * WSIZE);
  _names_size = unpack_word(_data,  4 * WSIZE);

  debug() << " ============ Module: " << _name_or_path << " ===========" << endl;
  debug() << "Header:magic: " << magic_number << " =?= " << MMCImage::MAGIC_NUMBER << endl;
  debug() << "Header:ot_size: " << _ot_size << endl;
  debug() << "Header:er_size: " << _er_size << endl;
  debug() << "Header:es_size: " << _es_size << endl;
  debug() << "Header:names_size: " << _names_size << endl;
}

void MMCImage::link_external_references() {
  const char* base = _data;
  int start_external_refs = HEADER_SIZE + _names_size + _ot_size;

  for (int i = 0; i < _er_size; i += (2 * WSIZE)) {
    word name_offset = unpack_word(_data, start_external_refs + i);
    char* name = (char*) (base + name_offset);
    word obj_offset = unpack_word(_data, start_external_refs + i + WSIZE);
    // word* obj = (word*) (base + obj_offset);
    // debug() << obj_offset << " - " << *obj << " [" << name << "] -> " << _core_image->get_prime(name) << endl;
    * (word*) (base + obj_offset) = (word) _core_image->get_prime(name);
    // debug() << "External refs " << obj_offset << " - " << (oop) *obj << " [" << name << "] -> " << _core_image->get_prime(name) << endl;
  }
}

oop MMCImage::instantiate_class(oop class_name, oop cclass, oop cclass_dict, std::map<std::string, oop>& mod_classes, oop imodule) {
  char* cname = _mmobj->mm_string_cstr(class_name);
  debug() << "Instantiating class " << cname << endl;
  oop super_name = _mmobj->mm_compiled_class_super_name(cclass);
  char* super_name_str = _mmobj->mm_string_cstr(super_name);
  debug() << "Super " << super_name_str << endl;

  oop cmod_params_list =   _mmobj->mm_compiled_module_params(_compiled_module);
  oop cmod_aliases_dict = _mmobj->mm_compiled_module_aliases(_compiled_module);
  // number num_params = _mmobj->mm_list_size(
  //   _mmobj->mm_compiled_module_params(_compiled_module));

  oop super_class = NULL;
  if (mod_classes.find(super_name_str) != mod_classes.end()) {
    debug() << "Super class already instantiated" << endl;
    super_class = mod_classes.at(super_name_str);
  } else if (_mmobj->mm_dictionary_has_key(cclass_dict, _vm->new_symbol(super_name_str))) {
    debug() << "Super class not instantiated. recursively instantiate it" << endl;
    super_class = instantiate_class(super_name, _mmobj->mm_dictionary_get(cclass_dict, _vm->new_symbol(super_name_str)), cclass_dict, mod_classes, imodule);
  } else if(_mmobj->mm_dictionary_has_key(cmod_aliases_dict, _vm->new_symbol(super_name_str))) {
    //when loading the aliases, have a _alias_idx_map<oop[string], int> to indicate where it will be in the imod
    super_class = _mmobj->mm_module_get_param(
      imodule, _alias_idx_map[_vm->new_symbol(super_name_str)]); //_mmobj->mm_dictionary_index_of(cmod_aliases_dict, super_name) + num_params
    debug() << "Super class is module alias: " << super_class << endl;
  } else  if (_mmobj->mm_list_index_of(cmod_params_list, super_name) != -1) {
    super_class = _mmobj->mm_module_get_param(
      imodule, _mmobj->mm_list_index_of(cmod_params_list, super_name));
    debug() << "Super class is module parameter: " << super_class << endl;
  }
  else if (_core_image->has_class(super_name_str)) {
    debug() << "Super class got from super module (core)" << endl;
    super_class = _core_image->get_prime(super_name_str);
  } else {
    bail("Super class not found");
  }

  oop class_funs_dict = _mmobj->mm_cfuns_to_funs_dict(_mmobj->mm_compiled_class_own_methods(cclass), imodule);
  oop class_behavior = _mmobj->mm_class_behavior_new(super_class, class_funs_dict);

  oop funs_dict = _mmobj->mm_cfuns_to_funs_dict(_mmobj->mm_compiled_class_methods(cclass), imodule);
  number num_fields = _mmobj->mm_compiled_class_num_fields(cclass);
  oop klass = _mmobj->mm_class_new(class_behavior, super_class, funs_dict, cclass, num_fields);
  mod_classes[cname] = klass;
{'fraction_non_alphanumeric': 0.15298709747370703, 'fraction_numerical': 0.0028190393581264232, 'mean_word_length': 5.203093476798924, 'pattern_counts': {'":': 0, '<': 235, '<?xml version=': 0, '>': 76, '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': '7356437', 'n_tokens_mistral': 3491, 'n_tokens_neox': 3375, 'n_words': 644}