Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: android native rejections should be instanceof Error #44487

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,70 @@ struct JNIArgs {
std::vector<jobject> globalRefs_;
};

jsi::Value createJSRuntimeError(
jsi::Runtime& runtime,
const std::string& message) {
return runtime.global()
.getPropertyAsFunction(runtime, "Error")
.call(runtime, message);
}

auto createRejectionError(jsi::Runtime& rt, folly::dynamic args) {
react_native_assert(
args.size() == 1 && "promise reject should has only one argument");

auto value = jsi::valueFromDynamic(rt, args[0]);
react_native_assert(value.isObject() && "promise reject should return a map");

const jsi::Object& valueAsObject = value.asObject(rt);

auto messageProperty = valueAsObject.getProperty(rt, "message");
auto jsError =
createJSRuntimeError(rt, messageProperty.asString(rt).utf8(rt));

auto jsErrorAsObject = jsError.asObject(rt);
auto propertyNames = valueAsObject.getPropertyNames(rt);
for (size_t i = 0; i < propertyNames.size(rt); ++i) {
auto propertyName = jsi::PropNameID::forString(
rt, propertyNames.getValueAtIndex(rt, i).asString(rt));
jsErrorAsObject.setProperty(
rt, propertyName, valueAsObject.getProperty(rt, propertyName));
}
return jsError;
}

auto createJavaCallback(
jsi::Runtime& rt,
jsi::Function&& function,
std::shared_ptr<CallInvoker> jsInvoker) {
std::shared_ptr<CallInvoker> jsInvoker,
bool returnAsJsError) {
std::optional<AsyncCallback<>> callback(
{rt, std::move(function), std::move(jsInvoker)});
return JCxxCallbackImpl::newObjectCxxArgs(
[callback = std::move(callback)](folly::dynamic args) mutable {
[callback = std::move(callback),
returnAsJsError](folly::dynamic args) mutable {
if (!callback) {
LOG(FATAL) << "Callback arg cannot be called more than once";
return;
}

callback->call([args = std::move(args)](
jsi::Runtime& rt, jsi::Function& jsFunction) {
std::vector<jsi::Value> jsArgs;
jsArgs.reserve(args.size());
for (const auto& val : args) {
jsArgs.emplace_back(jsi::valueFromDynamic(rt, val));
}
jsFunction.call(rt, (const jsi::Value*)jsArgs.data(), jsArgs.size());
});
if (returnAsJsError) {
callback->call([args = std::move(args)](
jsi::Runtime& rt, jsi::Function& jsFunction) {
auto error = createRejectionError(rt, std::move(args));
jsFunction.call(rt, error);
});
} else {
callback->call([args = std::move(args)](
jsi::Runtime& rt, jsi::Function& jsFunction) {
std::vector<jsi::Value> jsArgs;
jsArgs.reserve(args.size());
for (const auto& val : args) {
jsArgs.emplace_back(jsi::valueFromDynamic(rt, val));
}
jsFunction.call(
rt, (const jsi::Value*)jsArgs.data(), jsArgs.size());
});
}
callback = std::nullopt;
});
}
Expand Down Expand Up @@ -363,7 +405,7 @@ JNIArgs convertJSIArgsToJNIArgs(
}
jsi::Function fn = arg->getObject(rt).getFunction(rt);
jarg->l = makeGlobalIfNecessary(
createJavaCallback(rt, std::move(fn), jsInvoker).release());
createJavaCallback(rt, std::move(fn), jsInvoker, false).release());
} else if (type == "Lcom/facebook/react/bridge/ReadableArray;") {
if (!(arg->isObject() && arg->getObject(rt).isArray(rt))) {
throw JavaTurboModuleArgumentConversionException(
Expand Down Expand Up @@ -407,14 +449,6 @@ jsi::Value convertFromJMapToValue(JNIEnv* env, jsi::Runtime& rt, jobject arg) {
return jsi::valueFromDynamic(rt, result->cthis()->consume());
}

jsi::Value createJSRuntimeError(
jsi::Runtime& runtime,
const std::string& message) {
return runtime.global()
.getPropertyAsFunction(runtime, "Error")
.call(runtime, message);
}

/**
* Creates JSError with current JS runtime stack and Throwable stack trace.
*/
Expand Down Expand Up @@ -854,11 +888,13 @@ jsi::Value JavaTurboModule::invokeJavaMethod(
auto resolve = createJavaCallback(
runtime,
args[0].getObject(runtime).getFunction(runtime),
jsInvoker_);
jsInvoker_,
false);
auto reject = createJavaCallback(
runtime,
args[1].getObject(runtime).getFunction(runtime),
jsInvoker_);
jsInvoker_,
true);
javaPromise = JPromiseImpl::create(resolve, reject).release();

return jsi::Value::undefined();
Expand Down
Loading