pib icon indicating copy to clipboard operation
pib copied to clipboard

php in nodejs

Open domozhirov opened this issue 6 years ago • 4 comments

Hello. How to use PIB in nodejs or how to make PIN (php in nodejs)

domozhirov avatar Jul 28 '18 22:07 domozhirov

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:

  • Remove preload file, which only works in browser and service workers
  • Add -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])
})

oraoto avatar Jul 30 '18 03:07 oraoto

Exactly what is needed but without cli :(

domozhirov avatar Aug 08 '18 16:08 domozhirov

@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

oraoto avatar Aug 09 '18 02:08 oraoto

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.

octalmage avatar Nov 25 '18 06:11 octalmage