From fe528506dcb20f76eb2ac956fef35947b767ae02 Mon Sep 17 00:00:00 2001
From: Li Feng
Date: Fri, 16 Feb 2024 15:15:51 +1100
Subject: [PATCH] composer support lib
---
support-lib/composer-ts/module.yaml | 5 +++++
support-lib/composer-ts/src/Outcome.ts | 25 +++++++++++++++++++++
support-lib/composer-ts/src/ProtoSupport.ts | 13 +++++++++++
support-lib/composer-ts/tsconfig.json | 9 ++++++++
support-lib/composer/DataRef_composer.cpp | 10 +++++----
support-lib/composer/djinni_composer.cpp | 8 ++++---
support-lib/composer/djinni_composer.hpp | 23 +++++++++++++++----
7 files changed, 82 insertions(+), 11 deletions(-)
create mode 100644 support-lib/composer-ts/module.yaml
create mode 100644 support-lib/composer-ts/src/Outcome.ts
create mode 100644 support-lib/composer-ts/src/ProtoSupport.ts
create mode 100644 support-lib/composer-ts/tsconfig.json
diff --git a/support-lib/composer-ts/module.yaml b/support-lib/composer-ts/module.yaml
new file mode 100644
index 00000000..4e0350be
--- /dev/null
+++ b/support-lib/composer-ts/module.yaml
@@ -0,0 +1,5 @@
+output_target: release
+dependencies:
+ - composer_core
+ - coreutils
+ - foundation
diff --git a/support-lib/composer-ts/src/Outcome.ts b/support-lib/composer-ts/src/Outcome.ts
new file mode 100644
index 00000000..6a2e48d8
--- /dev/null
+++ b/support-lib/composer-ts/src/Outcome.ts
@@ -0,0 +1,25 @@
+/**
+ * Copyright 2021 Snap, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+export interface ResultOutcome {
+ result: T;
+}
+
+export interface ErrorOutcome {
+ error: E;
+}
+
+export type Outcome = ResultOutcome | ErrorOutcome;
diff --git a/support-lib/composer-ts/src/ProtoSupport.ts b/support-lib/composer-ts/src/ProtoSupport.ts
new file mode 100644
index 00000000..d5f0baa4
--- /dev/null
+++ b/support-lib/composer-ts/src/ProtoSupport.ts
@@ -0,0 +1,13 @@
+import { Arena } from 'composer_protobuf/src/Arena';
+
+declare const global: any;
+
+export function registerProtobufLib(name: string, protolib: any) {
+ if (!('protoSupport' in global)) {
+ global.protoSupport = {
+ Arena: Arena,
+ protoLibs: {},
+ };
+ }
+ global.protoSupport.protoLibs[name] = protolib;
+}
diff --git a/support-lib/composer-ts/tsconfig.json b/support-lib/composer-ts/tsconfig.json
new file mode 100644
index 00000000..46f3874f
--- /dev/null
+++ b/support-lib/composer-ts/tsconfig.json
@@ -0,0 +1,9 @@
+{
+ "extends": "../_configs/base.tsconfig.json",
+ "compilerOptions": {
+ "noImplicitReturns": true,
+ "noImplicitAny": true,
+ "strict": true,
+ "jsx": "preserve"
+ }
+}
diff --git a/support-lib/composer/DataRef_composer.cpp b/support-lib/composer/DataRef_composer.cpp
index dbf3340e..9b7e8034 100644
--- a/support-lib/composer/DataRef_composer.cpp
+++ b/support-lib/composer/DataRef_composer.cpp
@@ -40,18 +40,20 @@ class DataRefComposer: public DataRef::Impl {
// take over a std::vector's buffer without copying it
explicit DataRefComposer(std::vector&& vec) {
auto container = Composer::makeShared();
- auto bytes = vec.data();
- auto len = vec.size();
container->_data = std::move(vec);
+ const auto& containedVec = std::get>(container->_data);
+ auto bytes = containedVec.data();
+ auto len = containedVec.size();
_array = Composer::makeShared(Composer::kDefaultTypedArrayType,
Composer::BytesView(container, bytes, len));
}
// take over a std::string's buffer without copying it
explicit DataRefComposer(std::string&& str) {
auto container = Composer::makeShared();
- auto bytes = reinterpret_cast(str.data());
- auto len = str.size();
container->_data = std::move(str);
+ const std::string& containedStr = std::get(container->_data);
+ auto bytes = reinterpret_cast(containedStr.data());
+ auto len = containedStr.size();
_array = Composer::makeShared(Composer::kDefaultTypedArrayType,
Composer::BytesView(container, bytes, len));
}
diff --git a/support-lib/composer/djinni_composer.cpp b/support-lib/composer/djinni_composer.cpp
index c540ea7d..8f550937 100644
--- a/support-lib/composer/djinni_composer.cpp
+++ b/support-lib/composer/djinni_composer.cpp
@@ -73,15 +73,17 @@ const ValueSchema& Binary::schema() {
}
Date::CppType Date::toCpp(const Date::ComposerType& v) {
- return {};
+ auto millisecondsSinceEpoch = std::chrono::milliseconds(static_cast(v.toDouble()));
+ return CppType(std::chrono::duration_cast(millisecondsSinceEpoch));
}
Date::ComposerType Date::fromCpp(const Date::CppType& c) {
- return {};
+ auto millisecondsSinceEpoch = std::chrono::duration_cast(c.time_since_epoch());
+ return Composer::Value(static_cast(millisecondsSinceEpoch.count()));
}
const ValueSchema& Date::schema() {
- static auto schema = ValueSchema::untyped();
+ static auto schema = ValueSchema::date();
return schema;
}
diff --git a/support-lib/composer/djinni_composer.hpp b/support-lib/composer/djinni_composer.hpp
index 6ec87d1c..ca85e172 100644
--- a/support-lib/composer/djinni_composer.hpp
+++ b/support-lib/composer/djinni_composer.hpp
@@ -343,7 +343,11 @@ class Void {
}
};
-template
+template
+struct CTS { char data[N]; };
+template CTS(const char(&)[N]) -> CTS;
+
+template
class Protobuf {
public:
using CppType = CppProto;
@@ -352,15 +356,26 @@ class Protobuf {
static CppType toCpp(ComposerType v)
{
- return {};
+ auto array = v.getTypedArrayRef();
+ auto buffer = array->getBuffer();
+ CppProto ret;
+ ret.ParseFromArray(buffer.data(), static_cast(buffer.size()));
+ return ret;
}
static ComposerType fromCpp(const CppType& c)
{
- return {};
+ std::vector cbuf(c.ByteSizeLong());
+ c.SerializeToArray(cbuf.data(), static_cast(cbuf.size()));
+ auto bytes = Composer::makeShared();
+ bytes->assignVec(std::move(cbuf));
+ auto array = Composer::makeShared(Composer::kDefaultTypedArrayType, bytes);
+ return Composer::Value(array);
}
+
static const Composer::ValueSchema& schema() {
- static auto schema = Composer::ValueSchema::untyped();
+ constexpr std::array jsClassName = {JsClassName.data...};
+ static auto schema = Composer::ValueSchema::proto(jsClassName.begin(), jsClassName.end());
return schema;
}
};