i'm trying figure out why rhino not able function object inside function.
according rhino documentation, how extract function in javascript java side.
object fobj = scope.get("f", scope); if (!(fobj instanceof function)) { system.out.println("f undefined or not function."); } else { object functionargs[] = { "my arg" }; function f = (function)fobj; object result = f.call(cx, scope, scope, functionargs); string report = "f('my args') = " + context.tostring(result); system.out.println(report); }
if have javascript, work fine :
function f(){ //some lines }
however, problem :
assume have java function :
class remotejavaclass{ public void extractjavascript(string targetfunctionname){ object fobj = scope.get(targetfunctionname, scope); if (!(fobj instanceof function)) { system.out.println(targetfunctionname + " undefined or not function."); } } }
and javascript in abc.js file :
function foo(){ function inner(){ //something } remrem.extractjavascript("inner"); } foo()
before execute abc.js java, have "inject" variable remrem follows (in order javascript able call java function):
public void main (string args[]){ scope = cx.initstandardobjects(new importertoplevel(cx)); remotejavaclass inst = new remotejavaclass(); //inject inst javascript variable "remrem" scriptableobject.putproperty(scope, "remrem", context.javatojs(inst, scope)); //finally execute script string scriptstring = (read javascript text abc.js) object result = cx.evaluatestring(scope, scriptstring, "title", 1, null); }
and output :
"inner undefined or not function."
however, if script looks this, rhino able extract inner. if whole thing not inside function, rhino won't have problem extracting inner function.
function inner(){ //something } remrem.extractjavascript("inner");
i've played around enough scope, , tried didn't work. hypothesis rhino looking inner in global scope , went ahead , tried in function scope no avail, didn't work.
object fobj = scope.get("f", scope); if (!(fobj instanceof function)) { system.out.println("f undefined or not function."); } else { object functionargs[] = { "my arg" }; function f = (function)fobj; object result = f.call(cx, **fobj**, **fobj**, functionargs); string report = "f('my args') = " + context.tostring(result); system.out.println(report); }
and still got error :
org.mozilla.javascript.uniquetag@11769f4c: not_found
does have experience rhino , me out?
thank much!
Comments
Post a Comment