bool JsHttpRequestProcessor::Process(HttpRequest* request) { // Create a handle scope to keep the temporary object references. HandleScope handle_scope(GetIsolate()); v8::Local<v8::Context> context = v8::Local<v8::Context>::New(GetIsolate(), context_); // Enter this processor's context so all the remaining operations // take place there Context::Scope context_scope(context); // Wrap the C++ request object in a JavaScript wrapper Local<Object> request_obj = WrapRequest(request); // Set up an exception handler before calling the Process function TryCatch try_catch(GetIsolate()); // Invoke the process function, giving the global object as 'this' // and one argument, the request. const int argc = 1; Local<Value> argv[argc] = {request_obj}; v8::Local<v8::Function> process = v8::Local<v8::Function>::New(GetIsolate(), process_); Local<Value> result; if (!process->Call(context, context->Global(), argc, argv).ToLocal(&result)) { String::Utf8Value error(try_catch.Exception()); Log(*error); return false; } else { return true; } }
FX_BOOL CJS_Runtime::GetValueByName(const CFX_ByteStringC& utf8Name, CFXJSE_Value* pValue) { const FX_CHAR* name = utf8Name.c_str(); v8::Isolate::Scope isolate_scope(GetIsolate()); v8::HandleScope handle_scope(GetIsolate()); v8::Local<v8::Context> old_context = GetIsolate()->GetCurrentContext(); v8::Local<v8::Context> context = v8::Local<v8::Context>::New(GetIsolate(), m_context); v8::Context::Scope context_scope(context); // Caution: We're about to hand to XFA an object that in order to invoke // methods will require that the current v8::Context always has a pointer // to a CJS_Runtime in its embedder data slot. Unfortunately, XFA creates // its own v8::Context which has not initialized the embedder data slot. // Do so now. // TODO(tsepez): redesign PDF-side objects to not rely on v8::Context's // embedder data slots, and/or to always use the right context. FXJS_SetRuntimeForV8Context(old_context, this); v8::Local<v8::Value> propvalue = context->Global()->Get(v8::String::NewFromUtf8( GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); if (propvalue.IsEmpty()) { pValue->SetUndefined(); return FALSE; } pValue->ForceSetValue(propvalue); return TRUE; }
bool JsHttpRequestProcessor::InstallMaps(map<string, string>* opts, map<string, string>* output) { HandleScope handle_scope(GetIsolate()); // Wrap the map object in a JavaScript wrapper Local<Object> opts_obj = WrapMap(opts); v8::Local<v8::Context> context = v8::Local<v8::Context>::New(GetIsolate(), context_); // Set the options object as a property on the global object. context->Global() ->Set(context, String::NewFromUtf8(GetIsolate(), "options", NewStringType::kNormal) .ToLocalChecked(), opts_obj) .FromJust(); Local<Object> output_obj = WrapMap(output); context->Global() ->Set(context, String::NewFromUtf8(GetIsolate(), "output", NewStringType::kNormal) .ToLocalChecked(), output_obj) .FromJust(); return true; }
bool JsHttpRequestProcessor::ExecuteScript(Local<String> script) { HandleScope handle_scope(GetIsolate()); // We're just about to compile the script; set up an error handler to // catch any exceptions the script might throw. TryCatch try_catch(GetIsolate()); Local<Context> context(GetIsolate()->GetCurrentContext()); // Compile the script and check for errors. Local<Script> compiled_script; if (!Script::Compile(context, script).ToLocal(&compiled_script)) { String::Utf8Value error(try_catch.Exception()); Log(*error); // The script failed to compile; bail out. return false; } // Run the script! Local<Value> result; if (!compiled_script->Run(context).ToLocal(&result)) { // The TryCatch above is still in effect and will have caught the error. String::Utf8Value error(try_catch.Exception()); Log(*error); // Running the script failed; bail out. return false; } return true; }
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); }
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( v8::FunctionCallback callback) { const JsContext context(shared_from_this()); // Note: we are leaking this weak pointer, no obvious way to destroy it when // it's no longer used std::weak_ptr<JsEngine>* data = new std::weak_ptr<JsEngine>(shared_from_this()); v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(GetIsolate() , callback, v8::External::New(GetIsolate(), data)); return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); }
FX_BOOL util::scand(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError) { v8::Isolate* isolate = GetIsolate(cc); int iSize = params.size(); if (iSize < 2) return FALSE; CFX_WideString sFormat = params[0].ToCFXWideString(); CFX_WideString sDate = params[1].ToCFXWideString(); double dDate = JS_GetDateTime(); if (sDate.GetLength() > 0) { FX_BOOL bWrongFormat = FALSE; dDate = CJS_PublicMethods::MakeRegularDate(sDate,sFormat,bWrongFormat); } if (!JS_PortIsNan(dDate)) { CJS_Date date(isolate,dDate); vRet = date; } else { vRet.SetNull(); } return TRUE; }
FX_BOOL util::scand(OBJ_METHOD_PARAMS) { v8::Isolate* isolate = GetIsolate(cc); int iSize = params.size(); if (iSize < 2) return FALSE; CFX_WideString sFormat = params[0].operator CFX_WideString(); CFX_WideString sDate = params[1].operator CFX_WideString(); double dDate = JS_GetDateTime(); if (sDate.GetLength() > 0) { FX_BOOL bWrongFormat = FALSE; dDate = CJS_PublicMethods::MakeRegularDate(sDate,sFormat,bWrongFormat); } if (!JS_PortIsNan(dDate)) { CJS_Date date(isolate,dDate); vRet = date; } else { vRet.SetNull(); } return TRUE; }
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); }
FX_BOOL Document::getField(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError) { CJS_Context* pContext = (CJS_Context*)cc; if (params.size() < 1) { sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); return FALSE; } CFX_WideString wideName = params[0].ToCFXWideString(); CPDFSDK_InterForm* pInterForm = m_pDocument->GetInterForm(); CPDF_InterForm* pPDFForm = pInterForm->GetInterForm(); if (pPDFForm->CountFields(wideName) <= 0) { vRet.SetNull(); return TRUE; } CJS_Runtime* pRuntime = pContext->GetJSRuntime(); JSFXObject pFieldObj = JS_NewFxDynamicObj(*pRuntime, pContext, JS_GetObjDefnID(*pRuntime, L"Field")); v8::Isolate* isolate = GetIsolate(cc); CJS_Field* pJSField = (CJS_Field*)JS_GetPrivate(isolate,pFieldObj); Field* pField = (Field *)pJSField->GetEmbedObject(); pField->AttachField(this, wideName); vRet = pJSField; return TRUE; }
void CJS_Runtime::DefineJSObjects() { v8::Isolate::Scope isolate_scope(GetIsolate()); v8::HandleScope handle_scope(GetIsolate()); v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); v8::Context::Scope context_scope(context); // The call order determines the "ObjDefID" assigned to each class. // ObjDefIDs 0 - 2 CJS_Border::DefineJSObjects(this, FXJSOBJTYPE_STATIC); CJS_Display::DefineJSObjects(this, FXJSOBJTYPE_STATIC); CJS_Font::DefineJSObjects(this, FXJSOBJTYPE_STATIC); // ObjDefIDs 3 - 5 CJS_Highlight::DefineJSObjects(this, FXJSOBJTYPE_STATIC); CJS_Position::DefineJSObjects(this, FXJSOBJTYPE_STATIC); CJS_ScaleHow::DefineJSObjects(this, FXJSOBJTYPE_STATIC); // ObjDefIDs 6 - 8 CJS_ScaleWhen::DefineJSObjects(this, FXJSOBJTYPE_STATIC); CJS_Style::DefineJSObjects(this, FXJSOBJTYPE_STATIC); CJS_Zoomtype::DefineJSObjects(this, FXJSOBJTYPE_STATIC); // ObjDefIDs 9 - 11 CJS_App::DefineJSObjects(this, FXJSOBJTYPE_STATIC); CJS_Color::DefineJSObjects(this, FXJSOBJTYPE_STATIC); CJS_Console::DefineJSObjects(this, FXJSOBJTYPE_STATIC); // ObjDefIDs 12 - 14 CJS_Document::DefineJSObjects(this, FXJSOBJTYPE_GLOBAL); CJS_Event::DefineJSObjects(this, FXJSOBJTYPE_STATIC); CJS_Field::DefineJSObjects(this, FXJSOBJTYPE_DYNAMIC); // ObjDefIDs 15 - 17 CJS_Global::DefineJSObjects(this, FXJSOBJTYPE_STATIC); CJS_Icon::DefineJSObjects(this, FXJSOBJTYPE_DYNAMIC); CJS_Util::DefineJSObjects(this, FXJSOBJTYPE_STATIC); // ObjDefIDs 18 - 20 (these can't fail, return void). CJS_PublicMethods::DefineJSObjects(this); CJS_GlobalConsts::DefineJSObjects(this); CJS_GlobalArrays::DefineJSObjects(this); // ObjDefIDs 21 - 23. CJS_TimerObj::DefineJSObjects(this, FXJSOBJTYPE_DYNAMIC); CJS_PrintParamsObj::DefineJSObjects(this, FXJSOBJTYPE_DYNAMIC); CJS_Annot::DefineJSObjects(this, FXJSOBJTYPE_DYNAMIC); }
FX_BOOL Document::resetForm(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError) { ASSERT(m_pDocument != NULL); if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) || m_pDocument->GetPermissions(FPDFPERM_ANNOT_FORM) || m_pDocument->GetPermissions(FPDFPERM_FILL_FORM))) return FALSE; CPDFSDK_InterForm* pInterForm = (CPDFSDK_InterForm*)m_pDocument->GetInterForm(); ASSERT(pInterForm != NULL); CPDF_InterForm* pPDFForm = pInterForm->GetInterForm(); ASSERT(pPDFForm != NULL); v8::Isolate* isolate = GetIsolate(cc); CJS_Array aName(isolate); if (params.size() > 0) { switch (params[0].GetType()) { default: aName.Attach(params[0].ToV8Array()); break; case VT_string: aName.SetElement(0,params[0]); break; } CFX_PtrArray aFields; for (int i=0,isz=aName.GetLength(); i<isz; i++) { CJS_Value valElement(isolate); aName.GetElement(i,valElement); CFX_WideString swVal = valElement.ToCFXWideString(); for (int j=0,jsz=pPDFForm->CountFields(swVal); j<jsz; j++) { aFields.Add((void*)pPDFForm->GetField(j,swVal)); } } if (aFields.GetSize() > 0) { pPDFForm->ResetForm(aFields, TRUE, TRUE); m_pDocument->SetChangeMark(); } } else { pPDFForm->ResetForm(TRUE); m_pDocument->SetChangeMark(); } return TRUE; }
CJS_Runtime::~CJS_Runtime() { NotifyObservedPtrs(); ReleaseEngine(); if (m_isolateManaged) { GetIsolate()->Dispose(); SetIsolate(nullptr); } }
FX_BOOL Document::info(IFXJS_Context* cc, CJS_PropValue& vp, CFX_WideString& sError) { ASSERT(m_pDocument != NULL); CPDF_Dictionary* pDictionary = m_pDocument->GetDocument()->GetInfo(); if (!pDictionary)return FALSE; CFX_WideString cwAuthor = pDictionary->GetUnicodeText("Author"); CFX_WideString cwTitle = pDictionary->GetUnicodeText("Title"); CFX_WideString cwSubject = pDictionary->GetUnicodeText("Subject"); CFX_WideString cwKeywords = pDictionary->GetUnicodeText("Keywords"); CFX_WideString cwCreator = pDictionary->GetUnicodeText("Creator"); CFX_WideString cwProducer = pDictionary->GetUnicodeText("Producer"); CFX_WideString cwCreationDate = pDictionary->GetUnicodeText("CreationDate"); CFX_WideString cwModDate = pDictionary->GetUnicodeText("ModDate"); CFX_WideString cwTrapped = pDictionary->GetUnicodeText("Trapped"); v8::Isolate* isolate = GetIsolate(cc); if (vp.IsGetting()) { CJS_Context* pContext = (CJS_Context *)cc; CJS_Runtime* pRuntime = pContext->GetJSRuntime(); JSFXObject pObj = JS_NewFxDynamicObj(*pRuntime, pContext, -1); JS_PutObjectString(isolate, pObj, L"Author", cwAuthor.c_str()); JS_PutObjectString(isolate, pObj, L"Title", cwTitle.c_str()); JS_PutObjectString(isolate, pObj, L"Subject", cwSubject.c_str()); JS_PutObjectString(isolate, pObj, L"Keywords", cwKeywords.c_str()); JS_PutObjectString(isolate, pObj, L"Creator", cwCreator.c_str()); JS_PutObjectString(isolate, pObj, L"Producer", cwProducer.c_str()); JS_PutObjectString(isolate, pObj, L"CreationDate", cwCreationDate.c_str()); JS_PutObjectString(isolate, pObj, L"ModDate", cwModDate.c_str()); JS_PutObjectString(isolate, pObj, L"Trapped", cwTrapped.c_str()); // It's to be compatible to non-standard info dictionary. FX_POSITION pos = pDictionary->GetStartPos(); while(pos) { CFX_ByteString bsKey; CPDF_Object* pValueObj = pDictionary->GetNextElement(pos, bsKey); CFX_WideString wsKey = CFX_WideString::FromUTF8(bsKey, bsKey.GetLength()); if((pValueObj->GetType()==PDFOBJ_STRING) || (pValueObj->GetType()==PDFOBJ_NAME) ) JS_PutObjectString(isolate, pObj, wsKey.c_str(), pValueObj->GetUnicodeText().c_str()); if(pValueObj->GetType()==PDFOBJ_NUMBER) JS_PutObjectNumber(isolate,pObj, wsKey.c_str(), (float)pValueObj->GetNumber()); if(pValueObj->GetType()==PDFOBJ_BOOLEAN) JS_PutObjectBoolean(isolate,pObj, wsKey.c_str(), (bool)pValueObj->GetInteger()); } vp << pObj; return TRUE; } else { return TRUE; } }
SMJS_Plugin *LoadPlugin(const char *dir){ auto plugin = SMJS_Plugin::GetPluginByDir(dir); if(plugin != NULL) return plugin; bool isSandboxed = true; for(auto it = trustedPlugins.begin(); it != trustedPlugins.end(); ++it){ if(strcmp(dir, it->c_str()) == 0){ isSandboxed = false; break; } } plugin = new SMJS_Plugin(isSandboxed); char path[512]; smutils->BuildPath(Path_SM, path, sizeof(path), "plugins.js/%s", dir); plugin->SetDir(dir); plugin->SetPath(path); plugin->LoadModules(); plugin->CheckApi(); if(!plugin->LoadFile("Main.js", true)){ delete plugin; return NULL; } // Late loading if(smutils->IsMapRunning()){ HandleScope handle_scope(plugin->GetIsolate()); Context::Scope context_scope(plugin->GetContext()); auto hooks = plugin->GetHooks("OnMapStart"); for(auto it = hooks->begin(); it != hooks->end(); ++it){ (*it)->Call(plugin->GetContext()->Global(), 0, NULL); } for(int i = 0; i < sizeof(clients) / sizeof(clients[0]); ++i){ if(clients[i] == NULL) continue; v8::Handle<v8::Value> arg = clients[i]->GetWrapper(plugin); hooks = plugin->GetHooks("OnClientConnected"); for(auto it = hooks->begin(); it != hooks->end(); ++it){ (*it)->Call(plugin->GetContext()->Global(), 1, &arg); } if(clients[i]->inGame){ hooks = plugin->GetHooks("OnClientPutInGame"); for(auto it = hooks->begin(); it != hooks->end(); ++it){ (*it)->Call(plugin->GetContext()->Global(), 1, &arg); } } } } return plugin; }
// Execute the script and fetch the Process method. bool JsHttpRequestProcessor::Initialize(map<string, string>* opts, map<string, string>* output) { // Create a handle scope to hold the temporary references. HandleScope handle_scope(GetIsolate()); // Create a template for the global object where we set the // built-in global functions. Local<ObjectTemplate> global = ObjectTemplate::New(GetIsolate()); global->Set(String::NewFromUtf8(GetIsolate(), "log", NewStringType::kNormal) .ToLocalChecked(), FunctionTemplate::New(GetIsolate(), LogCallback)); // Each processor gets its own context so different processors don't // affect each other. Context::New returns a persistent handle which // is what we need for the reference to remain after we return from // this method. That persistent handle has to be disposed in the // destructor. v8::Local<v8::Context> context = Context::New(GetIsolate(), NULL, global); context_.Reset(GetIsolate(), context); // Enter the new context so all the following operations take place // within it. Context::Scope context_scope(context); // Make the options mapping available within the context if (!InstallMaps(opts, output)) return false; // Compile and run the script if (!ExecuteScript(script_)) return false; // The script compiled and ran correctly. Now we fetch out the // Process function from the global object. Local<String> process_name = String::NewFromUtf8(GetIsolate(), "Process", NewStringType::kNormal) .ToLocalChecked(); Local<Value> process_val; // If there is no Process function, or if it is not a function, // bail out if (!context->Global()->Get(context, process_name).ToLocal(&process_val) || !process_val->IsFunction()) { return false; } // It is a function; cast it to a Function Local<Function> process_fun = Local<Function>::Cast(process_val); // Store the function in a Global handle, since we also want // that to remain after this call returns process_.Reset(GetIsolate(), process_fun); // All done; all went well return true; }
ByteString CFX_V8::ToByteString(v8::Local<v8::Value> pValue) { if (pValue.IsEmpty()) return ByteString(); v8::Local<v8::Context> context = m_pIsolate->GetCurrentContext(); v8::MaybeLocal<v8::String> maybe_string = pValue->ToString(context); if (maybe_string.IsEmpty()) return ByteString(); v8::String::Utf8Value s(GetIsolate(), maybe_string.ToLocalChecked()); return ByteString(*s); }
bool CJS_Runtime::GetValueByName(const CFX_ByteStringC& utf8Name, CFXJSE_Value* pValue) { const FX_CHAR* name = utf8Name.c_str(); v8::Isolate::Scope isolate_scope(GetIsolate()); v8::HandleScope handle_scope(GetIsolate()); v8::Local<v8::Context> context = NewLocalContext(); v8::Context::Scope context_scope(context); v8::Local<v8::Value> propvalue = context->Global()->Get(v8::String::NewFromUtf8( GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); if (propvalue.IsEmpty()) { pValue->SetUndefined(); return false; } pValue->ForceSetValue(propvalue); return true; }
CJS_Runtime::~CJS_Runtime() { for (auto* obs : m_observers) obs->OnDestroyed(); m_ContextArray.clear(); m_ConstArrays.clear(); FXJS_ReleaseRuntime(GetIsolate(), &m_context, &m_StaticObjects); m_context.Reset(); if (m_isolateManaged) m_isolate->Dispose(); }
FX_BOOL CJS_Runtime::InitJSObjects() { v8::Isolate::Scope isolate_scope(GetIsolate()); v8::HandleScope handle_scope(GetIsolate()); v8::Handle<v8::Context> context = v8::Context::New(GetIsolate()); v8::Context::Scope context_scope(context); //0 - 8 if (CJS_Border::Init(*this, JS_STATIC) < 0) return FALSE; if (CJS_Display::Init(*this, JS_STATIC) < 0) return FALSE; if (CJS_Font::Init(*this, JS_STATIC) < 0) return FALSE; if (CJS_Highlight::Init(*this, JS_STATIC) < 0) return FALSE; if (CJS_Position::Init(*this, JS_STATIC) < 0) return FALSE; if (CJS_ScaleHow::Init(*this, JS_STATIC) < 0) return FALSE; if (CJS_ScaleWhen::Init(*this, JS_STATIC) < 0) return FALSE; if (CJS_Style::Init(*this, JS_STATIC) < 0) return FALSE; if (CJS_Zoomtype::Init(*this, JS_STATIC) < 0) return FALSE; //9 - 11 if (CJS_App::Init(*this, JS_STATIC) < 0) return FALSE; if (CJS_Color::Init(*this, JS_STATIC) < 0) return FALSE; if (CJS_Console::Init(*this, JS_STATIC) < 0) return FALSE; //12 - 14 if (CJS_Document::Init(*this, JS_DYNAMIC) < 0) return FALSE; if (CJS_Event::Init(*this, JS_STATIC) < 0) return FALSE; if (CJS_Field::Init(*this, JS_DYNAMIC) < 0) return FALSE; //15 - 17 if (CJS_Global::Init(*this, JS_STATIC) < 0) return FALSE; if (CJS_Icon::Init(*this, JS_DYNAMIC) < 0) return FALSE; if (CJS_Util::Init(*this, JS_STATIC) < 0) return FALSE; if (CJS_PublicMethods::Init(*this) < 0) return FALSE; if (CJS_GlobalConsts::Init(*this) < 0) return FALSE; if (CJS_GlobalArrays::Init(*this) < 0) return FALSE; if (CJS_TimerObj::Init(*this, JS_DYNAMIC) < 0) return FALSE; if (CJS_PrintParamsObj::Init(*this, JS_DYNAMIC) <0) return FALSE; return TRUE; }
bool CJS_Runtime::SetValueByName(const CFX_ByteStringC& utf8Name, CFXJSE_Value* pValue) { if (utf8Name.IsEmpty() || !pValue) return false; const FX_CHAR* name = utf8Name.c_str(); v8::Isolate* pIsolate = GetIsolate(); v8::Isolate::Scope isolate_scope(pIsolate); v8::HandleScope handle_scope(pIsolate); v8::Local<v8::Context> context = NewLocalContext(); v8::Context::Scope context_scope(context); // v8::Local<v8::Context> tmpCotext = // v8::Local<v8::Context>::New(GetIsolate(), m_context); v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); context->Global()->Set( v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, utf8Name.GetLength()), propvalue); return true; }
AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& source, const std::string& filename) { const JsContext context(shared_from_this()); const v8::TryCatch tryCatch; const v8::Handle<v8::Script> script = CompileScript(GetIsolate(), source, filename); CheckTryCatch(tryCatch); v8::Local<v8::Value> result = script->Run(); CheckTryCatch(tryCatch); return JsValuePtr(new JsValue(shared_from_this(), result)); }
/** * Utility function that wraps a C++ http request object in a * JavaScript object. */ Local<Object> JsHttpRequestProcessor::WrapRequest(HttpRequest* request) { // Local scope for temporary handles. EscapableHandleScope handle_scope(GetIsolate()); // Fetch the template for creating JavaScript http request wrappers. // It only has to be created once, which we do on demand. if (request_template_.IsEmpty()) { Local<ObjectTemplate> raw_template = MakeRequestTemplate(GetIsolate()); request_template_.Reset(GetIsolate(), raw_template); } Local<ObjectTemplate> templ = Local<ObjectTemplate>::New(GetIsolate(), request_template_); // Create an empty http request wrapper. Local<Object> result = templ->NewInstance(GetIsolate()->GetCurrentContext()).ToLocalChecked(); // Wrap the raw C++ pointer in an External so it can be referenced // from within JavaScript. Local<External> request_ptr = External::New(GetIsolate(), request); // Store the request pointer in the JavaScript wrapper. result->SetInternalField(0, request_ptr); // Return the result through the current handle scope. Since each // of these handles will go away when the handle scope is deleted // we need to call Close to let one, the result, escape into the // outer handle scope. return handle_scope.Escape(result); }
FX_BOOL Document::deletePages(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError) { v8::Isolate* isolate = GetIsolate(cc); ASSERT(m_pDocument != NULL); if (!(m_pDocument->GetPermissions(FPDFPERM_MODIFY) || m_pDocument->GetPermissions(FPDFPERM_ASSEMBLE))) return FALSE; int iSize = params.size(); int nStart = 0; int nEnd = 0; if (iSize < 1) { } else if (iSize == 1) { if (params[0].GetType() == VT_object) { JSObject pObj = params[0].ToV8Object(); v8::Handle<v8::Value> pValue = JS_GetObjectElement(isolate, pObj, L"nStart"); nStart = CJS_Value(m_isolate, pValue, GET_VALUE_TYPE(pValue)).ToInt(); pValue = JS_GetObjectElement(isolate, pObj, L"nEnd"); nEnd = CJS_Value(m_isolate, pValue, GET_VALUE_TYPE(pValue)).ToInt(); } else { nStart = params[0].ToInt(); } } else { nStart = params[0].ToInt(); nEnd = params[1].ToInt(); } int nTotal = m_pDocument->GetPageCount(); if (nStart < 0) nStart = 0; if (nStart >= nTotal) nStart = nTotal - 1; if (nEnd < 0) nEnd = 0; if (nEnd >= nTotal) nEnd = nTotal - 1; if (nEnd < nStart) nEnd = nStart; return TRUE; }
void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { if (m_pDocument != pReaderDoc) { v8::Isolate::Scope isolate_scope(m_isolate); v8::HandleScope handle_scope(m_isolate); v8::Local<v8::Context> context = v8::Local<v8::Context>::New(m_isolate, m_context); v8::Context::Scope context_scope(context); m_pDocument = pReaderDoc; if (pReaderDoc) { v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate()); if (!pThis.IsEmpty()) { if (FXJS_GetObjDefnID(pThis) == CJS_Document::g_nObjDefnID) { if (CJS_Document* pJSDocument = (CJS_Document*)FXJS_GetPrivate(GetIsolate(), pThis)) { if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject()) pDocument->AttachDoc(pReaderDoc); } } } } } }
CJS_Runtime::~CJS_Runtime() { for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) delete m_ContextArray.GetAt(i); m_ContextArray.RemoveAll(); FXJS_ReleaseRuntime(GetIsolate(), m_context); m_pApp = NULL; m_pDocument = NULL; m_context.Reset(); if (m_isolateManaged) m_isolate->Dispose(); }
void CJS_Runtime::SetFormFillEnvToDocument() { v8::Isolate::Scope isolate_scope(GetIsolate()); v8::HandleScope handle_scope(GetIsolate()); v8::Local<v8::Context> context = NewLocalContext(); v8::Context::Scope context_scope(context); v8::Local<v8::Object> pThis = GetThisObj(); if (pThis.IsEmpty()) return; if (CFXJS_Engine::GetObjDefnID(pThis) != CJS_Document::g_nObjDefnID) return; CJS_Document* pJSDocument = static_cast<CJS_Document*>(GetObjectPrivate(pThis)); if (!pJSDocument) return; Document* pDocument = static_cast<Document*>(pJSDocument->GetEmbedObject()); if (!pDocument) return; pDocument->SetFormFillEnv(m_pFormFillEnv.Get()); }
CJS_Runtime::~CJS_Runtime() { for (auto* obs : m_observers) obs->OnDestroyed(); for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) delete m_ContextArray.GetAt(i); m_ContextArray.RemoveAll(); FXJS_ReleaseRuntime(GetIsolate(), &m_context, &m_StaticObjects); m_pApp = NULL; m_pDocument = NULL; m_context.Reset(); if (m_isolateManaged) m_isolate->Dispose(); }
FX_BOOL color::equal(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError) { v8::Isolate* isolate = GetIsolate(cc); if (params.size() < 2) return FALSE; CJS_Array array1(isolate), array2(isolate); if (!params[0].ConvertToArray(array1)) return FALSE; if (!params[1].ConvertToArray(array2)) return FALSE; CPWL_Color color1; CPWL_Color color2; ConvertArrayToPWLColor(array1, color1); ConvertArrayToPWLColor(array2, color2); color1.ConvertColorType(color2.nColorType); vRet = color1 == color2; return TRUE; }
FX_BOOL color::convert(IFXJS_Context* cc, const CJS_Parameters& params, CJS_Value& vRet, CFX_WideString& sError) { v8::Isolate* isolate = GetIsolate(cc); int iSize = params.size(); if (iSize < 2) return FALSE; CJS_Array aSource(isolate); if (!params[0].ConvertToArray(aSource)) return FALSE; CPWL_Color crSource; ConvertArrayToPWLColor(aSource, crSource); CFX_ByteString sDestSpace = params[1].ToCFXByteString(); int nColorType = COLORTYPE_TRANSPARENT; if (sDestSpace == "T") { nColorType = COLORTYPE_TRANSPARENT; } else if (sDestSpace == "G") { nColorType = COLORTYPE_GRAY; } else if (sDestSpace == "RGB") { nColorType = COLORTYPE_RGB; } else if (sDestSpace == "CMYK") { nColorType = COLORTYPE_CMYK; } CJS_Array aDest(isolate); CPWL_Color crDest = crSource; crDest.ConvertColorType(nColorType); ConvertPWLColorToArray(crDest, aDest); vRet = aDest; return TRUE; }