Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cpp/WKTJsRuntimeFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
// Hermes
#include <hermes/hermes.h>
#elif __has_include(<React-jsc/JSCRuntime.h>)
// JSC
#include <React-jsc/JSCRuntime.h>
// JSC
#include <React-jsc/JSCRuntime.h>
#else
#include <jsc/JSCRuntime.h>
#include <jsc/JSCRuntime.h>
#endif

namespace RNWorklet {
Expand Down
26 changes: 15 additions & 11 deletions cpp/WKTJsiWorklet.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ class JsiWorklet : public JsiHostObject,
/**
Returns true if the character is a whitespace character
*/
static bool isWhitespace(unsigned char c) {
return std::isspace(c);
}
static bool isWhitespace(unsigned char c) { return std::isspace(c); }

private:
/**
Expand Down Expand Up @@ -309,14 +307,19 @@ class JsiWorklet : public JsiHostObject,
.asString(runtime)
.utf8(runtime);
}

// Double-check if the code property is valid.
bool isCodeEmpty = std::all_of(_code.begin(), _code.end(), isWhitespace);
if (isCodeEmpty) {
std::string error = "Failed to create Worklet, the provided code is empty. Tips:\n"
"* Is the babel plugin correctly installed?\n"
"* If you are using react-native-reanimated, make sure the react-native-reanimated plugin does not override the react-native-worklets-core/plugin.\n"
"* Make sure the JS Worklet contains a \"" + std::string(PropNameWorkletInitDataCode) + "\" property with the function's code.";
std::string error =
"Failed to create Worklet, the provided code is empty. Tips:\n"
"* Is the babel plugin correctly installed?\n"
"* If you are using react-native-reanimated, make sure the "
"react-native-reanimated plugin does not override the "
"react-native-worklets-core/plugin.\n"
"* Make sure the JS Worklet contains a \"" +
std::string(PropNameWorkletInitDataCode) +
"\" property with the function's code.";
throw jsi::JSError(runtime, error);
}

Expand Down Expand Up @@ -360,10 +363,11 @@ class WorkletInvoker {
jsi::Value call(jsi::Runtime &runtime, const jsi::Value &thisValue,
const jsi::Value *arguments, size_t count) {
if (_workletFunction.get(runtime) == nullptr) {
_workletFunction.get(runtime) = _worklet->createWorkletJsFunction(runtime);
_workletFunction.get(runtime) =
_worklet->createWorkletJsFunction(runtime);
}
return _worklet->call(_workletFunction.get(runtime), runtime, thisValue, arguments,
count);
return _worklet->call(_workletFunction.get(runtime), runtime, thisValue,
arguments, count);
}

private:
Expand Down
31 changes: 30 additions & 1 deletion cpp/WKTJsiWorkletApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,39 @@ class JsiWorkletApi : public JsiHostObject {
});
}

JSI_HOST_FUNCTION(__jsi_is_array) {
if (count == 0) {
throw jsi::JSError(runtime, "__getTypeIsArray expects one parameter.");
}

if (arguments[0].isObject()) {
auto obj = arguments[0].asObject(runtime);
arguments[0].asObject(runtime).isArray(runtime);
auto isArray = obj.isArray(runtime);
return isArray;
}

return false;
}

JSI_HOST_FUNCTION(__jsi_is_object) {
if (count == 0) {
throw jsi::JSError(runtime, "__getTypeIsObject expects one parameter.");
}

if (arguments[0].isObject()) {
return true;
}

return false;
}

JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(JsiWorkletApi, createSharedValue),
JSI_EXPORT_FUNC(JsiWorkletApi, createContext),
JSI_EXPORT_FUNC(JsiWorkletApi, createRunInContextFn),
JSI_EXPORT_FUNC(JsiWorkletApi, createRunInJsFn))
JSI_EXPORT_FUNC(JsiWorkletApi, createRunInJsFn),
JSI_EXPORT_FUNC(JsiWorkletApi, __jsi_is_array),
JSI_EXPORT_FUNC(JsiWorkletApi, __jsi_is_object))

JSI_PROPERTY_GET(defaultContext) {
return jsi::Object::createFromHostObject(
Expand Down
2 changes: 1 addition & 1 deletion cpp/WKTJsiWorkletContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void JsiWorkletContext::initialize(

// Register main runtime
BaseRuntimeAwareCache::setMainJsRuntime(jsRuntime);

_name = name;
_jsRuntime = jsRuntime;
_jsCallInvoker = jsCallInvoker;
Expand Down
10 changes: 5 additions & 5 deletions cpp/WKTJsiWorkletContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ class JsiWorkletContext
return defaultInstance;
}

/**
* @brief Get the Default Instance object
*
* @return JsiWorkletContext*
*/
/**
* @brief Get the Default Instance object
*
* @return JsiWorkletContext*
*/
static JsiWorkletContext *getDefaultInstance() {
return JsiWorkletContext::getDefaultInstanceAsShared().get();
}
Expand Down
24 changes: 13 additions & 11 deletions cpp/sharedvalues/WKTJsiSharedValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ class JsiSharedValue : public JsiHostObject {
Constructs a shared value - which is a wrapped value that can be accessed in
a thread safe across two javascript runtimes.
*/
JsiSharedValue(const jsi::Value &value)
: _valueWrapper(JsiWrapper::wrap(*JsiWorkletContext::getDefaultInstance()->getJsRuntime(), value)) {}
explicit JsiSharedValue(const jsi::Value &value)
: _valueWrapper(JsiWrapper::wrap(
*JsiWorkletContext::getDefaultInstance()->getJsRuntime(), value,
nullptr, true)) {}

/**
Destructor
Expand All @@ -33,15 +35,13 @@ class JsiSharedValue : public JsiHostObject {
_valueWrapper->toString(runtime));
}

JSI_PROPERTY_GET(value) {
return _valueWrapper->unwrapAsProxyOrValue(runtime);
}
JSI_PROPERTY_GET(value) { return _valueWrapper->unwrap(runtime); }

JSI_PROPERTY_SET(value) {
if (_valueWrapper->canUpdateValue(runtime, value)) {
_valueWrapper->updateValue(runtime, value);
} else {
_valueWrapper = JsiWrapper::wrap(runtime, value);
_valueWrapper = JsiWrapper::wrap(runtime, value, nullptr, true);
}
}

Expand Down Expand Up @@ -72,14 +72,16 @@ class JsiSharedValue : public JsiHostObject {
};

// Do not Wrap this Value - replace with undefined
auto thisValuePtr = JsiWrapper::wrap(runtime, jsi::Value::undefined());
auto thisValuePtr =
JsiWrapper::wrap(runtime, jsi::Value::undefined(), nullptr, true);

auto dispatcher = JsiDispatcher::createDispatcher(
runtime, thisValuePtr, functionPtr, nullptr,
[&runtime, this](const char *err) {
JsiWorkletContext::getCurrent(runtime)->invokeOnJsThread([err](jsi::Runtime &runtime) {
throw jsi::JSError(runtime, err);
});
JsiWorkletContext::getCurrent(runtime)->invokeOnJsThread(
[err](jsi::Runtime &runtime) {
throw jsi::JSError(runtime, err);
});
});

// Set up the callback to run on the correct runtime thread.
Expand Down Expand Up @@ -121,6 +123,6 @@ class JsiSharedValue : public JsiHostObject {
}

private:
std::shared_ptr<JsiWrapper> _valueWrapper;
std::shared_ptr<JsiWrapper> _valueWrapper;
};
} // namespace RNWorklet
Loading