@@ -195,7 +195,19 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
195195 const jsi::Value *args,
196196 size_t count
197197 ) mutable -> jsi::Value {
198- return funPtr->callWithThis (rt, *jsThis, args, count);
198+ jsi::Value oldJSThis = rt.global ().getProperty (rt, " jsThis" );
199+ rt.global ().setProperty (rt, " jsThis" , *jsThis); // set jsThis
200+
201+ jsi::Value res = jsi::Value::undefined ();
202+
203+ if (thisValue.isObject ()) {
204+ res = funPtr->callWithThis (rt, thisValue.asObject (rt), args, count);
205+ } else {
206+ res = funPtr->call (rt, args, count);
207+ }
208+
209+ rt.global ().setProperty (rt, " jsThis" , oldJSThis); // clean jsThis
210+ return res;
199211 };
200212 return jsi::Function::createFromHostFunction (rt, jsi::PropNameID::forAscii (rt, " workletFunction" ), 0 , clb);
201213 } else {
@@ -230,11 +242,15 @@ jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
230242
231243 jsi::Value returnedValue;
232244
233- returnedValue = funPtr->callWithThis (rt,
234- jsThis,
245+ jsi::Value oldJSThis = rt.global ().getProperty (rt, " jsThis" );
246+ rt.global ().setProperty (rt, " jsThis" , jsThis); // set jsThis
247+
248+ returnedValue = funPtr->call (rt,
235249 static_cast <const jsi::Value*>(args),
236250 (size_t )params.size ());
237251
252+ rt.global ().setProperty (rt, " jsThis" , oldJSThis); // clean jsThis
253+
238254 delete [] args;
239255 // ToDo use returned value to return promise
240256 });
@@ -292,14 +308,15 @@ void MutableValue::set(jsi::Runtime &rt, const jsi::PropNameID &name, const jsi:
292308
293309 if (module ->isHostRuntime (rt)) {
294310 if (propName == " value" ) {
295- {
296- std::lock_guard<std::mutex> lock (readWriteMutex);
297- value = ShareableValue::adapt (rt, newValue, module );
298- }
299- module ->scheduler ->scheduleOnUI ([this ] {
300- for (auto listener : listeners) {
301- listener.second ();
302- }
311+ auto shareable = ShareableValue::adapt (rt, newValue, module );
312+ module ->scheduler ->scheduleOnUI ([this , shareable] {
313+ jsi::Runtime &rt = *this ->module ->runtime .get ();
314+ auto setterProxy = jsi::Object::createFromHostObject (rt, std::make_shared<MutableValueSetterProxy>(shared_from_this ()));
315+ jsi::Value newValue = shareable->getValue (rt);
316+ module ->valueSetter ->getValue (rt)
317+ .asObject (rt)
318+ .asFunction (rt)
319+ .callWithThis (rt, setterProxy, newValue);
303320 });
304321 }
305322 return ;
0 commit comments