0

I have a global variable in my js code and I'm trying to set it to a different value from c++. I've tried:

    v8::Isolate* isolate = v8::Isolate::GetCurrent();
    v8::Local<v8::Context> ctx = isolate->GetCurrentContext();

    v8::Local<v8::Value> value= WrapV8::NewString(isolate, "true");
    ctx->Global()->Set(ctx, v8::String::NewFromUtf8(isolate, "globalVariable",
      v8::NewStringType::kNormal).ToLocalChecked(), value);

However this doesn't change the global variable.

Most of the other examples of this are old and sometimes deprecated.

My c code:

 void SignalHandler::intrpt(int signum) {
    printf("callign intrpt");
    v8::Isolate* isolate = v8::Isolate::GetCurrent();
    v8::Local<v8::Context> context = isolate->GetCurrentContext();
    v8::Local<v8::Value> boolC = WrapV8::NewString(isolate, "true");
    context->Global()->Set(context, v8::String::NewFromUtf8(isolate, "ctrC", v8::NewStringType::kNormal).ToLocalChecked(),
      boolC);
  }

void SignalHandler::JsSetUpSignals(const v8::FunctionCallbackInfo<v8::Value>& args) {
    v8::Isolate* isolate = args.GetIsolate();
    v8::HandleScope handleScope(isolate);
    v8::Local<v8::Context> context = isolate->GetCurrentContext();
    v8::Local<v8::Value> val = v8::Number::New(isolate, 1);
    (void)signal(SIGINT, intrpt);
    printf("got here\n");

  }

void SignalHandler::JsConstructor(const v8::FunctionCallbackInfo<v8::Value>& args) {
    if (WrappedObject::CallConstructor(args)) return;
    v8::Isolate* isolate = args.GetIsolate();
    SignalHandler* signalHandler = new SignalHandler();
    signalHandler->wrap(isolate, args);
  }

My JS code:

signal.startSignalWatching = () => {
  var thread1 = new Thread(
    () => {
      SignalHandler = new internals.SignalHandler()
      global.ctrC = false;
      SignalHandler.setUpSignals()
      waitTimer();
    }
  );


 function waitTimer() {
    if (global.ctrC != false) {
      console.log("found it")
      return;
    } else {
      console.log("waiting it", global.ctrC, Object.keys(global).length)
      setTimeout(waitTimer, 800); // try again in 300 milliseconds
      return;
    }
  }

The value of global.ctrC never changes. global is the list of global variables. setTimeout is a function from https://www.w3schools.com/jsref/met_win_settimeout.asp. The function signal.startSignalWatching calls on the function from the c++ code JsSetUpSignals() which is where the isolate and context are defined.

I know at one point that the interrupt is firing as the printf message is outputted into the console.

Alex
  • 131
  • 2
  • 13
  • The code looks fine at first glance. Please include a complete example of what isn't working. See: https://stackoverflow.com/help/mcve – jmrk Feb 04 '20 at 17:04
  • That's still not a complete example (e.g., where does `global` come from? Where are you entering Isolate and Context? How is `setTimeout` implemented? Are you sure the `intrpt` function is executed?). Please do read https://stackoverflow.com/help/mcve, in particular the "reproducible" part. – jmrk Feb 04 '20 at 18:06

0 Answers0