[V8] New adapter for external embed. js_base.h celaned up.

This commit is contained in:
Asethone
2023-06-02 17:31:01 +04:00
parent a29605e1b7
commit 11d91891a8
9 changed files with 73 additions and 81 deletions

View File

@ -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)

View File

@ -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_

View File

@ -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_

View File

@ -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
{

View File

@ -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;
};