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
|
/*
FALCON - The Falcon Programming Language.
FILE: function_ext.cpp
Methods of the function class.
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: Sun, 22 Mar 2009 00:12:42 +0100
-------------------------------------------------------------------
(C) Copyright 2008: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
#include "core_module.h"
#include <falcon/corefunc.h>
/*#
@beginmodule core
*/
namespace Falcon {
namespace core {
/*#
@method name Function
@brief Gets the symbolic name of the given function.
@return A string containing the function name
This is useful if the function symbol or has been
re-assigned to temporary variables, or if it is applied
to the @b fself keyword.
*/
FALCON_FUNC Function_name ( ::Falcon::VMachine *vm )
{
if ( vm->self().isFunction() )
{
vm->retval( vm->self().asFunction()->symbol()->name() );
}
else {
vm->retnil();
}
}
static void s_caller_internal( VMachine* vm, bool mode )
{
uint32 level;
Item *i_level = vm->param(0);
if( i_level != 0 )
{
if( i_level->isOrdinal() || i_level->isNil() )
{
int64 i64level = i_level->forceInteger();
if( i64level < 0 )
{
throw new ParamError( ErrorParam( e_param_range, __LINE__ )
.origin(e_orig_runtime)
.extra( ">=0" ) );
}
level = (uint32)i64level+1;
}
else
{
throw new ParamError( ErrorParam( e_inv_params, __LINE__ )
.origin(e_orig_runtime)
.extra( "[N]" ) );
}
}
else {
level = 1;
}
if( mode )
{
Item caller;
if ( vm->getCallerItem( caller, level ) )
{
vm->retval( caller );
return;
}
}
else
{
const Symbol *sym;
uint32 line;
uint32 pc;
if( vm->getTraceStep( level, sym, line, pc ) )
{
CoreArray* arr = new CoreArray( 3 );
arr->append( sym->name() );
arr->append( sym->module()->name() );
arr->append( sym->module()->path() );
arr->append( (int64) line );
arr->append( (int64) pc );
vm->retval( arr );
return;
}
}
// we're not called.
vm->retnil();
}
/*#
@method caller Function
@brief Gets the direct caller or one of the calling ancestors.
@optparam level Caller level (starting from zero, the default).
@return The item having performed the nth call.
This function returns the n-th caller (zero based) that caused
this function to be called. It may be a function, a method
or another callable item from which the call has originated.
@note The method can also be called statically on the Function metaclass.
*/
FALCON_FUNC Function_caller ( ::Falcon::VMachine *vm )
{
s_caller_internal( vm, true );
}
/*#
@method trace Function
@brief Gets a trace step in the call stack.
@optparam level Caller level (starting from zero, the default).
@return An array containing the data relative to the given trace level.
The returned data is organized as follows:
@code
[ 'symbol name', 'module name', 'module path', line_in_module, PC_in_vm]
@endcode
@note The method can also be called statically on the Function metaclass.
*/
FALCON_FUNC Function_trace ( ::Falcon::VMachine *vm )
{
s_caller_internal( vm, false );
}
/*#
@method stack Function
@brief Gets a string representation of the call stack.
@return A string containing all the stack trace up to this point.
The returned string looks like the stack trace printed by errors,
but is referred to the call stack up to the function from which
this method is called.
@note The method can also be called statically on the Function metaclass.
*/
FALCON_FUNC Function_stack ( ::Falcon::VMachine *vm )
{
const Symbol *sym;
uint32 line;
uint32 pc;
int level = 0;
CoreString& target = *(new CoreString());
while( vm->getTraceStep( level, sym, line, pc ) )
{
if ( target.size() )
target += "\n";
const Module* mod = sym->module();
if ( mod->path().size() )
{
target += "\"" + mod->path() + "\" ";
}
target += mod->name() + "." + sym->name() + ":";
target.writeNumber( (int64) line );
target += "(PC:";
switch( 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) pc );
}
target += ")";
++level;
}
vm->retval( &target );
}
}
}
/* end of functional_ext.cpp */
|