Implement scope chain with V8

39 views
Skip to first unread message

Puwanat Sretavevuth

unread,
Apr 21, 2014, 12:54:36 PM4/21/14
to v8-u...@googlegroups.com
Dear All

I'm new of V8 and would like to know how to implement scope chain with V8 , I was ever with Spider monkey which having object scope to calling with API  

JSBool JS_EvaluateScript(JSContext *cx, JSObject *obj,
    const char *src, uintN length, const char *filename,
    uintN lineno, jsval *rval);
or if you don't understand please kindly see this JAVA Script code below

var global_scope = this;
var some_value = 'hello';
global_scope.some_value ; //  'hello'

function (){
   var function_scope = this;
   var some_value = 'hi';
  function_scope.some_value ; // 'hi'
}

global_scope.some_value ; //  'hello'
function_scope.some_value = 'hi'; // error : function_scope is not defined; because scope chain (function_scope) was terminated;


============================

but when implementing with V8 , I have done something like this code below


 Handle<Context> contextA = Context::New(isolate);
 Handle<Context> contextB = Context::New(isolate);

 Context::Scope context_scope1(contextA);
Handle<String> source = String::NewFromUtf8(isolate, “var scope1 = this ; some_value=‘hello’;");
Handle<Script> script = Script::Compile(source);
Handle<Value> result = script->Run(); //

{
	Context::Scope context_scope2(contextB);
 	Handle<String> source = String::NewFromUtf8(isolate, “scope1.some_value");
 
Handle<Script> script = Script::Compile(source);
 
Handle<Value> result = script->Run(); // Error scope1 is not defined !!! result should be as 'hello' as above executing
	String::Utf8Value utf8(result);

}



==================================================

Please kindly advise and I do thank you very much for all that read my email.

Best Regards
Puwanat S.



Reply all
Reply to author
Forward
0 new messages