Open domozhirov opened 6 years ago
You can buile PIB for NodeJS by changing the build config to:
emcc -O3 \
-s WASM=1 \
-s EXPORTED_FUNCTIONS='["_pib_eval", "_php_embed_init", "_zend_eval_string", "_php_embed_shutdown"]' \
-s EXTRA_EXPORTED_RUNTIME_METHODS='["ccall"]' \
-s TOTAL_MEMORY=134217728 \
-s ASSERTIONS=0 \
-s INVOKE_RUN=0 \
-s ENVIRONMENT=node \
-s MODULARIZE=1 \
libs/libphp7.a pib_eval.o -o out/php.html
Some changes:
-s ENVIRONMENT=node
and -s MODULARIZE=1
Then you can run PHP in NodeJS like this:
let em_module = require('./php.js')
// No <?php tag
// Output is line-buffered, so EOL is needed
let code =`
echo "Hello from PHP" . PHP_EOL;
`
em_module().then((em) => {
em.ccall('pib_eval', 'number', ["string"], [code])
})
Exactly what is needed but without cli :(
@domozhirov To run PHP CLI is also possible:
Enable CLI, notice the --enable-cli
:
emconfigure ./configure \
--disable-all \
--disable-cgi \
--enable-cli \
--disable-rpath \
--disable-phpdbg \
--without-pear \
--without-pcre-jit \
--with-layout=GNU \
--enable-embed=static \
--enable-bcmath \
--enable-json \
--enable-ctype \
--enable-tokenizer
Build:
emmake make
# rename to *.o, so that emcc recognize it
mv sapi/cli/php sapi/cli/php.o
# compile to WASM
emcc -O3 \
-s WASM=1\
-s ENVIRONMENT=node \
-s TOTAL_MEMORY=134217728 \
sapi/cli/php.o -o out/php.html
Run:
$ cd out
$ node php.js -v
PHP 7.3.0alpha3 (cli) (built: Aug 9 2018 10:19:46) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.0-dev, Copyright (c) 1998-2018 Zend Technologies
$ node php.js -m
[PHP Modules]
bcmath
Core
ctype
date
json
pcre
Reflection
SPL
standard
tokenizer
[Zend Modules]
$ node php.js -r 'echo "Hello";'
Hello
However, it can't run PHP file directly, source code must be read from stdin:
$ node php.js ../Zend/bench.php
Could not open input file: ../Zend/bench.php
$ cat ../Zend/bench.php | node php.js
simple 0.178
simplecall 0.055
simpleucall 0.177
simpleudcall 0.196
mandel 0.769
mandel2 0.833
ackermann(7) 0.181
ary(50000) 0.028
ary2(50000) 0.022
ary3(2000) 0.431
fibo(30) 0.682
hash1(50000) 0.053
hash2(500) 0.066
heapsort(20000) 0.207
matrix(20) 0.220
nestedloop(12) 0.283
sieve(30) 0.124
strcat(200000) 0.031
------------------------
Total 4.535
How could you make it so pib could read files? Like through include? I’d like to try to run a PHP application to generate HTML client side.
Hello. How to use PIB in nodejs or how to make PIN (php in nodejs)