Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98262 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 31614 invoked from network); 8 Feb 2017 13:54:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 8 Feb 2017 13:54:23 -0000 Authentication-Results: pb1.pair.com header.from=michal@brzuchalski.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=michal@brzuchalski.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain brzuchalski.com designates 188.165.245.118 as permitted sender) X-PHP-List-Original-Sender: michal@brzuchalski.com X-Host-Fingerprint: 188.165.245.118 ns220893.ip-188-165-245.eu Received: from [188.165.245.118] ([188.165.245.118:55893] helo=poczta.brzuchalski.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9D/C2-33872-6032B985 for ; Wed, 08 Feb 2017 08:54:17 -0500 Received: from localhost (localhost.localdomain [127.0.0.1]) by poczta.brzuchalski.com (Postfix) with ESMTP id 66DE62984237 for ; Wed, 8 Feb 2017 14:54:11 +0100 (CET) Received: from poczta.brzuchalski.com ([127.0.0.1]) by localhost (poczta.brzuchalski.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id rVarKZKRMzxc for ; Wed, 8 Feb 2017 14:54:05 +0100 (CET) Received: from mail-vk0-f54.google.com (unknown [209.85.213.54]) by poczta.brzuchalski.com (Postfix) with ESMTPSA id E88C92984235 for ; Wed, 8 Feb 2017 14:54:04 +0100 (CET) Received: by mail-vk0-f54.google.com with SMTP id r136so101054934vke.1 for ; Wed, 08 Feb 2017 05:54:04 -0800 (PST) X-Gm-Message-State: AMke39nyN+/s1XSF/aA7kSM+wDXyYaIsxm3vfOsIpWrmpSJuFjoIvmw0hC4lJBVMIsCZf6h+sCCRnemsGjT2mQ== X-Received: by 10.31.230.131 with SMTP id d125mr8269374vkh.104.1486562044037; Wed, 08 Feb 2017 05:54:04 -0800 (PST) MIME-Version: 1.0 Received: by 10.103.27.133 with HTTP; Wed, 8 Feb 2017 05:54:03 -0800 (PST) In-Reply-To: <15caa5d8-07d1-d76e-974a-c0bb0c4e8126@php.net> References: <15caa5d8-07d1-d76e-974a-c0bb0c4e8126@php.net> Date: Wed, 8 Feb 2017 14:54:03 +0100 X-Gmail-Original-Message-ID: Message-ID: To: Michael Wallner Cc: Alex Bowers , PHP Content-Type: multipart/alternative; boundary=94eb2c095b2a9255d105480532ba Subject: Re: [PHP-DEV] [Discussion] FFI in PHP From: michal@brzuchalski.com (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=) --94eb2c095b2a9255d105480532ba Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 2017-02-08 12:51 GMT+01:00 Michael Wallner : > On 05/02/17 23:25, Alex Bowers wrote: > > And here is the previous messaging without borked formatting. Sorry > folks. > > > > > > FFI RFC > > =3D=3D=3D=3D=3D=3D > ... > > Example > > =3D=3D=3D > > > > Take an example of a rust program that takes two numbers in and gives > > you the sum of them. > > > > ```rust > > #[no_mangle] > > pub extern fn add(a: i32, b: i32) -> i32 { > > a + b > > } > > > > ``` > > > > with the Cargo.toml file containing: > > > > ``` > > [package] > > name =3D "math" > > version =3D "0.1.0" > > authors =3D ["Alex Bowers "] > > > > [dependencies] > > > > [lib] > > name =3D "math" > > crate-type =3D ["dylib"] > > ``` > > > > `cargo build --release` will create `.so`, `.dylib`, or `.dll` files > > depending on your system. > > > > These should be usable within PHP using the exposed functions. > > > > ```php > > $math =3D ffi("/path/to/math.so"); > > $result =3D $math->add(1, 5); > > > > 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); > } > ``` > What is the purpose of `intval` call if both variables were declared as int already? Also what to_int stands for if we already declared int return type? > > 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 > > -- > Regards, > Mike > > --=20 regards / pozdrawiam, -- Micha=C5=82 Brzuchalski about.me/brzuchal brzuchalski.com --94eb2c095b2a9255d105480532ba--