Skip to content

Commit

Permalink
composer support lib
Browse files Browse the repository at this point in the history
  • Loading branch information
li-feng-sc committed Feb 16, 2024
1 parent 849e0a4 commit fe52850
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 11 deletions.
5 changes: 5 additions & 0 deletions support-lib/composer-ts/module.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
output_target: release
dependencies:
- composer_core
- coreutils
- foundation
25 changes: 25 additions & 0 deletions support-lib/composer-ts/src/Outcome.ts
Original file line number Diff line number Diff line change
@@ -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<T> {
result: T;
}

export interface ErrorOutcome<E> {
error: E;
}

export type Outcome<T, E> = ResultOutcome<T> | ErrorOutcome<E>;
13 changes: 13 additions & 0 deletions support-lib/composer-ts/src/ProtoSupport.ts
Original file line number Diff line number Diff line change
@@ -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;
}
9 changes: 9 additions & 0 deletions support-lib/composer-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../_configs/base.tsconfig.json",
"compilerOptions": {
"noImplicitReturns": true,
"noImplicitAny": true,
"strict": true,
"jsx": "preserve"
}
}
10 changes: 6 additions & 4 deletions support-lib/composer/DataRef_composer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,20 @@ class DataRefComposer: public DataRef::Impl {
// take over a std::vector's buffer without copying it
explicit DataRefComposer(std::vector<uint8_t>&& vec) {
auto container = Composer::makeShared<ComposerDataObject>();
auto bytes = vec.data();
auto len = vec.size();
container->_data = std::move(vec);
const auto& containedVec = std::get<std::vector<uint8_t>>(container->_data);
auto bytes = containedVec.data();
auto len = containedVec.size();
_array = Composer::makeShared<Composer::ValueTypedArray>(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<ComposerDataObject>();
auto bytes = reinterpret_cast<uint8_t*>(str.data());
auto len = str.size();
container->_data = std::move(str);
const std::string& containedStr = std::get<std::string>(container->_data);
auto bytes = reinterpret_cast<const uint8_t*>(containedStr.data());
auto len = containedStr.size();
_array = Composer::makeShared<Composer::ValueTypedArray>(Composer::kDefaultTypedArrayType,
Composer::BytesView(container, bytes, len));
}
Expand Down
8 changes: 5 additions & 3 deletions support-lib/composer/djinni_composer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@ const ValueSchema& Binary::schema() {
}

Date::CppType Date::toCpp(const Date::ComposerType& v) {
return {};
auto millisecondsSinceEpoch = std::chrono::milliseconds(static_cast<int64_t>(v.toDouble()));
return CppType(std::chrono::duration_cast<std::chrono::system_clock::duration>(millisecondsSinceEpoch));
}

Date::ComposerType Date::fromCpp(const Date::CppType& c) {
return {};
auto millisecondsSinceEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(c.time_since_epoch());
return Composer::Value(static_cast<double>(millisecondsSinceEpoch.count()));
}

const ValueSchema& Date::schema() {
static auto schema = ValueSchema::untyped();
static auto schema = ValueSchema::date();
return schema;
}

Expand Down
23 changes: 19 additions & 4 deletions support-lib/composer/djinni_composer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@ class Void {
}
};

template<typename CppProto>
template<size_t N>
struct CTS { char data[N]; };
template<size_t N> CTS(const char(&)[N]) -> CTS<N>;

template<typename CppProto, CTS ... JsClassName>
class Protobuf {
public:
using CppType = CppProto;
Expand All @@ -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<int>(buffer.size()));
return ret;
}

static ComposerType fromCpp(const CppType& c)
{
return {};
std::vector<uint8_t> cbuf(c.ByteSizeLong());
c.SerializeToArray(cbuf.data(), static_cast<int>(cbuf.size()));
auto bytes = Composer::makeShared<Composer::Bytes>();
bytes->assignVec(std::move(cbuf));
auto array = Composer::makeShared<Composer::ValueTypedArray>(Composer::kDefaultTypedArrayType, bytes);
return Composer::Value(array);
}

static const Composer::ValueSchema& schema() {
static auto schema = Composer::ValueSchema::untyped();
constexpr std::array<const std::string_view, sizeof...(JsClassName)> jsClassName = {JsClassName.data...};
static auto schema = Composer::ValueSchema::proto(jsClassName.begin(), jsClassName.end());
return schema;
}
};
Expand Down

0 comments on commit fe52850

Please sign in to comment.