Skip to content
This repository
tag: RELEASE_0_6_4
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 422 lines (269 sloc) 8.15 kb
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
/*
Copyright (C) 2001-2008, The Perl Foundation.
$Id$

=head1 NAME

src/pmc/exception.pmc - Exception PMC

=head1 DESCRIPTION

This is the exception base class.

For now, this class is based on C<ResziablePMCArray>. This will change when the
full PMC PDD is implemented.

An exception object has these attributes:

=over 4

=item 0 C<_message>

Textual representation of the exception.

=item 1 C<_type>

The exception type, (see F<include/parrot/exceptions.h>, F<except_type.pasm>).

=item 2 C<_severity>

The severity of the exception, (see F<src/exceptions.h>,
F<except_severity.pasm>).

=item 3 C<payload>

Additional data for the exception.

=item 4 C<unused>

=back

Optional:

=over 4

=item 5 C<_C_file>

The C code file in which the exception was raised.

=item 6 C<_C_line>

The line of C code on which the exception was raised.

=item 7 C<_P_file>

The PASM/PIR/Perl file in which the exception was raised.

=item 8 C<_P_line>

The line of PASM/PIR/Perl code on which the exception was raised.

=item 9, 10 C<unused>

Any information attached by the HL. This shouldn't start with an
underscore (that is reserved for Parrot's internal usage. These are
stored as properties.

Note: currently, HLL information must be indexed by number. Slots 9 and 10
are available for the HLL - This is subject to change.

=back

When an exception handler is called, the exception object is passed as
as the first argument, the message as the second argument of the call.
These arguments can be retrieved with the C<get_results> opcode.

=cut

*/

#include "parrot/parrot.h"

/* This finds the index of an attribute in an object's attribute store and
 * returns it. Returns -1 if the attribute does not exist. */
static INTVAL
get_attrib_index(PARROT_INTERP, PMC *self, STRING *name)
{
        if (!string_compare(interp, name, CONST_STRING(interp, "message")))
            return 0;

        if (!string_compare(interp, name, CONST_STRING(interp, "type")))
            return 1;

        if (!string_compare(interp, name, CONST_STRING(interp, "severity")))
            return 2;

        if (!string_compare(interp, name, CONST_STRING(interp, "payload")))
            return 3;

            return -1;
}

/*

=head2 Methods

=over 4

=cut

*/

pmclass Exception extends ResizablePMCArray need_ext {

/*

=item C<void init()>

Initializes the exception with default values.

=cut

*/

    VTABLE void init() {
        SUPER();
        /* pre-fill 11 slots with PMCNULL */
        SELF.set_integer_native(11);
    }

/*

=item C<STRING *get_string_keyed(PMC *key)>

Returns the Parrot string value for C<*key>. The only current recognized
C<key> is "_message".

=item C<STRING *get_string()>

Return the exception message.

=cut

*/

    VTABLE STRING *get_string_keyed(PMC *key) {
        STRING *s = key_string(INTERP, key);

        if (!string_compare(INTERP, s, CONST_STRING(INTERP, "_message")))
            return SELF.get_string_keyed_int(0);

        return 0;
    }

    VTABLE STRING *get_string() {
        return SELF.get_string_keyed_int(0);
    }

/*

=item C<INTVAL get_integer_keyed(PMC *key)>

Returns the integer value for C<*key>.

=cut

*/

    VTABLE INTVAL get_integer_keyed(PMC *key) {
        STRING *s = key_string(INTERP, key);

        if (!string_compare(INTERP, s, CONST_STRING(INTERP, "_type")))
            return SELF.get_integer_keyed_int(1);

        if (!string_compare(INTERP, s, CONST_STRING(INTERP, "_severity")))
            return SELF.get_integer_keyed_int(2);

        return 0;
    }

/*

=item C<PMC *get_pmc_keyed(PMC *key)>

Returns the PMC value for C<*key>.

=cut

*/

    VTABLE PMC *get_pmc_keyed(PMC *key) {
        STRING *s = key_string(INTERP, key);
        return SELF.getprop(s);
    }

/*

=item C<PMC *get_pmc_keyed_int(INTVAL key)>

Returns the PMC value of the element at index C<key>, but ignores too
big negative keys.

=cut

*/

    VTABLE PMC *get_pmc_keyed_int(INTVAL key) {
        if (key <= -PMC_int_val(SELF))
            return PMCNULL;

        return SUPER(key);
    }

/*

=item C<void set_string_keyed(PMC *key, STRING *value)>

Sets the Parrot string value for C<*key>.

=cut

*/

    VTABLE void set_string_keyed(PMC *key, STRING *value) {
        STRING *s = key_string(INTERP, key);

        if (!string_compare(INTERP, s, CONST_STRING(INTERP, "_message")))
            SELF.set_string_keyed_int(0, value);
    }

/*

=item C<void set_integer_keyed(PMC *key, INTVAL value) >

Sets the integer value for C<*key>.

=cut

*/

    VTABLE void set_integer_keyed(PMC *key, INTVAL value) {
        STRING *s = key_string(INTERP, key);

        if (!string_compare(INTERP, s, CONST_STRING(INTERP, "_type")))
            SELF.set_integer_keyed_int(1, value);

        if (!string_compare(INTERP, s, CONST_STRING(INTERP, "_severity")))
            SELF.set_integer_keyed_int(2, value);
    }

/*

=item C<void set_pmc_keyed(PMC *key, PMC *value)>

Sets the PMC value for C<*key>.

=cut

*/

    VTABLE void set_pmc_keyed(PMC *key, PMC *value) {
        STRING *s = key_string(INTERP, key);
        SELF.setprop(s, value);
    }
/*

=item C<INTVAL is_equal(PMC *value)>

Compare the passed in Exception with SELF. Returns True if C<SELF> isa C<value>

=cut

*/
    VTABLE INTVAL is_equal(PMC *value) {
        /* RT#46689 check parents */
        if (value->vtable->base_type == enum_class_Exception
        || VTABLE_isa(INTERP, value, CONST_STRING(INTERP, "Exception")))
            return 1;

        return 0;
    }

/*

=item C<get_attr_str>

Retrieve an attribute value for the exception object.

=cut

*/
    VTABLE PMC *get_attr_str(STRING *name) {
        INTVAL index = get_attrib_index(interp, SELF, name);

        /* If lookup failed, exception. */
        if (index == -1)
            real_exception(interp, NULL, ATTRIB_NOT_FOUND,
                "No such attribute '%S'", name);

       return SELF.get_pmc_keyed_int(index);
    }

/*

=item C<set_attr_str>

Set an attribute value for the exception object.

=cut

*/
    VTABLE void set_attr_str(STRING *name, PMC *value) {
        INTVAL index = get_attrib_index(interp, SELF, name);

        /* If lookup failed, exception. */
        if (index == -1)
            real_exception(interp, NULL, ATTRIB_NOT_FOUND,
                "No such attribute '%S'", name);

        SELF.set_pmc_keyed_int(index, value);
    }

/*

=item C<shift_*>, C<unshift_*>, C<pop_*>, C<push_*>

These methods are silently ignored.

=cut

*/
    VTABLE PMC *shift_pmc() {
        /* fprintf(stderr, "don't do that then\n"); RT#46691 */
        return PMCNULL;
    }

    VTABLE FLOATVAL shift_float() {
        (void) SELF.shift_pmc();
        return 0.0;
    }

    VTABLE INTVAL shift_integer() {
        (void) SELF.shift_pmc();
        return 0;
    }

    VTABLE STRING *shift_string() {
        (void) SELF.shift_pmc();
        return NULL;
    }

    void unshift_pmc(PMC *value) {
        /* fprintf(stderr, "don't do that then\n"); RT#46691 */
    }

    VTABLE void unshift_float(FLOATVAL value) {
        SELF.unshift_pmc(NULL);
    }

    VTABLE void unshift_integer(INTVAL value) {
        SELF.unshift_pmc(NULL);
    }

    VTABLE void unshift_string(STRING *value) {
        SELF.unshift_pmc(NULL);
    }

    void push_pmc(PMC *value) {
        /* fprintf(stderr, "don't do that then\n"); RT#46691 */
    }

    VTABLE void push_float(FLOATVAL value) {
        SELF.push_pmc(PMCNULL);
    }

    VTABLE void push_integer(INTVAL value) {
        SELF.push_pmc(PMCNULL);
    }

    VTABLE void push_string(STRING *value) {
        SELF.push_pmc(PMCNULL);
    }

    VTABLE PMC *pop_pmc() {
        /* fprintf(stderr, "don't do that then\n"); RT#46691 */
        return PMCNULL;
    }

    VTABLE FLOATVAL pop_float() {
        (void) SELF.pop_pmc();
        return 0.0;
    }

    VTABLE INTVAL pop_integer() {
        (void) SELF.pop_pmc();
        return 0;
    }

    VTABLE STRING *pop_string() {
        (void) SELF.pop_pmc();
        return NULL;
    }
}

/*

=back

=head1 HISTORY

Initial revision by leo 2003.07.10.

=cut

*/

/*
 * Local variables:
 * c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4:
 */
Something went wrong with that request. Please try again.