objc-js 0.0.14 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
package/package.json CHANGED
@@ -39,7 +39,7 @@
39
39
  "format": "prettier --write \"**/*.{ts,js,json,md}\"",
40
40
  "preinstall-disabled": "npm run build-scripts && npm run make-clangd-config"
41
41
  },
42
- "version": "0.0.14",
42
+ "version": "0.0.15",
43
43
  "description": "Objective-C bridge for Node.js",
44
44
  "main": "dist/index.js",
45
45
  "dependencies": {
@@ -173,6 +173,10 @@ template <typename T>
173
173
  T ConvertToNativeValue(const Napi::Value &value,
174
174
  const ObjcArgumentContext &context) {
175
175
  if constexpr (std::is_same_v<T, id>) {
176
+ // Handle null/undefined as nil
177
+ if (value.IsNull() || value.IsUndefined()) {
178
+ return nil;
179
+ }
176
180
  // is value an ObjcObject instance?
177
181
  if (value.IsObject()) {
178
182
  Napi::Object obj = value.As<Napi::Object>();
@@ -183,6 +187,10 @@ T ConvertToNativeValue(const Napi::Value &value,
183
187
  }
184
188
  }
185
189
  if constexpr (std::is_same_v<T, SEL>) {
190
+ // Handle null/undefined as NULL selector
191
+ if (value.IsNull() || value.IsUndefined()) {
192
+ return nullptr;
193
+ }
186
194
  if (!value.IsString()) {
187
195
  throw Napi::TypeError::New(value.Env(),
188
196
  CONVERT_ARG_ERROR_MSG("Expected a string"));
@@ -191,18 +199,30 @@ T ConvertToNativeValue(const Napi::Value &value,
191
199
  return sel_registerName(selName.c_str());
192
200
  }
193
201
  if constexpr (std::is_same_v<T, bool>) {
202
+ // Handle null/undefined as false
203
+ if (value.IsNull() || value.IsUndefined()) {
204
+ return false;
205
+ }
194
206
  if (!value.IsBoolean()) {
195
207
  throw Napi::TypeError::New(value.Env(),
196
208
  CONVERT_ARG_ERROR_MSG("Expected a boolean"));
197
209
  }
198
210
  return value.As<Napi::Boolean>().Value();
199
211
  } else if constexpr (std::is_same_v<T, std::string>) {
212
+ // Handle null/undefined as empty string
213
+ if (value.IsNull() || value.IsUndefined()) {
214
+ return std::string();
215
+ }
200
216
  if (!value.IsString()) {
201
217
  throw Napi::TypeError::New(value.Env(),
202
218
  CONVERT_ARG_ERROR_MSG("Expected a string"));
203
219
  }
204
220
  return value.As<Napi::String>().Utf8Value();
205
221
  } else if constexpr (std::is_arithmetic_v<T>) {
222
+ // Handle null/undefined as 0
223
+ if (value.IsNull() || value.IsUndefined()) {
224
+ return static_cast<T>(0);
225
+ }
206
226
  if (value.IsNumber()) {
207
227
  return ConvertJSNumberToNativeValue<T>(value, context);
208
228
  } else if (value.IsBigInt()) {
@@ -478,13 +478,7 @@ inline void SetInvocationReturnFromJS(NSInvocation *invocation,
478
478
  ObjcObject *objcObj = Napi::ObjectWrap<ObjcObject>::Unwrap(resultObj);
479
479
  id objcValue = objcObj->objcObject;
480
480
  [invocation setReturnValue:&objcValue];
481
- } else if (result.IsNull() || result.IsUndefined()) {
482
- id nilValue = nil;
483
- [invocation setReturnValue:&nilValue];
484
481
  }
485
- } else if (result.IsNull() || result.IsUndefined()) {
486
- id nilValue = nil;
487
- [invocation setReturnValue:&nilValue];
488
482
  }
489
483
  break;
490
484
  }