objc-js 0.0.6 → 0.0.7

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/dist/index.js CHANGED
@@ -36,7 +36,12 @@ class NobjcObject {
36
36
  if (p === "toString")
37
37
  return true;
38
38
  // check if the object responds to the selector
39
- return target.$msgSend("respondsToSelector:", NobjcMethodNameToObjcSelector(p.toString()));
39
+ try {
40
+ return target.$msgSend("respondsToSelector:", NobjcMethodNameToObjcSelector(p.toString()));
41
+ }
42
+ catch (e) {
43
+ return false;
44
+ }
40
45
  },
41
46
  get(target, methodName, receiver) {
42
47
  // Return the underlying native object when Symbol is accessed
@@ -51,6 +56,23 @@ class NobjcObject {
51
56
  if (methodName === "toString") {
52
57
  return () => String(object.$msgSend("description"));
53
58
  }
59
+ // handle other built-in Object.prototype properties
60
+ const builtInProps = [
61
+ "constructor",
62
+ "valueOf",
63
+ "hasOwnProperty",
64
+ "isPrototypeOf",
65
+ "propertyIsEnumerable",
66
+ "toLocaleString",
67
+ "__proto__",
68
+ "__defineGetter__",
69
+ "__defineSetter__",
70
+ "__lookupGetter__",
71
+ "__lookupSetter__"
72
+ ];
73
+ if (builtInProps.includes(methodName)) {
74
+ return Reflect.get(target, methodName);
75
+ }
54
76
  if (!(methodName in receiver)) {
55
77
  throw new Error(`Method ${methodName} not found on object ${receiver}`);
56
78
  }
@@ -66,6 +88,12 @@ function unwrapArg(arg) {
66
88
  }
67
89
  return arg;
68
90
  }
91
+ function wrapObjCObjectIfNeeded(result) {
92
+ if (typeof result == "object" && result instanceof ObjcObject) {
93
+ return new NobjcObject(result);
94
+ }
95
+ return result;
96
+ }
69
97
  // Note: This is actually a factory function that returns a callable Proxy
70
98
  const NobjcMethod = function (object, methodName) {
71
99
  const selector = NobjcMethodNameToObjcSelector(methodName);
@@ -73,10 +101,7 @@ const NobjcMethod = function (object, methodName) {
73
101
  function methodFunc() {
74
102
  const unwrappedArgs = Array.from(arguments).map(unwrapArg);
75
103
  const result = object.$msgSend(selector, ...unwrappedArgs);
76
- if (typeof result == "object" && result instanceof ObjcObject) {
77
- return new NobjcObject(result);
78
- }
79
- return result;
104
+ return wrapObjCObjectIfNeeded(result);
80
105
  }
81
106
  const handler = {};
82
107
  return new Proxy(methodFunc, handler);
@@ -87,19 +112,15 @@ class NobjcProtocol {
87
112
  const convertedMethods = {};
88
113
  for (const [methodName, impl] of Object.entries(methodImplementations)) {
89
114
  const selector = NobjcMethodNameToObjcSelector(methodName);
90
- // Wrap the implementation to unwrap args and wrap return values
115
+ // Wrap the implementation to wrap args and unwrap return values
91
116
  convertedMethods[selector] = function (...args) {
92
- const unwrappedArgs = args.map(unwrapArg);
93
- const result = impl(...unwrappedArgs);
117
+ // Wrap native ObjcObject arguments in NobjcObject proxies
118
+ const wrappedArgs = args.map((arg) => {
119
+ return wrapObjCObjectIfNeeded(arg);
120
+ });
121
+ const result = impl(...wrappedArgs);
94
122
  // If the result is already a NobjcObject, unwrap it to get the native object
95
- if (result && typeof result === "object" && NATIVE_OBJC_OBJECT in result) {
96
- return result[NATIVE_OBJC_OBJECT];
97
- }
98
- // If the result is a native ObjcObject, return it as-is
99
- if (typeof result === "object" && result instanceof ObjcObject) {
100
- return result;
101
- }
102
- return result;
123
+ return unwrapArg(result);
103
124
  };
104
125
  }
105
126
  // Call native implementation
package/package.json CHANGED
@@ -36,7 +36,7 @@
36
36
  "format": "prettier --write \"**/*.{ts,js,json,md}\"",
37
37
  "preinstall-disabled": "npm run build-scripts && npm run make-clangd-config"
38
38
  },
39
- "version": "0.0.6",
39
+ "version": "0.0.7",
40
40
  "description": "Objective-C bridge for Node.js",
41
41
  "main": "dist/index.js",
42
42
  "devDependencies": {