@nativescript/windows 0.1.0-alpha.77 → 0.1.0-alpha.78

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.
@@ -33,11 +33,23 @@ public static partial class Bridge
33
33
  {
34
34
  var op = r.ReadByte();
35
35
 
36
- if (op == 0x04) // release
37
- {
38
- s_handles.TryRemove(r.ReadI32(), out _);
39
- return DispatchResult.Void;
40
- }
36
+ if (op == 0x04) // release
37
+ {
38
+ var handleToRemove = r.ReadI32();
39
+ s_handles.TryRemove(handleToRemove, out _);
40
+ if (s_nativePtrs.TryRemove(handleToRemove, out var nativePtr))
41
+ {
42
+ try
43
+ {
44
+ Marshal.Release(new IntPtr(nativePtr));
45
+ }
46
+ catch
47
+ {
48
+ // best-effort
49
+ }
50
+ }
51
+ return DispatchResult.Void;
52
+ }
41
53
 
42
54
  if (op == 0x05) // members by handle
43
55
  {
@@ -18,11 +18,24 @@ public static partial class Bridge
18
18
  {
19
19
  var method = req.Method ?? throw new ArgumentException("Method is required");
20
20
 
21
- if (method == "__release" && req.Handle.HasValue)
22
- {
23
- s_handles.TryRemove(req.Handle.Value, out _);
24
- return DispatchResult.Void;
25
- }
21
+ if (method == "__release" && req.Handle.HasValue)
22
+ {
23
+ var id = req.Handle.Value;
24
+ s_handles.TryRemove(id, out _);
25
+ if (s_nativePtrs.TryRemove(id, out var nativePtr))
26
+ {
27
+ try
28
+ {
29
+ Marshal.Release(new IntPtr(nativePtr));
30
+ }
31
+ catch
32
+ {
33
+ // swallow - best-effort release
34
+ }
35
+ }
36
+
37
+ return DispatchResult.Void;
38
+ }
26
39
 
27
40
  object? target = null;
28
41
  Type type;
@@ -244,8 +257,28 @@ public static partial class Bridge
244
257
  catch { }
245
258
  }
246
259
 
247
- var id = Interlocked.Increment(ref s_nextHandle);
248
- s_handles[id] = value;
260
+ var id = Interlocked.Increment(ref s_nextHandle);
261
+ s_handles[id] = value;
262
+
263
+ // Try to obtain a canonical IUnknown pointer for COM/WinRT objects so the
264
+ // runtime can call native vtable methods directly. We addref via
265
+ // Marshal.GetIUnknownForObject and store the raw pointer; it will be
266
+ // released on __release.
267
+ try
268
+ {
269
+ if (value != null)
270
+ {
271
+ var p = Marshal.GetIUnknownForObject(value);
272
+ if (p != IntPtr.Zero)
273
+ {
274
+ s_nativePtrs[id] = p.ToInt64();
275
+ }
276
+ }
277
+ }
278
+ catch
279
+ {
280
+ // Not a COM object or failed to obtain native pointer; ignore.
281
+ }
249
282
  var typeName = t.FullName ?? t.Name;
250
283
  return IsAwaitable(value, t)
251
284
  ? DispatchResult.TaskHandle(id, typeName)
@@ -48,9 +48,15 @@ public static partial class Bridge
48
48
  s_propCache.Clear();
49
49
  s_ctorCache.Clear();
50
50
  s_handles.Clear();
51
+ s_nativePtrs.Clear();
51
52
  s_nextHandle = 0;
52
53
  }
53
54
 
55
+ // Optional mapping from exported handle id -> native IUnknown pointer (Int64)
56
+ // Populated when a managed object can yield a native COM pointer via
57
+ // Marshal.GetIUnknownForObject. Cleared and released on __release.
58
+ internal static readonly ConcurrentDictionary<int, long> s_nativePtrs = new();
59
+
54
60
  // Returns a JSON object mapping top-level namespace roots (e.g. "NativeScript")
55
61
  // to the assembly simple-name that most likely contains that namespace's types.
56
62
  // Reuses s_assemblySearchDirs so plugin and NuGet assemblies are included.
@@ -372,6 +378,36 @@ public static partial class Bridge
372
378
  return 0;
373
379
  }
374
380
 
381
+ // Return a canonical native pointer (IUnknown / IInspectable) for an
382
+ // exported handle id. Returns 0 when no pointer is available. If a
383
+ // pointer isn't already cached in `s_nativePtrs`, attempt to obtain one
384
+ // via `Marshal.GetIUnknownForObject` and cache it for subsequent calls.
385
+ public static long GetNativePtrForHandle(int handleId)
386
+ {
387
+ if (handleId <= 0) return 0;
388
+ if (s_nativePtrs.TryGetValue(handleId, out var p)) return p;
389
+
390
+ if (!s_handles.TryGetValue(handleId, out var obj) || obj == null)
391
+ return 0;
392
+
393
+ try
394
+ {
395
+ var ip = Marshal.GetIUnknownForObject(obj);
396
+ if (ip != IntPtr.Zero)
397
+ {
398
+ var val = ip.ToInt64();
399
+ s_nativePtrs[handleId] = val;
400
+ return val;
401
+ }
402
+ }
403
+ catch
404
+ {
405
+ // best-effort: not a COM object or failed to obtain native ptr
406
+ }
407
+
408
+ return 0;
409
+ }
410
+
375
411
  private static void OnAppDomainUnhandledException(object? sender, UnhandledExceptionEventArgs e)
376
412
  {
377
413
  try
@@ -404,22 +440,19 @@ public static partial class Bridge
404
440
  catch { }
405
441
  }
406
442
 
407
- private static void SendUnhandledToJs(string kind, string message)
443
+ private static unsafe void SendUnhandledToJs(string kind, string message)
408
444
  {
409
445
  try
410
446
  {
411
447
  if (s_jsInvoker == null || s_unhandledExceptionCallbackId <= 0) return;
412
448
  var payload = JsonSerializer.Serialize(new { kind = kind, message = message });
413
449
  var bytes = System.Text.Encoding.UTF8.GetBytes(payload);
414
- unsafe
450
+ fixed (byte* p = bytes)
415
451
  {
416
- fixed (byte* p = bytes)
417
- {
418
- byte* respPtr = null;
419
- int respLen = 0;
420
- s_jsInvoker(s_unhandledExceptionCallbackId, p, bytes.Length, &respPtr, &respLen);
421
- if (respPtr != null && respLen > 0) Marshal.FreeHGlobal((IntPtr)respPtr);
422
- }
452
+ byte* respPtr = null;
453
+ int respLen = 0;
454
+ s_jsInvoker(s_unhandledExceptionCallbackId, p, bytes.Length, &respPtr, &respLen);
455
+ if (respPtr != null && respLen > 0) Marshal.FreeHGlobal((IntPtr)respPtr);
423
456
  }
424
457
  }
425
458
  catch { }
@@ -115,10 +115,23 @@ internal readonly struct DispatchResult
115
115
  break;
116
116
 
117
117
  case DispatchKind.Handle:
118
- w.WriteStartObject();
119
- w.WriteNumber("__handle"u8, _handle);
120
- w.WriteString("__type"u8, _typeName);
121
- w.WriteEndObject();
118
+ w.WriteStartObject();
119
+ w.WriteNumber("__handle"u8, _handle);
120
+ w.WriteString("__type"u8, _typeName);
121
+ // If the bridge has a canonical native pointer for this handle,
122
+ // expose it as a hex string so the runtime can detect and use it.
123
+ if (Bridge.s_nativePtrs.TryGetValue(_handle, out var nativePtr))
124
+ {
125
+ try
126
+ {
127
+ w.WriteString("__native_ptr"u8, nativePtr.ToString("x"));
128
+ }
129
+ catch
130
+ {
131
+ // ignore formatting errors
132
+ }
133
+ }
134
+ w.WriteEndObject();
122
135
  break;
123
136
 
124
137
  case DispatchKind.TaskHandle:
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nativescript/windows",
3
- "version": "0.1.0-alpha.77",
3
+ "version": "0.1.0-alpha.78",
4
4
  "description": "NativeScript for using Windows v8",
5
5
  "repository": {
6
6
  "type": "git",