Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113432 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 53213 invoked from network); 10 Mar 2021 11:32:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 Mar 2021 11:32:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 944F61804F4 for ; Wed, 10 Mar 2021 03:24:47 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-io1-f44.google.com (mail-io1-f44.google.com [209.85.166.44]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 10 Mar 2021 03:24:47 -0800 (PST) Received: by mail-io1-f44.google.com with SMTP id 81so17456964iou.11 for ; Wed, 10 Mar 2021 03:24:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=3aqJ7M9R6fIS9j7LE8N6ZUg6XUcqeMIs1h/OHO40zrA=; b=nNEzJYmXezs+uwmldFHm/Dbqwya8k7nV2c9TCtL7lSoME+JtQzNwGg6EbaqfrW9iYh RdAJ45Rkb28U4b3EJrHRbMGk0YMcDMpnfeUGX0cu/HuhaVjOCBVXipT71ThumA8DsbQv 7f4b92X9syrTU5eBrQuA18CBcO8S2zk+73tw/jtVBW029NiTcfet+FhsAM3C4DlAwW48 UvqSV7h6+2Jfd8l0IqlOCku4kntCmpqNh/ZLDwkP3M5XKp3X2nTiWgDaS2jI6U09/hEi GLQuk8sfpyimmuGnAnPiRlY999RU2aZvW14W188DNwTtlBnxu8qY8XZzDqp215/Wx41L 6Uwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=3aqJ7M9R6fIS9j7LE8N6ZUg6XUcqeMIs1h/OHO40zrA=; b=sNfYRUgmCtJF7kAPoich6nuRZibeeBVZWTXOtDWO3Hi0D+bUoZq0OMNlir/8JyWrHR P8CJ3Zml8Sgo4iEpuWaw36RmCLbEcL8uW3nnWtK912s39eFXbS89lfGudM1sqxDWmq8J TVNjsYdzjlBDnNCg2zX7UnsFMzwZvZldkpli9/WEO5apW2Uam+XM6WNEiwaiIEllEopE 6OBhlGD0QjiyHtvRmc+hFMCsVviFaG7h1WTD/tHVcNJzv18o7IhDuw/lwUYFd3cWB7X5 qoZCA9MEL7k1s9ShVm70/4NjanOpQ11SpHgY8DHzxJmcQvMIl1NnGTYPdL2lN39AOArG 1wkw== X-Gm-Message-State: AOAM530odBWuVwZZBv0MHdZqAUL5U466yKDQLJYSnAtJyYdickN6DQ1Y XqwnZ+xtT5uOeEO1mgD4NsfYDzq+qTzx4pnhgnaGatdmjuV5lQ== X-Google-Smtp-Source: ABdhPJw3s0Ko9otn3ws/4eXaFVoKTxzf4MWzc9GzKMPq6OqFexjypaeI4MotKETcWQYgM5gB5t4gZqBpSPYIBbY/3bo= X-Received: by 2002:a6b:c881:: with SMTP id y123mr1863067iof.152.1615375484889; Wed, 10 Mar 2021 03:24:44 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 10 Mar 2021 03:24:33 -0800 Message-ID: To: Michael Wallner Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000f4afb405bd2ce9c7" Subject: Re: [PHP-DEV] Tracking Object Property Changes From: sutabi@gmail.com (Joseph Montanez) --000000000000f4afb405bd2ce9c7 Content-Type: text/plain; charset="UTF-8" While attempting this I ran into the need to use GC_ADDREF() or else the returned sub-object data would be gargled. Here is an example struct: typedef struct _php_raylib_camera2d_object { Camera2D camera2d; HashTable *prop_handler; php_raylib_vector2_object *offset; php_raylib_vector2_object *target; zend_object std; } php_raylib_camera2d_object; When I returned a say "target" i.e: static zend_object * php_raylib_camera2d_target(php_raylib_camera2d_object *obj) { return &obj->target->std; } In PHP the values would be incorrect. However by adding GC_ADDREF(&obj->target->std), this seems to have fixed the problem. So it now reads: static zend_object * php_raylib_camera2d_target(php_raylib_camera2d_object *obj) { GC_ADDREF(&obj->target->std); return &obj->target->std; } However in PHP if someone is repeatedly access the subobject "target" does this now much it will have many references? Will I need to manage those references in my C code? Here is the resulting object code in PHP: target->x = 5; $camera->target->y = 6; var_dump($camera->target); Here "target" is accessed 3 times. Thats 3 additional reference counts. If I unset($camera), do I now need to manage those references and explicitly call GC_DELREF/GC_SET_REFCOUNT in my `free_obj` function? Thanks, Joseph Montanez On Tue, Mar 9, 2021 at 10:09 PM Joseph Montanez wrote: > Michael, > > Thanks for the feedback! The part about having Zend Object at the end of a > struct saved me because I was starting to have issues with cloning objects. > > This is for an extension I was working on and have picked back up, I was > just isolating it to make it easy to work with. Its bindings for > https://www.raylib.com/. The general issue is that I won't be able to > change RayLib's structs (embedded vs pointers), so what I showed was an > isolated example. There is another author who also has a RayLib binding in > PHP and has a similar issue: > > https://github.com/oraoto/raylib-phpcpp > > $v = Vector2(1, 2); > // $v->x += 1; doesn't work > $v->x = $v->x + 1; > > $camera = Camera2D(Vector2(1, 1), Vector2(0, 0), 1.0, 1.0); > // $camera->target->x = 1; // doesn't work > $target = $camera->target; > $target->x = 1; > $camera->target = $target; > > This is far from ideal, and I can only assume people will run into this > and assume it's a bug. On my end, I've written my own (incomplete) bindings > https://github.com/joseph-montanez/raylib-php | > https://joseph-montanez.github.io/raylib-php-docs/ I am working towards > those issues, this is just a stumbling block. > > The more I think about this, I probably don't need a listening system, > rather when the object is used for an internal RayLib function call, I can > unravel the sub-objects and write back to the original struct. For example > in RayLib there is a function DrawRay(Ray ray, Color color), when I > implement DrawRay, I can do the unravel work to pull the values from > position and direction PHP objects back into the Ray struct, and then pass > the Ray struct to DrawRay. > > > > Thanks, > Joseph Montanez > > > On Tue, Mar 9, 2021 at 3:03 AM Michael Wallner wrote: > >> Hi Joseph! >> >> Is this for educational purposes or real world usage? >> >> I ask, because, if you don't have to adhere to a predefined C-API you >> would avoid lots of headache by baking all this stuff into your PHP >> objects with the APIs provided by Zend. But then again, it would be way >> more efficient to just write it all in PHP code. >> >> More comments following inline. >> >> On 09/03/2021 03.32, Joseph Montanez wrote: >> > I am not sure what to title this but the gist is that I have two structs >> > with a one way dependency: >> > >> > // Vector3 type >> > typedef struct Vector3 { >> > float x; >> > float y; >> > float z; >> > } Vector3; >> > >> > // Ray type (useful for raycast) >> > typedef struct Ray { >> > Vector3 position; // Ray position (origin) >> > Vector3 direction; // Ray direction >> > } Ray; >> >> As these are embedded structs instead of pointers to, neither do point >> to the memory of the other. >> >> >> > The PHP Ray object is defined as so: >> > typedef struct _skeleton_ray_object { >> > Ray ray; >> > HashTable *prop_handler; >> > zend_object std; >> ^^^^^^^^^^^^^^^^ >> NOTE: This has to be the last line in your object struct because of an >> VLA at the end of the zend_object struct. See your xyz_new() function >> where you allocate additional zend_object_properties_size() bytes for >> that VLA (variable length array). >> >> > >> > skeleton_vector3_object *position; >> > skeleton_vector3_object *direction; >> > } skeleton_ray_object; >> > >> > When I update either vector3 object, I don't have a way to propagate the >> > changes back to the parent object(Ray). This means the raw Ray struct is >> > never updated. Is there a built-in way to handle propagation >> (listeners) in >> > a PHP extension or will I need to hand roll this? >> >> Yes, you gotta write all of this on your own, but let me repeat that >> this is a lot of boilerplate which also may entail lots of headache. >> >> >> -- >> Regards, >> Mike >> >> --000000000000f4afb405bd2ce9c7--