niklasvh / php.js

PHP to JavaScript converter and VM written in JavaScript
http://phpjs.hertzen.com
MIT License
856 stars 116 forks source link

Operator Overloading #2

Open srgoogleguy opened 12 years ago

srgoogleguy commented 12 years ago

There may be a problem with operator overloading behavior in the library.

Take:

<?php $a = new stdclass; $b = array();

var_dump($a + $b, $b + $a); ?>

Both $a + $b and $b + $a will cause a fatal error of unsupported operand types in PHP. However, in JS neither case is true. They yield unexpected results in JS, actually as {} + [] and [] + {} result in 0 (or NaN depending on the engine), and "[object Object]", respectively. Here the code above results in:

int(0) int(0)

Which is what I expect the library VM is doing with the operator ADD here.

niklasvh commented 12 years ago

Seems like a bug indeed, which most likely is also present in minus, div, multiply and mod operators as well then, will have a closer look at this later