|
| 1 | +#ifndef SRC_NAPI_INL_DEPRECATED_H_ |
| 2 | +#define SRC_NAPI_INL_DEPRECATED_H_ |
| 3 | + |
| 4 | +//////////////////////////////////////////////////////////////////////////////// |
| 5 | +// PropertyDescriptor class |
| 6 | +//////////////////////////////////////////////////////////////////////////////// |
| 7 | + |
| 8 | +template <typename Getter> |
| 9 | +inline PropertyDescriptor |
| 10 | +PropertyDescriptor::Accessor(const char* utf8name, |
| 11 | + Getter getter, |
| 12 | + napi_property_attributes attributes, |
| 13 | + void* /*data*/) { |
| 14 | + typedef details::CallbackData<Getter, Napi::Value> CbData; |
| 15 | + // TODO: Delete when the function is destroyed |
| 16 | + auto callbackData = new CbData({ getter, nullptr }); |
| 17 | + |
| 18 | + return PropertyDescriptor({ |
| 19 | + utf8name, |
| 20 | + nullptr, |
| 21 | + nullptr, |
| 22 | + CbData::Wrapper, |
| 23 | + nullptr, |
| 24 | + nullptr, |
| 25 | + attributes, |
| 26 | + callbackData |
| 27 | + }); |
| 28 | +} |
| 29 | + |
| 30 | +template <typename Getter> |
| 31 | +inline PropertyDescriptor PropertyDescriptor::Accessor(const std::string& utf8name, |
| 32 | + Getter getter, |
| 33 | + napi_property_attributes attributes, |
| 34 | + void* data) { |
| 35 | + return Accessor(utf8name.c_str(), getter, attributes, data); |
| 36 | +} |
| 37 | + |
| 38 | +template <typename Getter> |
| 39 | +inline PropertyDescriptor PropertyDescriptor::Accessor(napi_value name, |
| 40 | + Getter getter, |
| 41 | + napi_property_attributes attributes, |
| 42 | + void* /*data*/) { |
| 43 | + typedef details::CallbackData<Getter, Napi::Value> CbData; |
| 44 | + // TODO: Delete when the function is destroyed |
| 45 | + auto callbackData = new CbData({ getter, nullptr }); |
| 46 | + |
| 47 | + return PropertyDescriptor({ |
| 48 | + nullptr, |
| 49 | + name, |
| 50 | + nullptr, |
| 51 | + CbData::Wrapper, |
| 52 | + nullptr, |
| 53 | + nullptr, |
| 54 | + attributes, |
| 55 | + callbackData |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +template <typename Getter> |
| 60 | +inline PropertyDescriptor PropertyDescriptor::Accessor(Name name, |
| 61 | + Getter getter, |
| 62 | + napi_property_attributes attributes, |
| 63 | + void* data) { |
| 64 | + napi_value nameValue = name; |
| 65 | + return PropertyDescriptor::Accessor(nameValue, getter, attributes, data); |
| 66 | +} |
| 67 | + |
| 68 | +template <typename Getter, typename Setter> |
| 69 | +inline PropertyDescriptor PropertyDescriptor::Accessor(const char* utf8name, |
| 70 | + Getter getter, |
| 71 | + Setter setter, |
| 72 | + napi_property_attributes attributes, |
| 73 | + void* /*data*/) { |
| 74 | + typedef details::AccessorCallbackData<Getter, Setter> CbData; |
| 75 | + // TODO: Delete when the function is destroyed |
| 76 | + auto callbackData = new CbData({ getter, setter }); |
| 77 | + |
| 78 | + return PropertyDescriptor({ |
| 79 | + utf8name, |
| 80 | + nullptr, |
| 81 | + nullptr, |
| 82 | + CbData::GetterWrapper, |
| 83 | + CbData::SetterWrapper, |
| 84 | + nullptr, |
| 85 | + attributes, |
| 86 | + callbackData |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +template <typename Getter, typename Setter> |
| 91 | +inline PropertyDescriptor PropertyDescriptor::Accessor(const std::string& utf8name, |
| 92 | + Getter getter, |
| 93 | + Setter setter, |
| 94 | + napi_property_attributes attributes, |
| 95 | + void* data) { |
| 96 | + return Accessor(utf8name.c_str(), getter, setter, attributes, data); |
| 97 | +} |
| 98 | + |
| 99 | +template <typename Getter, typename Setter> |
| 100 | +inline PropertyDescriptor PropertyDescriptor::Accessor(napi_value name, |
| 101 | + Getter getter, |
| 102 | + Setter setter, |
| 103 | + napi_property_attributes attributes, |
| 104 | + void* /*data*/) { |
| 105 | + typedef details::AccessorCallbackData<Getter, Setter> CbData; |
| 106 | + // TODO: Delete when the function is destroyed |
| 107 | + auto callbackData = new CbData({ getter, setter }); |
| 108 | + |
| 109 | + return PropertyDescriptor({ |
| 110 | + nullptr, |
| 111 | + name, |
| 112 | + nullptr, |
| 113 | + CbData::GetterWrapper, |
| 114 | + CbData::SetterWrapper, |
| 115 | + nullptr, |
| 116 | + attributes, |
| 117 | + callbackData |
| 118 | + }); |
| 119 | +} |
| 120 | + |
| 121 | +template <typename Getter, typename Setter> |
| 122 | +inline PropertyDescriptor PropertyDescriptor::Accessor(Name name, |
| 123 | + Getter getter, |
| 124 | + Setter setter, |
| 125 | + napi_property_attributes attributes, |
| 126 | + void* data) { |
| 127 | + napi_value nameValue = name; |
| 128 | + return PropertyDescriptor::Accessor(nameValue, getter, setter, attributes, data); |
| 129 | +} |
| 130 | + |
| 131 | +template <typename Callable> |
| 132 | +inline PropertyDescriptor PropertyDescriptor::Function(const char* utf8name, |
| 133 | + Callable cb, |
| 134 | + napi_property_attributes attributes, |
| 135 | + void* /*data*/) { |
| 136 | + typedef decltype(cb(CallbackInfo(nullptr, nullptr))) ReturnType; |
| 137 | + typedef details::CallbackData<Callable, ReturnType> CbData; |
| 138 | + // TODO: Delete when the function is destroyed |
| 139 | + auto callbackData = new CbData({ cb, nullptr }); |
| 140 | + |
| 141 | + return PropertyDescriptor({ |
| 142 | + utf8name, |
| 143 | + nullptr, |
| 144 | + CbData::Wrapper, |
| 145 | + nullptr, |
| 146 | + nullptr, |
| 147 | + nullptr, |
| 148 | + attributes, |
| 149 | + callbackData |
| 150 | + }); |
| 151 | +} |
| 152 | + |
| 153 | +template <typename Callable> |
| 154 | +inline PropertyDescriptor PropertyDescriptor::Function(const std::string& utf8name, |
| 155 | + Callable cb, |
| 156 | + napi_property_attributes attributes, |
| 157 | + void* data) { |
| 158 | + return Function(utf8name.c_str(), cb, attributes, data); |
| 159 | +} |
| 160 | + |
| 161 | +template <typename Callable> |
| 162 | +inline PropertyDescriptor PropertyDescriptor::Function(napi_value name, |
| 163 | + Callable cb, |
| 164 | + napi_property_attributes attributes, |
| 165 | + void* /*data*/) { |
| 166 | + typedef decltype(cb(CallbackInfo(nullptr, nullptr))) ReturnType; |
| 167 | + typedef details::CallbackData<Callable, ReturnType> CbData; |
| 168 | + // TODO: Delete when the function is destroyed |
| 169 | + auto callbackData = new CbData({ cb, nullptr }); |
| 170 | + |
| 171 | + return PropertyDescriptor({ |
| 172 | + nullptr, |
| 173 | + name, |
| 174 | + CbData::Wrapper, |
| 175 | + nullptr, |
| 176 | + nullptr, |
| 177 | + nullptr, |
| 178 | + attributes, |
| 179 | + callbackData |
| 180 | + }); |
| 181 | +} |
| 182 | + |
| 183 | +template <typename Callable> |
| 184 | +inline PropertyDescriptor PropertyDescriptor::Function(Name name, |
| 185 | + Callable cb, |
| 186 | + napi_property_attributes attributes, |
| 187 | + void* data) { |
| 188 | + napi_value nameValue = name; |
| 189 | + return PropertyDescriptor::Function(nameValue, cb, attributes, data); |
| 190 | +} |
| 191 | + |
| 192 | +#endif // !SRC_NAPI_INL_DEPRECATED_H_ |
0 commit comments