function js_add($a, $b) { return js_int(php_int($a) + php_int($b)); }
static function exec($str) { $obj = jsrt::this(); if (get_class($obj) != "js_regexp") { throw new js_exception(new js_typeerror()); } $s = $str->toStr()->value; $len = strlen($s); $lastIndex = $obj->get("lastIndex")->toInteger()->value; $i = $lastIndex; if ($obj->get("global")->toBoolean()->value == false) { $i = 0; } do { if ($i < 0 or $i > $len) { $obj->put("lastIndex", jsrt::$zero); return jsrt::$null; } $r = $obj->match($s, $i); // XXX write js_regexp::match() $i++; } while ($r == NULL); $e = $r["endIndex"]; $n = $r["length"]; if ($obj->get("global")->toBoolean()->value == true) { $obj->put("lastIndex", js_int($e)); } $array = new js_array(); $array->put("index", js_int($i - 1)); $array->put("input", $str); $array->put("length", $n + 1); $array->put(0, js_str(substr($s, $i - 1, $e - $i))); for ($i = 0; $i < $n; $i++) { $array->put($i + 1, js_str($r[$i])); } return $array; }
/** * 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); } }