void test_body() { std::istringstream stream; stream.str("!RBIX\n1\n42\nt"); CompiledFile* cf = CompiledFile::load(stream); TS_ASSERT_EQUALS(cf->body(state), cTrue); }
// // HACK: remove this when performance is better and compiled_file.rb // unmarshal_data method works. Object* System::compiledfile_load(STATE, String* path, Integer* version) { if(!state->probe->nil_p()) { state->probe->load_runtime(state, std::string(path->c_str())); } std::ifstream stream(path->c_str()); if(!stream) { return Primitives::failure(); } CompiledFile* cf = CompiledFile::load(stream); if(cf->magic != "!RBIX") { return Primitives::failure(); } uint64_t ver = version->to_ulong_long(); if(ver > 0 && cf->version > 0 && cf->version != ver) { return Primitives::failure(); } Object *body = cf->body(state); delete cf; return body; }
void Environment::run_file(std::string file) { if(!state->probe->nil_p()) state->probe->load_runtime(state, file); std::ifstream stream(file.c_str()); if(!stream) throw std::runtime_error("Unable to open file to run"); CompiledFile* cf = CompiledFile::load(stream); if(cf->magic != "!RBIX") throw std::runtime_error("Invalid file"); // TODO check version number cf->execute(state); if(!G(current_task)->exception()->nil_p()) { // Reset the context so we can show the backtrace // HACK need to use write barrier aware stuff? Exception* exc = G(current_task)->exception(); G(current_task)->active(state, exc->context()); std::ostringstream msg; msg << "exception detected at toplevel: "; if(!exc->message()->nil_p()) { msg << exc->message()->c_str(); } msg << " (" << exc->klass()->name()->c_str(state) << ")"; Assertion::raise(msg.str().c_str()); } }
int main(int argc, char **argv) { JITCompiler compiler; VMManager * manager = new VMManager; SharedState * shared = manager->create_shared_state(); ConfigParser * config = new ConfigParser; shared->user_config = config; VM* state = shared->thread_nexus()->new_vm(); state->initialize(VM::default_bytes); state->boot(); state->global_lock().lock(); std::ifstream stream(argv[1]); if(!stream) { cout << "Can't open ' " << argv[1] << "'\n"; return 1; } CompiledFile* cf = CompiledFile::load(state, stream); if(cf->magic != "!RBIX") { cout << "Invalid file.\n"; } MachineCode* mcode = as<CompiledCode>(cf->body(state))->formalize(state, false); delete cf; compiler.compile(state, mcode); MachineMethod* mm = MachineMethod::create(state, mcode, compiler); mm->show(); return 0; }
void test_load_file() { std::fstream stream("vm/test/fixture.rbc_"); TS_ASSERT(!!stream); CompiledFile* cf = CompiledFile::load(stream); TS_ASSERT_EQUALS(cf->magic, "!RBIX"); CompiledCode* code = try_as<CompiledCode>(cf->body(state)); TS_ASSERT(code); }
void Environment::run_file(std::string path) { std::stringstream stream; std::ifstream file(path.c_str(), std::ios::ate|std::ios::binary); if(!file) { std::string msg = std::string("Unable to open file to run: "); msg.append(path); throw std::runtime_error(msg); } std::streampos length = file.tellg(); std::vector<char> buffer(length); file.seekg(0, std::ios::beg); file.read(&buffer[0], length); stream.rdbuf()->pubsetbuf(&buffer[0], length); CompiledFile* cf = CompiledFile::load(stream); if(cf->magic != "!RBIX") throw std::runtime_error("Invalid file"); if((signature_ > 0 && cf->signature != signature_) || cf->version != version_) { throw BadKernelFile(path); } cf->execute(state); if(state->vm()->thread_state()->raise_reason() == cException) { Exception* exc = as<Exception>(state->vm()->thread_state()->current_exception()); std::ostringstream msg; msg << "exception detected at toplevel: "; if(!exc->message()->nil_p()) { if(String* str = try_as<String>(exc->message())) { msg << str->c_str(state); } else { msg << "<non-string Exception message>"; } } else if(Exception::argument_error_p(state, exc)) { msg << "given " << as<Fixnum>(exc->get_ivar(state, state->symbol("@given")))->to_native() << ", expected " << as<Fixnum>(exc->get_ivar(state, state->symbol("@expected")))->to_native(); } msg << " (" << exc->klass()->debug_str(state) << ")"; std::cout << msg.str() << "\n"; exc->print_locations(state); Assertion::raise(msg.str().c_str()); } delete cf; }
void Environment::run_file(std::string file) { if(!state->probe->nil_p()) state->probe->load_runtime(state, file); std::ifstream stream(file.c_str()); if(!stream) { std::string msg = std::string("Unable to open file to run: "); msg.append(file); throw std::runtime_error(msg); } CompiledFile* cf = CompiledFile::load(stream); if(cf->magic != "!RBIX") throw std::runtime_error("Invalid file"); if(signature_ > 0) { if(cf->version != signature_) throw BadKernelFile(file); } /** @todo Redundant? CompiledFile::execute() does this. --rue */ state->thread_state()->clear_exception(true); // TODO check version number cf->execute(state); if(state->thread_state()->raise_reason() == cException) { Exception* exc = as<Exception>(state->thread_state()->raise_value()); std::ostringstream msg; msg << "exception detected at toplevel: "; if(!exc->message()->nil_p()) { if(String* str = try_as<String>(exc->message())) { msg << str->c_str(); } else { msg << "<non-string Exception message>"; } } else if(Exception::argument_error_p(state, exc)) { msg << "given " << as<Fixnum>(exc->get_ivar(state, state->symbol("@given")))->to_native() << ", expected " << as<Fixnum>(exc->get_ivar(state, state->symbol("@expected")))->to_native(); } msg << " (" << exc->klass()->name()->c_str(state) << ")"; std::cout << msg.str() << "\n"; exc->print_locations(state); Assertion::raise(msg.str().c_str()); } delete cf; }
void Environment::run_file(std::string file) { std::ifstream stream(file.c_str()); if(!stream) { std::string msg = std::string("Unable to open file to run: "); msg.append(file); throw std::runtime_error(msg); } CompiledFile* cf = CompiledFile::load(stream); if(cf->magic != "!RBIX") { std::ostringstream msg; msg << "attempted to open a bytecode file with invalid magic identifier" << ": path: " << file << ", magic: " << cf->magic; throw std::runtime_error(msg.str().c_str()); } if((signature_ > 0 && cf->signature != signature_) || cf->version != version_) { throw BadKernelFile(file); } cf->execute(state); if(state->vm()->thread_state()->raise_reason() == cException) { Exception* exc = as<Exception>(state->vm()->thread_state()->current_exception()); std::ostringstream msg; msg << "exception detected at toplevel: "; if(!exc->reason_message()->nil_p()) { if(String* str = try_as<String>(exc->reason_message())) { msg << str->c_str(state); } else { msg << "<non-string Exception message>"; } } else if(Exception::argument_error_p(state, exc)) { msg << "given " << as<Fixnum>(exc->get_ivar(state, state->symbol("@given")))->to_native() << ", expected " << as<Fixnum>(exc->get_ivar(state, state->symbol("@expected")))->to_native(); } msg << " (" << exc->klass()->debug_str(state) << ")"; std::cout << msg.str() << "\n"; exc->print_locations(state); Assertion::raise(msg.str().c_str()); } delete cf; }
// // HACK: remove this when performance is better and compiled_file.rb // unmarshal_data method works. Object* System::compiledfile_load(STATE, String* path, Object* version) { if(!state->probe->nil_p()) { state->probe->load_runtime(state, std::string(path->c_str())); } std::ifstream stream(path->c_str()); if(!stream) { std::ostringstream msg; msg << "unable to open file to run: " << path->c_str(); Exception::io_error(state, msg.str().c_str()); } CompiledFile* cf = CompiledFile::load(stream); if(cf->magic != "!RBIX") { std::ostringstream msg; msg << "Invalid file: " << path->c_str(); Exception::io_error(state, msg.str().c_str()); } return cf->body(state); }
// // HACK: remove this when performance is better and compiled_file.rb // unmarshal_data method works. Object* System::compiledfile_load(STATE, String* path, Integer* version) { std::ifstream stream(path->c_str(state)); if(!stream) { return Primitives::failure(); } CompiledFile* cf = CompiledFile::load(stream); if(cf->magic != "!RBIX") { delete cf; return Primitives::failure(); } uint64_t ver = version->to_ulong_long(); if((ver > 0 && cf->version != ver) || cf->sum != "x") { delete cf; return Primitives::failure(); } Object *body = cf->body(state); delete cf; return body; }
void AST_t::prettyprint_in_file(const CompiledFile& compiled_file, bool internal) const { if (!internal) { prettyprint_set_not_internal_output(); } else { prettyprint_set_internal_output(); } FILE *file = fopen(compiled_file.get_filename().c_str(), "a"); if (file == NULL) { running_error("Could not open output file '%s' (%s)\n", compiled_file.get_filename().c_str(), strerror(errno)); } ::prettyprint(file, this->_ast); fclose(file); }