/** * sample use: * * define("external", * array("include"=>"my_js_include", "require"=>"my_js_require"), * array("PI"=>3.1415926535, "ZERO"=>0) ); * * A few notes: * - this function create a new Object() and assign it as $objname on the global object. * - every function and variable is placed as a direct property of $objname * - those functions will have a length of 0. I'd be surprised if someone cares. * - those functions won't have a prototype. Again, not likely to matter much. * - variables cannot contain arrays or objects. use the real jsrt:: API for that stuff. * */ static function define($objname, $funcarray, $vararray = null) { #-- start by covering our basics js::init(); #-- define the main object $obj = new js_object(); jsrt::define_variable($objname, $obj); #-- start linking our functions jsrt::push_context($obj); foreach ($funcarray as $js => $php) { jsrt::define_function($php, $js); } jsrt::pop_context(); #-- put variables in place foreach ((array) $vararray as $js => $php) { #-- odd, but php.net discourages the use of gettype, so watch me comply. switch (true) { case is_bool($php): $v = new js_val(js_val::BOOLEAN, $php); break; case is_string($php): $v = js_str($php); break; case is_numeric($php): $v = js_int($php); break; case is_null($php): $v = jsrt::$null; break; case is_array($php): /* we could do something smarter here. maybe later. */ /* we could do something smarter here. maybe later. */ default: $v = jsrt::$undefined; break; } $obj->put($js, $v); } }