/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "bindings_implementation.h" namespace OHOS::Ace::Framework { template typename ImplDetail> std::unordered_map JSClassImpl::functions_; template typename ImplDetail> int JSClassImpl::nextFreeId_ = 0; template typename ImplDetail> std::string JSClassImpl::jsName; template typename ImplDetail> void JSClassImpl::Declare(const char* name) { jsName = name; ImplDetail::Declare(name); } template typename ImplDetail> template void JSClassImpl::Method(const char* name, R (C::*func)(Args...), MethodOptions options) { functions_.emplace(nextFreeId_, new FunctionBinding(name, options, func)); ImplDetail::Method(name, func, nextFreeId_++); } template typename ImplDetail> template void JSClassImpl::Method(const char* name, R (Base::*func)(Args...), MethodOptions options) { static_assert(std::is_base_of_v, "Trying to bind an unrelated method!"); functions_.emplace(nextFreeId_, new FunctionBinding(name, options, func)); ImplDetail::Method(name, func, nextFreeId_++); } template typename ImplDetail> template void JSClassImpl::StaticMethod(const char* name, R (C::*func)(Args...), MethodOptions options) { functions_.emplace(nextFreeId, new FunctionBinding(name, options, func)); ImplDetail::StaticMethod(name, func, nextFreeId++); } template typename ImplDetail> template void JSClassImpl::StaticMethod(const char* name, R (Base::*func)(Args...), MethodOptions options) { static_assert(std::is_base_of_v, "Trying to bind an unrelated method!"); functions_.emplace(nextFreeId, new FunctionBinding(name, options, func)); ImplDetail::StaticMethod(name, func, nextFreeId++); } template typename ImplDetail> template void JSClassImpl::CustomMethod( const char* name, typename ImplDetail::template MemberFunctionCallback callback) { static_assert(std::is_base_of_v, "Trying to bind an unrelated method!"); functions_.emplace(nextFreeId_, new FunctionBinding(name, MethodOptions::NONE, callback)); ImplDetail::CustomMethod(name, callback, nextFreeId_++); } template typename ImplDetail> void JSClassImpl::CustomMethod(const char* name, typename ImplDetail::FunctionCallback callback) { ImplDetail::CustomMethod(name, callback); } template typename ImplDetail> template void JSClassImpl::CustomProperty( const char* name, typename ImplDetail::template MemberFunctionGetCallback getter, typename ImplDetail::template MemberFunctionSetCallback setter) { int getFuncId; int setFuncId; static_assert(std::is_base_of_v, "Trying to bind an unrelated method!"); getFunctions_.emplace(nextFreeId_, new FunctionBinding(name, MethodOptions::NONE, getter)); setFunctions_.emplace(nextFreeId_, new FunctionBinding(name, MethodOptions::NONE, setter)); functions_.emplace(nextFreeId_, new FunctionBinding(name, MethodOptions::NONE, getter)); getFuncId = nextFreeId_++; functions_.emplace(nextFreeId_, new FunctionBinding(name, MethodOptions::NONE, setter)); setFuncId = nextFreeId_++; ImplDetail::CustomProperty(name, getter, getFuncId, setFuncId); } template typename ImplDetail> void JSClassImpl::CustomProperty(const char* name, typename ImplDetail::FunctionGetCallback getter, typename ImplDetail::FunctionSetCallback setter) { ImplDetail::CustomProperty(name, getter, setter); } template typename ImplDetail> template void JSClassImpl::StaticMethod( const char* name, typename ImplDetail::template MemberFunctionCallback callback) { static_assert(std::is_base_of_v, "Trying to bind an unrelated method!"); functions_.emplace(nextFreeId, new FunctionBinding(name, MethodOptions::NONE, callback)); ImplDetail::StaticMethod(name, callback, nextFreeId++); } template typename ImplDetail> void JSClassImpl::StaticMethod(const char* name, typename ImplDetail::FunctionCallback callback) { ImplDetail::StaticMethod(name, callback); } template typename ImplDetail> template void JSClassImpl::StaticConstant(const char* name, T value) { ImplDetail::StaticConstant(name, value); } template typename ImplDetail> void JSClassImpl::Bind( typename ImplDetail::BindingTarget bindTarget, typename ImplDetail::FunctionCallback ctor) { ImplDetail::Bind(bindTarget, ctor); } template typename ImplDetail> template void JSClassImpl::Bind(typename ImplDetail::BindingTarget bindTarget) { ImplDetail::template Bind(bindTarget); } template typename ImplDetail> template void JSClassImpl::Inherit() { static_assert(std::is_base_of_v, "Calling Inherit() on unrelated classes!"); ImplDetail::template Inherit(); } template typename ImplDetail> template void JSClassImpl::InheritAndBind( typename ImplDetail::BindingTarget bindTarget, ImplDetail::JSFunctionCallback ctor, ImplDetail::JSDestructorCallback dtor, ImplDetail::JSGCMarkCallback gcMark) { static_assert(std::is_base_of_v, "Calling Inherit() on unrelated classes!"); ImplDetail::template InheritAndBind(bindTarget, ctor, dtor, gcMark); } template typename ImplDetail> IFunctionBinding* JSClassImpl::GetFunctionBinding(int id) { return functions_[id]; } template typename ImplDetail> const char* JSClassImpl::JSName() { return jsName.c_str(); } }; // namespace OHOS::Ace::Framework