Skip to content

Commit

Permalink
Add PropNameID::fromSymbol
Browse files Browse the repository at this point in the history
Summary:
Changelog:
[General][Added] - Add ability to access properties with symbol keys through JSI

Reviewed By: mhorowitz

Differential Revision: D33830544

fbshipit-source-id: 8de366b4c7d5ea9d2fd5df70dfb776a056e23806
  • Loading branch information
neildhar authored and facebook-github-bot committed Feb 15, 2022
1 parent 3c4850d commit 9010bfe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ReactCommon/jsi/JSCRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class JSCRuntime : public jsi::Runtime {
jsi::PropNameID createPropNameIDFromUtf8(const uint8_t *utf8, size_t length)
override;
jsi::PropNameID createPropNameIDFromString(const jsi::String &str) override;
jsi::PropNameID createPropNameIDFromSymbol(const jsi::Symbol &sym) override;
std::string utf8(const jsi::PropNameID &) override;
bool compare(const jsi::PropNameID &, const jsi::PropNameID &) override;

Expand Down Expand Up @@ -653,6 +654,13 @@ jsi::PropNameID JSCRuntime::createPropNameIDFromString(const jsi::String &str) {
return createPropNameID(stringRef(str));
}

jsi::PropNameID JSCRuntime::createPropNameIDFromSymbol(const jsi::Symbol &sym) {
// TODO: Support for symbols through the native API in JSC is very limited.
// While we could construct a PropNameID here, we would not be able to get a
// symbol property through the C++ API.
throw std::logic_error("Not implemented");
}

std::string JSCRuntime::utf8(const jsi::PropNameID &sym) {
return JSStringToSTLString(stringRef(sym));
}
Expand Down
3 changes: 3 additions & 0 deletions ReactCommon/jsi/jsi/decorator.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation {
PropNameID createPropNameIDFromString(const String& str) override {
return plain_.createPropNameIDFromString(str);
};
PropNameID createPropNameIDFromSymbol(const Symbol& sym) override {
return plain_.createPropNameIDFromSymbol(sym);
};
std::string utf8(const PropNameID& id) override {
return plain_.utf8(id);
};
Expand Down
6 changes: 6 additions & 0 deletions ReactCommon/jsi/jsi/jsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ class JSI_EXPORT Runtime {
const uint8_t* utf8,
size_t length) = 0;
virtual PropNameID createPropNameIDFromString(const String& str) = 0;
virtual PropNameID createPropNameIDFromSymbol(const Symbol& sym) = 0;
virtual std::string utf8(const PropNameID&) = 0;
virtual bool compare(const PropNameID&, const PropNameID&) = 0;

Expand Down Expand Up @@ -425,6 +426,11 @@ class JSI_EXPORT PropNameID : public Pointer {
return runtime.createPropNameIDFromString(str);
}

/// Create a PropNameID from a JS symbol.
static PropNameID forSymbol(Runtime& runtime, const jsi::Symbol& sym) {
return runtime.createPropNameIDFromSymbol(sym);
}

// Creates a vector of PropNameIDs constructed from given arguments.
template <typename... Args>
static std::vector<PropNameID> names(Runtime& runtime, Args&&... args);
Expand Down

0 comments on commit 9010bfe

Please sign in to comment.