Author: jonathan
Date: Mon Apr 16 16:58:53 2007
New Revision: 18246

Modified:
   trunk/src/pmc/class.pmc
   trunk/src/pmc/object.pmc
   trunk/t/pmc/object.t

Log:
[PDD15]: Refactoring to move object creation into Class, and change so you can 
only instantiate an object through a class. Patch partly courtesy of Alek Storm.

Modified: trunk/src/pmc/class.pmc
==============================================================================
--- trunk/src/pmc/class.pmc     (original)
+++ trunk/src/pmc/class.pmc     Mon Apr 16 16:58:53 2007
@@ -805,6 +805,7 @@
         Parrot_Class *class = PARROT_CLASS(SELF);
         PMC *obj;
         PMC *iter;
+        Parrot_Object *obj_guts = NULL;
 
         /* If we've not been instantiated before... */
         if (!class->instantiated) {
@@ -835,7 +836,18 @@
         class->instantiated = 1;
 
         /* Create object. */
-        obj = pmc_new_init(interp, enum_class_Object, SELF);
+        obj = pmc_new_noinit(interp, enum_class_Object);
+
+        /* Set custom DOD mark and destroy on the object. */
+        PObj_custom_mark_SET(obj);
+        PObj_active_destroy_SET(obj);
+
+        /* Initialize the object's underlying structure, pointing it to this
+         * class. */
+        obj_guts = mem_sys_allocate_zeroed(sizeof(Parrot_Object));
+        obj_guts->class = SELF;
+        obj_guts->attrib_store = pmc_new(interp, enum_class_ResizablePMCArray);
+        PMC_data(obj) = obj_guts;
 
         /* Initialize attributes with the supplied values. */
         iter = VTABLE_get_iter(interp, args);

Modified: trunk/src/pmc/object.pmc
==============================================================================
--- trunk/src/pmc/object.pmc    (original)
+++ trunk/src/pmc/object.pmc    Mon Apr 16 16:58:53 2007
@@ -66,26 +66,33 @@
 
 /*
 
-=item C<void init_pmc(PMC *class)>
+=item C<void init()>
 
-Instantiates an object of the given class.
+Raises an exception; you can only instantiate objects from a class.
 
 =cut
 
 */
 
-    void init_pmc(PMC* class) {
-        Parrot_Object *obj = NULL;
+    void init() {
+        real_exception(interp, NULL, INVALID_OPERATION,
+                "Object must be created by a class.");
+    }
+
+/*
+
 
-        /* Custom DOD mark and destroy. */
-        PObj_custom_mark_SET(SELF);
-        PObj_active_destroy_SET(SELF);
+=item C<void init_pmc(PMC *class)>
+
+Raises an exception; you can only instantiate objects from a class.
+
+=cut
+
+*/
 
-        /* Set up the object. */
-        obj = mem_sys_allocate_zeroed(sizeof(Parrot_Object));
-        obj->class = class;
-        obj->attrib_store = pmc_new(interp, enum_class_ResizablePMCArray);
-        PMC_data(SELF) = obj;
+    void init_pmc(PMC* worreva) {
+        real_exception(interp, NULL, INVALID_OPERATION,
+                "Object must be created by a class.");
     }
 
 /*

Modified: trunk/t/pmc/object.t
==============================================================================
--- trunk/t/pmc/object.t        (original)
+++ trunk/t/pmc/object.t        Mon Apr 16 16:58:53 2007
@@ -26,18 +26,15 @@
 # TODO fix smartlinks once this is specced
 pir_output_is( <<'CODE', <<'OUT', 'new' );
 .sub 'test' :main
-    $P0 = new .Object
-    say 'ok 1 - $P0 = new .Object'
-
-    $I0 = isa $P0, 'Object'
-    if $I0 goto ok_2
-    print 'not '
-  ok_2:
-    say "ok 2 - isa $P0, 'Object'"
+    push_eh ok_1
+    $P0 = new 'Object'
+    clear_eh
+    print "not "
+ok_1:
+    say "ok 1 - $P0 = new 'Object' throws exception"
 .end
 CODE
-ok 1 - $P0 = new .Object
-ok 2 - isa $P0, 'Object'
+ok 1 - $P0 = new 'Object' throws exception
 OUT
 
 ## TODO add more tests as this is documented and implemented

Reply via email to