1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 #include <utility>
6 
7 #include <jsi/jsi.h>
8 
9 #include "JsiSkHostObjects.h"
10 #include "JsiSkRuntimeEffect.h"
11 
12 namespace RNSkia {
13 
14 namespace jsi = facebook::jsi;
15 
16 class JsiSkRuntimeEffectFactory : public JsiSkHostObject {
17 public:
JSI_HOST_FUNCTION(Make)18   JSI_HOST_FUNCTION(Make) {
19     auto sksl = arguments[0].asString(runtime).utf8(runtime);
20     auto result = SkRuntimeEffect::MakeForShader(SkString(sksl));
21     auto effect = result.effect;
22     auto errorText = result.errorText;
23     if (!effect) {
24       throw jsi::JSError(runtime, std::string("Error in sksl:\n" +
25                                               std::string(errorText.c_str()))
26                                       .c_str());
27       return jsi::Value::null();
28     }
29     return jsi::Object::createFromHostObject(
30         runtime,
31         std::make_shared<JsiSkRuntimeEffect>(getContext(), std::move(effect)));
32   }
33 
JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC (JsiSkRuntimeEffectFactory,Make))34   JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiSkRuntimeEffectFactory, Make))
35 
36   explicit JsiSkRuntimeEffectFactory(
37       std::shared_ptr<RNSkPlatformContext> context)
38       : JsiSkHostObject(std::move(context)) {}
39 };
40 
41 } // namespace RNSkia
42