Skip to content

Instantly share code, notes, and snippets.

@timo
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timo/c4f0a662957771347a31 to your computer and use it in GitHub Desktop.
Save timo/c4f0a662957771347a31 to your computer and use it in GitHub Desktop.
MVM_PUBLIC MVMObject * MVM_code_location(MVMThreadContext *tc, MVMObject *code) {
MVMObject *BOOTHash = tc->instance->boot_types.BOOTHash;
MVMCodeBody *body = &((MVMCode*)code)->body;
if (REPR(code)->ID != MVM_REPR_ID_MVMCode) {
MVM_exception_throw_adhoc(tc, "getcodelocation needs an object of MVMCode REPR, got %s instead", REPR(code)->name);
} else {
MVMObject * result = REPR(BOOTHash)->allocate(tc, STABLE(BOOTHash));
MVMBytecodeAnnotation *ann = MVM_bytecode_resolve_annotation(tc, &body->sf->body, 0);
MVMCompUnit *cu = body->sf->body.cu;
MVMint32 str_idx = ann ? ann->filename_string_heap_index : 0;
MVMint32 line_nr = ann ? ann->line_number : 1;
MVMString *filename = cu->body.filename;
MVMObject *filename_boxed;
MVMObject *linenumber_boxed;
MVMString *filename_key;
MMVM_gc_root_temp_push(tc, (MVMCollectable **)&result);
MVMString *filename_key = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "file");
MMVM_gc_root_temp_push(tc, (MVMCollectable **)&filename_key);
MVMString *linenumber_key = MVM_string_ascii_decode_nt(tc, tc->instance->VMString, "line");
MMVM_gc_root_temp_push(tc, (MVMCollectable **)&linenumber_key);
if (ann && str_idx < cu->body.num_strings) {
filename = cu->body.strings[str_idx];
}
filename_boxed = MVM_repr_box_str(tc, tc->instance->boot_types.BOOTStr, filename)
MMVM_gc_root_temp_push(tc, (MVMCollectable **)&filename_boxed);
linenumber_boxed = MVM_repr_box_int(tc, tc->instance->boot_types.BOOTInt, line_nr)
MMVM_gc_root_temp_push(tc, (MVMCollectable **)&linenumber_boxed);
MVM_repr_bind_key_o(tc, result, filename_key,
filename_boxed
);
MVM_repr_bind_key_o(tc, result, linenumber_key,
linenumber_boxed
);
MVM_gc_root_temp_pop_n(tc, 5);
return result;
}
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment