Subj : Help clarifying the concept of scope in spidermonkey To : netscape.public.mozilla.jseng From : J.P. Date : Fri Jul 09 2004 01:38 pm In the code below, I create two JSObjects (obj1 and oj2), for two scope. They are using the same context. I run script "x=0;x=x+1;" in obj1, then run "x=x+1" in obj2. I expected that x equal to 1 in obj1 and getting an error in obj2, since obj1 and obj2 are seperated scopes and x is not defined in obj2. But the actual result is Scope 1: x=1 Scope 2: x=2 So the question is: Why doesn't obj2 have its own x, but use the x in obj1? To make the code do what I intend to do, I have to call JS_ClearScope(js_context, obj1) after done using obj1. //////////////////////////////////////////////////// JSRuntime * js_runtime = JS_NewRuntime(1024L * 1024L); JSContext * js_context = JS_NewContext(js_runtime, 8192); JSClass scope_class = { "clsScope", 0, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub }; //scope 1 JSObject *obj1 = JS_NewObject(js_context, &scope_class, NULL, NULL); std::string s; JS_InitStandardClasses(js_context, obj1); jsval val; s = "x=0; x=x+1;"; JS_EvaluateScript(js_context, obj1, s.c_str(), s.length(), NULL, 0, &val); int i = JSVAL_TO_INT(val); std::cout << "Scope 1: x=" << i << std::endl; //scope 2 JSObject *obj2 = JS_NewObject(js_context, &scope_class, NULL, NULL); JS_InitStandardClasses(js_context, obj2); s = "x=x+1;"; JS_EvaluateScript(js_context, obj2, s.c_str(), s.length(), NULL, 0, &val); i = JSVAL_TO_INT(val); std::cout << "Scope 2: x=" << i << std::endl; JS_DestroyContext(js_context); JS_DestroyRuntime(js_runtime); JS_ShutDown(); /////////////////////////////////////////////////////////// .