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

Size optimizations for Composer #167

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/source/ComposerGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class ComposerGenerator(spec: Spec) extends Generator(spec) {
w.wl("static CppType toCpp(const ComposerType& v);")
w.wl("static ComposerType fromCpp(const CppType& c);")
w.wl
w.wl("static const Composer::ValueSchema& schema();")
w.wl("static const Composer::ValueSchema& schema() noexcept;")
}
}), (w => {}))
writeCppFileGeneric(spec.composerOutFolder.get, helperNamespace(), composerFilenameStyle, spec.composerIncludePrefix) (ident.name, origin, refs.cpp, (w => {
Expand All @@ -219,7 +219,7 @@ class ComposerGenerator(spec: Spec) extends Generator(spec) {
}
w.wl("return Value(o);")
}
w.w(s"const ValueSchema& $helper::schema()").braced {
w.w(s"const ValueSchema& $helper::schema() noexcept").braced {
//FIXME: _djinnin_record_namespace?
w.wl(s"""static auto schema = ValueSchema::cls(STRING_LITERAL("${schemaTypeNameForRecord(ident)}"), false,""").bracedEnd(");") {
for (f <- r.fields) {
Expand All @@ -246,9 +246,9 @@ class ComposerGenerator(spec: Spec) extends Generator(spec) {
val helper = helperClass(ident)
writeHppFileGeneric(spec.composerOutFolder.get, helperNamespace(), composerFilenameStyle)(ident.name, origin, refs.hpp, Nil, (w => {
w.w(s"struct $helper : ::djinni::composer::JsInterface<$cls, $helper>").bracedSemi {
w.wl("static void registerSchema(bool resolve);")
w.wl("static const Composer::ValueSchema& schemaRef();")
w.wl("static const Composer::ValueSchema& schema();")
w.wl("static void registerSchema(bool resolve) noexcept;")
w.wl("static const Composer::ValueSchema& schemaRef() noexcept;")
w.wl("static const Composer::ValueSchema& schema() noexcept;")

// cpp marshal helper
if (i.ext.cpp) {
Expand All @@ -273,7 +273,7 @@ class ComposerGenerator(spec: Spec) extends Generator(spec) {
//static methods
val staticMethods = i.methods.filter(m => m.static)
if (!staticMethods.isEmpty) {
w.wl("static void djinniInitStaticMethods(Composer::Ref<Composer::ValueMap> m);")
w.wl("static void djinniInitStaticMethods(Composer::Ref<Composer::ValueMap> m) noexcept;")
}

//TODO ???
Expand Down Expand Up @@ -327,7 +327,7 @@ class ComposerGenerator(spec: Spec) extends Generator(spec) {
}
}
//value schema
w.w(s"static const ValueSchema& unresolvedSchema()").braced {
w.w(s"static const ValueSchema& unresolvedSchema() noexcept").braced {
w.wl(s"static auto schema = ValueSchema::cls(schemaName(), true,").bracedEnd(");") {
for (m <- i.methods.filter(m => !m.static)) {
w.w(s"""ClassPropertySchema(STRING_LITERAL("${idJs.method(m.ident)}"), ValueSchema::function(${stubRetSchema(m)},""").bracedEnd(")),") {
Expand All @@ -339,20 +339,20 @@ class ComposerGenerator(spec: Spec) extends Generator(spec) {
}
w.wl("return schema;")
}
w.w(s"void $helper::registerSchema(bool resolve)").braced {
w.w(s"void $helper::registerSchema(bool resolve) noexcept").braced {
//register dependent interfaces (params and return that are interfaces but not this one)
for (t <- refs.interfaces.filter(t => t != withNs(Some(helperNamespace), helperClass(ident)))) {
w.wl(s"${t}::registerSchema(resolve);")
}
w.wl("static std::once_flag flag[2];")
w.wl("std::call_once(flag[resolve ? 1 : 0], [resolve] { djinni::composer::registerSchemaImpl(unresolvedSchema(), resolve); });")
w.wl("static bool flag[2] = {false, false};")
w.wl("if (std::exchange(flag[resolve ? 1 : 0], true) == false) { djinni::composer::registerSchemaImpl(unresolvedSchema(), resolve); }")
}
// type reference
w.w(s"const ValueSchema& $helper::schemaRef()").braced {
w.w(s"const ValueSchema& $helper::schemaRef() noexcept").braced {
w.wl("static auto ref = ValueSchema::typeReference(ValueSchemaTypeReference::named(schemaName()));")
w.wl("return ref;")
}
w.w(s"const ValueSchema& $helper::schema()").braced {
w.w(s"const ValueSchema& $helper::schema() noexcept").braced {
w.wl(s"static auto schema = djinni::composer::getResolvedSchema<$helper>(schemaName());")
w.wl("return schema;")
}
Expand Down Expand Up @@ -385,7 +385,7 @@ class ComposerGenerator(spec: Spec) extends Generator(spec) {
val staticMethods = i.methods.filter(m => m.static && m.lang.js)
if (!staticMethods.isEmpty) {
// object for static methods
w.wl(s"void ${helper}::djinniInitStaticMethods(Ref<ValueMap> m)").braced {
w.wl(s"void ${helper}::djinniInitStaticMethods(Ref<ValueMap> m) noexcept").braced {
w.wl(s"""auto unresolvedStaticSchema = ValueSchema::cls(STRING_LITERAL("${schemaTypeNameForStaticInterface(ident)}"), false,""").bracedEnd(");") {
for (m <- staticMethods) {
w.w(s"""ClassPropertySchema(STRING_LITERAL("${idJs.method(m.ident)}"), ValueSchema::function(${stubRetSchema(m)},""").bracedEnd(")),") {
Expand Down
14 changes: 10 additions & 4 deletions src/source/YamlGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ class YamlGenerator(spec: Spec) extends Generator(spec) {
w.wl("objcpp:").nested { write(w, objcpp(td)) }
w.wl("java:").nested { write(w, java(td)) }
w.wl("jni:").nested { write(w, jni(td)) }
w.wl("wasm:").nested { write(w, wasm(td)) }
w.wl("composer:").nested { write(w, composer(td)) }
w.wl("ts:").nested {write(w, ts(td)) }
if (spec.wasmOutFolder.isDefined) {
w.wl("wasm:").nested { write(w, wasm(td)) }
}
if (spec.composerOutFolder.isDefined) {
w.wl("composer:").nested { write(w, composer(td)) }
}
if (spec.wasmOutFolder.isDefined || spec.composerOutFolder.isDefined) {
w.wl("ts:").nested {write(w, ts(td)) }
}
}

private def write(w: IndentWriter, m: Map[String, Any]) {
Expand Down Expand Up @@ -307,7 +313,7 @@ object YamlGenerator {
nested(td, key)(subKey).toString
} catch {
case e: java.util.NoSuchElementException => {
println(s"Warning: in ${td.origin}, missing field $key/$subKey")
// println(s"Warning: in ${td.origin}, missing field $key/$subKey")
"[unspecified]"
}
}
Expand Down
4 changes: 2 additions & 2 deletions support-lib/composer/Future_composer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ class FutureAdaptor

template<typename U>
struct ExceptionHandlingTraits<FutureAdaptor<U>> {
static Composer::Value handleNativeException(const std::exception& e, const Composer::ValueFunctionCallContext& callContext) {
static Composer::Value handleNativeException(const std::exception& e, const Composer::ValueFunctionCallContext& callContext) noexcept {
// store C++ exception in JS Error and raise in JS runtime
auto msg = STRING_FORMAT("C++: {}", e.what());
auto composerPromise = Composer::makeShared<Composer::ResolvablePromise>();
composerPromise->fulfill(Composer::Result<Composer::Value>(Composer::Error(std::move(msg))));
return Composer::Value(composerPromise);
}
static Composer::Value handleNativeException(const JsException& e, const Composer::ValueFunctionCallContext& callContext) {
static Composer::Value handleNativeException(const JsException& e, const Composer::ValueFunctionCallContext& callContext) noexcept {
// JS error passthrough
auto composerPromise = Composer::makeShared<Composer::ResolvablePromise>();
composerPromise->fulfill(Composer::Result<Composer::Value>(e.cause()));
Expand Down
16 changes: 8 additions & 8 deletions support-lib/composer/djinni_composer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ void checkForNull(void* ptr, const char* context) {
}
}

ValueSchema resolveSchema(const ValueSchema& unresolved, std::function<void()> registerSchemaFunc) {
ValueSchema resolveSchema(const ValueSchema& unresolved, std::function<void()> registerSchemaFunc) noexcept {
registerSchemaFunc();
Composer::ValueSchemaTypeResolver resolver(Composer::ValueSchemaRegistry::sharedInstance().get());
auto result = resolver.resolveTypeReferences(unresolved);
result.ensureSuccess();
return result.moveValue();
}

void registerSchemaImpl(const ValueSchema& schema, bool resolve) {
void registerSchemaImpl(const ValueSchema& schema, bool resolve) noexcept {
auto registry = ValueSchemaRegistry::sharedInstance();
if (!resolve) {
registry->registerSchema(schema);
Expand All @@ -54,35 +54,35 @@ void registerSchemaImpl(const ValueSchema& schema, bool resolve) {
}
}

Binary::CppType Binary::toCpp(const Binary::ComposerType& v) {
Binary::CppType Binary::toCpp(const Binary::ComposerType& v) noexcept {
auto a = v.getTypedArrayRef();
auto buffer = a->getBuffer();
return CppType(buffer.begin(), buffer.end());
}

Binary::ComposerType Binary::fromCpp(const Binary::CppType& c) {
Binary::ComposerType Binary::fromCpp(const Binary::CppType& c) noexcept {
auto bytes = makeShared<Bytes>();
bytes->assignData(c.data(), c.size());
auto va = makeShared<ValueTypedArray>(TypedArrayType::Uint8Array, bytes);
return Value(va);
}

const ValueSchema& Binary::schema() {
const ValueSchema& Binary::schema() noexcept {
static auto schema = ValueSchema::valueTypedArray();
return schema;
}

Date::CppType Date::toCpp(const Date::ComposerType& v) {
Date::CppType Date::toCpp(const Date::ComposerType& v) noexcept {
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) {
Date::ComposerType Date::fromCpp(const Date::CppType& c) noexcept {
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() {
const ValueSchema& Date::schema() noexcept {
static auto schema = ValueSchema::date();
return schema;
}
Expand Down
Loading
Loading