diff --git a/package/cpp/api/JsiSkContourMeasure.h b/package/cpp/api/JsiSkContourMeasure.h index b7a9662902..4b5673f520 100644 --- a/package/cpp/api/JsiSkContourMeasure.h +++ b/package/cpp/api/JsiSkContourMeasure.h @@ -29,7 +29,7 @@ namespace RNSkia { if (contourMeasure == nullptr) { - jsi::detail::throwJSError(*context->getJsRuntime(), "Contour measure is null"); + throw jsi::JSError(*context->getJsRuntime(), "Contour measure is null"); } }; @@ -41,7 +41,7 @@ namespace RNSkia auto result = getObject()->getPosTan(dist, &position, &tangent); if (!result) { - jsi::detail::throwJSError(runtime, "getSegment() failed"); + throw jsi::JSError(runtime, "getSegment() failed"); } auto posTan = jsi::Object(runtime); posTan.setProperty(runtime, "px", position.x()); @@ -70,7 +70,7 @@ namespace RNSkia auto result = getObject()->getSegment(start, end, &path, startWithMoveTo); if (!result) { - jsi::detail::throwJSError(runtime, "getSegment() failed"); + throw jsi::JSError(runtime, "getSegment() failed"); } return JsiSkPath::toValue(runtime, getContext(), std::move(path)); } @@ -99,4 +99,4 @@ namespace RNSkia ->getObject(); } }; -} // namespace RNSkia \ No newline at end of file +} // namespace RNSkia diff --git a/package/cpp/api/JsiSkDataFactory.h b/package/cpp/api/JsiSkDataFactory.h index 85223b5614..8dc298c8f6 100644 --- a/package/cpp/api/JsiSkDataFactory.h +++ b/package/cpp/api/JsiSkDataFactory.h @@ -64,7 +64,7 @@ namespace RNSkia { size_t len; auto err = SkBase64::Decode(&base64.utf8(runtime).c_str()[0], size, nullptr, &len); if(err != SkBase64::Error::kNoError) { - jsi::detail::throwJSError(runtime, "Error decoding base64 string"); + throw jsi::JSError(runtime, "Error decoding base64 string"); return jsi::Value::undefined(); } @@ -72,10 +72,10 @@ namespace RNSkia { auto data = SkData::MakeUninitialized(len); err = SkBase64::Decode(&base64.utf8(runtime).c_str()[0], size, data->writable_data(), &len); if(err != SkBase64::Error::kNoError) { - jsi::detail::throwJSError(runtime, "Error decoding base64 string"); + throw jsi::JSError(runtime, "Error decoding base64 string"); return jsi::Value::undefined(); } - + return jsi::Object::createFromHostObject(runtime, std::make_shared( getContext(), std::move(data))); diff --git a/package/cpp/api/JsiSkFont.h b/package/cpp/api/JsiSkFont.h index 2807c1ca80..10301dde01 100644 --- a/package/cpp/api/JsiSkFont.h +++ b/package/cpp/api/JsiSkFont.h @@ -152,7 +152,7 @@ namespace RNSkia if (glyphs.size() > positions.size()) { - jsi::detail::throwJSError(runtime, "Not enough x,y position pairs for glyphs"); + throw jsi::JSError(runtime, "Not enough x,y position pairs for glyphs"); return jsi::Value::null(); } auto sects = getObject()->getIntercepts(glyphs.data(), SkToInt(glyphs.size()), positions.data(), top, bottom); diff --git a/package/cpp/api/JsiSkPathFactory.h b/package/cpp/api/JsiSkPathFactory.h index 2f533a9c4d..50510f114f 100644 --- a/package/cpp/api/JsiSkPathFactory.h +++ b/package/cpp/api/JsiSkPathFactory.h @@ -41,7 +41,7 @@ class JsiSkPathFactory : public JsiSkHostObject { SkPath result; if (!SkParsePath::FromSVGString(svgString.c_str(), &result)) { - jsi::detail::throwJSError(runtime, "Could not parse Svg path"); + throw jsi::JSError(runtime, "Could not parse Svg path"); return jsi::Value(nullptr); } diff --git a/package/cpp/api/JsiSkPictureFactory.h b/package/cpp/api/JsiSkPictureFactory.h index 8e18eb3ccb..2e2b4ba879 100644 --- a/package/cpp/api/JsiSkPictureFactory.h +++ b/package/cpp/api/JsiSkPictureFactory.h @@ -22,7 +22,7 @@ class JsiSkPictureFactory : public JsiSkHostObject { public: JSI_HOST_FUNCTION(MakePicture) { if(!arguments[0].isObject()) { - jsi::detail::throwJSError(runtime, "Expected arraybuffer as first parameter"); + throw jsi::JSError(runtime, "Expected arraybuffer as first parameter"); } auto array = arguments[0].asObject(runtime); jsi::ArrayBuffer buffer = array diff --git a/package/cpp/api/JsiSkRuntimeEffect.h b/package/cpp/api/JsiSkRuntimeEffect.h index b42e59fd92..e4a638adfe 100644 --- a/package/cpp/api/JsiSkRuntimeEffect.h +++ b/package/cpp/api/JsiSkRuntimeEffect.h @@ -35,7 +35,7 @@ namespace RNSkia : public JsiSkWrappingSkPtrHostObject { public: - + static sk_sp fromValue(jsi::Runtime &runtime, const jsi::Value &obj) { const auto& object = obj.asObject(runtime); return object.asHostObject(runtime)->getObject(); @@ -44,7 +44,7 @@ namespace RNSkia JSI_HOST_FUNCTION(makeShader) { auto uniforms = castUniforms(runtime, arguments[0]); - + auto matrix = count >= 2 && !arguments[1].isUndefined() && !arguments[1].isNull() ? JsiSkMatrix::fromValue(runtime, arguments[1]).get() : nullptr; // Create and return shader as host object @@ -57,7 +57,7 @@ namespace RNSkia JSI_HOST_FUNCTION(makeShaderWithChildren) { auto uniforms = castUniforms(runtime, arguments[0]); - + // Children std::vector> children; auto jsiChildren = arguments[1].asObject(runtime).asArray(runtime); @@ -96,7 +96,7 @@ namespace RNSkia { auto i = static_cast(arguments[0].asNumber()); if (i < 0 || i >= getObject()->uniforms().size()) { - jsi::detail::throwJSError(runtime, "invalid uniform index"); + throw jsi::JSError(runtime, "invalid uniform index"); } auto it = getObject()->uniforms().begin() + i; return jsi::String::createFromAscii(runtime, it->name.c_str()); @@ -106,7 +106,7 @@ namespace RNSkia { auto i = static_cast(arguments[0].asNumber()); if (i < 0 || i >= getObject()->uniforms().size()) { - jsi::detail::throwJSError(runtime, "invalid uniform index"); + throw jsi::JSError(runtime, "invalid uniform index"); } auto it = getObject()->uniforms().begin() + i; auto result = jsi::Object(runtime); @@ -144,7 +144,7 @@ namespace RNSkia std::to_string(jsiUniformsSize) + " expected " + std::to_string(getObject()->uniformSize() / sizeof(float)); - jsi::detail::throwJSError(runtime, msg.c_str()); + throw jsi::JSError(runtime, msg.c_str()); } auto uniforms = SkData::MakeUninitialized(getObject()->uniformSize()); diff --git a/package/cpp/api/JsiSkRuntimeEffectFactory.h b/package/cpp/api/JsiSkRuntimeEffectFactory.h index b2152eb0bf..36091c40be 100644 --- a/package/cpp/api/JsiSkRuntimeEffectFactory.h +++ b/package/cpp/api/JsiSkRuntimeEffectFactory.h @@ -21,7 +21,7 @@ class JsiSkRuntimeEffectFactory : public JsiSkHostObject { auto effect = result.effect; auto errorText = result.errorText; if (!effect) { - jsi::detail::throwJSError( + throw jsi::JSError( runtime, std::string("Error in sksl:\n" + std::string(errorText.c_str())) .c_str()); diff --git a/package/cpp/rnskia/RNSkJsiViewApi.h b/package/cpp/rnskia/RNSkJsiViewApi.h index 3c0d8ae3a0..725a1cff44 100644 --- a/package/cpp/rnskia/RNSkJsiViewApi.h +++ b/package/cpp/rnskia/RNSkJsiViewApi.h @@ -44,16 +44,16 @@ class RNSkJsiViewApi : public JsiHostObject, public std::enable_shared_from_this "setJsiProperty: First argument must be a number"); return jsi::Value::undefined(); } - + if (!arguments[1].isString()) { _platformContext->raiseError( "setJsiProperty: Second argument must be the name of the property to set."); - + return jsi::Value::undefined(); } - auto nativeId = arguments[0].asNumber(); + auto nativeId = arguments[0].asNumber(); auto info = getEnsuredViewInfo(nativeId); - + std::lock_guard lock(_mutex); info->props.emplace(arguments[1].asString(runtime).utf8(runtime), JsiValueWrapper(runtime, arguments[2])); @@ -64,10 +64,10 @@ class RNSkJsiViewApi : public JsiHostObject, public std::enable_shared_from_this info->view->setJsiProperties(info->props); info->props.clear(); } - + return jsi::Value::undefined(); } - + /** Calls a custom command / method on a view by the view id. */ @@ -76,34 +76,34 @@ class RNSkJsiViewApi : public JsiHostObject, public std::enable_shared_from_this _platformContext->raiseError( std::string("callCustomCommand: Expected at least 2 arguments, got " + std::to_string(count) + ".")); - + return jsi::Value::undefined(); } if (!arguments[0].isNumber()) { _platformContext->raiseError( "callCustomCommand: First argument must be a number"); - + return jsi::Value::undefined(); } - + if (!arguments[1].isString()) { _platformContext->raiseError( "callCustomCommand: Second argument must be the name of the action to call."); - + return jsi::Value::undefined(); } - + auto nativeId = arguments[0].asNumber(); auto action = arguments[1].asString(runtime).utf8(runtime); - + auto info = getEnsuredViewInfo(nativeId); - + if(info->view == nullptr) { - jsi::detail::throwJSError(runtime, + throw jsi::JSError(runtime, std::string("callCustomCommand: Could not call action " + action + " on view - view not ready.").c_str()); - + return jsi::Value::undefined(); } @@ -112,7 +112,7 @@ class RNSkJsiViewApi : public JsiHostObject, public std::enable_shared_from_this const jsi::Value* params = paramsCount > 0 ? &arguments[2] : nullptr; return info->view->callJsiMethod(runtime, action, params, paramsCount); } - + JSI_HOST_FUNCTION(requestRedraw) { if (count < 2) { _platformContext->raiseError( @@ -137,7 +137,7 @@ class RNSkJsiViewApi : public JsiHostObject, public std::enable_shared_from_this } return jsi::Value::undefined(); } - + JSI_HOST_FUNCTION(makeImageSnapshot) { if (count < 1) { _platformContext->raiseError(std::string("makeImageSnapshot: Expected at least 1 argument, got " + std::to_string(count) + ".")); @@ -148,7 +148,7 @@ class RNSkJsiViewApi : public JsiHostObject, public std::enable_shared_from_this _platformContext->raiseError("makeImageSnapshot: First argument must be a number"); return jsi::Value::undefined(); } - + // find Skia view int nativeId = arguments[0].asNumber(); sk_sp image; @@ -161,25 +161,25 @@ class RNSkJsiViewApi : public JsiHostObject, public std::enable_shared_from_this image = info->view->makeImageSnapshot(nullptr); } if(image == nullptr) { - jsi::detail::throwJSError(runtime, "Could not create image from current surface."); + throw jsi::JSError(runtime, "Could not create image from current surface."); return jsi::Value::undefined(); } return jsi::Object::createFromHostObject(runtime, std::make_shared(_platformContext, image)); } - jsi::detail::throwJSError(runtime, "No Skia View currently available."); + throw jsi::JSError(runtime, "No Skia View currently available."); return jsi::Value::undefined(); } - + JSI_HOST_FUNCTION(registerValuesInView) { // Check params if(!arguments[1].isObject() || !arguments[1].asObject(runtime).isArray(runtime)) { - jsi::detail::throwJSError(runtime, "Expected array of Values as second parameter"); + throw jsi::JSError(runtime, "Expected array of Values as second parameter"); return jsi::Value::undefined(); } - + // Get identifier of native SkiaView int nativeId = arguments[0].asNumber(); - + // Get values that should be added as dependencies auto values = arguments[1].asObject(runtime).asArray(runtime); std::vector> unsubscribers; @@ -187,7 +187,7 @@ class RNSkJsiViewApi : public JsiHostObject, public std::enable_shared_from_this unsubscribers.reserve(size); for(size_t i=0; i(runtime); - + if(value != nullptr) { // Add change listener unsubscribers.push_back(value->addListener([weakSelf = weak_from_this(), nativeId](jsi::Runtime&){ @@ -201,7 +201,7 @@ class RNSkJsiViewApi : public JsiHostObject, public std::enable_shared_from_this })); } } - + // Return unsubscribe method that unsubscribes to all values // that we subscribed to. return jsi::Function::createFromHostFunction(runtime, @@ -215,7 +215,7 @@ class RNSkJsiViewApi : public JsiHostObject, public std::enable_shared_from_this return jsi::Value::undefined(); }); } - + JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(RNSkJsiViewApi, setJsiProperty), JSI_EXPORT_FUNC(RNSkJsiViewApi, callJsiMethod), JSI_EXPORT_FUNC(RNSkJsiViewApi, registerValuesInView), @@ -272,12 +272,12 @@ class RNSkJsiViewApi : public JsiHostObject, public std::enable_shared_from_this return; } auto info = getEnsuredViewInfo(nativeId); - + std::lock_guard lock(_mutex); info->view = nullptr; _viewInfos.erase(nativeId); } - + /** Sets a skia draw view for the given id. This function can be used to mark that an underlying SkiaView is not available (it could be @@ -314,7 +314,7 @@ class RNSkJsiViewApi : public JsiHostObject, public std::enable_shared_from_this } return &_viewInfos.at(nativeId); } - + std::unordered_map _viewInfos; std::shared_ptr _platformContext; std::mutex _mutex; diff --git a/package/cpp/rnskia/values/RNSkComputedValue.h b/package/cpp/rnskia/values/RNSkComputedValue.h index ea958725c4..b871b70bb2 100644 --- a/package/cpp/rnskia/values/RNSkComputedValue.h +++ b/package/cpp/rnskia/values/RNSkComputedValue.h @@ -31,32 +31,32 @@ class RNSkComputedValue : public RNSkReadonlyValue // Verify input if(!arguments[0].isObject() || !arguments[0].asObject(runtime).isFunction(runtime)) { - jsi::detail::throwJSError(runtime, "Expected callback function as first parameter"); + throw jsi::JSError(runtime, "Expected callback function as first parameter"); } - + if(!arguments[1].isObject() || !arguments[1].asObject(runtime).isArray(runtime)) { - jsi::detail::throwJSError(runtime, "Expected array of dependencies as second parameter"); + throw jsi::JSError(runtime, "Expected array of dependencies as second parameter"); } - + // Get callback for calculating result _callback = std::make_shared(arguments[0].asObject(runtime).asFunction(runtime)); } - + void invalidate() override { RNSkReadonlyValue::invalidate(); - + // Unregister listeners for(const auto &unsubscribe: _unsubscribers) { unsubscribe(); } _unsubscribers.clear(); } - + void initializeDependencies(jsi::Runtime &runtime, const jsi::Value *arguments, size_t count) { // Save dependencies std::vector> dependencies; - + // Ensure that all dependencies are Values auto deps = arguments[1].asObject(runtime).asArray(runtime); const std::size_t size = deps.size(runtime); @@ -73,7 +73,7 @@ class RNSkComputedValue : public RNSkReadonlyValue } dependencies.push_back(value); } - + // register change handler on dependencies _unsubscribers.reserve(_unsubscribers.size() + size); for(const auto &dep: dependencies) { @@ -85,11 +85,11 @@ class RNSkComputedValue : public RNSkReadonlyValue } })); } - + // Set initial value dependencyUpdated(runtime); } - + private: void dependencyUpdated(jsi::Runtime &runtime) { // Calculate new value diff --git a/package/cpp/rnskia/values/RNSkReadonlyValue.h b/package/cpp/rnskia/values/RNSkReadonlyValue.h index 8bd0da750d..a779bbd105 100644 --- a/package/cpp/rnskia/values/RNSkReadonlyValue.h +++ b/package/cpp/rnskia/values/RNSkReadonlyValue.h @@ -29,7 +29,7 @@ class RNSkReadonlyValue : public JsiSkHostObject, : JsiSkHostObject(platformContext), _valueHolder(std::make_unique(*platformContext->getJsRuntime())) { } - + virtual ~RNSkReadonlyValue() { invalidate(); } @@ -37,21 +37,21 @@ class RNSkReadonlyValue : public JsiSkHostObject, JSI_PROPERTY_GET(__typename__) { return jsi::String::createFromUtf8(runtime, "RNSkValue"); } - + JSI_PROPERTY_GET(current) { return getCurrent(runtime); } - + JSI_EXPORT_PROPERTY_GETTERS(JSI_EXPORT_PROP_GET(RNSkReadonlyValue, __typename__), JSI_EXPORT_PROP_GET(RNSkReadonlyValue, current)) - + JSI_HOST_FUNCTION(addListener) { if(!arguments[0].isObject() || !arguments[0].asObject(runtime).isFunction(runtime)) { - jsi::detail::throwJSError(runtime, "Expected function as first parameter."); + throw jsi::JSError(runtime, "Expected function as first parameter."); return jsi::Value::undefined(); } auto callback = std::make_shared(arguments[0].asObject(runtime).asFunction(runtime)); - + auto unsubscribe = addListener([weakSelf = weak_from_this(), callback = std::move(callback)](jsi::Runtime& runtime){ auto self = weakSelf.lock(); @@ -60,7 +60,7 @@ class RNSkReadonlyValue : public JsiSkHostObject, callback->call(runtime, selfReadonlyValue->get_current(runtime)); } }); - + return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "unsubscribe"), 0, @@ -69,17 +69,17 @@ class RNSkReadonlyValue : public JsiSkHostObject, return jsi::Value::undefined(); }); } - - - + + + JSI_HOST_FUNCTION(__invalidate) { invalidate(); return jsi::Value::undefined(); } - + JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(RNSkReadonlyValue, addListener), JSI_EXPORT_FUNC(RNSkReadonlyValue, __invalidate)) - + /** * Adds a callback that will be called whenever the value changes * @param cb Callback @@ -96,7 +96,7 @@ class RNSkReadonlyValue : public JsiSkHostObject, } }; } - + /** Updates the underlying value and notifies all listeners about the change. Listeners are only notified if the value was actually changed for numeric, boolean and string @@ -111,7 +111,7 @@ class RNSkReadonlyValue : public JsiSkHostObject, notifyListeners(runtime); } } - + /** Override to implement invalidation logic for the value. In the base class this function clears all subscribers. @@ -120,13 +120,13 @@ class RNSkReadonlyValue : public JsiSkHostObject, std::lock_guard lock(_mutex); _listeners.clear(); } - + jsi::Value getCurrent(jsi::Runtime &runtime) { return _valueHolder->getCurrent(runtime); } - + protected: - + /** Notifies listeners about changes @param runtime Current JS Runtime @@ -141,7 +141,7 @@ class RNSkReadonlyValue : public JsiSkHostObject, listener.second(runtime); } } - + /** Removes a subscription listeners @param listenerId identifier of listener to remove @@ -153,7 +153,7 @@ class RNSkReadonlyValue : public JsiSkHostObject, private: std::unique_ptr _valueHolder; - + long _listenerId = 0; std::unordered_map> _listeners; std::mutex _mutex; diff --git a/package/cpp/rnskia/values/RNSkValue.h b/package/cpp/rnskia/values/RNSkValue.h index 8b20471a55..141632a5ec 100644 --- a/package/cpp/rnskia/values/RNSkValue.h +++ b/package/cpp/rnskia/values/RNSkValue.h @@ -26,22 +26,22 @@ class RNSkValue : public RNSkReadonlyValue update(runtime, arguments[0]); } } - + ~RNSkValue() { unsubscribe(); } - + JSI_PROPERTY_SET(current) { // When someone else is setting the value we need to stop any ongoing // animations unsubscribe(); update(runtime, value); } - + JSI_PROPERTY_SET(animation) { // Cancel existing animation unsubscribe(); - + // Verify input if(value.isObject() && value.asObject(runtime).isHostObject(runtime)) { auto animation = value.asObject(runtime).getHostObject(runtime); @@ -52,24 +52,24 @@ class RNSkValue : public RNSkReadonlyValue } else if(value.isUndefined() || value.isNull()) { // Do nothing - we've already unsubscribed } else { - jsi::detail::throwJSError(runtime, "Animation expected."); + throw jsi::JSError(runtime, "Animation expected."); } } - + JSI_PROPERTY_GET(animation) { if(_animation != nullptr) { return jsi::Object::createFromHostObject(runtime, _animation); } return jsi::Value::undefined(); } - + JSI_EXPORT_PROPERTY_SETTERS(JSI_EXPORT_PROP_SET(RNSkValue, current), JSI_EXPORT_PROP_SET(RNSkValue, animation)) - + JSI_EXPORT_PROPERTY_GETTERS(JSI_EXPORT_PROP_GET(RNSkReadonlyValue, __typename__), JSI_EXPORT_PROP_GET(RNSkValue, current), JSI_EXPORT_PROP_GET(RNSkValue, animation)) - + JSI_EXPORT_FUNCTIONS( JSI_EXPORT_FUNC(RNSkValue, addListener), JSI_EXPORT_FUNC(RNSkReadonlyValue, __invalidate) @@ -91,26 +91,26 @@ class RNSkValue : public RNSkReadonlyValue _animation->startClock(); } } - + void animationDidUpdate(jsi::Runtime& runtime) { if(_animation != nullptr) { // Update ourselves from the current animation value update(runtime, _animation->get_current(runtime)); } } - + void unsubscribe() { if(_unsubscribe != nullptr) { (*_unsubscribe)(); _unsubscribe = nullptr; } - + if(_animation != nullptr) { _animation->stopClock(); _animation = nullptr; } } - + std::shared_ptr _animation; std::shared_ptr> _unsubscribe; }; diff --git a/package/cpp/utils/RNSkLog.h b/package/cpp/utils/RNSkLog.h index 5ffbc137a7..8bd1ee0daa 100644 --- a/package/cpp/utils/RNSkLog.h +++ b/package/cpp/utils/RNSkLog.h @@ -54,24 +54,24 @@ class RNSkLogger { #endif va_end(args); } - + static void logToJavascriptConsole(jsi::Runtime& runtime, const std::string& message) { auto console = RNSkLogger::getJavascriptConsole(runtime).asObject(runtime); auto log = console.getPropertyAsFunction(runtime, "log"); log.call(runtime, jsi::String::createFromUtf8(runtime, message)); } - + static void warnToJavascriptConsole(jsi::Runtime& runtime, const std::string& message) { auto console = RNSkLogger::getJavascriptConsole(runtime).asObject(runtime); auto warn = console.getPropertyAsFunction(runtime, "warn"); warn.call(runtime, jsi::String::createFromUtf8(runtime, message)); } - + private: static jsi::Value getJavascriptConsole(jsi::Runtime& runtime) { auto console = runtime.global().getProperty(runtime, "console"); if(console.isUndefined() || console.isNull()) { - jsi::detail::throwJSError(runtime, "Could not find console object."); + throw jsi::JSError(runtime, "Could not find console object."); return jsi::Value::undefined(); } return console;