cocos-engine icon indicating copy to clipboard operation
cocos-engine copied to clipboard

Switch JS binding tools from binding-generator to Swig

Open dumganhar opened this issue 2 years ago • 2 comments

This issue is a sub task for https://github.com/cocos/cocos-engine/issues/10792

Auto binding glue code is generated by bindings-generator in the past, the bindings-generator is old, hard to maintain, lack of features. You could refer to the issue above.

Modules (tools/tojs) need to be handled include:

  • [x] assets
  • [x] audio
  • [x] cocos
  • [x] dragonbones
  • [x] editor_support
  • [x] extension
  • [x] geometry
  • [x] gfx
  • [x] network
  • [x] physics
  • [x] pipeline
  • [x] render
  • [x] scene
  • [x] spine
  • [x] video
  • [x] webview

dumganhar avatar May 05 '22 06:05 dumganhar

class Material : public Asset {
public:
    ......
    const MaterialPropertyVariant *getProperty(const ccstd::string &name, index_t passIdx = CC_INVALID_INDEX) const;
    ......
};

```c++
(1)
// The correct conversion function for the return value of getProperty
template <typename... ARGS>
bool nativevalue_to_se(const ccstd::variant<ARGS...> *from, se::Value &to, se::Object *ctx) { 
    return nativevalue_to_se(*from, to, ctx);
}

(2)
// The wrong conversion function for the return value of getProperty
template <typename T>
inline typename std::enable_if<std::is_pointer<T>::value, bool>::type
nativevalue_to_se(const T &from, se::Value &to, se::Object *ctx) {
    return native_ptr_to_seval(from, &to);
}
static bool js_cc_Material__getProperty__SWIG_0(se::State& s)
{
    ......
    cc::MaterialPropertyVariant *result = 0 ; // Swig remove const keyword
    ......
    
    result = (cc::MaterialPropertyVariant *)((cc::Material const *)arg1)->getProperty((ccstd::string const &)*arg2,arg3); // swig casts `const cc::MaterialPropertyVariant *` to `cc::MaterialPropertyVariant *`
    ok &= nativevalue_to_se(result, s.rval(), s.thisObject() /*ctx*/); // Will match wrong the conversion function (2)
    SE_PRECONDITION2(ok, false, "Material__getProperty, Error processing arguments");
    SE_HOLD_RETURN_VALUE(result, s.thisObject(), s.rval()); 
    
    return true;
}

Solution

Add a conversion function without const prefix.

template <typename... ARGS>
bool nativevalue_to_se(ccstd::variant<ARGS...> *from, se::Value &to, se::Object *ctx) { // NOLINT
    return nativevalue_to_se(*from, to, ctx);
}

dumganhar avatar May 30 '22 02:05 dumganhar

This won't be shipped with v3.6, rescheduled to v3.7

pandamicro avatar Jun 15 '22 15:06 pandamicro