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

feat: Make every hybrid std::shared_ptr<T> nullable #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mrousavy
Copy link
Member

Makes every std::shared_ptr<T> nullable by default. We now have to treat every std::shared_ptr as nullable in our codebase.

We still need to think about if this change makes sense.

Before:

void test(std::shared_ptr<AnotherHostObject> hostObject) {
  hostObject->sayHello();
}

registerHybridMethod("test", &MyHostObject::test, this);
const anotherHostObject = getAnotherHostObject()
myHostObject.test(anotherHostObject) // <-- works
myHostObject.test(undefined) // <-- crashes with "jsi::HostObject cannot be casted" or something

After:

void test(std::shared_ptr<AnotherHostObject> hostObject) {
  if (hostObject == nullptr) {
    // do something if it is null, maybe throw an error, maybe default behaviour
  } else {
    hostObject->sayHello();
  }
}

registerHybridMethod("test", &MyHostObject::test, this);
const anotherHostObject = getAnotherHostObject()
myHostObject.test(anotherHostObject) // <-- works
myHostObject.test(undefined) // <-- works, uses nullable branch in code

Note

This was also possible before by simply using std::optional<std::shared_ptr<T>>, so this PR simply changes that shared ptrs are nullable by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant