title: Android JNI JavascriptCore JSEvaluateScript giving Parse Error randomly
body: <p>I am new to JSCore and JNI (using c++ from Android perspective). I want to evaluate a javascript through JSEvaluateScript function. When I start the app, results are ok, but after i pause the app or re-run the script while the app is open, it randomly gives "Syntax Error: Pasre Error". The strange thing is that the same script runs ok on restart (which means that the script is ok). Also the same script runs in iOS code perfectly with JSCore. </p>
  
  <p>I create a global context every time before running a script and cache it like this:</p>
  
  <pre><code>static JSGlobalContextRef globalContext = JSGlobalContextCreate(NULL);
  </code></pre>
  
  <p>While I run the script like this:</p>
  
  <pre><code>void runJS(JNIEnv* env, jstring jsString)
  {
      const char *cString = env-&gt;GetStringUTFChars(jsString, 0);                     
      JSStringRef scriptJS = JSStringCreateWithUTF8CString(cString);
      JSValueRef exception = NULL;
      if(JSCheckScriptSyntax(globalContext,scriptJS,NULL,0,&amp;exception))
      {
          JSValueRef returnedValue = JSEvaluateScript(globalContext, scriptJS, NULL, NULL, 0, &amp;exception);
  
          if (exception)
          {
              LOGD("runJS: JSEvaluateScript exception");
              JSValuePrint(globalContext, exception, NULL);
          }
      }
      else if (exception)
      {
          LOGD("runJS: JSCheckScriptSyntax exception");
          JSValuePrint(globalContext, exception, NULL);
      }
  
      env-&gt;ReleaseStringUTFChars(jsString, cString);
      JSStringRelease(scriptJS);
  }
  </code></pre>
  
  <p>I have banged my head to the point that I am thinking to replace the complete Javascript engine with an alternative one. Any kind of help would be great..</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>It turns out the I was calling runJS in a thread which was creating issues. But I cannot run it on main thread as I have to pause the operation of JS parsing by sleeping the thread, and sleeping the main thread would sleep everything. Any ideas?</p>