I'm working on the concurrency branch, and specifically the Exception PMC.
Here's a patch that does what I think it needs.... I'll run more tests and
see if Parrot still works.
-- c
Index: src/pmc/exception.pmc
===================================================================
--- src/pmc/exception.pmc (revision 27224)
+++ src/pmc/exception.pmc (working copy)
@@ -10,32 +10,23 @@
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>
+=item C<message>
Textual representation of the exception.
-=item 1 C<_type>
+=item C<severity>
-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>,
+The severity of the exception, (see F<include/parrot/exceptions.h>,
F<except_severity.pasm>).
-=item 3 C<payload>
+=item C<payload>
Additional data for the exception.
-=item 4 C<unused>
-
=back
Optional:
@@ -64,9 +55,6 @@
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
@@ -79,26 +67,6 @@
#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
@@ -110,6 +78,9 @@
*/
pmclass Exception extends ResizablePMCArray need_ext {
+ ATTR STRING *message;
+ ATTR PMC *payload;
+ ATTR INTVAL severity;
/*
@@ -122,13 +93,39 @@
*/
VTABLE void init() {
- SUPER();
- /* pre-fill 11 slots with PMCNULL */
- SELF.set_integer_native(11);
+ Parrot_Exception * const exception =
+ mem_allocate_zeroed_typed(Parrot_Exception);
+
+ PObj_custom_mark_SET(SELF);
+ PObj_active_destroy_SET(SELF);
+
+ PMC_data(SELF) = exception;
+ exception->message = CONST_STRING(interp, "");
+ exception->severity = 0;
+ exception->payload = PMCNULL;
}
/*
+=item C<void mark()>
+
+Mark any active exception data as live.
+
+=cut
+
+*/
+
+ VTABLE void mark() {
+ Parrot_Exception * const exception = PARROT_EXCEPTION(SELF);
+
+ if (exception->message)
+ pobject_lives(interp, (PObj *)exception->message);
+ if (exception->payload)
+ pobject_lives(interp, (PObj *)exception->payload);
+ }
+
+/*
+
=item C<STRING *get_string_keyed(PMC *key)>
Returns the Parrot string value for C<*key>. The only current recognized
@@ -145,14 +142,17 @@
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);
+ if (!string_compare(INTERP, s, CONST_STRING(INTERP, "_message"))) {
+ Parrot_Exception * const exception = PARROT_EXCEPTION(SELF);
+ return exception->message;
+ }
- return 0;
+ return CONST_STRING(interp, "");
}
VTABLE STRING *get_string() {
- return SELF.get_string_keyed_int(0);
+ Parrot_Exception * const exception = PARROT_EXCEPTION(SELF);
+ return exception->message;
}
/*
@@ -168,12 +168,11 @@
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"))) {
+ Parrot_Exception * const exception = PARROT_EXCEPTION(SELF);
+ return exception->severity;
+ }
- if (!string_compare(INTERP, s, CONST_STRING(INTERP, "_severity")))
- return SELF.get_integer_keyed_int(2);
-
return 0;
}
@@ -194,24 +193,6 @@
/*
-=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>.
@@ -223,8 +204,10 @@
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);
+ if (!string_compare(INTERP, s, CONST_STRING(INTERP, "_message"))) {
+ Parrot_Exception * const exception = PARROT_EXCEPTION(SELF);
+ exception->message = value;
+ }
}
/*
@@ -240,11 +223,10 @@
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);
+ if (!string_compare(INTERP, s, CONST_STRING(INTERP, "_severity"))) {
+ Parrot_Exception * const exception = PARROT_EXCEPTION(SELF);
+ exception->severity = value;
+ }
}
/*
@@ -271,7 +253,7 @@
*/
VTABLE INTVAL is_equal(PMC *value) {
- /* RT#46689 check parents */
+ /* RT #46689 check parents */
if (value->vtable->base_type == enum_class_Exception
|| VTABLE_isa(INTERP, value, CONST_STRING(INTERP, "Exception")))
return 1;
@@ -289,14 +271,26 @@
*/
VTABLE PMC *get_attr_str(STRING *name) {
- INTVAL index = get_attrib_index(interp, SELF, name);
+ Parrot_Exception * const exception = PARROT_EXCEPTION(SELF);
- /* If lookup failed, exception. */
- if (index == -1)
- real_exception(interp, NULL, ATTRIB_NOT_FOUND,
- "No such attribute '%S'", name);
+ if (!string_compare(interp, name, CONST_STRING(interp, "message"))) {
+ PMC *message_pmc = pmc_new(interp, enum_class_String);
+ VTABLE_set_string_native(interp, message_pmc, exception->message);
+ return message_pmc;
+ }
- return SELF.get_pmc_keyed_int(index);
+ if (!string_compare(interp, name, CONST_STRING(interp, "severity"))) {
+ PMC *severity_pmc = pmc_new(interp, enum_class_Integer);
+ VTABLE_set_integer_native(interp, severity_pmc, exception->severity);
+ return severity_pmc;
+ }
+
+ if (!string_compare(interp, name, CONST_STRING(interp, "payload")))
+ return exception->payload;
+
+ /* If lookup failed, exception. */
+ real_exception(interp, NULL, ATTRIB_NOT_FOUND,
+ "No such attribute '%S'", name);
}
/*
@@ -309,96 +303,27 @@
*/
VTABLE void set_attr_str(STRING *name, PMC *value) {
- INTVAL index = get_attrib_index(interp, SELF, name);
+ Parrot_Exception * const exception = PARROT_EXCEPTION(SELF);
- /* If lookup failed, exception. */
- if (index == -1)
- real_exception(interp, NULL, ATTRIB_NOT_FOUND,
- "No such attribute '%S'", name);
+ if (!string_compare(interp, name, CONST_STRING(interp, "message"))) {
+ exception->message = VTABLE_get_string(interp, value);
+ return;
+ }
- SELF.set_pmc_keyed_int(index, value);
- }
+ if (!string_compare(interp, name, CONST_STRING(interp, "severity"))) {
+ exception->severity = VTABLE_get_integer(interp, value);
+ return;
+ }
-/*
+ if (!string_compare(interp, name, CONST_STRING(interp, "payload"))) {
+ exception->payload = value;
+ return;
+ }
-=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;
+ /* If lookup failed, exception. */
+ real_exception(interp, NULL, ATTRIB_NOT_FOUND,
+ "No such attribute '%S'", name);
}
-
- 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;
- }
}
/*