phpv8 / v8js

V8 Javascript Engine for PHP — This PHP extension embeds the Google V8 Javascript Engine
http://pecl.php.net/package/v8js
MIT License
1.83k stars 200 forks source link

Extensions are not loaded as in utf8 encoding or charset #317

Closed HoffmannP closed 7 years ago

HoffmannP commented 7 years ago

I had to find out the hard way that extensions loaded in a strange encoding, but see for yourself:

<?php

# insert umlaute via extension
V8Js::registerExtension('extensionUtf8', "var extensionUtf8 = {umlaute: 'äöüßÜÄÖÜß'}", array(), true);
V8Js::registerExtension('extensionLatin1', iconv('utf8', 'latin1', "var extensionLatin1 = {umlaute: 'äöüßÜÄÖÜß'}"), array(), true);

# insert umlaute via php var
$jscript = new V8Js('php');
$jscript->umlaute = "äöüßÜÄÖÜß";

# insert umlaute via executeString
$jscript->executeString("var execStr = {umlaute: 'äöüßÜÄÖÜß'}");

# now show me
echo "\n";
$jscript->executeString('print("extensionUtf8: "); var_dump(extensionUtf8.umlaute)');
$jscript->executeString('print("extensionLatin1: "); var_dump(extensionLatin1.umlaute)');
$jscript->executeString('print("php: "); var_dump(php.umlaute)');
$jscript->executeString('print("execStr: "); var_dump(execStr.umlaute)');

# and finaly return  to php
echo "\n";
$jscript->executeString("values = {}");
$jscript->executeString("values['extensionUtf8'] = extensionUtf8.umlaute");
$jscript->executeString("values['extensionLatin1'] = extensionLatin1.umlaute");
$jscript->executeString("values['php'] = php.umlaute");
$jscript->executeString("values['execStr'] = execStr.umlaute");
$values = $jscript->executeString("values", V8Js::FLAG_FORCE_ARRAY);
echo "extensionUtf8: $values->extensionUtf8\n";
echo "extensionLatin1: $values->extensionLatin1\n";
echo "php: $values->php\n";
echo "execStr: $values->execStr\n";

This is what you might see:

extensionUtf8: string(36) "äöüßÜÄÖÜß"
extensionLatin1: string(18) "äöüßÜÄÖÜß"
php: string(18) "äöüßÜÄÖÜß"
string: string(18) "äöüßÜÄÖÜß"

extensionUtf8: äöüßÜÄÖÜß
extensionLatin1: äöüßÜÄÖÜß
php: äöüßÜÄÖÜß
string: äöüßÜÄÖÜß

And yes, my php is configured to use utf-8/unicode encoding/charset

stesie commented 7 years ago

interesting :-)

... anyways, you might not want to use extensions anymore anyways, snapshots are far better performance wise and commonjs module feature isn't slower performance wise yet has better error reporting

stesie commented 7 years ago

Won't fix, see discusssion above + extensions now marked deprecated (with pull #330)