omar kilani

home | projects

libical | misc | php-js | php-json | postfix_memcached


           

php-js

php-js is an experimental PHP extension which embeds Mozilla SpiderMonkey into PHP.

Download

Version 0.1.2 (php-js SRPM, libjs TGZ, libjs SRPM) - Released 2006-01-14 - Better handling of null objects.

Version 0.1.1 (php-js SRPM, libjs TBZ, libjs SRPM) - Released 2005-09-29 - Fix session shutdown issue.

Version 0.1.0 - Released 2005-09-07 - Initial release, implements js_eval.

Mailing List

Google Groups Subscribe to php-js
Email:
Browse Archives at groups.google.com

Documentation

A simple ./configure; make; make install should do the trick. Make sure to add an extension=js.so line to your php.ini/php.d. Note: you need to install libjs first. If you're using a Redhat-esque system, you can use the SRPM provided above, else, use the TBZ.

Then, just use js_eval to evaluate your JavaScript. js_eval returns the value returned by the JavaScript interpreter to PHP.

For example:

js_eval("var a = 123;");
js_eval("var b = 456;");

$c = js_eval("[a, b];");
echo "a is ".$c[0]."\n";
echo "b is ".$c[1]."\n";

js_eval("var sum = function(x, y) { return x + y; }");

$d = js_eval("sum(a, b);");
echo "The sum of a and b is ".$d."\n";
	

Would produce:

a is 123
b is 456
The sum of a and b is 579
	

js_eval takes an optional boolean argument, assoc, which returns objects as associative arrays instead of PHP objects.

The php-js execution environment provides two built-in JavaScript system functions:

  • print
  • gc

print outputs its argument to the php output stream.

gc forces garbage collection within the JavaScript environment.

Performance

Following are some performance metrics for the php-js C extension compared to a raw C implementation.

js_eval("'Hello, World'.toUpperCase();");
js_eval("[10, 9, 8, 7, 6, 5, 4, 3, 2, 1].sort();");
js_eval("var a = 123;");
js_eval("var b = 456;");
js_eval("[a, b];");
js_eval("var sum = function(x, y) { return x + y; }");
js_eval("sum(a, b);");

---
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)
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]# ./js
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)
response time [seconds per 1000] (min/max/avg): 0.660778/0.667399/0.662425