chundiliu / pyv8

Automatically exported from code.google.com/p/pyv8
0 stars 0 forks source link

2-nd context object "kills" (ok, makes it invalid) the 1-st one #97

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
    def test_2_ctx(self):
        ctx1 = PyV8.JSContext()
        ctx1.enter()
        ctx1.eval("function f1() {return 1;} f1();")
        r1 = ctx1.eval("f1();")
        assert r1 == 1, "assert1: expected=1, actual=%s" % r1

        ctx2 = PyV8.JSContext()
        ctx2.enter()
        r2 = ctx2.eval("1+2")
        assert r2 == 3, "assert2: expected=3, actual=%s" % r2

        r3 = ctx1.eval("f1();") # fails here with ReferenceError: f1 is not defined (  @ 1 : 0 )  -> f1();
        assert r3 == 1, "assert3: expected=1, actual=%s" % r3

What is the expected output? What do you see instead?
Listed above test should pass, but fails with "ReferenceError: f1 is not 
defined"

What version of the product are you using? On what operating system?
MacOs 10.6.7 64-bit
Python 2.6.5 64-bin
boost 1.46.1 manually compiled using
./bjam threading=multi link=static toolset=darwin architecture=x86 
address-model=64 install --with-python --layout=tagged --build-type=complete
V8 3.4.4
PyV8 from trunk (svn r381)

Please provide any additional information below.

Original issue reported on code.google.com by viy....@gmail.com on 26 Jul 2011 at 2:31

GoogleCodeExporter commented 8 years ago
There are only one context could be entered at same time, so, if you use ctx1 
in the ctx2 scope, it will not work.

Please enter the ctx1 before use it, it is a design issue

with ctx1:
    r3 = ctx1.eval("f1();") # fails here with ReferenceError: f1 is not defined (  @ 1 : 0 )  -> f1();
    assert r3 == 1, "assert3: expected=1, actual=%s" % r3

Original comment by flier...@gmail.com on 10 Aug 2011 at 4:19

GoogleCodeExporter commented 8 years ago
Thanks Flier, it works.
Sorry for the invalid issue.

PS. Should I also always exit ctx correctly? Or does the entering ctx2 always 
indirectly exit ctx1 previously entered?

Original comment by viy....@gmail.com on 10 Aug 2011 at 5:56

GoogleCodeExporter commented 8 years ago
You are welcome :)

I prefer to use 'with' statement to manage the context scope, even you could 
enter a new context before exit the current context.

with Context() as ctxt:
    ctxt.eval(...)

Original comment by flier...@gmail.com on 10 Aug 2011 at 6:01

GoogleCodeExporter commented 8 years ago
I planned to store several ctx filled (evaluated) with heavy JS code at app 
startup time.
And later (during app runtime) I need to switch between these ctx depending on 
some condition.
As I now understand - it will work fine with ctx.enter() and I got rid of 
reevaluating heavy JS each time.

Thanks.

Original comment by viy....@gmail.com on 10 Aug 2011 at 6:37

GoogleCodeExporter commented 8 years ago
If you just want to cache the script, you may try JSEngine.compile

http://code.google.com/p/pyv8/source/browse/trunk/PyV8.py#1698

Original comment by flier...@gmail.com on 11 Aug 2011 at 2:58