ccrma / chuck

ChucK Music Programming Language
http://chuck.stanford.edu/
GNU General Public License v2.0
816 stars 130 forks source link

ck_create does not call pre-constructor #350

Open nshaheed opened 1 year ago

nshaheed commented 1 year ago

When calling API->object_create() in a chugin, it instantiates the object, but does not call the pre-constructor. This means that chugin_ctor is not called when objects are created this way.

An example:

  Chuck_DL_Api::Object obj = API->object->create(API, SHRED, API->object->get_type(API, SHRED, "Hydra"));
  // Need to be cast from Object to Chuck_Object in a separate line for some reason...
  Chuck_Object * object = (Chuck_Object *) obj;
  Hydra * obj_class = (Hydra *) OBJ_MEMBER_INT(object, hydra_data_offset);

  // because hydra_ctor was never called, this Hydra object was never initializes, this segfaults.
  obj_class->init(config_path, config_name);

If you +d on declaring an object inside chuck itself you get this vm code:

[hydra_test.ck]: Hydra h;
[hydra_test.ck]: [0] Chuck_Instr_Instantiate_Object( Hydra )
[hydra_test.ck]: [1] Chuck_Instr_Pre_Constructor(  )
[hydra_test.ck]: [2] Chuck_Instr_Pre_Constructor(  )
[hydra_test.ck]: [3] Chuck_Instr_Alloc_Word( 0 )
[hydra_test.ck]: [4] Chuck_Instr_Assign_Object(  )
[hydra_test.ck]: [5] Chuck_Instr_Reg_Pop_Word4( 2 )

Since the code emitter doesn't come into play inside chugin code, these steps need to be done inside ck_create.