Skip to content

Commit 49e09d9

Browse files
committed
feat(generator): add converter for class wrapper
1 parent b81846f commit 49e09d9

File tree

4 files changed

+46
-37
lines changed

4 files changed

+46
-37
lines changed

generator/binder/templates/class_wrapper.h.j2

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ public:
1818
: ScriptClass(ScriptClass::ConstructFromCpp<{{get_wrapper_class_name(class_info)}}>{}),
1919
mResourceHolder(std::move(res)) {}
2020

21+
explicit {{get_wrapper_class_name(class_info)}}(Local<Object> const& thiz, {{class_full_name}}* res)
22+
: ScriptClass(thiz), mResourceHolder(res) {}
23+
24+
explicit {{get_wrapper_class_name(class_info)}}(Local<Object> const& thiz, std::shared_ptr<{{class_full_name}}> res)
25+
: ScriptClass(thiz), mResourceHolder(std::move(res)) {}
26+
27+
explicit {{get_wrapper_class_name(class_info)}}(Local<Object> const& thiz, std::unique_ptr<{{class_full_name}}> res)
28+
: ScriptClass(thiz), mResourceHolder(std::move(res)) {}
29+
2130
[[nodiscard]] static Local<Object> newInstance({{class_full_name}}* res) { return (new {{get_wrapper_class_name(class_info)}}(res))->getScriptObject(); }
2231
[[nodiscard]] static Local<Object> newInstance(std::shared_ptr<{{class_full_name}}> res) { return (new {{get_wrapper_class_name(class_info)}}(std::move(res)))->getScriptObject(); }
2332
[[nodiscard]] static Local<Object> newInstance(std::unique_ptr<{{class_full_name}}> res) { return (new {{get_wrapper_class_name(class_info)}}(std::move(res)))->getScriptObject(); }
@@ -33,3 +42,6 @@ public:
3342
public:
3443
static ClassDefine<{{get_wrapper_class_name(class_info)}}> builder;
3544
};
45+
46+
// implementation Converter
47+
ImplConverter({{class_full_name}}, {{get_wrapper_class_name(class_info)}});

src/converter/Convert.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include "detail/StlConverter.hpp"
55

6-
// #include "ProjectConverters.hpp" // circular reference, please include manually
6+
#include "ConverterTemplate.hpp" // circular reference, please include manually
77

88

99
namespace jse {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
#include "detail/Converter.hpp"
3+
4+
5+
#define ImplConverter(NATIVE_TYPE, WRAPPER_TYPE) \
6+
namespace detail { \
7+
template <> \
8+
struct Converter<NATIVE_TYPE> { \
9+
static Local<Value> toScript(NATIVE_TYPE* res) { return WRAPPER_TYPE::newInstance(res); } \
10+
static Local<Value> toScript(std::shared_ptr<NATIVE_TYPE> res) { \
11+
return WRAPPER_TYPE::newInstance(std::move(res)); \
12+
} \
13+
static Local<Value> toScript(std::unique_ptr<NATIVE_TYPE> res) { \
14+
return WRAPPER_TYPE::newInstance(std::move(res)); \
15+
} \
16+
static NATIVE_TYPE* toCpp(Local<Value> const& val) { \
17+
if (EngineScope::currentEngine()->isInstanceOf<WRAPPER_TYPE>(val)) { \
18+
return EngineScope ::currentEngine()->getNativeInstance<WRAPPER_TYPE>(val)->get(); \
19+
} \
20+
throw script::Exception("Failed to convert value to " #NATIVE_TYPE); \
21+
} \
22+
}; \
23+
template <> \
24+
struct Converter<WRAPPER_TYPE> { \
25+
static Local<Value> toScript(WRAPPER_TYPE* res) { return res->getScriptObject(); } \
26+
static WRAPPER_TYPE* toCpp(Local<Value> const& val) { \
27+
if (EngineScope::currentEngine()->isInstanceOf<WRAPPER_TYPE>(val)) { \
28+
return EngineScope ::currentEngine()->getNativeInstance<WRAPPER_TYPE>(val); \
29+
} \
30+
throw script::Exception("Failed to convert value to " #WRAPPER_TYPE); \
31+
} \
32+
}; \
33+
}

src/converter/ProjectConverters.hpp

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)