@@ -58,10 +58,27 @@ customStyleProp={[styles.container, styles.otherProp]}
58
58
Copying a Unistyle style outside of a JSX element will remove its internal C++ state, leading to unexpected behavior.)" );
59
59
}
60
60
61
- inline static jsi::Object generateUnistylesPrototype (jsi::Runtime& rt, Unistyle::Shared unistyle) {
61
+ inline static jsi::Object generateUnistylesPrototype (
62
+ jsi::Runtime& rt,
63
+ std::shared_ptr<HybridUnistylesRuntime> unistylesRuntime,
64
+ Unistyle::Shared unistyle,
65
+ std::optional<Variants> variants,
66
+ std::optional<jsi::Array> arguments
67
+ ) {
62
68
// add prototype metadata for createUnistylesComponent
63
69
auto proto = jsi::Object (rt);
70
+ auto hostFn = jsi::Function::createFromHostFunction (rt, jsi::PropNameID::forUtf8 (rt, " getStyle" ), 0 , [unistyle, unistylesRuntime](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count){
71
+ auto variants = helpers::variantsToPairs (rt, thisValue.asObject (rt).getProperty (rt, " variants" ).asObject (rt));
72
+ auto arguments = helpers::parseDynamicFunctionArguments (rt, thisValue.asObject (rt).getProperty (rt, " arguments" ).asObject (rt).asArray (rt));
64
73
74
+ parser::Parser (unistylesRuntime).rebuildUnistyle (rt, unistyle->parent , unistyle, variants, std::make_optional<std::vector<folly::dynamic>>(arguments));
75
+
76
+ return jsi::Value (rt, unistyle->parsedStyle .value ()).asObject (rt);
77
+ });
78
+
79
+ proto.setProperty (rt, " getStyle" , std::move (hostFn));
80
+ proto.setProperty (rt, " arguments" , arguments.has_value () ? std::move (arguments.value ()) : jsi::Array (rt, 0 ));
81
+ proto.setProperty (rt, " variants" , variants.has_value () ? helpers::pairsToVariantsValue (rt, variants.value ()) : jsi::Object (rt));
65
82
proto.setProperty (rt, helpers::STYLE_DEPENDENCIES.c_str (), helpers::dependenciesToJSIArray (rt, unistyle->dependencies ));
66
83
67
84
return proto;
@@ -71,31 +88,31 @@ inline static std::vector<Unistyle::Shared> unistyleFromValue(jsi::Runtime& rt,
71
88
if (value.isNull () || !value.isObject ()) {
72
89
return {};
73
90
}
74
-
91
+
75
92
auto maybeArray = value.asObject (rt);
76
-
93
+
77
94
helpers::assertThat (rt, maybeArray.isArray (rt), " Unistyles: can't retrieve Unistyle state from node as it's not an array." );
78
-
95
+
79
96
std::vector<Unistyle::Shared> unistyles;
80
97
jsi::Array unistylesArray = maybeArray.asArray (rt);
81
-
98
+
82
99
helpers::iterateJSIArray (rt, unistylesArray, [&rt, &unistyles](size_t index , jsi::Value& value){
83
100
auto obj = value.getObject (rt);
84
101
85
102
// possible if user used React Native styles or inline styles or did spread styles
86
103
if (!obj.hasNativeState (rt)) {
87
104
auto exoticUnistyles = unistylesFromNonExistentNativeState (rt, obj);
88
-
105
+
89
106
for (auto & exoticUnistyle: exoticUnistyles) {
90
107
unistyles.emplace_back (exoticUnistyle);
91
108
}
92
-
109
+
93
110
return ;
94
111
}
95
112
96
113
unistyles.emplace_back (value.getObject (rt).getNativeState <UnistyleWrapper>(rt)->unistyle );
97
114
});
98
-
115
+
99
116
return unistyles;
100
117
}
101
118
@@ -111,7 +128,7 @@ inline static jsi::Value valueFromUnistyle(jsi::Runtime& rt, std::shared_ptr<Hyb
111
128
helpers::defineHiddenProperty (rt, obj, helpers::STYLE_DEPENDENCIES.c_str (), helpers::dependenciesToJSIArray (rt, unistyle->dependencies ));
112
129
helpers::mergeJSIObjects (rt, obj, unistyle->parsedStyle .value ());
113
130
114
- obj.setProperty (rt, " __proto__" , generateUnistylesPrototype (rt, unistyle));
131
+ obj.setProperty (rt, " __proto__" , generateUnistylesPrototype (rt, unistylesRuntime, unistyle, std::nullopt, std::nullopt ));
115
132
116
133
return obj;
117
134
}
0 commit comments