Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using 'eval' to define a toplevel function when called from within an object.

306 views
Skip to first unread message

mschaef

unread,
Jun 8, 2010, 11:25:36 AM6/8/10
to
I have what I hope is a quick and naive question. I've written (in
JavaScript) an interactive read-eval-print-loop that's encapsulated
within an object. However, I recently noticed that toplevel function
definitions specified to the interpreter do not appear to be
'remembered' by the interpreter. After some diagnostic work, I've
reduced the core problem to this:

var evaler = {
eval: function (str)
{
return eval(str);
},
};

eval("function t1() { return 1; }"); // GOOD
evaler.eval("function t2() { return 2; }"); // FAIL

After running this script, I have a definition for t1, and no
defintion for t2. The act of calling eval from within evaler is
sufficiently different from the toplevel call that the global
definition does not get recorded. What does happen is that the call to
evaler.eval returns a function object, so I'm presuming that t2 is
being defined and stored in some other set of bindings that I don't
have access to. (It's not defined in evaler.)

Is there any easy fix for this? I've tried all sorts of fixes, and
haven't stumbled upon one that works. (Most of what I've done has
centered around putting the call to eval in an anonymous function, and
altering the way that's called, chainging __parent__, etc.)

My suspicion is that I'm missing something fundamental (that I need to
know anyway).

Thanks,
Mike

Vinicius Isola

unread,
Jun 9, 2010, 9:04:17 AM6/9/10
to mschaef, dev-tech-js-...@lists.mozilla.org
You're changing the context when you call eval from inside another object.
You're creating a function eval inside the evaler object. So the "this" for
the eval method is evaler and not the main context.

For that to work you would need to change the context (this) back to the
root context. Inside a browser that would be the "window" object.
Inside Rhino you don't have that. You have to create it.

I would do something like this:
var window = this;


var evaler = {
eval : function (str) {

eval.call(window, str);
}
};

Every function have a "call" function inside it that can be used to set the
"this" and call it using the new one. That is what I'm doing when I call
eval.call(window,str);
It is calling the eval function with window as the "this" for it.
After that, calling:


evaler.eval("function t2() {return 2;}");

would create function t2 that could be called in the main context.
Here is my test:

js> var window = this;
js> var evaler = {eval:function(str) { return eval.call(window, str); }}
js> evaler.eval("print('works'); function t2() {return 2;}");
works

function t2() {
return 2;
}

js> t2()
2

Hope that helps.
Vinicius Isola
"Não podemos escolher as consequências, mas podemos escolher nossos atos."

> _______________________________________________
> dev-tech-js-engine-rhino mailing list
> dev-tech-js-...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino
>

0 new messages