Skip to content

Commit

Permalink
Using asSigned instead of int64_t
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed Apr 19, 2023
1 parent bb74833 commit fb444bb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/bindgen/src/realm_js_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,13 @@ auto schedulerWrapBlockingFunction(F&& f)
};
}

/**
* Helps with correct handling of -1/npos for count_t
*/
template <typename T>
auto asSigned(T num) {
return std::make_signed_t<T>(num);
}

} // namespace
} // namespace realm::js
8 changes: 4 additions & 4 deletions packages/bindgen/src/templates/jsi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ function convertPrimToJsi(addon: JsiAddon, type: string, expr: string): string {
return `jsi::Value(double(${expr}))`;

case "count_t":
// NOTE: using int64_t cast here to get -1.0 for size_t(-1), aka npos.
return `jsi::Value(double(int64_t(${expr})))`;
// NOTE: using asSigned() here to get -1.0 for size_t(-1), aka npos.
return `jsi::Value(double(asSigned(${expr})))`;

case "int64_t":
return `bigIntFromI64(_env, ${expr})`;
Expand Down Expand Up @@ -317,8 +317,8 @@ function convertPrimFromJsi(addon: JsiAddon, type: string, expr: string): string
return `int32_t((${expr}).asNumber())`;

case "count_t":
// NOTE: using int64_t here is important to correctly handle -1.0 aka npos.
return `size_t(int64_t((${expr}).asNumber()))`;
// NOTE: using size_t here is important to correctly handle -1.0 aka npos.
return `size_t(std::size_t((${expr}).asNumber()))`;

case "int64_t":
return `bigIntToI64(_env, jsi::Value(_env, ${expr}))`;
Expand Down
4 changes: 2 additions & 2 deletions packages/bindgen/src/templates/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ function convertPrimToNode(addon: NodeAddon, type: string, expr: string): string
return `Napi::Number::New(${env}, ${expr})`;

case "count_t":
// NOTE: using int64_t cast here to get -1.0 for size_t(-1), aka npos.
return `Napi::Number::New(${env}, int64_t(${expr}))`;
// NOTE: using asSigned() here to get -1.0 for size_t(-1), aka npos.
return `Napi::Number::New(${env}, asSigned(${expr}))`;

case "int64_t":
case "uint64_t":
Expand Down

0 comments on commit fb444bb

Please sign in to comment.