title: How to use the same context across multiple functions in v8 Javascript?
body: <p>I am new to V8 and trying to create some bindings between my C++ and JS code. The issue is that I am not able to access a global Javascript variable across different Javascript functions. The reason is because each Javascript function is called my different C++ function. I guess the problem with my code is that I am creating a new local context within each C++ function.</p>
  
  <p><strong>My JS Code</strong>:</p>
  
  <pre><code>var test = [];
  
  function f1()
  {
      var obj = {};
      obj.name = "Testing";
      test.push(obj);
  }
  
  function f2()
  {
      var value = test[0].name;
  }
  </code></pre>
  
  <p><strong>My C++ code</strong>:</p>
  
  <pre><code>class Test 
  {
  
    v8::Persistent&lt;v8::Script&gt; compiledScript;
    v8::Local&lt;v8::Value&gt; result;
    v8::Isolate* isolate;
    v8::Persistent&lt;v8::Context&gt; context;
  
    Test(filePath) {
  
        // Create and allocate isolate
  
        v8::Locker isolateLocker(isolate);
        v8::Isolate::Scope isolate_scope(isolate);
        v8::HandleScope handle_scope(isolate);
        // Create some bindings
  
        v8::Local&lt;v8::Context&gt; con = v8::Context::New(isolate, nullptr, binding_template);
        con-&gt;SetAlignedPointerInEmbedderData(0, &amp;binder);
  
        context.Reset(isolate, con);
        v8::Context::Scope context_scope(con);
  
        std::string source_file = LoadFile(filePath);
        v8::Local&lt;v8::String&gt; sourceScript = v8::String::NewFromUtf8(isolate, source_file.c_str(), v8::NewStringType::kNormal).ToLocalChecked();
        v8::Local&lt;v8::Script&gt; script = v8::Script::Compile(con, sourceScript).ToLocalChecked();
        compiledScript.Reset(isolate, script);
  }
  
  void function1() 
  {
      v8::Locker isolateLocker(isolate);
      v8::Isolate::Scope isolate_scope(isolate);               
      v8::HandleScope handle_scope(isolate);
  
      v8::Local&lt;v8::Context&gt; con= v8::Local&lt;v8::Context&gt;::New(isolate, context);
      v8::Local&lt;v8::Script&gt; script = v8::Local&lt;v8::Script&gt;::New(isolate, compiledScript);
  
      v8::Context::Scope context_scope(con);
  
      // Code to call the Javascript function f1
  
      result = script-&gt;Run(con).ToLocalChecked();
      v8::Local&lt;v8::Object&gt; global = con-&gt;Global();
      v8::Local&lt;v8::Value&gt; function_value1 = global-&gt;Get(v8::String::NewFromUtf8(isolate, "f1"));
      v8::Local&lt;v8::Function&gt; function1 = v8::Local&lt;v8::Function&gt;::Cast(function_value1);
  
      v8::Local&lt;v8::Value&gt; js_result1;
  
      js_result1 = function1-&gt;Call(con, global, 0, nullptr).ToLocalChecked();
  }
  
  void function2()
  {
      v8::Locker isolateLocker(isolate);
      v8::Isolate::Scope isolate_scope(isolate);
      v8::HandleScope handle_scope(isolate);
  
      v8::Local&lt;v8::Context&gt; con = v8::Local&lt;v8::Context&gt;::New(isolate, context);
      v8::Local&lt;v8::Script&gt; script = v8::Local&lt;v8::Script&gt;::New(isolate, compiledScript);
  
      v8::Context::Scope context_scope(con);
  
      // Code to call the Javascript function f2
  
      result = script-&gt;Run(con).ToLocalChecked();
      v8::Local&lt;v8::Object&gt; global = con-&gt;Global();
      v8::Local&lt;v8::Value&gt; function_value2 = global-&gt;Get(v8::String::NewFromUtf8(isolate, "f2"));
      v8::Local&lt;v8::Function&gt; function2 = v8::Local&lt;v8::Function&gt;::Cast(function_value2);
  
      v8::Local&lt;v8::Value&gt; js_result2;
  
      js_result2 = function2-&gt;Call(con, global, 0, nullptr).ToLocalChecked();
  
   }
  };
  void main(int argcs, char* argvs[])
  {
      V8::InitializeICUDefaultLocation(argvs[0]);
      V8::InitializeExternalStartupData(argvs[0]);
      Platform* platform = platform::CreateDefaultPlatform();
      V8::InitializePlatform(platform);
      V8::Initialize();
  
      Test test(jsFile);
      test.function1();
      test.function2();
  }
  </code></pre>
  
  <p>When the above JS code is executed, I get an error that says <strong>name</strong> is a property of undefined. My understanding is that variable <strong>test</strong> is not recognized in second function probably because each function is being executed in different contexts. If so, how do I make them execute in the same context? If not, can someone please help me understand and fix the issue?</p>
  
  <p>Any help will be greatly appreciated :)</p>