Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98259 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22469 invoked from network); 8 Feb 2017 11:52:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Feb 2017 11:52:00 -0000 Authentication-Results: pb1.pair.com header.from=mike.php.net@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=mike.php.net@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.128.176 as permitted sender) X-PHP-List-Original-Sender: mike.php.net@gmail.com X-Host-Fingerprint: 209.85.128.176 mail-wr0-f176.google.com Received: from [209.85.128.176] ([209.85.128.176:34622] helo=mail-wr0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D3/B1-33872-E560B985 for ; Wed, 08 Feb 2017 06:51:59 -0500 Received: by mail-wr0-f176.google.com with SMTP id o16so59127416wra.1 for ; Wed, 08 Feb 2017 03:51:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:to:references:from:message-id:date:user-agent :mime-version:in-reply-to; bh=+EXgH5i1U36qm84cLUKPGdPO8X35lTP+NCF2OKHEWqg=; b=c2tm+ocnCeMd3LoYs19Yp1C7epm1nUEh++78WNMnMrd5uxYz5urfDM4bGuZ1Vtdy+T MdkC3pqAb7mLlMQqT4bKZmvU8pAUun1zdQgDcvaWh7J1mbJ6w5FUMB3l0f4FGWZ90Je0 5Dn15v4EMFV5yjwDVHHQ2ZKlRv9Neuc5pp4JWBS/4p37IiplTSEGnYI+GE8r0DG3ezE5 FIOmzIYjQoNTsS1BS/GUTJmRDJVK7v2Q/PNhyINKwLsVquyYdky/rLou45hFiRJ8UPdj IDgVJZVVcxT04GA4SyAdRDs8z5ShTRXVEJSelrg/5LdnQk+eVIYzxuFd/Y/+PVWhqtiD PO6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:to:references:from:message-id :date:user-agent:mime-version:in-reply-to; bh=+EXgH5i1U36qm84cLUKPGdPO8X35lTP+NCF2OKHEWqg=; b=AxSMhkb9ojlifU2w92dqHzm2SGp15McDkxoMyR+aNqZXAjmpUiXAjrHmdVJO8O4jTw fgzP02SDUJm96knFcTAxI1mD4Y6rHwtWKaOhQSa3J1ylcnmjNcHX6QLHP8XQR7R4LqcL e5rCxG/i+Car4PGMPTviO/XETrFMZTZb/JlsLu1uqI8BiGaFRYJk6j6N0hfAakXMAqHe PSCx3B19oExBLEH1O5Sc6Hh/c4MAoWTSjeL98rIoNT80SJvvO0LtwKIhTlV+QB0z7uZW EJVJd7IgBwrJyFZ/XREeJ9hFzVpMyXgt/2C+EpDwll6/DISDxBgAmQ+8KDstvE200g30 nOkA== X-Gm-Message-State: AIkVDXJEzOx3MRHcm3Fn2FS871DEJh1+VTRRk+kiSnx/doJE3NK0m6m8BW3K505Cu/EY7w== X-Received: by 10.223.135.146 with SMTP id b18mr18330221wrb.189.1486554714720; Wed, 08 Feb 2017 03:51:54 -0800 (PST) Received: from [192.168.2.207] (89-104-28-113.customer.bnet.at. [89.104.28.113]) by smtp.googlemail.com with ESMTPSA id 123sm2956357wml.6.2017.02.08.03.51.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 Feb 2017 03:51:54 -0800 (PST) Sender: Michael Wallner To: Alex Bowers , PHP References: Message-ID: <15caa5d8-07d1-d76e-974a-c0bb0c4e8126@php.net> Date: Wed, 8 Feb 2017 12:51:53 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="4n4kNPTRWMsqao7DV5l5fR5P0t9P74v4K" Subject: Re: [PHP-DEV] [Discussion] FFI in PHP From: mike@php.net (Michael Wallner) --4n4kNPTRWMsqao7DV5l5fR5P0t9P74v4K Content-Type: multipart/mixed; boundary="QJUkhTda4irP06rbgorjqDgkxHnN6LjPN"; protected-headers="v1" From: Michael Wallner To: Alex Bowers , PHP Message-ID: <15caa5d8-07d1-d76e-974a-c0bb0c4e8126@php.net> Subject: Re: [PHP-DEV] [Discussion] FFI in PHP References: In-Reply-To: --QJUkhTda4irP06rbgorjqDgkxHnN6LjPN Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 05/02/17 23:25, Alex Bowers wrote: > And here is the previous messaging without borked formatting. Sorry fol= ks. >=20 >=20 > FFI RFC > =3D=3D=3D=3D=3D=3D =2E.. > Example > =3D=3D=3D >=20 > Take an example of a rust program that takes two numbers in and gives > you the sum of them. >=20 > ```rust > #[no_mangle] > pub extern fn add(a: i32, b: i32) -> i32 { > a + b > } >=20 > ``` >=20 > with the Cargo.toml file containing: >=20 > ``` > [package] > name =3D "math" > version =3D "0.1.0" > authors =3D ["Alex Bowers "] >=20 > [dependencies] >=20 > [lib] > name =3D "math" > crate-type =3D ["dylib"] > ``` >=20 > `cargo build --release` will create `.so`, `.dylib`, or `.dll` files > depending on your system. >=20 > These should be usable within PHP using the exposed functions. >=20 > ```php > $math =3D ffi("/path/to/math.so"); > $result =3D $math->add(1, 5); >=20 > echo $result; // 6 > ``` With ext/psi [1], that would work differently. You wouldn't define the interface at runtime, like the above example, pecl/ffi [2] or MFFI [3]. First, you would create an interface description file (a PSI file [4]), where you specify the native declaration and the library to load: ``` // https://github.com/m6w6/ext-psi#lib lib "/path/to/math.so"; // https://github.com/m6w6/ext-psi#declarations extern int32_t add(int32_t a, int32_t b); ``` The PHP interface to this function would usually be defined in the same file possibly looking along the following lines: ``` // https://github.com/m6w6/ext-psi#implementations function math\add(int $a, int $b) : int { let a =3D intval($a); let b =3D intval($b); return to_int(add); } ``` Those PSI files would be loaded from a directory configured in php.ini (psi.directory) and loaded at startup, i.e. the interfaces would be available to user land for the lifetime of the process, not the request. Have a look at the complete libidn example [5] of the README or the sqlite test [6] utilizing a user land callback. I've been working on ext/psi for the good parts of more than a year now, and I would be more than happy about insights, suggestions or collaboration with other interested parties. The (currently) two major to-dos are decent CPP support in the parser, so one could basically just parse C headers, and levelling out support for input/output marshalling, which currently differ in sophistication, AFAIR, while quite a lot is already possible. Other random bits can be found in the TODO [7]. It has been successfully built and run on Linux and OSX but YMMV; also, grab a coffee when building the first time, because `configure` performs a ton of checks for POSIX builtins (still like, 10% only yet), so don't forget the -C/--cache-file switch. [1] https://github.com/m6w6/ext-psi [2] https://pecl.php.net/ffi [3] https://github.com/mgdm/MFFI [4] https://github.com/m6w6/ext-psi#psi-files [5] https://github.com/m6w6/ext-psi#complete-example [6] https://github.com/m6w6/ext-psi/tree/master/tests/sqlite [7] https://github.com/m6w6/ext-psi/blob/master/TODO --=20 Regards, Mike --QJUkhTda4irP06rbgorjqDgkxHnN6LjPN-- --4n4kNPTRWMsqao7DV5l5fR5P0t9P74v4K Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAEBCgAdFiEEHsPHHd1jWDGjN9aESA4+FLCkx8cFAlibBlkACgkQSA4+FLCk x8eLPQf/cjWl0Q8zx/gL8+2PcCCbj6XzYvS+g+V0td6rwHM70x8X3I/lF8yzBARf JS6jOde5kP4relwKBv/Tz94NRcE/6zTJmwwKGmDxwlM7wk8CM9duZSJr6nB+kbUi i868/TGyFoVQ8zq8O02tqDe87fgQiASPOoQ86GnSDzy1Y6PCVbpd4vvAlW+HgKDr HnE09+F+62bd15O4zTalRv3v222SCPNWci6w49uCv0l7haW4sxqRQqUXSCKx08gG 66sUGhD4RF9ZikWKXvUlfY/8DjIoOoUfSco0we5Eiy/JXll3i6p260E9eIgsz1My TlE4AML4pTuJTwg/j4vcMfJLxWrL1w== =T5ng -----END PGP SIGNATURE----- --4n4kNPTRWMsqao7DV5l5fR5P0t9P74v4K--