MVMString * MVM_coerce_n_s(MVMThreadContext *tc, MVMnum64 n) { if (n == MVM_num_posinf(tc)) { return MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "Inf"); } else if (n == MVM_num_neginf(tc)) { return MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "-Inf"); } else if (n != n) { return MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "NaN"); } else { char buf[64]; int i; if (snprintf(buf, 64, "%.15g", n) < 0) MVM_exception_throw_adhoc(tc, "Could not stringify number"); if (strstr(buf, ".")) { MVMint64 is_not_scientific = !strstr(buf, "e"); i = strlen(buf); while (i > 1 && ((buf[--i] == '0' && is_not_scientific) || buf[i] == ' ')) buf[i] = '\0'; if (buf[i] == '.') buf[i] = '\0'; } return MVM_string_ascii_decode(tc, tc->instance->VMString, buf, strlen(buf)); } }
/* Sets up some string constants. */ static void string_consts(MVMThreadContext *tc) { MVMInstance * const instance = tc->instance; instance->str_consts.empty = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, ""); MVM_gc_root_add_permanent(tc, (MVMCollectable **)&instance->str_consts.empty); instance->str_consts.Str = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "Str"); MVM_gc_root_add_permanent(tc, (MVMCollectable **)&instance->str_consts.Str); instance->str_consts.Num = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "Num"); MVM_gc_root_add_permanent(tc, (MVMCollectable **)&instance->str_consts.Num); }
MVMint64 MVM_file_isexecutable(MVMThreadContext *tc, MVMString *filename, MVMint32 use_lstat) { if (!MVM_file_exists(tc, filename, use_lstat)) return 0; else { MVMint64 r = 0; uv_stat_t statbuf = file_info(tc, filename, use_lstat); if ((statbuf.st_mode & S_IFMT) == S_IFDIR) return 1; else { // true if fileext is in PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC MVMString *dot = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "."); MVMROOT(tc, dot, { MVMint64 n = MVM_string_index_from_end(tc, filename, dot, 0); if (n >= 0) { MVMString *fileext = MVM_string_substring(tc, filename, n, -1); char *ext = MVM_string_utf8_encode_C_string(tc, fileext); char *pext = getenv("PATHEXT"); int plen = strlen(pext); int i; for (i = 0; i < plen; i++) { if (0 == stricmp(ext, pext++)) { r = 1; break; } } MVM_free(ext); MVM_free(pext); } }); } return r; }
int MVM_repr_register_dynamic_repr(MVMThreadContext *tc, MVMREPROps *repr) { MVMReprRegistry *entry; MVMString *name; uv_mutex_lock(&tc->instance->mutex_repr_registry); name = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, repr->name); MVM_string_flatten(tc, name); MVM_HASH_GET(tc, tc->instance->repr_hash, name, entry); if (entry) { uv_mutex_unlock(&tc->instance->mutex_repr_registry); return 0; } if (!(tc->instance->num_reprs < MVM_REPR_MAX_COUNT)) { uv_mutex_unlock(&tc->instance->mutex_repr_registry); MVM_exception_throw_adhoc(tc, "Cannot register more than %u representations", MVM_REPR_MAX_COUNT); } repr->ID = tc->instance->num_reprs++; register_repr(tc, repr, name); uv_mutex_unlock(&tc->instance->mutex_repr_registry); return 1; }
static void code_pair_configure_container_spec(MVMThreadContext *tc, MVMSTable *st, MVMObject *config) { CodePairContData *data = (CodePairContData *)st->container_data; MVMROOT(tc, config, { MVMString *fetch = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "fetch"); MVMString *store; if (!MVM_repr_exists_key(tc, config, fetch)) MVM_exception_throw_adhoc(tc, "Container spec 'code_pair' must be configured with a fetch"); MVM_ASSIGN_REF(tc, &(st->header), data->fetch_code, MVM_repr_at_key_o(tc, config, fetch)); store = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "store"); if (!MVM_repr_exists_key(tc, config, store)) MVM_exception_throw_adhoc(tc, "Container spec 'code_pair' must be configured with a store"); MVM_ASSIGN_REF(tc, &(st->header), data->store_code, MVM_repr_at_key_o(tc, config, store)); });
MVMString * MVM_io_get_hostname(MVMThreadContext *tc) { char hostname[MAXHOSTNAMELEN+1]; size_t size = MAXHOSTNAMELEN+1; int result = uv_os_gethostname(hostname, &size); if(result < 0) { MVM_exception_throw_adhoc(tc, "Failed to get hostname: %i", result); } return MVM_string_ascii_decode_nt(tc, tc->instance->VMString, hostname); }
/* Bootstraps the KnowHOW type. */ static void bootstrap_KnowHOW(MVMThreadContext *tc) { MVMObject *VMString = tc->instance->VMString; MVMObject *BOOTArray = tc->instance->boot_types->BOOTArray; MVMObject *BOOTHash = tc->instance->boot_types->BOOTHash; /* Create our KnowHOW type object. Note we don't have a HOW just yet, so * pass in NULL. */ MVMREPROps *REPR = MVM_repr_get_by_id(tc, MVM_REPR_ID_KnowHOWREPR); MVMObject *knowhow = REPR->type_object_for(tc, NULL); /* We create a KnowHOW instance that can describe itself. This means * (once we tie the knot) that .HOW.HOW.HOW.HOW etc will always return * that, which closes the model up. Note that the STable for it must * be allocated first, since that holds the allocation size. */ MVMKnowHOWREPR *knowhow_how; MVMSTable *st = MVM_gc_allocate_stable(tc, REPR, NULL); st->WHAT = (MVMObject *)knowhow; st->size = sizeof(MVMKnowHOWREPR); knowhow_how = (MVMKnowHOWREPR *)REPR->allocate(tc, st); st->HOW = (MVMObject *)knowhow_how; knowhow_how->common.st = st; /* Add various methods to the KnowHOW's HOW. */ REPR->initialize(tc, NULL, (MVMObject *)knowhow_how, &knowhow_how->body); add_knowhow_how_method(tc, knowhow_how, "new_type", new_type); add_knowhow_how_method(tc, knowhow_how, "add_method", add_method); add_knowhow_how_method(tc, knowhow_how, "add_attribute", add_attribute); add_knowhow_how_method(tc, knowhow_how, "compose", compose); add_knowhow_how_method(tc, knowhow_how, "attributes", attributes); add_knowhow_how_method(tc, knowhow_how, "methods", methods); add_knowhow_how_method(tc, knowhow_how, "name", name); /* Set name KnowHOW for the KnowHOW's HOW. */ knowhow_how->body.name = MVM_string_ascii_decode_nt(tc, VMString, "KnowHOW"); /* Set this built up HOW as the KnowHOW's HOW. */ STABLE(knowhow)->HOW = (MVMObject *)knowhow_how; /* Give it an authoritative method cache; this in turn will make the * method dispatch bottom out. */ STABLE(knowhow)->method_cache = knowhow_how->body.methods; STABLE(knowhow)->mode_flags = MVM_METHOD_CACHE_AUTHORITATIVE; STABLE(knowhow_how)->method_cache = knowhow_how->body.methods; STABLE(knowhow_how)->mode_flags = MVM_METHOD_CACHE_AUTHORITATIVE; /* Associate the created objects with the initial core serialization * context. */ /* XXX TODO */ /* Stash the created KnowHOW. */ tc->instance->KnowHOW = (MVMObject *)knowhow; MVM_gc_root_add_permanent(tc, (MVMCollectable **)&tc->instance->KnowHOW); }
int MVM_ext_load(MVMThreadContext *tc, MVMString *lib, MVMString *ext) { MVMString *colon, *prefix, *name; MVMExtRegistry *entry; MVMDLLSym *sym; void (*init)(MVMThreadContext *); MVMROOT2(tc, lib, ext, { colon = MVM_string_ascii_decode_nt( tc, tc->instance->VMString, ":"); prefix = MVM_string_concatenate(tc, lib, colon); name = MVM_string_concatenate(tc, prefix, ext); });
/* Takes a stub object that existed before we had bootstrapped things and * gives it a meta-object. */ static void add_meta_object(MVMThreadContext *tc, MVMObject *type_obj, char *name) { MVMObject *meta_obj; MVMString *name_str; /* Create meta-object. */ meta_obj = MVM_repr_alloc_init(tc, STABLE(tc->instance->KnowHOW)->HOW); MVMROOT(tc, meta_obj, { /* Put it in place. */ MVM_ASSIGN_REF(tc, STABLE(type_obj), STABLE(type_obj)->HOW, meta_obj); /* Set name. */ name_str = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, name); MVM_ASSIGN_REF(tc, meta_obj, ((MVMKnowHOWREPR *)meta_obj)->body.name, name_str); });
MVMObject * Kiji_bootstrap_Pair(MVMCompUnit* cu, MVMThreadContext*tc) { MVMObject * type = MVM_6model_find_method( tc, STABLE(tc->instance->KnowHOW)->HOW, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, (char*)"new_type") ); MVMObject * how = STABLE(type)->HOW; CLASS_INIT(); /* CLASS_ADD_METHOD("elems", Hash_elems); */ CLASS_REGISTER(type); return type; }
/* Adds a method into the KnowHOW.HOW method table. */ static void add_knowhow_how_method(MVMThreadContext *tc, MVMKnowHOWREPR *knowhow_how, char *name, void (*func) (MVMThreadContext *, MVMCallsite *, MVMRegister *)) { MVMObject *BOOTCCode, *code_obj, *method_table, *name_str; /* Create string for name. */ name_str = (MVMObject *)MVM_string_ascii_decode_nt(tc, tc->instance->VMString, name); /* Allocate a BOOTCCode and put pointer in. */ BOOTCCode = tc->instance->boot_types->BOOTCCode; code_obj = REPR(BOOTCCode)->allocate(tc, STABLE(BOOTCCode)); ((MVMCFunction *)code_obj)->body.func = func; /* Add into the table. */ method_table = knowhow_how->body.methods; REPR(method_table)->ass_funcs->bind_key_boxed(tc, STABLE(method_table), method_table, OBJECT_BODY(method_table), name_str, code_obj); }
/* Registers a representation. */ static void register_repr(MVMThreadContext *tc, const MVMREPROps *repr, MVMString *name) { MVMReprRegistry *entry; if (!name) name = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, repr->name); /* Fill a registry entry. */ entry = malloc(sizeof(MVMReprRegistry)); entry->name = name; entry->repr = repr; /* Name should become a permanent GC root. */ MVM_gc_root_add_permanent(tc, (MVMCollectable **)&entry->name); /* Enter into registry. */ tc->instance->repr_list[repr->ID] = entry; MVM_string_flatten(tc, name); MVM_HASH_BIND(tc, tc->instance->repr_hash, name, entry); }
/* Sets up some string constants. */ static void string_consts(MVMThreadContext *tc) { MVMInstance * const instance = tc->instance; instance->str_consts.empty = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, ""); MVM_gc_root_add_permanent(tc, (MVMCollectable **)&instance->str_consts.empty); instance->str_consts.Str = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "Str"); MVM_gc_root_add_permanent(tc, (MVMCollectable **)&instance->str_consts.Str); instance->str_consts.Num = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "Num"); MVM_gc_root_add_permanent(tc, (MVMCollectable **)&instance->str_consts.Num); instance->str_consts.find_method = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "find_method"); MVM_gc_root_add_permanent(tc, (MVMCollectable **)&instance->str_consts.find_method); instance->str_consts.type_check = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "type_check"); MVM_gc_root_add_permanent(tc, (MVMCollectable **)&instance->str_consts.type_check); instance->str_consts.accepts_type = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "accepts_type"); MVM_gc_root_add_permanent(tc, (MVMCollectable **)&instance->str_consts.accepts_type); }
MVMString * MVM_io_get_hostname(MVMThreadContext *tc) { char hostname[65]; gethostname(hostname, 65); return MVM_string_ascii_decode_nt(tc, tc->instance->VMString, hostname); }
/* Returns a list of hashes containing file, line, sub and annotations. */ MVMObject * MVM_exception_backtrace(MVMThreadContext *tc, MVMObject *ex_obj) { MVMFrame *cur_frame; MVMObject *arr = NULL, *annotations = NULL, *row = NULL, *value = NULL; MVMuint32 count = 0; MVMString *k_file = NULL, *k_line = NULL, *k_sub = NULL, *k_anno = NULL; if (IS_CONCRETE(ex_obj) && REPR(ex_obj)->ID == MVM_REPR_ID_MVMException) cur_frame = ((MVMException *)ex_obj)->body.origin; else MVM_exception_throw_adhoc(tc, "Op 'backtrace' needs an exception object"); MVM_gc_root_temp_push(tc, (MVMCollectable **)&arr); MVM_gc_root_temp_push(tc, (MVMCollectable **)&annotations); MVM_gc_root_temp_push(tc, (MVMCollectable **)&row); MVM_gc_root_temp_push(tc, (MVMCollectable **)&value); MVM_gc_root_temp_push(tc, (MVMCollectable **)&k_file); MVM_gc_root_temp_push(tc, (MVMCollectable **)&k_line); MVM_gc_root_temp_push(tc, (MVMCollectable **)&k_sub); MVM_gc_root_temp_push(tc, (MVMCollectable **)&k_anno); k_file = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "file"); k_line = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "line"); k_sub = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "sub"); k_anno = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "annotations"); arr = MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTArray); while (cur_frame != NULL) { MVMuint8 *cur_op = count ? cur_frame->return_address : cur_frame->throw_address; MVMuint32 offset = cur_op - cur_frame->effective_bytecode; MVMBytecodeAnnotation *annot = MVM_bytecode_resolve_annotation(tc, &cur_frame->static_info->body, offset > 0 ? offset - 1 : 0); MVMint32 fshi = annot ? (MVMint32)annot->filename_string_heap_index : -1; char *line_number = malloc(16); snprintf(line_number, 16, "%d", annot ? annot->line_number : 1); /* annotations hash will contain "file" and "line" */ annotations = MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTHash); /* file */ if (fshi >= 0 && fshi < cur_frame->static_info->body.cu->body.num_strings) value = MVM_repr_box_str(tc, MVM_hll_current(tc)->str_box_type, cur_frame->static_info->body.cu->body.strings[fshi]); else value = MVM_repr_box_str(tc, MVM_hll_current(tc)->str_box_type, cur_frame->static_info->body.cu->body.filename); MVM_repr_bind_key_o(tc, annotations, k_file, value); /* line */ value = (MVMObject *)MVM_string_ascii_decode_nt(tc, tc->instance->VMString, line_number); value = MVM_repr_box_str(tc, MVM_hll_current(tc)->str_box_type, (MVMString *)value); MVM_repr_bind_key_o(tc, annotations, k_line, value); free(line_number); /* row will contain "sub" and "annotations" */ row = MVM_repr_alloc_init(tc, tc->instance->boot_types.BOOTHash); MVM_repr_bind_key_o(tc, row, k_sub, cur_frame->code_ref); MVM_repr_bind_key_o(tc, row, k_anno, annotations); MVM_repr_push_o(tc, arr, row); cur_frame = cur_frame->caller; while (cur_frame && cur_frame->static_info->body.is_thunk) cur_frame = cur_frame->caller; count++; } MVM_gc_root_temp_pop_n(tc, 8); return arr; }
static MVMString * str(MVMThreadContext *tc, const char *buf) { return MVM_string_ascii_decode_nt(tc, tc->instance->VMString, buf); }
/* Initializes the representations registry, building up all of the various * representations. */ void MVM_repr_initialize_registry(MVMThreadContext *tc) { /* Add all core representations. (If order changed, update reprs.h IDs.) */ register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "MVMString"), MVMString_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "VMArray"), MVMArray_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "VMHash"), MVMHash_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "MVMCFunction"), MVMCFunction_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "KnowHOWREPR"), MVMKnowHOWREPR_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "P6opaque"), MVMP6opaque_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "MVMCode"), MVMCode_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "MVMOSHandle"), MVMOSHandle_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "P6int"), P6int_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "P6num"), P6num_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "Uninstantiable"), Uninstantiable_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "HashAttrStore"), HashAttrStore_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "KnowHOWAttributeREPR"), MVMKnowHOWAttributeREPR_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "P6str"), P6str_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "MVMThread"), MVMThread_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "VMIter"), MVMIter_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "MVMContext"), MVMContext_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "SCRef"), MVMSCRef_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "Lexotic"), MVMLexotic_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "MVMCallCapture"), MVMCallCapture_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "P6bigint"), P6bigint_initialize(tc)); register_repr(tc, MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "NFA"), MVMNFA_initialize(tc)); }