Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3386659
D8888.id30136.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
D8888.id30136.diff
View Options
diff --git a/native/cpp/CommonCpp/NativeModules/CommUtilsModule.h b/native/cpp/CommonCpp/NativeModules/CommUtilsModule.h
--- a/native/cpp/CommonCpp/NativeModules/CommUtilsModule.h
+++ b/native/cpp/CommonCpp/NativeModules/CommUtilsModule.h
@@ -32,6 +32,8 @@
virtual jsi::Object
base64DecodeBuffer(jsi::Runtime &rt, jsi::String base64) override;
virtual jsi::String sha256(jsi::Runtime &rt, jsi::Object data) override;
+ virtual jsi::String decodeString(jsi::Runtime &rt, jsi::Object data) override;
+ virtual jsi::Object encodeString(jsi::Runtime &rt, jsi::String str) override;
public:
CommUtilsModule(std::shared_ptr<facebook::react::CallInvoker> jsInvoker);
diff --git a/native/cpp/CommonCpp/NativeModules/CommUtilsModule.cpp b/native/cpp/CommonCpp/NativeModules/CommUtilsModule.cpp
--- a/native/cpp/CommonCpp/NativeModules/CommUtilsModule.cpp
+++ b/native/cpp/CommonCpp/NativeModules/CommUtilsModule.cpp
@@ -150,4 +150,30 @@
return jsi::String::createFromUtf8(rt, sha256String);
}
+jsi::String CommUtilsModule::decodeString(jsi::Runtime &rt, jsi::Object data) {
+ auto arrayBuffer = data.getArrayBuffer(rt);
+ auto dataPtr = arrayBuffer.data(rt);
+ auto size = arrayBuffer.size(rt);
+
+ auto bytes = std::vector<uint8_t>{dataPtr, dataPtr + size};
+ auto str = std::string(bytes.begin(), bytes.end());
+ return jsi::String::createFromUtf8(rt, str);
+}
+
+jsi::Object
+CommUtilsModule::encodeString(jsi::Runtime &rt, jsi::String inputStr) {
+ auto str = inputStr.utf8(rt);
+ auto bytes = std::vector<uint8_t>(str.begin(), str.end());
+ auto size = bytes.size();
+
+ auto arrayBuffer = rt.global()
+ .getPropertyAsFunction(rt, "ArrayBuffer")
+ .callAsConstructor(rt, {static_cast<double>(size)})
+ .asObject(rt)
+ .getArrayBuffer(rt);
+
+ memcpy(arrayBuffer.data(rt), bytes.data(), size);
+ return std::move(arrayBuffer);
+}
+
} // namespace comm
diff --git a/native/cpp/CommonCpp/_generated/utilsJSI-generated.cpp b/native/cpp/CommonCpp/_generated/utilsJSI-generated.cpp
--- a/native/cpp/CommonCpp/_generated/utilsJSI-generated.cpp
+++ b/native/cpp/CommonCpp/_generated/utilsJSI-generated.cpp
@@ -27,6 +27,12 @@
static jsi::Value __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_sha256(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
return static_cast<CommUtilsModuleSchemaCxxSpecJSI *>(&turboModule)->sha256(rt, args[0].asObject(rt));
}
+static jsi::Value __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_decodeString(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
+ return static_cast<CommUtilsModuleSchemaCxxSpecJSI *>(&turboModule)->decodeString(rt, args[0].asObject(rt));
+}
+static jsi::Value __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_encodeString(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {
+ return static_cast<CommUtilsModuleSchemaCxxSpecJSI *>(&turboModule)->encodeString(rt, args[0].asString(rt));
+}
CommUtilsModuleSchemaCxxSpecJSI::CommUtilsModuleSchemaCxxSpecJSI(std::shared_ptr<CallInvoker> jsInvoker)
: TurboModule("CommUtilsTurboModule", jsInvoker) {
@@ -35,6 +41,8 @@
methodMap_["base64EncodeBuffer"] = MethodMetadata {1, __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_base64EncodeBuffer};
methodMap_["base64DecodeBuffer"] = MethodMetadata {1, __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_base64DecodeBuffer};
methodMap_["sha256"] = MethodMetadata {1, __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_sha256};
+ methodMap_["decodeString"] = MethodMetadata {1, __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_decodeString};
+ methodMap_["encodeString"] = MethodMetadata {1, __hostFunction_CommUtilsModuleSchemaCxxSpecJSI_encodeString};
}
diff --git a/native/cpp/CommonCpp/_generated/utilsJSI.h b/native/cpp/CommonCpp/_generated/utilsJSI.h
--- a/native/cpp/CommonCpp/_generated/utilsJSI.h
+++ b/native/cpp/CommonCpp/_generated/utilsJSI.h
@@ -25,6 +25,8 @@
virtual jsi::String base64EncodeBuffer(jsi::Runtime &rt, jsi::Object data) = 0;
virtual jsi::Object base64DecodeBuffer(jsi::Runtime &rt, jsi::String base64) = 0;
virtual jsi::String sha256(jsi::Runtime &rt, jsi::Object data) = 0;
+ virtual jsi::String decodeString(jsi::Runtime &rt, jsi::Object data) = 0;
+ virtual jsi::Object encodeString(jsi::Runtime &rt, jsi::String str) = 0;
};
@@ -86,6 +88,22 @@
return bridging::callFromJs<jsi::String>(
rt, &T::sha256, jsInvoker_, instance_, std::move(data));
}
+ jsi::String decodeString(jsi::Runtime &rt, jsi::Object data) override {
+ static_assert(
+ bridging::getParameterCount(&T::decodeString) == 2,
+ "Expected decodeString(...) to have 2 parameters");
+
+ return bridging::callFromJs<jsi::String>(
+ rt, &T::decodeString, jsInvoker_, instance_, std::move(data));
+ }
+ jsi::Object encodeString(jsi::Runtime &rt, jsi::String str) override {
+ static_assert(
+ bridging::getParameterCount(&T::encodeString) == 2,
+ "Expected encodeString(...) to have 2 parameters");
+
+ return bridging::callFromJs<jsi::Object>(
+ rt, &T::encodeString, jsInvoker_, instance_, std::move(str));
+ }
private:
T *instance_;
diff --git a/native/schema/CommUtilsModuleSchema.js b/native/schema/CommUtilsModuleSchema.js
--- a/native/schema/CommUtilsModuleSchema.js
+++ b/native/schema/CommUtilsModuleSchema.js
@@ -15,6 +15,9 @@
+base64DecodeBuffer: (base64: string) => JSIArrayBuffer;
// crypto utils
+sha256: (data: JSIArrayBuffer) => string;
+ // encoding utils
+ +decodeString: (data: JSIArrayBuffer) => string;
+ +encodeString: (str: string) => JSIArrayBuffer;
}
// for public interface, we use the correct types
@@ -24,6 +27,8 @@
+base64EncodeBuffer: (data: ArrayBuffer) => string;
+base64DecodeBuffer: (base64: string) => ArrayBuffer;
+sha256: (data: ArrayBuffer) => string;
+ +decodeString: (data: ArrayBuffer) => string;
+ +encodeString: (str: string) => ArrayBuffer;
}
export default (TurboModuleRegistry.getEnforcing<Spec>(
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Nov 30, 5:35 AM (21 h, 44 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2599785
Default Alt Text
D8888.id30136.diff (6 KB)
Attached To
Mode
D8888: [client-backup] implement C++ functions for encoding/decoding string in `utf-8`
Attached
Detach File
Event Timeline
Log In to Comment