1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
|
/*
FALCON - The Falcon Programming Language
FILE: error.cpp
Error management.
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: lun ago 28 2006
-------------------------------------------------------------------
(C) Copyright 2004: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
/** \file
Error management.
*/
#include <falcon/setup.h>
#include <falcon/error.h>
#include <falcon/sys.h>
#include <falcon/vm.h>
#include <falcon/coreobject.h>
#include <falcon/eng_messages.h>
namespace Falcon {
const String &errorDesc( int code )
{
switch( code )
{
#define FLC_MAKE_ERROR_MESSAGE_SELECTOR
#include <falcon/eng_messages.h>
}
return Engine::getMessage( msg_unknown_error );
}
String &TraceStep::toString( String &target ) const
{
if ( m_modpath.size() )
{
target += "\"" + m_modpath + "\" ";
}
target += m_module + "." + m_symbol + ":";
target.writeNumber( (int64) m_line );
target += "(PC:";
switch( m_pc )
{
case VMachine::i_pc_call_external: target += "ext"; break;
case VMachine::i_pc_call_external_return: target += "ext.r"; break;
case VMachine::i_pc_redo_request: target += "redo"; break;
case VMachine::i_pc_call_external_ctor: target += "ext.c"; break;
case VMachine::i_pc_call_external_ctor_return: target += "ext.cr"; break;
default:
target.writeNumber( (int64) m_pc );
}
target += ")";
return target;
}
//==================================================
// Error
//==================================================
Error::Error( const Error &e ):
m_nextError( 0 ),
m_LastNextError( 0 ),
m_boxed( 0 )
{
m_errorCode = e.m_errorCode;
m_line = e.m_line;
m_pc = e.m_pc;
m_character = e.m_character;
m_sysError = e.m_sysError;
m_origin = e.m_origin;
m_catchable = e.m_catchable;
m_description = e.m_description;
m_extra = e.m_extra;
m_module = e.m_module;
m_symbol = e.m_symbol;
m_raised = e.m_raised;
m_className = e.m_className;
if ( e.m_boxed != 0 )
{
boxError(m_boxed);
}
m_refCount = 1;
ListElement *step_i = m_steps.begin();
while( step_i != 0 )
{
TraceStep *step = (TraceStep *) step_i->data();
addTrace( step->module(), step->symbol(), step->line(), step->pcounter() );
step_i = step_i->next();
}
}
Error::~Error()
{
ListElement *step_i = m_steps.begin();
while( step_i != 0 )
{
TraceStep *step = (TraceStep *) step_i->data();
delete step;
step_i = step_i->next();
}
Error *ptr = m_nextError;
while( ptr != 0 )
{
Error *ptrnext = ptr->m_nextError;
ptr->m_nextError = 0;
ptr->decref();
ptr = ptrnext;
}
if ( m_boxed != 0 )
{
m_boxed->decref();
}
}
void Error::incref()
{
atomicInc( m_refCount );
}
void Error::decref()
{
if( atomicDec( m_refCount ) <= 0 )
{
delete this;
}
}
String &Error::toString( String &target ) const
{
if ( m_boxed != 0 )
{
target += m_boxed->toString();
target += " =====================================================\n";
target += " Boxed in ";
}
heading( target );
target += "\n";
if ( ! m_steps.empty() )
{
target += " Traceback:\n";
ListElement *iter = m_steps.begin();
while( iter != 0 )
{
target += " ";
TraceStep *step = (TraceStep *) iter->data();
step->toString( target );
target += "\n";
iter = iter->next();
}
}
// recursive stringation
if ( m_nextError != 0 )
{
m_nextError->toString( target );
}
return target;
}
String &Error::heading( String &target ) const
{
target += m_className;
target += " ";
switch( m_origin )
{
case e_orig_compiler: target += "CO"; break;
case e_orig_assembler: target += "AS"; break;
case e_orig_loader: target += "LD"; break;
case e_orig_vm: target += "VM"; break;
case e_orig_runtime: target += "RT"; break;
case e_orig_mod: target += "MD"; break;
case e_orig_script: target += "SS"; break;
default: target += "??";
}
uint32 ecode = (uint32) m_errorCode;
for ( int number = 1000; number > 0; number /= 10 )
{
int64 cipher = ecode / number;
ecode %= number;
target.writeNumber( cipher );
}
if ( m_sysError != 0 )
{
target += "(sys: ";
target.writeNumber( (int64) m_sysError );
String temp;
Sys::_describeError( m_sysError, temp );
target += " " + temp;
target += ")";
}
if( m_line != 0 || m_module.size() != 0 )
target += " at ";
if ( m_module.size() != 0 )
{
target += m_module;
if ( m_symbol.size() != 0 )
target += "." + m_symbol;
target += ":";
}
if ( m_line != 0 )
target.writeNumber( (int64) m_line );
if ( m_character != 0 ) {
target += "/";
target.writeNumber( (int64) m_character );
}
if ( m_pc != 0 )
{
target += "(PC:";
switch( m_pc )
{
case VMachine::i_pc_call_external: target += "ext"; break;
case VMachine::i_pc_call_external_return: target += "ext.r"; break;
case VMachine::i_pc_redo_request: target += "redo"; break;
case VMachine::i_pc_call_external_ctor: target += "ext.c"; break;
case VMachine::i_pc_call_external_ctor_return: target += "ext.cr"; break;
default:
target.writeNumber( (int64) m_pc );
}
target += ")";
}
if ( m_description.size() > 0 )
{
target += ": " + m_description;
}
else {
target += ": " + errorDesc( m_errorCode );
}
if ( m_extra.size() > 0 )
{
target += " (" + m_extra + ")";
}
if ( ! m_raised.isNil() )
{
String temp;
m_raised.toString( temp );
target += "\n"+ temp;
}
return target;
}
void Error::addTrace( const String &module, const String &symbol, uint32 line, uint32 pc )
{
m_steps.pushBack( new TraceStep( module, symbol, line, pc ) );
m_stepIter = m_steps.begin();
}
void Error::addTrace( const String &module, const String &modpath, const String &symbol, uint32 line, uint32 pc )
{
m_steps.pushBack( new TraceStep( module, modpath, symbol, line, pc ) );
m_stepIter = m_steps.begin();
}
void Error::appendSubError( Error *error )
{
if ( m_LastNextError == 0 )
{
m_LastNextError = m_nextError = error;
}
else {
m_LastNextError->m_nextError = error;
m_LastNextError = error;
}
error->incref();
}
void Error::boxError( Error *error )
{
if ( m_boxed != 0 )
{
m_boxed->decref();
}
m_boxed = error;
if ( error != 0 )
{
error->incref();
}
}
bool Error::nextStep( String &module, String &symbol, uint32 &line, uint32 &pc )
{
if ( m_steps.empty() || m_stepIter == 0 )
return false;
TraceStep *step = (TraceStep *) m_stepIter->data();
module = step->module();
symbol = step->symbol();
line = step->line();
pc = step->pcounter();
m_stepIter = m_stepIter->next();
return true;
}
void Error::rewindStep()
{
if ( ! m_steps.empty() )
m_stepIter = m_steps.begin();
}
CoreObject *Error::scriptize( VMachine *vm )
{
Item *error_class = vm->findWKI( m_className );
// in case of 0, try with global items
if ( error_class == 0 )
error_class = vm->findGlobalItem( m_className );
if ( error_class == 0 || ! error_class->isClass() ) {
throw new GenericError(
ErrorParam( e_undef_sym, __LINE__)
.origin(e_orig_vm)
.module( "core.Error" ).
symbol( "Error::scriptize" )
.extra( m_className ).hard()
);
}
// CreateInstance will use ErrorObject, which increfs to us.
CoreObject *cobject = error_class->asClass()->createInstance( this );
return cobject;
}
Error *Error::clone() const
{
return new Error( *this );
}
//==============================================================================
// Reflections
namespace core {
void Error_code_rfrom( CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_INTEGER_FROM( error, errorCode );
}
void Error_description_rfrom(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_STRING_FROM( error, errorDescription );
}
void Error_message_rfrom(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_STRING_FROM( error, extraDescription );
}
void Error_systemError_rfrom(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_INTEGER_FROM( error, systemError );
}
void Error_origin_rfrom(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
String origin;
switch( error->origin() )
{
case e_orig_compiler: origin = "compiler"; break;
case e_orig_assembler: origin = "assembler"; break;
case e_orig_loader: origin = "loader"; break;
case e_orig_vm: origin = "vm"; break;
case e_orig_script: origin = "script"; break;
case e_orig_runtime: origin = "runtime"; break;
case e_orig_mod: origin = "module"; break;
default: origin = "unknown";
}
property = new CoreString( origin );
}
void Error_module_rfrom(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_STRING_FROM( error, module );
}
void Error_symbol_rfrom(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_STRING_FROM( error, symbol );
}
void Error_line_rfrom(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_INTEGER_FROM( error, line );
}
void Error_pc_rfrom(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_INTEGER_FROM( error, pcounter );
}
void Error_boxed_rfrom(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
if ( error->getBoxedError() != 0 )
{
VMachine* vm = VMachine::getCurrent();
fassert( vm != 0 );
property = error->getBoxedError()->scriptize(vm);
}
else
{
property.setNil();
}
}
void Error_subErrors_rfrom(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
VMachine* vm = VMachine::getCurrent();
fassert( vm != 0 );
// scriptize sub-errors
Error *ptr = error->subError();
if ( ptr != 0)
{
CoreArray *errorList = new CoreArray();
do
{
// CreateInstance will use ErrorObject, which increfs ptr.
CoreObject *subobject = ptr->scriptize( vm );
errorList->append( subobject );
ptr = ptr->subError();
}
while( ptr != 0 );
property = errorList;
}
else
property.setNil();
}
void Error_code_rto( CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_INTEGER_TO( error, errorCode );
}
void Error_description_rto(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_STRING_TO( error, errorDescription );
}
void Error_message_rto(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_STRING_TO( error, extraDescription );
}
void Error_systemError_rto(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_INTEGER_TO( error, systemError );
}
void Error_origin_rto(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
if ( property.isString() )
{
String &origin = *property.asString();
if( origin == "compiler" )
{
error->origin( e_orig_compiler );
}
else if( origin == "assembler" )
{
error->origin( e_orig_assembler );
}
else if( origin == "loader" )
{
error->origin( e_orig_loader );
}
else if( origin == "vm" )
{
error->origin( e_orig_vm );
}
else if( origin == "script" )
{
error->origin( e_orig_script );
}
else if( origin == "runtime" )
{
error->origin( e_orig_runtime );
}
else if( origin == "module" )
{
error->origin( e_orig_mod);
}
else {
throw new ParamError( ErrorParam( e_param_range ) );
}
return;
}
throw new ParamError( ErrorParam( e_inv_params ).extra( "S" ) );
}
void Error_module_rto(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_STRING_TO( error, module );
}
void Error_symbol_rto(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_STRING_TO( error, symbol );
}
void Error_line_rto(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_INTEGER_TO( error, line );
}
void Error_pc_rto(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
FALCON_REFLECT_INTEGER_TO( error, pcounter );
}
void Error_boxed_rto(CoreObject *instance, void *userData, Item &property, const PropEntry& )
{
Error *error = static_cast<Error *>(userData);
if ( property.isObject() && property.asObject()->derivedFrom("Error") )
{
error->boxError( static_cast<Error*>(property.asObject()->getUserData()) );
}
}
//============================================================
// Reflector
ErrorObject::ErrorObject( const CoreClass* cls, Error *err ):
CRObject( cls )
{
if ( err != 0 )
{
err->incref();
setUserData( err );
}
}
ErrorObject::~ErrorObject()
{
if ( m_user_data != 0 )
getError()->decref();
}
void ErrorObject::gcMark( uint32 mark )
{
Error* error = getError();
if ( error != 0 )
memPool->markItem( const_cast<Item&>(error->raised()) );
}
ErrorObject *ErrorObject::clone() const
{
return new ErrorObject( m_generatedBy, getError() );
}
//========================================================
// Factory function
//
CoreObject* ErrorObjectFactory( const CoreClass *cls, void *user_data, bool )
{
return new ErrorObject( cls, (Error *) user_data );
}
}} // namespace Falcon::core
/* end of error.cpp */
|