Skip to content

Commit

Permalink
fix: Fix boxing primitives in promise
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Sep 16, 2024
1 parent ae46acb commit ccaa130
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions packages/nitrogen/src/syntax/kotlin/KotlinCxxBridgedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,20 @@ export class KotlinCxxBridgedType implements BridgedType<'kotlin', 'c++'> {
}
}

getTypeCode(language: 'kotlin' | 'c++'): string {
getTypeCode(language: 'kotlin' | 'c++', isBoxed = false): string {
switch (this.type.kind) {
case 'number':
case 'bigint':
case 'boolean':
if (isBoxed) {
return getKotlinBoxedPrimitiveType(this.type)
} else {
if (this.type.kind === 'boolean' && language === 'c++') {
// JNI does not use `bool`, so it is a jboolean instead.
return 'jboolean'
}
return this.type.getCode(language)
}
case 'array':
switch (language) {
case 'c++':
Expand All @@ -188,13 +200,6 @@ export class KotlinCxxBridgedType implements BridgedType<'kotlin', 'c++'> {
default:
return this.type.getCode(language)
}
case 'boolean':
switch (language) {
case 'c++':
return 'jboolean'
default:
return this.type.getCode(language)
}
case 'string':
switch (language) {
case 'c++':
Expand Down Expand Up @@ -631,7 +636,7 @@ export class KotlinCxxBridgedType implements BridgedType<'kotlin', 'c++'> {
if (resultingType.hasType) {
// it's a Promise<T>
resolveBody = `
auto result = jni::static_ref_cast<${resultingType.getTypeCode('c++')}>(boxedResult);
auto result = jni::static_ref_cast<${resultingType.getTypeCode('c++', true)}>(boxedResult);
promise->set_value(${resultingType.parseFromKotlinToCpp('result', 'c++', true)});
`.trim()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ namespace margelo::nitro::image {
return [&]() {
auto promise = std::make_shared<std::promise<int64_t>>();
result->cthis()->addOnResolvedListener([=](const jni::alias_ref<jni::JObject>& boxedResult) {
auto result = jni::static_ref_cast<int64_t>(boxedResult);
auto result = jni::static_ref_cast<jni::JLong>(boxedResult);
promise->set_value(result->value());
});
result->cthis()->addOnRejectedListener([=](const jni::alias_ref<jni::JString>& message) {
Expand Down

0 comments on commit ccaa130

Please sign in to comment.