/** * 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); } }
static function toString() { $obj = jsrt::this(); if (!$obj instanceof js_error) { throw new js_exception(new js_typeeror()); } return js_str(get_class($obj) . ": " . $obj->get("message")->toStr()->value); }
function &rt_var($namespace, $is_function = false, $force_create = false) { global $jsi_vars, $jsi_funcs; $result = null; if ($is_function) { $result =& jsrt::var_search($jsi_funcs, $namespace, true, false); if ($result) { return $result; // avoid function cleaning later, return now instead } } if ($namespace[0] != '__arguments') { $result =& jsrt::var_search($jsi_vars, array_merge(array('__arguments'), $namespace), $is_function, false); } if (!$result && $namespace[0] != 'this') { $result =& jsrt::var_search($jsi_vars, array_merge(array('this'), $namespace), $is_function, false); } if (!$result) { $result =& jsrt::var_search($jsi_vars, $namespace, $is_function, $force_create); } // function cleaning (make sure that user functions are sandboxed) if ($is_function && is_scalar($result)) { return 'jsrt_' . $result; } return $result; }