Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:106917 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 70281 invoked from network); 9 Sep 2019 13:58:23 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 9 Sep 2019 13:58:23 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 8DEEE2CAF7C for ; Mon, 9 Sep 2019 04:33:29 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,HTML_MESSAGE, RCVD_IN_DNSWL_MED,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3215 2.0.0.0/16 X-Spam-Virus: No Received: from mail.sensational.ch (mail.sensational.ch [IPv6:2001:8a8:6004:0:dead:beef:0:1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Mon, 9 Sep 2019 04:33:28 -0700 (PDT) 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; bh=lDQnX27HfO44BL4NMSdEEjk4sB42mAO7DJepnxB0Nnk=; b=qlGvqsqlMYkQN03cGt+edgUIKtO0THQOB0i/tCr25gbNOxK97Zb3nXSsc+d1roOiBr ojoic0CBUWgEfkQHTkrgyHUjwyhtqGeXURpotRMr9C2Cc1e04tZmz2wvJWzyczcN5YTk 2BXOE21iYyjalA43yHazk+QqZTqCq24tJZjCYPpoeMPbSL9LEs5vpRRLfjoL4EWtCZZi GwQ09881nPAVMjphtB3oImRiAnytW9tChewzUEsnr1R2eGgKLoGq1PFd4nld1o5vcZi/ G7GxbPNu+8akocvBB9YN/QUbd96FrRfeRrt4Aad/Lybs5OXkvWbACmfsgAZ48es7856W bySA== X-Gm-Message-State: APjAAAURWwI6oYSt2OLtBxNIDY76BoZ9RDDyppmJizMwe+4u2x4uVd8b 2jSe5udp5sw1LPXYSPQ3RKTQoAjuLT0HIqMOVjM8sBf5EZ/Lco1lFvZ9F87QKsRdmX/7vcCNZLc OorAHVVtAfY6eY95ODsbSm4X6ym7j+0RmRJ+lzGPRguKN X-Received: by 2002:adf:ed44:: with SMTP id u4mr18790753wro.185.1568028805502; Mon, 09 Sep 2019 04:33:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqyT4XVDIIQbBab8hGB/RW7L7BLqrlS49zNM15DifXZ+6le2RkBfUp3shKVrxtCM1N8doo5SW8gOuYGrGm2KrHQ= X-Received: by 2002:adf:ed44:: with SMTP id u4mr18790727wro.185.1568028805134; Mon, 09 Sep 2019 04:33:25 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 9 Sep 2019 13:33:14 +0200 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="000000000000ed976e05921d2744" X-SA-Do-Not-Run: Yes X-Envelope-From: Subject: Re: FFI extension / NULL-Checks From: phofstetter@sensational.ch (Philip Hofstetter) --000000000000ed976e05921d2744 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable In response to these findings so far, I have now submitted https://github.com/php/php-src/pull/4691 that adds an FFI::isNull() method that returns true if the passed CData instance points to NULL. If you agree that something like this is indeed needed, I will gladly update the PR and add unit tests. Philip On Mon, Sep 9, 2019 at 8:03 AM Philip Hofstetter wrote: > Hello, > > (I'm writing to the internals list because I don't think that at this > point, FFI usage is wide-spread enough to get an opinion on other venues) > > maybe I'm just stupid, but I think there has been a slight oversight in > the API design for the FFI interface. > > My problem is with functions that return a pointer to a struct as an out > parameter. > > So in C, it would look like this: > > Error* err; > func(&err); > > if (err !=3D NULL){ > do_error_handling(); > } > > If I translate this to PHP FFI, I would do > > $err =3D $ffi->new("Error*"); > $ffi->func(FFI::addr($err)); > > if I `var_dump` $err, I do see a public {0} property be set to NULL or to > actual error data. > > My issue is though: How do I check whether err* as been assgined to? > > isset($err) > $err[0] !=3D null > -> is always true, regardless of whether func as assigned an error or not > > isset($err->{0}) > isset($err[0]) > -> Cannot use object of type FFI\CData as array > > $err->{0} !=3D NULL; > -> FFI\Exception: NULL pointer dereference > > $err[0]; > -> segfault > > I'm sure I must be doing something wrong, but I don't know what. Also, th= e > documentation could do with an example on how to achieve the desired resu= lt > and, TBH, I think some of the various attempts produce a somewhat > unexpected output. > > Any comments? > > Philip > > > var_dump(isset($r->{0})); > -> Error: Cannot use object of type FFI\CData as array > > $a =3D '{0}'; > var_dump(isset($r->$a)); > -> Error: Cannot use object of type FFI\CData as array > > > var_dump(isset($r[0])); > var_dump($r[0]); > > > --=20 Sensational AG Giessh=C3=BCbelstrasse 62c, Postfach 1966, 8021 Z=C3=BCrich Tel. +41 43 544 09 60, Mobile +41 79 341 01 99 info@sensational.ch, http://www.sensational.ch --000000000000ed976e05921d2744--