asmblah / uniter

🎉 PHP in the browser and Node.js => Docs: https://phptojs.com/
https://asmblah.github.io/uniter/
Other
446 stars 42 forks source link

Non-numeric PHP arrays passed to JS should be converted to Objects #39

Closed dave-irvine closed 7 years ago

dave-irvine commented 8 years ago
var php = require('uniter').createEngine('PHP');
php.getStdout().on('data', function (text) { console.log(text); });
php.getStderr().on('data', function (text) { console.error(text); });

var jsFunction = function(data) {
        console.log(data);
}

php.expose(jsFunction, 'jsFunction');
php.execute('<?php $x = array("A" => "B"); $jsFunction($x);');

[ A: 'B' ]
IngwiePhoenix commented 8 years ago

You are right. Once the array becomes associative, ergo, it has a non-numeric indice, it should become an array. However, this would require keeping the length property in a correct state also... So this is not exactly very easy ^^; /cc @asmblah

asmblah commented 8 years ago

To keep things simple, I've now changed the behaviour in PHPCore v3.17.2 to still export a JS array when an array is returned from PHP-land but that JS array (which is still an object) will have non-numeric properties set on it. Here's the relevant commit: https://github.com/uniter/phpcore/commit/85c128d4527a4476cf0e42c31517ab1e1eaf4271

asmblah commented 7 years ago

Closing as I think the referenced fix does the job, feel free to reopen if needed.

Thanks!