oraoto / pib

PHP in Browser (powered by WebAssembly)
https://oraoto.github.io/pib/
Apache License 2.0
953 stars 109 forks source link

Can I use custom php-extension inside pib? #24

Open kaineer opened 5 years ago

kaineer commented 5 years ago

Usual way to use extension with php is to copy compiled extension into extension_dir and then adding extension=ext-name.o to php.ini.

Is there any way to include some custom extension into pib bundle?

I'm just started to learn something about emacsen, so I yet can't figure solution by myself.

oraoto commented 5 years ago

Sorry, I don't have much time to investigate loading a extension dynamically from network or filesystem. Currently the only tested way to use php extension is to static link it into the binary.

kaineer commented 5 years ago

Static linking would be ok. But is it possible for php extensions? Where can I read about it?

oraoto commented 5 years ago

A simple extension demo is add: https://github.com/oraoto/pib/commit/a5f47e70c5a950d6c5a3823d22cc9bf81403d8a8

Explanation:

  1. Symbolic link (or copy) the extension source into php's ext folder

    ln -rsfv ext-example $PHP_PATH/ext
  2. Run buildconf --force to re-generate configs

    # Install
    apt-get update
    apt-get install -y autoconf
    ./buildconf --force
  3. Enable the extension in configure

    emconfigure ./configure --
      --disable-all \
      --disable-cgi \
      --disable-cli \
      --disable-rpath \
      --disable-phpdbg \
      --with-valgrind=no \
      --without-pear \
      --without-pcre-jit \
      --with-layout=GNU \
      --enable-embed=static \
      --enable-bcmath \
      --enable-json \
      --enable-ctype \
      --enable-mbstring \
      --disable-mbregex \
      --enable-tokenizer \
      --enable-example
  4. Build it as usual. For the latest emscripten, this step fails.

oraoto commented 5 years ago

With today's latest emscripten, it compiles and runs

kaineer commented 5 years ago

Great :) Thank you. I'll try this.

kaineer commented 5 years ago

Updated emscripten to 1.38.31 and got bunch of errors like:

error: asm2wasm seeing an invalid argument type at index 1 (this will not validate) in call from $__php_s
tream_fopen_from_fd to $_lseek (this is likely due to undefined behavior in C, like defining a function $
ne way and calling it in another, which is important to fix)

Did you use 1.38.31 version, or some more later one?

kaineer commented 5 years ago

After switching to php-7.3.0, both functions from example extension became available, yay :) Thanks again :)

oraoto commented 5 years ago

I build the example using docker image trzeci/emscripten:sdk-incoming-64bit.

kaineer commented 5 years ago

Oh, now I see. Fortunately, emacsen 1.38.31 was enough for me.

kaineer commented 5 years ago

Good. First part of my problem is solved :)

But, there's second one :-/ I need to call Javascript function from my extension.

Samples that I've found use something like extern functionName(); notation and then just call that function. It seems like in php-extension I should use something else.

Didn't find anything useful in google, though. Perhaps, I don't know what to search for.

oraoto commented 5 years ago

You can call javascript functions using emacripten's emscripten_run_script.

seanmorris commented 2 years ago

@kaineer I did it when I created vrzno. If you're still interested let me know.