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: Perform safer checks for toContain(..) in example, and add NitroModules.hasNativeState(value) #324

Merged
merged 3 commits into from
Nov 12, 2024
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
5 changes: 4 additions & 1 deletion example/src/Testers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ export class State<T> {
toContain(key: keyof T): State<T> {
if (
this.result != null &&
(this.result[key] != null ||
(Object.hasOwn(this.result, key) ||
this.result[key] != null ||
// @ts-expect-error maybe a TypeScript bug? It's an object, so it's safe
(typeof key === 'object' && key in this.result) ||
Object.keys(this.result).includes(key as string))
) {
this.onPassed()
Expand Down
20 changes: 16 additions & 4 deletions example/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NitroModules } from 'react-native-nitro-modules'

export function stringify(value: unknown): string {
if (value == null) {
return 'null'
Expand All @@ -19,14 +21,24 @@ export function stringify(value: unknown): string {
}
try {
if ('toString' in value) {
const string = value.toString()
if (string !== '[object Object]') return string
if (Object.hasOwn(value, 'toString')) {
// It is some kind of Object that has a toString() method directly.
const string = value.toString()
if (string !== '[object Object]') return string
} else {
// It is some kind of Object that has a toString() method somewhere in the prototype chain..
// It is _likely_ that this method requires NativeState
if (NitroModules.hasNativeState(value)) {
// If we have NativeState, toString() will likely work.
const string = value.toString()
if (string !== '[object Object]') return string
}
}
}
return `{ ${value} ${Object.keys(value).join(', ')} }`
} catch {
// toString() threw - maybe because we accessed it on a prototype.
return `{ [Object] ${Object.keys(value).join(', ')} }`
}
return `{ [Object] ${Object.keys(value).join(', ')} }`
default:
return `${value}`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ void HybridNitroModulesProxy::loadHybridMethods() {

prototype.registerHybridMethod("box", &HybridNitroModulesProxy::box);

prototype.registerRawHybridMethod("hasNativeState", 1, &HybridNitroModulesProxy::hasNativeState);

prototype.registerHybridGetter("buildType", &HybridNitroModulesProxy::getBuildType);
});
}
Expand All @@ -43,6 +45,13 @@ std::shared_ptr<BoxedHybridObject> HybridNitroModulesProxy::box(const std::share
return std::make_shared<BoxedHybridObject>(hybridObject);
}

jsi::Value HybridNitroModulesProxy::hasNativeState(jsi::Runtime& runtime, const jsi::Value&, const jsi::Value* args, size_t size) {
if (size != 1 || !args[0].isObject()) {
return false;
}
return args[0].getObject(runtime).hasNativeState(runtime);
}

// Build Info
std::string HybridNitroModulesProxy::getBuildType() {
#ifdef NITRO_DEBUG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class HybridNitroModulesProxy : public HybridObject {

// Helpers
std::shared_ptr<BoxedHybridObject> box(const std::shared_ptr<HybridObject>& hybridObject);
jsi::Value hasNativeState(jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* args, size_t size);

// Build Info
std::string getBuildType();
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native-nitro-modules/src/NitroModulesProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ export interface NitroModulesProxy extends HybridObject {
* ```
*/
box<T extends HybridObject>(obj: T): BoxedHybridObject<T>

/**
* Returns whether the given {@linkcode object} has NativeState or not.
*/
hasNativeState(object: unknown): boolean
}
Loading