Skip to content

Commit 1c65083

Browse files
committed
Update P6opaque to more fully handle box/unbox of native types.
1 parent c46d7b0 commit 1c65083

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

src/6model/reprs/P6opaque.c

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -567,43 +567,85 @@ static INTVAL hint_for(PARROT_INTERP, PMC *self, PMC *class_handle, STRING *name
567567
/* Used with boxing. Sets an integer value, for representations that can hold
568568
* one. */
569569
static void set_int(PARROT_INTERP, PMC *self, PMC *obj, INTVAL value) {
570-
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
571-
"P6opaque cannot yet box a native int");
570+
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
571+
REPRP6opaque *repr = P6O_REPR_STRUCT(self);
572+
if (repr->unbox_int_offset) {
573+
set_int_at_offset(instance, repr->unbox_int_offset, value);
574+
}
575+
else {
576+
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
577+
"This type cannot box a native integer");
578+
}
572579
}
573580

574581
/* Used with boxing. Gets an integer value, for representations that can
575582
* hold one. */
576583
static INTVAL get_int(PARROT_INTERP, PMC *self, PMC *obj) {
577-
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
578-
"P6opaque cannot yet unbox to a native int");
584+
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
585+
REPRP6opaque *repr = P6O_REPR_STRUCT(self);
586+
if (repr->unbox_int_offset) {
587+
return get_int_at_offset(instance, repr->unbox_int_offset);
588+
}
589+
else {
590+
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
591+
"This type cannot unbox to a native integer");
592+
}
579593
}
580594

581595
/* Used with boxing. Sets a floating point value, for representations that can
582596
* hold one. */
583597
static void set_num(PARROT_INTERP, PMC *self, PMC *obj, FLOATVAL value) {
584-
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
585-
"P6opaque cannot yet box a native num");
598+
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
599+
REPRP6opaque *repr = P6O_REPR_STRUCT(self);
600+
if (repr->unbox_num_offset) {
601+
set_num_at_offset(instance, repr->unbox_num_offset, value);
602+
}
603+
else {
604+
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
605+
"This type cannot box a native number");
606+
}
586607
}
587608

588609
/* Used with boxing. Gets a floating point value, for representations that can
589610
* hold one. */
590611
static FLOATVAL get_num(PARROT_INTERP, PMC *self, PMC *obj) {
591-
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
592-
"P6opaque cannot yet unbox to a native num");
612+
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
613+
REPRP6opaque *repr = P6O_REPR_STRUCT(self);
614+
if (repr->unbox_num_offset) {
615+
return get_num_at_offset(instance, repr->unbox_num_offset);
616+
}
617+
else {
618+
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
619+
"This type cannot unbox to a native number");
620+
}
593621
}
594622

595623
/* Used with boxing. Sets a string value, for representations that can hold
596624
* one. */
597625
static void set_str(PARROT_INTERP, PMC *self, PMC *obj, STRING *value) {
598-
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
599-
"P6opaque cannot yet box a native string");
626+
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
627+
REPRP6opaque *repr = P6O_REPR_STRUCT(self);
628+
if (repr->unbox_str_offset) {
629+
set_str_at_offset(instance, repr->unbox_str_offset, value);
630+
}
631+
else {
632+
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
633+
"This type cannot box a native string");
634+
}
600635
}
601636

602637
/* Used with boxing. Gets a string value, for representations that can hold
603638
* one. */
604639
static STRING * get_str(PARROT_INTERP, PMC *self, PMC *obj) {
605-
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
606-
"P6opaque cannot yet unbox to a native string");
640+
P6opaqueInstance *instance = (P6opaqueInstance *)PMC_data(obj);
641+
REPRP6opaque *repr = P6O_REPR_STRUCT(self);
642+
if (repr->unbox_str_offset) {
643+
return get_str_at_offset(instance, repr->unbox_str_offset);
644+
}
645+
else {
646+
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
647+
"This type cannot unbox to a native string");
648+
}
607649
}
608650

609651
/* This Parrot-specific addition to the API is used to mark an object. */

0 commit comments

Comments
 (0)