mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-02-10 18:05:41 +08:00
[V8] New adapter for external embed. js_base.h celaned up.
This commit is contained in:
@ -24,7 +24,7 @@ namespace NSHashEmbed
|
||||
}
|
||||
}
|
||||
|
||||
class CHashEmbedAdapter : public CJSEmbedObjectAdapterV8
|
||||
class CHashEmbedAdapter : public CJSEmbedObjectAdapterV8Template
|
||||
{
|
||||
public:
|
||||
virtual v8::Local<v8::ObjectTemplate> getTemplate(v8::Isolate* isolate) override
|
||||
@ -42,15 +42,6 @@ CJSEmbedObjectAdapterBase* CHashEmbed::getAdapter()
|
||||
return m_pAdapter;
|
||||
}
|
||||
|
||||
std::vector<std::string> CHashEmbed::getMethodNames()
|
||||
{
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
|
||||
void CHashEmbed::initFunctions()
|
||||
{
|
||||
}
|
||||
|
||||
std::string CHashEmbed::getName() { return "CHashEmbed"; }
|
||||
|
||||
CJSEmbedObject* CHashEmbed::getCreator()
|
||||
|
||||
@ -44,7 +44,7 @@ namespace NSZipEmbed
|
||||
}
|
||||
}
|
||||
|
||||
class CZipEmbedAdapter : public CJSEmbedObjectAdapterV8
|
||||
class CZipEmbedAdapter : public CJSEmbedObjectAdapterV8Template
|
||||
{
|
||||
public:
|
||||
virtual v8::Local<v8::ObjectTemplate> getTemplate(v8::Isolate* isolate) override
|
||||
@ -62,15 +62,6 @@ CJSEmbedObjectAdapterBase* CZipEmbed::getAdapter()
|
||||
return m_pAdapter;
|
||||
}
|
||||
|
||||
std::vector<std::string> CZipEmbed::getMethodNames()
|
||||
{
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
|
||||
void CZipEmbed::initFunctions()
|
||||
{
|
||||
}
|
||||
|
||||
std::string CZipEmbed::getName() { return "CZipEmbed"; }
|
||||
|
||||
CJSEmbedObject* CZipEmbed::getCreator()
|
||||
|
||||
@ -49,25 +49,11 @@ namespace NSJSBase {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::vector<std::string> CJSEmbedObject::getMethodNames()
|
||||
{
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
|
||||
void CJSEmbedObject::initFunctions()
|
||||
{
|
||||
}
|
||||
|
||||
CJSEmbedObjectAdapterBase* CJSEmbedObject::getAdapter()
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
JSSmart<CJSValue> CJSEmbedObject::Call(const int& index, CJSFunctionArguments* args)
|
||||
{
|
||||
return m_functions[index](args);
|
||||
}
|
||||
|
||||
void CJSContext::AddEmbedCreator(const std::string& name,
|
||||
EmbedObjectCreator creator,
|
||||
const IsolateAdditionalDataType& type)
|
||||
|
||||
@ -93,26 +93,17 @@ namespace NSJSBase
|
||||
|
||||
class JS_DECL CJSEmbedObject
|
||||
{
|
||||
public:
|
||||
using EmbedFunctionType = std::function<JSSmart<CJSValue>(CJSFunctionArguments*)>;
|
||||
|
||||
public:
|
||||
CJSEmbedObject();
|
||||
virtual ~CJSEmbedObject();
|
||||
|
||||
public:
|
||||
virtual void* getObject();
|
||||
virtual std::vector<std::string> getMethodNames();
|
||||
virtual void initFunctions();
|
||||
|
||||
virtual CJSEmbedObjectAdapterBase* getAdapter();
|
||||
|
||||
JSSmart<CJSValue> Call(const int& index, CJSFunctionArguments* args);
|
||||
|
||||
protected:
|
||||
CJSEmbedObjectPrivateBase* embed_native_internal;
|
||||
CJSEmbedObjectAdapterBase* m_pAdapter;
|
||||
std::vector<EmbedFunctionType> m_functions;
|
||||
|
||||
friend class CJSEmbedObjectPrivateBase;
|
||||
friend class CJSEmbedObjectPrivate;
|
||||
@ -301,19 +292,10 @@ namespace NSJSBase
|
||||
};
|
||||
}
|
||||
|
||||
// defines for embed
|
||||
#ifndef JS_ENGINE_JAVASCRIPTCORE
|
||||
#define _DECLARE_EMBED_EXTRA_METHODS \
|
||||
virtual std::vector<std::string> getMethodNames() override; \
|
||||
void initFunctions() override;
|
||||
#else
|
||||
#define _DECLARE_EMBED_EXTRA_METHODS
|
||||
#endif
|
||||
|
||||
// macro for embedding
|
||||
#define DECLARE_EMBED_METHODS \
|
||||
static std::string getName(); \
|
||||
static CJSEmbedObject* getCreator(); \
|
||||
virtual CJSEmbedObjectAdapterBase* getAdapter() override; \
|
||||
_DECLARE_EMBED_EXTRA_METHODS
|
||||
virtual CJSEmbedObjectAdapterBase* getAdapter() override;
|
||||
|
||||
#endif // _CORE_EXT_JS_BASE_H_
|
||||
|
||||
@ -58,6 +58,33 @@ namespace NSJSBase
|
||||
};
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
namespace NSJSBase
|
||||
{
|
||||
class JS_DECL CJSEmbedObjectAdapterV8 : public CJSEmbedObjectAdapterBase
|
||||
{
|
||||
public:
|
||||
using EmbedFunctionType = std::function<JSSmart<CJSValue>(CJSFunctionArguments*)>;
|
||||
|
||||
public:
|
||||
CJSEmbedObjectAdapterV8() = default;
|
||||
virtual ~CJSEmbedObjectAdapterV8() = default;
|
||||
|
||||
virtual std::vector<std::string> getMethodNames() = 0;
|
||||
virtual void initFunctions(CJSEmbedObject* pNativeObjBase) = 0;
|
||||
|
||||
public:
|
||||
JSSmart<CJSValue> Call(const int& index, CJSFunctionArguments* args)
|
||||
{
|
||||
return m_functions[index](args);
|
||||
}
|
||||
|
||||
protected:
|
||||
std::vector<EmbedFunctionType> m_functions;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif // _BUILD_NATIVE_CONTROL_JS_EMBED_H_
|
||||
|
||||
@ -496,19 +496,21 @@ namespace NSJSBase
|
||||
CJSEmbedObject* _this = (CJSEmbedObject*)unwrap_native(args.Holder());
|
||||
CJSFunctionArgumentsV8 _args(&args, 0);
|
||||
JSSmart<CJSValue> funcIndex = js_value(args.Data());
|
||||
JSSmart<CJSValue> ret = _this->Call(funcIndex->toInt32(), &_args);
|
||||
CJSEmbedObjectAdapterV8* _adapter = static_cast<CJSEmbedObjectAdapterV8*>(_this->getAdapter());
|
||||
JSSmart<CJSValue> ret = _adapter->Call(funcIndex->toInt32(), &_args);
|
||||
js_return(args, ret);
|
||||
}
|
||||
|
||||
v8::Handle<v8::ObjectTemplate> CreateEmbedObjectTemplate(v8::Isolate* isolate, CJSEmbedObject* pNativeObj)
|
||||
{
|
||||
v8::EscapableHandleScope handle_scope(isolate);
|
||||
CJSEmbedObjectAdapterV8* pAdapter = static_cast<CJSEmbedObjectAdapterV8*>(pNativeObj->getAdapter());
|
||||
|
||||
v8::Local<v8::ObjectTemplate> result = v8::ObjectTemplate::New(isolate);
|
||||
result->SetInternalFieldCount(1);
|
||||
|
||||
pNativeObj->initFunctions();
|
||||
std::vector<std::string> arNames = pNativeObj->getMethodNames();
|
||||
pAdapter->initFunctions(pNativeObj);
|
||||
std::vector<std::string> arNames = pAdapter->getMethodNames();
|
||||
for (int i = 0, len = arNames.size(); i < len; ++i)
|
||||
{
|
||||
// associate all methods with corresponding Call() index
|
||||
@ -555,10 +557,10 @@ namespace NSJSBase
|
||||
|
||||
CJSEmbedObject* pNativeObj = oInfo.m_creator();
|
||||
v8::Local<v8::ObjectTemplate> oCurTemplate;
|
||||
CJSEmbedObjectAdapterV8* pAdapter = static_cast<CJSEmbedObjectAdapterV8*>(pNativeObj->getAdapter());
|
||||
if (pAdapter)
|
||||
CJSEmbedObjectAdapterV8Template* pTemplateAdapter = dynamic_cast<CJSEmbedObjectAdapterV8Template*>(pNativeObj->getAdapter());
|
||||
if (pTemplateAdapter)
|
||||
{
|
||||
oCurTemplate = pAdapter->getTemplate(isolate);
|
||||
oCurTemplate = pTemplateAdapter->getTemplate(isolate);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
#include "inspector/inspector_pool.h"
|
||||
#endif
|
||||
|
||||
#include "../js_embed.h"
|
||||
#include "../js_base.h"
|
||||
#include "../js_base_p.h"
|
||||
#include "../js_logger.h"
|
||||
@ -832,11 +833,11 @@ namespace NSJSBase
|
||||
// embed
|
||||
void CreateEmbedNativeObject(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
class CJSEmbedObjectAdapterV8 : public CJSEmbedObjectAdapterBase
|
||||
class CJSEmbedObjectAdapterV8Template : public CJSEmbedObjectAdapterBase
|
||||
{
|
||||
public:
|
||||
CJSEmbedObjectAdapterV8() = default;
|
||||
virtual ~CJSEmbedObjectAdapterV8() = default;
|
||||
CJSEmbedObjectAdapterV8Template() = default;
|
||||
virtual ~CJSEmbedObjectAdapterV8Template() = default;
|
||||
|
||||
virtual v8::Local<v8::ObjectTemplate> getTemplate(v8::Isolate* isolate) = 0;
|
||||
};
|
||||
|
||||
@ -209,6 +209,7 @@ int main(int argc, char *argv[])
|
||||
|
||||
#if 1
|
||||
// Mixed embed example
|
||||
|
||||
JSSmart<CJSContext> oContext1 = new CJSContext();
|
||||
|
||||
// External CTestEmbed
|
||||
@ -257,6 +258,7 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
std::cout << oFiles1->get(i)->toStringA() << std::endl;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
|
||||
@ -2,28 +2,38 @@
|
||||
// IF YOU NEED TO UPDATE THIS CODE, JUST RERUN PYTHON SCRIPT.
|
||||
|
||||
#include "../Embed.h"
|
||||
#include "js_embed.h"
|
||||
|
||||
std::vector<std::string> CTestEmbed::getMethodNames()
|
||||
class CTestEmbedAdapter : public CJSEmbedObjectAdapterV8
|
||||
{
|
||||
return std::vector<std::string> {
|
||||
"FunctionSum",
|
||||
"FunctionSquare",
|
||||
"FunctionDel",
|
||||
"FunctionGet"
|
||||
};
|
||||
}
|
||||
public:
|
||||
virtual std::vector<std::string> getMethodNames() override
|
||||
{
|
||||
return std::vector<std::string> {
|
||||
"FunctionSum",
|
||||
"FunctionSquare",
|
||||
"FunctionDel",
|
||||
"FunctionGet"
|
||||
};
|
||||
}
|
||||
|
||||
void CTestEmbed::initFunctions()
|
||||
{
|
||||
m_functions = std::vector<EmbedFunctionType> {
|
||||
[this](CJSFunctionArguments* args) { return this->FunctionSum(args->Get(0), args->Get(1)); },
|
||||
[this](CJSFunctionArguments* args) { return this->FunctionSquare(args->Get(0)); },
|
||||
[this](CJSFunctionArguments* args) { return this->FunctionDel(args->Get(0), args->Get(1)); },
|
||||
[this](CJSFunctionArguments* args) { return this->FunctionGet(); }
|
||||
};
|
||||
}
|
||||
virtual void initFunctions(CJSEmbedObject* pNativeObjBase) override
|
||||
{
|
||||
CTestEmbed* pNativeObj = static_cast<CTestEmbed*>(pNativeObjBase);
|
||||
m_functions = std::vector<EmbedFunctionType> {
|
||||
[pNativeObj](CJSFunctionArguments* args) { return pNativeObj->FunctionSum(args->Get(0), args->Get(1)); },
|
||||
[pNativeObj](CJSFunctionArguments* args) { return pNativeObj->FunctionSquare(args->Get(0)); },
|
||||
[pNativeObj](CJSFunctionArguments* args) { return pNativeObj->FunctionDel(args->Get(0), args->Get(1)); },
|
||||
[pNativeObj](CJSFunctionArguments* args) { return pNativeObj->FunctionGet(); }
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
CJSEmbedObjectAdapterBase* CTestEmbed::getAdapter() { return nullptr; }
|
||||
CJSEmbedObjectAdapterBase* CTestEmbed::getAdapter() {
|
||||
if (m_pAdapter == nullptr)
|
||||
m_pAdapter = new CTestEmbedAdapter();
|
||||
return m_pAdapter;
|
||||
}
|
||||
|
||||
std::string CTestEmbed::getName() { return "CTestEmbed"; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user