I wrote (in JavaScript) an interactive read-eval-print loop that is encapsulated inside an object. However, I recently noticed that the definitions of top-level functions specified by the interpreter are not interpreted by the interpreter. After some diagnostic work, I reduced the main problem:
var evaler = {
eval: function (str)
{
return eval(str);
},
};
eval("function t1() { return 1; }");
evaler.eval("function t2() { return 2; }");
At this point, I hope that the following two statements will work as expected:
print(t1()); // => Results in 1 (This works)
print(t2()); // => Results in 2 (this fails with an error that t2 is undefined.)
Instead, I get the expected value for the row t1, and the row t2fails with an error, which t2is unrelated.
IOW: script, t1, defintion t2. eval evaler , . , ,
evaler.eval , , t2 , . ( evaler.)
- ? , . ( , , , eval , chainging __parent__ ..).
, ?
:
tl; dr: Rhino . t2 , . @Matt: "" .
- , jdb, , . , function t1() { return 42; } .
- , ,
function() { return 42; } t1.
, , , eval .
, Rhino, -, org.mozilla.javascript.ScriptRuntime.initFunction.
if (type == FunctionNode.FUNCTION_STATEMENT) {
....
scope.put(name, scope, function);
t1 , scope - , . , , :
main[1] print function.getFunctionName()
function.getFunctionName() = "t1"
main[1] print scope
scope = "com.me.testprogram.Main@24148662"
t2 scope - :
main[1] print function.getFunctionName()
function.getFunctionName() = "t2"
main[1] print scope
scope = "org.mozilla.javascript.NativeCall@23abcc03"
NativeCall, :
main[1] print scope.getParentScope()
scope.getParentScope() = "com.me.testprogram.Main@24148662"
, , : " eval t2 . evaler " ". , ' ' NativeCall... t2 , t2 NativeCall, NativeCall , evaler.eval .
... , , , NativeCall , this evaler evaler.eval. ( , NativeCall Interpreter.initFrame, " " . , , , . , .)