How to use the same context across multiple functions in v8 Javascript?

1.5k views Asked by At

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.

My JS Code:

var test = [];

function f1()
{
    var obj = {};
    obj.name = "Testing";
    test.push(obj);
}

function f2()
{
    var value = test[0].name;
}

My C++ code:

class Test 
{

  v8::Persistent<v8::Script> compiledScript;
  v8::Local<v8::Value> result;
  v8::Isolate* isolate;
  v8::Persistent<v8::Context> 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<v8::Context> con = v8::Context::New(isolate, nullptr, binding_template);
      con->SetAlignedPointerInEmbedderData(0, &binder);

      context.Reset(isolate, con);
      v8::Context::Scope context_scope(con);

      std::string source_file = LoadFile(filePath);
      v8::Local<v8::String> sourceScript = v8::String::NewFromUtf8(isolate, source_file.c_str(), v8::NewStringType::kNormal).ToLocalChecked();
      v8::Local<v8::Script> 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<v8::Context> con= v8::Local<v8::Context>::New(isolate, context);
    v8::Local<v8::Script> script = v8::Local<v8::Script>::New(isolate, compiledScript);

    v8::Context::Scope context_scope(con);

    // Code to call the Javascript function f1

    result = script->Run(con).ToLocalChecked();
    v8::Local<v8::Object> global = con->Global();
    v8::Local<v8::Value> function_value1 = global->Get(v8::String::NewFromUtf8(isolate, "f1"));
    v8::Local<v8::Function> function1 = v8::Local<v8::Function>::Cast(function_value1);

    v8::Local<v8::Value> js_result1;

    js_result1 = function1->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<v8::Context> con = v8::Local<v8::Context>::New(isolate, context);
    v8::Local<v8::Script> script = v8::Local<v8::Script>::New(isolate, compiledScript);

    v8::Context::Scope context_scope(con);

    // Code to call the Javascript function f2

    result = script->Run(con).ToLocalChecked();
    v8::Local<v8::Object> global = con->Global();
    v8::Local<v8::Value> function_value2 = global->Get(v8::String::NewFromUtf8(isolate, "f2"));
    v8::Local<v8::Function> function2 = v8::Local<v8::Function>::Cast(function_value2);

    v8::Local<v8::Value> js_result2;

    js_result2 = function2->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();
}

When the above JS code is executed, I get an error that says name is a property of undefined. My understanding is that variable test 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?

Any help will be greatly appreciated :)

1

There are 1 answers

0
Kavin Mani On BEST ANSWER

I figured out myself. The mistake was that I was running the script each time in all functions. Rather, I should run just once in the constructor like compiling.