seanmorris / php-wasm

PHP in Browser, powered by WebAssembly.
https://seanmorris.github.io/php-wasm/
Apache License 2.0
606 stars 32 forks source link

PhpWeb returnValue: -1 #18

Closed kenorb closed 2 years ago

kenorb commented 2 years ago

I'm trying to test PhpWeb inside NextJS, and it loads fine, but PhpWeb returns -1 error when running PHP code.

My code in brief:

PhpWeb = (await require('php-wasm/PhpWeb')).PhpWeb;
php = new PhpWeb;
console.log(php.run('<?php echo "Hello, world!";').retVal);

I don't have any obvious errors, it's just PhpWeb instance returns code -1 ({"returnValue": -1, "binary": {}}).

Are there any additional steps before running the code for the first time? Or any other way to debug the reason of the error?

I'm using the latest available version of php-wasm (0.0.5).

seanmorris commented 2 years ago

Sorry about that, looks like there's some vestigial code in there still. Can you try the following form, and let me know if it works for you?

php.run('<?php echo "Hello, world!";').then(retVal => {
    // retVal contains the return code.
});
kenorb commented 2 years ago

I've tried:

console.log(php.run('<?php echo "Hello, world!";').then(retVal => { console.log('retVal: ', retVal); }));

and retVal is zero now, but no sign of Hello text.

Results of console.log messages:

Promise {<pending>}
retVal:  0

Any additional suggestions how to return the echo'ed string?

seanmorris commented 2 years ago

You'll need a separate event listener to grab the output:

php.addEventListener('output', (event) => {
    console.log(event.detail);
});

I know its a little awkward, but imagine sending code into the STDIN of a running PHP binary and grabbing the output from STDOUT. That's essentially what's going on under the hood here.

seanmorris commented 2 years ago

You might also want to wait for READY before you call RUN, this way you know the binary is downloaded and ready:

php.addEventListener('ready', () => {
    php.run('<?php echo "Hello, world!";');
});
seanmorris commented 2 years ago

@kenorb are we able to close this issue?

kenorb commented 2 years ago

Sure, the original issue has been solved. I'm going to do more testing as per suggestion. Update: Above suggestions works, thank you.