@nativescript/windows 0.1.0-alpha.52 → 0.1.0-alpha.54

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.
@@ -10,6 +10,13 @@ internal readonly struct HandleRef(int id)
10
10
  public readonly int Id = id;
11
11
  }
12
12
 
13
+ // Carries a raw IUnknown/IInspectable pointer from a WinRT proxy (tag 0x0A).
14
+ // CoerceBin calls Marshal.GetObjectForIUnknown to create a managed RCW.
15
+ internal readonly struct WinRtRef(long ptr)
16
+ {
17
+ public readonly long Ptr = ptr;
18
+ }
19
+
13
20
  internal ref struct BinReader(ReadOnlySpan<byte> buf)
14
21
  {
15
22
  private readonly ReadOnlySpan<byte> _buf = buf;
@@ -24,6 +31,13 @@ internal ref struct BinReader(ReadOnlySpan<byte> buf)
24
31
  return v;
25
32
  }
26
33
 
34
+ public long ReadI64()
35
+ {
36
+ var v = BinaryPrimitives.ReadInt64LittleEndian(_buf[_pos..]);
37
+ _pos += 8;
38
+ return v;
39
+ }
40
+
27
41
  public ushort ReadU16()
28
42
  {
29
43
  var v = BinaryPrimitives.ReadUInt16LittleEndian(_buf[_pos..]);
@@ -65,6 +79,7 @@ internal ref struct BinReader(ReadOnlySpan<byte> buf)
65
79
  0x04 => (object)ReadF64(),
66
80
  0x05 => (object)ReadString16(),
67
81
  0x06 => (object)new HandleRef(ReadI32()),
82
+ 0x0A => (object)new WinRtRef(ReadI64()),
68
83
  _ => null,
69
84
  };
70
85
  }
@@ -100,6 +115,12 @@ internal ref struct BinWriter(ArrayBufferWriter<byte> buf)
100
115
  _buf.Advance(4);
101
116
  }
102
117
 
118
+ public void WriteI64(long v)
119
+ {
120
+ BinaryPrimitives.WriteInt64LittleEndian(_buf.GetSpan(8), v);
121
+ _buf.Advance(8);
122
+ }
123
+
103
124
  public void WriteF64(double v)
104
125
  {
105
126
  BinaryPrimitives.WriteDoubleLittleEndian(_buf.GetSpan(8), v);
@@ -158,6 +158,24 @@ public static partial class Bridge
158
158
  s_handles.TryGetValue(hr.Id, out var obj);
159
159
  return obj;
160
160
  }
161
+ if (value is WinRtRef wr)
162
+ {
163
+ if (wr.Ptr == 0) return null;
164
+ var nativePtr = new IntPtr((long)wr.Ptr);
165
+ // 1. Typed QI first: works for COM/CsWinRT interface types that carry a
166
+ // [Guid] attribute. More precise than a generic RCW for strongly-typed
167
+ // parameters such as Windows.UI.Xaml.UIElement.
168
+ if (targetType != typeof(object) && targetType.GUID != Guid.Empty)
169
+ {
170
+ try { return Marshal.GetTypedObjectForIUnknown(nativePtr, targetType); }
171
+ catch { }
172
+ }
173
+ // 2. Generic RCW: .NET WinRT interop calls IInspectable::GetRuntimeClassName
174
+ // and projects to the appropriate CsWinRT type automatically.
175
+ // Do NOT swallow the exception — a null return silently breaks the call
176
+ // downstream; a thrown exception surfaces a meaningful error instead.
177
+ return Marshal.GetObjectForIUnknown(nativePtr);
178
+ }
161
179
  if (value.GetType() == targetType) return value;
162
180
  try { return Convert.ChangeType(value, targetType); }
163
181
  catch { return value; }
@@ -86,8 +86,6 @@ public static partial class Bridge
86
86
  finally { if (builtArgs.Length > 0) ReturnArgs(builtArgs); }
87
87
  }
88
88
 
89
- // ── compiled dispatch cache ───────────────────────────────────────────────
90
-
91
89
  private static DispatchEntry GetCachedMethod(Type type, string name, int argCount, BindingFlags flags)
92
90
  => s_methodCache.GetOrAdd(
93
91
  new MethodKey(type, name, argCount, flags),
@@ -148,8 +146,6 @@ public static partial class Bridge
148
146
  new PropKey(type, method[prefixLen..], flags),
149
147
  static k => k.Type.GetProperty(k.Name, k.Flags));
150
148
 
151
- // ── argument coercion ─────────────────────────────────────────────────────
152
-
153
149
  // Pooled: rented array passed to the compiled delegate (which accesses by index,
154
150
  // not by Length). Caller must return via ReturnArgs immediately after invoke.
155
151
  private static object?[] BuildArgs(JsonElement[] elements, ParameterInfo[] parameters)
@@ -186,8 +182,6 @@ public static partial class Bridge
186
182
  return el.Deserialize(targetType, s_coerceOpts);
187
183
  }
188
184
 
189
- // ── async unwrapping ──────────────────────────────────────────────────────
190
-
191
185
  private static object? AwaitIfTask(object? value)
192
186
  {
193
187
  if (value is null) return null;
@@ -216,8 +210,6 @@ public static partial class Bridge
216
210
  return value;
217
211
  }
218
212
 
219
- // ── result boxing ─────────────────────────────────────────────────────────
220
-
221
213
  private static DispatchResult Box(object? value)
222
214
  {
223
215
  if (value is null) return DispatchResult.Void;
@@ -239,8 +231,6 @@ public static partial class Bridge
239
231
  return DispatchResult.Handle(id, t.FullName ?? t.Name);
240
232
  }
241
233
 
242
- // ── type/member resolution ────────────────────────────────────────────────
243
-
244
234
  internal static DispatchResult BuildMembersResult(Type t)
245
235
  {
246
236
  const BindingFlags inst = BindingFlags.Public | BindingFlags.Instance;
@@ -351,7 +341,6 @@ public static partial class Bridge
351
341
  internal static Exception Unwrap(Exception ex) =>
352
342
  ex is TargetInvocationException { InnerException: { } inner } ? Unwrap(inner) : ex;
353
343
 
354
- // ── full-pipeline helpers (parse + dispatch + serialise) ──────────────────
355
344
  // Used by benchmarks to give a fair end-to-end comparison of protocols.
356
345
 
357
346
  internal static byte[] PipelineJson(ReadOnlySpan<byte> jsonRequest)
@@ -377,8 +366,6 @@ public static partial class Bridge
377
366
  return buf.WrittenSpan.ToArray();
378
367
  }
379
368
 
380
- // ── JSON response writers ─────────────────────────────────────────────────
381
-
382
369
  private static unsafe void WriteResult(DispatchResult res, byte** outPtr, int* outLen)
383
370
  {
384
371
  var buf = new ArrayBufferWriter<byte>(256);
@@ -10,8 +10,6 @@ namespace NativeScriptBridge;
10
10
 
11
11
  public static partial class Bridge
12
12
  {
13
- // ── JS delegate creation ──────────────────────────────────────────────────
14
- //
15
13
  // opcode 0x09: given a delegate type name (or "" for System.Action) and a
16
14
  // JS callback id, compile a .NET delegate that serialises its parameters as
17
15
  // binary and calls back into V8 via the s_jsInvoker function pointer.
@@ -51,8 +49,6 @@ public static partial class Bridge
51
49
  return Box(del);
52
50
  }
53
51
 
54
- // ── Callback invocation helper ────────────────────────────────────────────
55
-
56
52
  internal static unsafe void CallJsCallback(int id, object?[] args)
57
53
  {
58
54
  if (s_jsInvoker == null) return;
@@ -97,7 +97,6 @@ public static partial class Bridge
97
97
  try { return JsonSerializer.Serialize(map); } catch { return "{}"; }
98
98
  }
99
99
 
100
- // Static ctor: initialize assembly search directories and register resolver.
101
100
  static Bridge()
102
101
  {
103
102
  try
@@ -12,14 +12,10 @@ using System.Threading.Tasks;
12
12
 
13
13
  namespace NativeScriptBridge;
14
14
 
15
- // ── JSON source-gen context ───────────────────────────────────────────────────
16
-
17
15
  [JsonSerializable(typeof(InvokeRequest))]
18
16
  [JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
19
17
  internal partial class BridgeJsonContext : JsonSerializerContext { }
20
18
 
21
- // ── JSON wire type ────────────────────────────────────────────────────────────
22
-
23
19
  internal sealed record InvokeRequest(
24
20
  string? Assembly,
25
21
  string? TypeName,
@@ -28,7 +24,6 @@ internal sealed record InvokeRequest(
28
24
  JsonElement[]? Args
29
25
  );
30
26
 
31
- // ── cache key structs ─────────────────────────────────────────────────────────
32
27
  //
33
28
  // Using Type as the key field (rather than Type.FullName) is correct and faster:
34
29
  // Type instances are singletons within an AssemblyLoadContext, so reference
@@ -38,8 +33,6 @@ internal readonly record struct MethodKey(Type Type, string Name, int ArgCount,
38
33
  internal readonly record struct PropKey(Type Type, string Name, BindingFlags Flags);
39
34
  internal readonly record struct CtorKey(Type Type, int ArgCount);
40
35
 
41
- // ── cache value types ─────────────────────────────────────────────────────────
42
-
43
36
  internal readonly struct DispatchEntry(Func<object?, object?[], object?>? invoke, ParameterInfo[] parameters)
44
37
  {
45
38
  public readonly Func<object?, object?[], object?>? Invoke = invoke;
@@ -53,8 +46,6 @@ internal readonly struct CtorEntry(ConstructorInfo? ctor, ParameterInfo[] parame
53
46
  public readonly ParameterInfo[] Parameters = parameters;
54
47
  }
55
48
 
56
- // ── dispatch result ───────────────────────────────────────────────────────────
57
-
58
49
  internal enum DispatchKind : byte { Void, Primitive, Handle, Collection, Members }
59
50
 
60
51
  internal readonly struct DispatchResult
@@ -108,8 +99,6 @@ internal readonly struct DispatchResult
108
99
 
109
100
  internal int HandleId() => _handle;
110
101
 
111
- // ── JSON serialisation ────────────────────────────────────────────────────
112
-
113
102
  public void WriteTo(Utf8JsonWriter w, JsonSerializerOptions opts)
114
103
  {
115
104
  switch (_kind)
@@ -158,7 +147,6 @@ internal readonly struct DispatchResult
158
147
  w.WriteEndArray();
159
148
  }
160
149
 
161
- // ── binary serialisation ──────────────────────────────────────────────────
162
150
  //
163
151
  // Response tags:
164
152
  // 0x00 = null 0x01 = false 0x02 = true
@@ -262,8 +250,6 @@ internal readonly struct DispatchResult
262
250
  }
263
251
  }
264
252
 
265
- // ── async result helpers ──────────────────────────────────────────────────────
266
-
267
253
  internal static class TaskResultCache
268
254
  {
269
255
  private static readonly ConcurrentDictionary<Type, Func<Task, object?>> s_cache = new();
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nativescript/windows",
3
- "version": "0.1.0-alpha.52",
3
+ "version": "0.1.0-alpha.54",
4
4
  "description": "NativeScript for using Windows v8",
5
5
  "repository": {
6
6
  "type": "git",