phpv8 / php-v8

PHP extension for V8 JavaScript engine
https://php-v8.readthedocs.io
MIT License
217 stars 14 forks source link

Call to undefined method V8\ObjectValue::value() #106

Closed qodeboy closed 6 years ago

qodeboy commented 6 years ago

Trying to extend bundled example to work with object, I'm not able to get value. Consider following code:

$isolate = new \V8\Isolate();
$context = new \V8\Context($isolate);
$source = new \V8\StringValue($isolate, "var say = function() {return {foo: 'bar'};}; say();");

$script = new \V8\Script($context, $source);
$result = $script->run($context);

echo $result->value() . PHP_EOL;

I understand that return value is no longer StringValue but ObjectValue instead. How do I properly get it's value?

My use-case is rendering VueJS app on server to implement server side rendering. For this, I need to execute some wrapper javascript and app itself on the server. This js code results in ObjectValue just like my example above. If I try to do $result->toString($context)->value() I get [object Object] which is not usable. What is a correct way to do this?

pinepain commented 6 years ago

Hi @qodeboy!

That's right, ::value() is a method which comes from PrimitiveValue abstract class, so only primitive values (non-objects) have this feature.

As converting object to string is not quite trivial thing and as it's a bit higher abstraction then this extension provides, you'll have to do it by yourself, probably, by iterating over properties and output key-value pairs. Alternatively, you can use JSON:stringify() method to get serialized object representation (it will work for any Value, actually).

pinepain commented 6 years ago

I'm moving away from PHP world and all my PHP projects going to be abandoned too. Abandoning this project too as I have no intent to continue working on it unless there would be strong request from community and commercial interest. No more updates or documentation will be made. If someone is interested, feels free to contact me using email specified in my GitHub profile.