CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
    : m_pApp(pApp),
      m_pDocument(nullptr),
      m_bBlocking(FALSE),
      m_isolate(nullptr),
      m_isolateManaged(false) {
#ifndef PDF_ENABLE_XFA
  IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform;
  if (pPlatform->version <= 2) {
    unsigned int embedderDataSlot = 0;
    v8::Isolate* pExternalIsolate = nullptr;
    if (pPlatform->version == 2) {
      pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate);
      embedderDataSlot = pPlatform->m_v8EmbedderSlot;
    }
    FXJS_Initialize(embedderDataSlot, pExternalIsolate);
  }
  m_isolateManaged = FXJS_GetIsolate(&m_isolate);
#else
  if (CPDFXFA_App::GetInstance()->GetJSERuntime()) {
    // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate.
    m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime();
  } else {
    IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform;
    if (pPlatform->version <= 2) {
      unsigned int embedderDataSlot = 0;
      v8::Isolate* pExternalIsolate = nullptr;
      if (pPlatform->version == 2) {
        pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate);
        embedderDataSlot = pPlatform->m_v8EmbedderSlot;
      }
      FXJS_Initialize(embedderDataSlot, pExternalIsolate);
    }
    m_isolateManaged = FXJS_GetIsolate(&m_isolate);
  }

  v8::Isolate* isolate = m_isolate;
  v8::Isolate::Scope isolate_scope(isolate);
  v8::HandleScope handle_scope(isolate);
  if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) {
    CJS_Context* pContext = (CJS_Context*)NewContext();
    FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects);
    ReleaseContext(pContext);
    return;
  }
#endif

  if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0)
    DefineJSObjects();

#ifdef PDF_ENABLE_XFA
  CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE);
#endif

  CJS_Context* pContext = (CJS_Context*)NewContext();
  FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects);
  ReleaseContext(pContext);
}
Beispiel #2
0
CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
    : m_pApp(pApp),
      m_pDocument(NULL),
      m_bBlocking(FALSE),
      m_isolate(NULL),
      m_isolateManaged(false) {
  unsigned int embedderDataSlot = 0;
  if (m_pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) {
    m_isolate = reinterpret_cast<v8::Isolate*>(
        m_pApp->GetFormFillInfo()->m_pJsPlatform->m_isolate);
    embedderDataSlot = pApp->GetFormFillInfo()->m_pJsPlatform->m_v8EmbedderSlot;
  }
  if (!m_isolate) {
    m_pArrayBufferAllocator.reset(new FXJS_ArrayBufferAllocator());

    v8::Isolate::CreateParams params;
    params.array_buffer_allocator = m_pArrayBufferAllocator.get();
    m_isolate = v8::Isolate::New(params);
    m_isolateManaged = true;
  }

  FXJS_Initialize(embedderDataSlot);
  DefineJSObjects();

  CJS_Context* pContext = (CJS_Context*)NewContext();
  FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context);
  ReleaseContext(pContext);
}
  void SetUp() override {
    EmbedderTest::SetExternalIsolate(m_pIsolate);
    EmbedderTest::SetUp();

    v8::Isolate::Scope isolate_scope(m_pIsolate);
    v8::HandleScope handle_scope(m_pIsolate);
    FXJS_PerIsolateData::SetUp(m_pIsolate);
    FXJS_InitializeRuntime(m_pIsolate, nullptr, &m_pPersistentContext,
                           &m_StaticObjects);
  }
TEST_F(FXJSV8EmbedderTest, MultipleRutimes) {
  v8::Isolate::Scope isolate_scope(isolate());
  v8::HandleScope handle_scope(isolate());

  v8::Global<v8::Context> global_context1;
  std::vector<v8::Global<v8::Object>*> static_objects1;
  FXJS_InitializeRuntime(isolate(), nullptr, &global_context1,
                         &static_objects1);

  v8::Global<v8::Context> global_context2;
  std::vector<v8::Global<v8::Object>*> static_objects2;
  FXJS_InitializeRuntime(isolate(), nullptr, &global_context2,
                         &static_objects2);

  v8::Context::Scope context_scope(GetV8Context());
  ExecuteInCurrentContext(CFX_WideString(kScript0));
  CheckAssignmentInCurrentContext(kExpected0);

  {
    v8::Local<v8::Context> context1 =
        v8::Local<v8::Context>::New(isolate(), global_context1);
    v8::Context::Scope context_scope1(context1);
    ExecuteInCurrentContext(CFX_WideString(kScript1));
    CheckAssignmentInCurrentContext(kExpected1);
  }
  FXJS_ReleaseRuntime(isolate(), &global_context1, &static_objects1);

  {
    v8::Local<v8::Context> context2 =
        v8::Local<v8::Context>::New(isolate(), global_context2);
    v8::Context::Scope context_scope2(context2);
    ExecuteInCurrentContext(CFX_WideString(kScript2));
    CheckAssignmentInCurrentContext(kExpected2);
  }
  FXJS_ReleaseRuntime(isolate(), &global_context2, &static_objects2);

  CheckAssignmentInCurrentContext(kExpected0);
}
Beispiel #5
0
CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
    : m_pApp(pApp),
      m_pDocument(NULL),
      m_bBlocking(FALSE),
      m_isolate(NULL),
      m_isolateManaged(false) {
  IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform;
  if (pPlatform->version <= 2) {
    unsigned int embedderDataSlot = 0;
    v8::Isolate* pExternalIsolate = nullptr;
    if (pPlatform->version == 2) {
      pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate);
      embedderDataSlot = pPlatform->m_v8EmbedderSlot;
    }
    FXJS_Initialize(embedderDataSlot, pExternalIsolate);
  }
  m_isolateManaged = FXJS_GetIsolate(&m_isolate);
  if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0)
    DefineJSObjects();

  CJS_Context* pContext = (CJS_Context*)NewContext();
  FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects);
  ReleaseContext(pContext);
}