Have you ever woken up one morning and thought to yourself, "Self,
wouldn't it be *super sweet* if I could evaluate JavaScript from within
PHP? Wouldn't it be *totally awesome* if I could use the 20,000 lines
of JavaScript I painstakingly developed *within* PHP so I wouldn't have
to rewrite all that functionality *again*?"
Well, I have, and here's the monstrosity^H^H^H^H^H^H^H^H^H^H^Hresult:
http://aurore.net/projects/php-js/
That's right folks, JavaScript *in* PHP.
If this is wrong then I don't want to be right!
Enjoy.
Best Regards,
Omar Kilani
---
Some test results:
js_eval("'Hello, World'.toUpperCase();");
string(12) "HELLO, WORLD" refcount(2)
js_eval("[10, 9, 8, 7, 6, 5, 4, 3, 2, 1].sort();");
array(10) refcount(2){
[0]=>
long(1) refcount(1)
[1]=>
long(10) refcount(1)
[2]=>
long(2) refcount(1)
[3]=>
long(3) refcount(1)
[4]=>
long(4) refcount(1)
[5]=>
long(5) refcount(1)
[6]=>
long(6) refcount(1)
[7]=>
long(7) refcount(1)
[8]=>
long(8) refcount(1)
[9]=>
long(9) refcount(1)
}
js_eval("var a = 123;");
js_eval("var b = 456;");
js_eval("[a, b];");
array(2) refcount(2){
[0]=>
long(123) refcount(1)
[1]=>
long(456) refcount(1)
}
js_eval("var sum = function(x, y) { return x + y; }");
js_eval("sum(a, b);");
long(579) refcount(2)
---
PHP
The above called 10,000 times:
[root@vorlon ~]# php test001.phpt
1000 (0.26575112342834)
2000 (0.26451206207275)
3000 (0.26420903205872)
4000 (0.26577401161194)
5000 (0.3562798500061)
6000 (0.25892996788025)
7000 (0.26618099212646)
8000 (0.26237297058105)
9000 (0.38350915908813)
response time [seconds per 1000] (min/max/avg):
0.25892996788025/0.38350915908813/0.28750212987264
The above called 10,000 times (explicit garbage collection via
js_eval("gc()")):
[root@vorlon ~]# php test001gc.phpt
1000 (0.71484088897705)
2000 (0.71011710166931)
3000 (0.67023801803589)
4000 (0.6698169708252)
5000 (0.67017698287964)
6000 (0.6723940372467)
7000 (0.67803597450256)
8000 (0.66521191596985)
9000 (0.66061592102051)
response time [seconds per 1000] (min/max/avg):
0.66061592102051/0.71484088897705/0.67904975679186
---
C
The above called 10,000 times:
[root@vorlon jsexp]# ./js
1000 (0.273781)
2000 (0.275317)
3000 (0.273173)
4000 (0.393171)
5000 (0.270962)
6000 (0.270556)
7000 (0.269467)
8000 (0.425113)
9000 (0.292452)
dist[1] = 0.273781
dist[2] = 0.275317
dist[3] = 0.273173
dist[4] = 0.393171
dist[5] = 0.270962
dist[6] = 0.270556
dist[7] = 0.269467
dist[8] = 0.425113
dist[9] = 0.292452
response time [seconds per 1000] (min/max/avg):
0.269467/0.425113/0.304888
The above called 10,000 times (explicit garbage collection via
JS_EvaluateString with gc()):
[root@vorlon jsexp]# ./jsgc
1000 (0.661728)
2000 (0.660778)
3000 (0.662642)
4000 (0.661576)
5000 (0.661953)
6000 (0.660882)
7000 (0.661087)
8000 (0.663779)
9000 (0.667399)
dist[1] = 0.661728
dist[2] = 0.660778
dist[3] = 0.662642
dist[4] = 0.661576
dist[5] = 0.661953
dist[6] = 0.660882
dist[7] = 0.661087
dist[8] = 0.663779
dist[9] = 0.667399
response time [seconds per 1000] (min/max/avg):
0.660778/0.667399/0.662425