Home | History | Annotate | Download | only in src
      1 // Copyright 2014 PDFium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 
      7 #ifndef FXJSE_SCOPE_INLINE_H_
      8 #define FXJSE_SCOPE_INLINE_H_
      9 #include "runtime.h"
     10 #include "context.h"
     11 class CFXJSE_ScopeUtil_IsolateHandle {
     12  protected:
     13   v8::Isolate* m_isolate;
     14   v8::Locker m_locker;
     15   v8::Isolate::Scope m_iscope;
     16   v8::HandleScope m_hscope;
     17 
     18  public:
     19   explicit CFXJSE_ScopeUtil_IsolateHandle(v8::Isolate* pIsolate)
     20       : m_isolate(pIsolate),
     21         m_locker(pIsolate),
     22         m_iscope(pIsolate),
     23         m_hscope(pIsolate) {}
     24   v8::Isolate* GetIsolate() { return m_isolate; }
     25 
     26  private:
     27   CFXJSE_ScopeUtil_IsolateHandle(const CFXJSE_ScopeUtil_IsolateHandle&);
     28   void operator=(const CFXJSE_ScopeUtil_IsolateHandle&);
     29   void* operator new(size_t size);
     30   void operator delete(void*, size_t);
     31 };
     32 class CFXJSE_ScopeUtil_IsolateHandleRootContext {
     33   CFXJSE_ScopeUtil_IsolateHandle m_parent;
     34   v8::Context::Scope m_cscope;
     35 
     36  public:
     37   explicit CFXJSE_ScopeUtil_IsolateHandleRootContext(v8::Isolate* pIsolate)
     38       : m_parent(pIsolate),
     39         m_cscope(v8::Local<v8::Context>::New(
     40             pIsolate,
     41             CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext)) {}
     42 
     43  private:
     44   CFXJSE_ScopeUtil_IsolateHandleRootContext(
     45       const CFXJSE_ScopeUtil_IsolateHandleRootContext&);
     46   void operator=(const CFXJSE_ScopeUtil_IsolateHandleRootContext&);
     47   void* operator new(size_t size);
     48   void operator delete(void*, size_t);
     49 };
     50 class CFXJSE_ScopeUtil_IsolateHandleContext {
     51   CFXJSE_Context* m_context;
     52   CFXJSE_ScopeUtil_IsolateHandle m_parent;
     53   v8::Context::Scope m_cscope;
     54 
     55  public:
     56   explicit CFXJSE_ScopeUtil_IsolateHandleContext(CFXJSE_Context* pContext)
     57       : m_context(pContext),
     58         m_parent(pContext->m_pIsolate),
     59         m_cscope(v8::Local<v8::Context>::New(pContext->m_pIsolate,
     60                                              pContext->m_hContext)) {}
     61   v8::Isolate* GetIsolate() { return m_context->m_pIsolate; }
     62   v8::Local<v8::Context> GetLocalContext() {
     63     return v8::Local<v8::Context>::New(m_context->m_pIsolate,
     64                                        m_context->m_hContext);
     65   }
     66 
     67  private:
     68   CFXJSE_ScopeUtil_IsolateHandleContext(
     69       const CFXJSE_ScopeUtil_IsolateHandleContext&);
     70   void operator=(const CFXJSE_ScopeUtil_IsolateHandleContext&);
     71   void* operator new(size_t size);
     72   void operator delete(void*, size_t);
     73 };
     74 class CFXJSE_ScopeUtil_IsolateHandleRootOrNormalContext {
     75   CFXJSE_Context* m_context;
     76   CFXJSE_ScopeUtil_IsolateHandle m_parent;
     77   v8::Context::Scope m_cscope;
     78 
     79  public:
     80   explicit CFXJSE_ScopeUtil_IsolateHandleRootOrNormalContext(
     81       v8::Isolate* pIsolate,
     82       CFXJSE_Context* pContext)
     83       : m_context(pContext),
     84         m_parent(pIsolate),
     85         m_cscope(v8::Local<v8::Context>::New(
     86             pIsolate,
     87             pContext ? pContext->m_hContext
     88                      : CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext)) {}
     89   v8::Isolate* GetIsolate() { return m_parent.GetIsolate(); }
     90   v8::Local<v8::Context> GetLocalContext() {
     91     v8::Isolate* pIsolate = m_parent.GetIsolate();
     92     return v8::Local<v8::Context>::New(
     93         pIsolate, m_context
     94                       ? m_context->m_hContext
     95                       : CFXJSE_RuntimeData::Get(pIsolate)->m_hRootContext);
     96   }
     97 
     98  private:
     99   CFXJSE_ScopeUtil_IsolateHandleRootOrNormalContext(
    100       const CFXJSE_ScopeUtil_IsolateHandleRootOrNormalContext&);
    101   void operator=(const CFXJSE_ScopeUtil_IsolateHandleRootOrNormalContext&);
    102   void* operator new(size_t size);
    103   void operator delete(void*, size_t);
    104 };
    105 #endif
    106