| OLD | NEW |
| 1 // Copyright 2015 PDFium Authors. All rights reserved. | 1 // Copyright 2015 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "../../../core/include/fpdfapi/fpdf_parser.h" | 5 #include "../../../core/include/fpdfapi/fpdf_parser.h" |
| 6 #include "../../../testing/embedder_test.h" | 6 #include "../../../testing/embedder_test.h" |
| 7 #include "../../include/fsdk_mgr.h" | 7 #include "../../include/fsdk_mgr.h" |
| 8 #include "../../include/javascript/JS_Runtime.h" | 8 #include "../../include/javascript/JS_Runtime.h" |
| 9 #include "../../include/jsapi/fxjs_v8.h" | 9 #include "../../include/jsapi/fxjs_v8.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 const wchar_t kScript[] = L"fred = 7"; | 14 const wchar_t kScript[] = L"fred = 7"; |
| 15 | 15 |
| 16 } // namespace | 16 } // namespace |
| 17 | 17 |
| 18 class FXJSV8Embeddertest : public EmbedderTest { | 18 class FXJSV8Embeddertest : public EmbedderTest { |
| 19 public: | 19 public: |
| 20 FXJSV8Embeddertest() : m_pIsolate(nullptr) {} | 20 FXJSV8Embeddertest() |
| 21 ~FXJSV8Embeddertest() override {} | 21 : m_pArrayBufferAllocator(new FXJS_ArrayBufferAllocator) { |
| 22 v8::Isolate::CreateParams params; |
| 23 params.array_buffer_allocator = m_pArrayBufferAllocator.get(); |
| 24 m_pIsolate = v8::Isolate::New(params); |
| 25 } |
| 26 |
| 27 ~FXJSV8Embeddertest() override { m_pIsolate->Dispose(); } |
| 22 | 28 |
| 23 void SetUp() override { | 29 void SetUp() override { |
| 30 EmbedderTest::SetExternalIsolate(m_pIsolate); |
| 24 EmbedderTest::SetUp(); | 31 EmbedderTest::SetUp(); |
| 25 m_pAllocator.reset(new FXJS_ArrayBufferAllocator()); | |
| 26 | |
| 27 v8::Isolate::CreateParams params; | |
| 28 params.array_buffer_allocator = m_pAllocator.get(); | |
| 29 m_pIsolate = v8::Isolate::New(params); | |
| 30 | 32 |
| 31 v8::Isolate::Scope isolate_scope(m_pIsolate); | 33 v8::Isolate::Scope isolate_scope(m_pIsolate); |
| 32 v8::HandleScope handle_scope(m_pIsolate); | 34 v8::HandleScope handle_scope(m_pIsolate); |
| 33 FXJS_Initialize(0, nullptr); | |
| 34 FXJS_PerIsolateData::SetUp(m_pIsolate); | 35 FXJS_PerIsolateData::SetUp(m_pIsolate); |
| 35 FXJS_InitializeRuntime(m_pIsolate, nullptr, nullptr, m_pPersistentContext); | 36 FXJS_InitializeRuntime(m_pIsolate, nullptr, nullptr, m_pPersistentContext); |
| 36 } | 37 } |
| 37 | 38 |
| 38 void TearDown() override { | 39 void TearDown() override { |
| 39 FXJS_ReleaseRuntime(m_pIsolate, m_pPersistentContext); | 40 FXJS_ReleaseRuntime(m_pIsolate, m_pPersistentContext); |
| 41 m_pPersistentContext.Reset(); |
| 40 FXJS_Release(); | 42 FXJS_Release(); |
| 41 EmbedderTest::TearDown(); | 43 EmbedderTest::TearDown(); |
| 42 } | 44 } |
| 43 | 45 |
| 44 v8::Isolate* isolate() const { return m_pIsolate; } | 46 v8::Isolate* isolate() { return m_pIsolate; } |
| 45 v8::Local<v8::Context> GetV8Context() { | 47 v8::Local<v8::Context> GetV8Context() { |
| 46 return v8::Local<v8::Context>::New(m_pIsolate, m_pPersistentContext); | 48 return m_pPersistentContext.Get(m_pIsolate); |
| 47 } | 49 } |
| 48 | 50 |
| 49 private: | 51 private: |
| 52 nonstd::unique_ptr<FXJS_ArrayBufferAllocator> m_pArrayBufferAllocator; |
| 50 v8::Isolate* m_pIsolate; | 53 v8::Isolate* m_pIsolate; |
| 51 v8::Global<v8::Context> m_pPersistentContext; | 54 v8::Global<v8::Context> m_pPersistentContext; |
| 52 nonstd::unique_ptr<v8::ArrayBuffer::Allocator> m_pAllocator; | |
| 53 }; | 55 }; |
| 54 | 56 |
| 55 TEST_F(FXJSV8Embeddertest, Getters) { | 57 TEST_F(FXJSV8Embeddertest, Getters) { |
| 56 v8::Isolate::Scope isolate_scope(isolate()); | 58 v8::Isolate::Scope isolate_scope(isolate()); |
| 57 v8::HandleScope handle_scope(isolate()); | 59 v8::HandleScope handle_scope(isolate()); |
| 58 v8::Context::Scope context_scope(GetV8Context()); | 60 v8::Context::Scope context_scope(GetV8Context()); |
| 59 | 61 |
| 60 FXJSErr error; | 62 FXJSErr error; |
| 61 CFX_WideString wsInfo; | 63 CFX_WideString wsInfo; |
| 62 CFX_WideString wsScript(kScript); | 64 CFX_WideString wsScript(kScript); |
| 63 int sts = FXJS_Execute(isolate(), nullptr, kScript, wcslen(kScript), &error); | 65 int sts = FXJS_Execute(isolate(), nullptr, kScript, wcslen(kScript), &error); |
| 64 EXPECT_EQ(0, sts); | 66 EXPECT_EQ(0, sts); |
| 65 | 67 |
| 66 v8::Local<v8::Object> This = FXJS_GetThisObj(isolate()); | 68 v8::Local<v8::Object> This = FXJS_GetThisObj(isolate()); |
| 67 v8::Local<v8::Value> fred = FXJS_GetObjectElement(isolate(), This, L"fred"); | 69 v8::Local<v8::Value> fred = FXJS_GetObjectElement(isolate(), This, L"fred"); |
| 68 EXPECT_TRUE(fred->IsNumber()); | 70 EXPECT_TRUE(fred->IsNumber()); |
| 69 } | 71 } |
| OLD | NEW |