@nstudio/nativescript-compiler 9.2.0-alpha.19 → 9.2.0-alpha.20

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.
@@ -308,18 +308,33 @@ private class JSMethodRest3(override val method: JSMethod) : JSMethodHolder, (An
308
308
  inline fun <T> jsUnlessNullish(value: Any?, read: (Any?) -> T): T? = if (jsIsNullish(value)) null else read(value)
309
309
 
310
310
  /** A function value held untyped, as a Kotlin function taking its `this` first: a JSMethod gets it as its receiver. */
311
- fun jsThisFunction(f: Any?, arity: Int): Any = if (f !is JSMethod) when (arity) {
312
- 1 -> jsFunction1<Any?, Any?>(f)
313
- 2 -> jsFunction2<Any?, Any?, Any?>(f)
314
- 3 -> jsFunction3<Any?, Any?, Any?, Any?>(f)
315
- 4 -> jsFunction4<Any?, Any?, Any?, Any?, Any?>(f)
316
- else -> jsFunction5<Any?, Any?, Any?, Any?, Any?, Any?>(f)
317
- } else when (arity) {
318
- 1 -> { a: Any? -> f.call(a, arrayOf()) }
319
- 2 -> { a: Any?, b: Any? -> f.call(a, arrayOf(b)) }
320
- 3 -> { a: Any?, b: Any?, c: Any? -> f.call(a, arrayOf(b, c)) }
321
- 4 -> { a: Any?, b: Any?, c: Any?, d: Any? -> f.call(a, arrayOf(b, c, d)) }
322
- else -> { a: Any?, b: Any?, c: Any?, d: Any?, e: Any? -> f.call(a, arrayOf(b, c, d, e)) }
311
+ fun jsThisFunction(f: Any?, arity: Int, hasThis: Boolean = true): Any {
312
+ val m = when (f) {
313
+ is JSMethod -> f
314
+ is JSMethodHolder -> f.method
315
+ else -> null
316
+ }
317
+ if (m != null) return when (arity) {
318
+ 1 -> { a: Any? -> m.call(a, arrayOf()) }
319
+ 2 -> { a: Any?, b: Any? -> m.call(a, arrayOf(b)) }
320
+ 3 -> { a: Any?, b: Any?, c: Any? -> m.call(a, arrayOf(b, c)) }
321
+ 4 -> { a: Any?, b: Any?, c: Any?, d: Any? -> m.call(a, arrayOf(b, c, d)) }
322
+ else -> { a: Any?, b: Any?, c: Any?, d: Any?, e: Any? -> m.call(a, arrayOf(b, c, d, e)) }
323
+ }
324
+ if (!hasThis) return when (arity) {
325
+ 1 -> { _: Any? -> jsCall(f) }
326
+ 2 -> { _: Any?, b: Any? -> jsCall(f, b) }
327
+ 3 -> { _: Any?, b: Any?, c: Any? -> jsCall(f, b, c) }
328
+ 4 -> { _: Any?, b: Any?, c: Any?, d: Any? -> jsCall(f, b, c, d) }
329
+ else -> { _: Any?, b: Any?, c: Any?, d: Any?, e: Any? -> jsCall(f, b, c, d, e) }
330
+ }
331
+ return when (arity) {
332
+ 1 -> jsFunction1<Any?, Any?>(f)
333
+ 2 -> jsFunction2<Any?, Any?, Any?>(f)
334
+ 3 -> jsFunction3<Any?, Any?, Any?, Any?>(f)
335
+ 4 -> jsFunction4<Any?, Any?, Any?, Any?, Any?>(f)
336
+ else -> jsFunction5<Any?, Any?, Any?, Any?, Any?, Any?>(f)
337
+ }
323
338
  }
324
339
 
325
340
  /** Argument `index` of a dynamic call; a missing one is undefined. */
@@ -334,7 +349,7 @@ interface JSDeletable {
334
349
  fun jsAssign(target: Any?, vararg sources: Any?): Any? {
335
350
  for (source in sources) {
336
351
  when (source) {
337
- is JSDynamic -> for (key in source.jsKeys) jsSet(target, key, source.jsGet(key))
352
+ is JSDynamic -> for (key in source.jsKeys.toList()) jsSet(target, key, source.jsGet(key))
338
353
  is JSArray<*>, is String -> for (key in jsKeysOf(source)) jsSet(target, key, jsField(source, key))
339
354
  else -> {}
340
355
  }
@@ -293,6 +293,8 @@ fun jsObjectRest(source: Any?, taken: List<String>): JSObject {
293
293
  class JSPropertySlot(
294
294
  var get: ((Any?) -> Any?)? = null,
295
295
  var set: ((Any?, Any?) -> Unit)? = null,
296
+ var rawGet: Any? = null,
297
+ var rawSet: Any? = null,
296
298
  var isAccessor: Boolean = false,
297
299
  var enumerable: Boolean = true,
298
300
  var writable: Boolean = true,
@@ -307,6 +309,8 @@ class JSPropertyDescriptor(
307
309
  val hasValue: Boolean = false,
308
310
  val get: ((Any?) -> Any?)? = null,
309
311
  val set: ((Any?, Any?) -> Unit)? = null,
312
+ val rawGet: Any? = null,
313
+ val rawSet: Any? = null,
310
314
  val enumerable: Boolean? = null,
311
315
  val writable: Boolean? = null,
312
316
  val configurable: Boolean? = null,
@@ -316,12 +320,16 @@ class JSPropertyDescriptor(
316
320
  fun of(o: Any?): JSPropertyDescriptor {
317
321
  val d = jsBox(o) as? JSDynamic ?: throw JSException(JSTypeError("Property description must be an object: ${jsToString(o)}"))
318
322
  val has = { k: String -> jsHasKey(d, k) }
319
- val g = if (has("get")) jsBox(d.jsGet("get"))?.let { jsReceiving(it) } else null
320
- val s = if (has("set")) jsBox(d.jsGet("set"))?.let { jsReceiving(it) } else null
323
+ val rawG = if (has("get")) jsBox(d.jsGet("get")) else null
324
+ val rawS = if (has("set")) jsBox(d.jsGet("set")) else null
325
+ val g = rawG?.let { jsReceiving(it) }
326
+ val s = rawS?.let { jsReceiving(it) }
321
327
  return JSPropertyDescriptor(
322
328
  value = if (has("value")) d.jsGet("value") else null, hasValue = has("value"),
323
329
  get = g?.let { f -> { self: Any? -> f(self, emptyArray()) } },
324
330
  set = s?.let { f -> { self: Any?, v: Any? -> f(self, arrayOf(v)); Unit } },
331
+ rawGet = rawG,
332
+ rawSet = rawS,
325
333
  enumerable = if (has("enumerable")) jsTruthy(d.jsGet("enumerable")) else null,
326
334
  writable = if (has("writable")) jsTruthy(d.jsGet("writable")) else null,
327
335
  configurable = if (has("configurable")) jsTruthy(d.jsGet("configurable")) else null,
@@ -379,9 +379,9 @@ class JSObject() : JSDynamic, JSSymbolKeyed, JSAccessorKeyed, JSReactiveConverti
379
379
  if (changes) throw JSException(JSTypeError("Cannot redefine property: $key"))
380
380
  }
381
381
  if (d.get != null || d.set != null) {
382
- slot.get = d.get; slot.set = d.set; slot.isAccessor = true; slot.writable = false
382
+ slot.get = d.get; slot.set = d.set; slot.rawGet = d.rawGet; slot.rawSet = d.rawSet; slot.isAccessor = true; slot.writable = false
383
383
  } else if (d.hasValue || d.writable != null) {
384
- if (slot.isAccessor) { slot.get = null; slot.set = null; slot.isAccessor = false }
384
+ if (slot.isAccessor) { slot.get = null; slot.set = null; slot.rawGet = null; slot.rawSet = null; slot.isAccessor = false }
385
385
  }
386
386
  d.enumerable?.let { slot.enumerable = it }
387
387
  d.writable?.let { slot.writable = it }
@@ -399,7 +399,8 @@ class JSObject() : JSDynamic, JSSymbolKeyed, JSAccessorKeyed, JSReactiveConverti
399
399
  val g = slot.get
400
400
  val s = slot.set
401
401
  return JSObject(listOf(
402
- Pair("get", g?.let { { -> it(this) } }), Pair("set", s?.let { { v: Any? -> it(this, v) } }),
402
+ Pair("get", slot.rawGet ?: g?.let { JSMethod(0) { self, _ -> it(self) } }),
403
+ Pair("set", slot.rawSet ?: s?.let { JSMethod(1) { self, args -> it(self, args.getOrNull(0)); null } }),
403
404
  Pair("enumerable", slot.enumerable), Pair("configurable", slot.configurable)))
404
405
  }
405
406
  return JSObject(listOf(Pair("value", storage[key]), Pair("writable", slot.writable), Pair("enumerable", slot.enumerable), Pair("configurable", slot.configurable)))
@@ -488,7 +489,7 @@ class JSObject() : JSDynamic, JSSymbolKeyed, JSAccessorKeyed, JSReactiveConverti
488
489
  jsTracker?.track()
489
490
  val idx = indexKeys
490
491
  val s = slots
491
- if ((idx == null || idx.isEmpty()) && !hasSymbols && (s == null || s.isEmpty())) return namedKeys
492
+ if ((idx == null || idx.isEmpty()) && !hasSymbols && (s == null || s.isEmpty())) return namedKeys.toList()
492
493
  val names = if (hasSymbols) namedKeys.filter { !jsIsSymbolKey(it) } else namedKeys
493
494
  val all = if (idx == null || idx.isEmpty()) names else idx.map { it.toString() } + names
494
495
  return if (s == null || s.isEmpty()) all else all.filter { s[it]?.enumerable ?: true }
@@ -678,22 +679,22 @@ fun jsCallOptional(function: Any?, vararg args: Any?): Any? = if (jsIsNullish(fu
678
679
  /** `Object.keys` for what translated code holds: a typed object, an untyped one, an array. */
679
680
  fun jsKeysOf(value: Any?): List<String> = when (value) {
680
681
  is String -> value.indices.map { it.toString() }
681
- is JSDynamic -> value.jsKeys
682
- is Function<*>, is JSFunction -> jsFunctionProperties(value, false)?.jsKeys ?: emptyList()
683
- is JSArray<*> -> (0 until value.size).filter { value.jsHoles?.get(it) != true }.map { it.toString() } + (value.jsProperties?.jsKeys ?: emptyList())
682
+ is JSDynamic -> value.jsKeys.toList()
683
+ is Function<*>, is JSFunction -> jsFunctionProperties(value, false)?.jsKeys?.toList() ?: emptyList()
684
+ is JSArray<*> -> (0 until value.size).filter { value.jsHoles?.get(it) != true }.map { it.toString() } + (value.jsProperties?.jsKeys?.toList() ?: emptyList())
684
685
  else -> emptyList()
685
686
  }
686
687
 
687
688
  fun jsObjectKeys(value: Any?): JSArray<String> = JSArray(jsKeysOf(value))
688
689
 
689
690
  fun jsObjectValues(value: Any?): JSArray<Any?> = when (value) {
690
- is JSDynamic -> JSArray(value.jsKeys.map { value.jsGet(it) })
691
+ is JSDynamic -> JSArray(value.jsKeys.toList().map { value.jsGet(it) })
691
692
  is JSArray<*> -> JSArray(jsKeysOf(value).map { jsField(value, it) })
692
693
  else -> JSArray()
693
694
  }
694
695
 
695
696
  fun jsObjectEntries(value: Any?): JSArray<Pair<String, Any?>> = when (value) {
696
- is JSDynamic -> JSArray(value.jsKeys.map { Pair(it, value.jsGet(it)) })
697
+ is JSDynamic -> JSArray(value.jsKeys.toList().map { Pair(it, value.jsGet(it)) })
697
698
  is JSArray<*> -> JSArray(jsKeysOf(value).map { Pair(it, jsField(value, it)) })
698
699
  else -> JSArray()
699
700
  }
@@ -702,7 +703,7 @@ fun jsObjectEntries(value: Any?): JSArray<Pair<String, Any?>> = when (value) {
702
703
  fun <T : JSDynamic> jsObjectAssign(target: T, vararg sources: Any?): T {
703
704
  for (source in sources) {
704
705
  when (source) {
705
- is JSDynamic -> for (key in source.jsKeys) target.jsSet(key, source.jsGet(key))
706
+ is JSDynamic -> for (key in source.jsKeys.toList()) target.jsSet(key, source.jsGet(key))
706
707
  is JSArray<*>, is String -> for (key in jsKeysOf(source)) target.jsSet(key, jsField(source, key))
707
708
  else -> {}
708
709
  }
@@ -37,6 +37,8 @@ public func jsDelete(_ object: Any?, _ key: String) throws -> Bool {
37
37
  struct JSPropertySlot {
38
38
  var get: ((Any?) throws -> Any?)?
39
39
  var set: ((Any?, Any?) throws -> Void)?
40
+ var rawGet: Any?
41
+ var rawSet: Any?
40
42
  var isAccessor = false
41
43
  var enumerable = true
42
44
  var writable = true
@@ -50,6 +52,8 @@ public struct JSPropertyDescriptor {
50
52
  public var value: Any??
51
53
  public var get: ((Any?) throws -> Any?)?
52
54
  public var set: ((Any?, Any?) throws -> Void)?
55
+ var rawGet: Any?
56
+ var rawSet: Any?
53
57
  public var enumerable: Bool?
54
58
  public var writable: Bool?
55
59
  public var configurable: Bool?
@@ -68,8 +72,8 @@ public struct JSPropertyDescriptor {
68
72
  guard let o = jsFlat(object) as? JSDynamic else { throw JSException(JSTypeError("Property description must be an object: \(jsToString(object))")) }
69
73
  let has = { (k: String) in jsHasKey(o, k) }
70
74
  if has("value") { value = .some(o[jsKey: "value"]) }
71
- if has("get"), let g = jsFlat(o[jsKey: "get"]) { let f = jsReceiving(g); get = { this in try f(this, []) } }
72
- if has("set"), let s = jsFlat(o[jsKey: "set"]) { let f = jsReceiving(s); set = { this, v in _ = try f(this, [v]) } }
75
+ if has("get"), let g = jsFlat(o[jsKey: "get"]) { rawGet = g; let f = jsReceiving(g); get = { this in try f(this, []) } }
76
+ if has("set"), let s = jsFlat(o[jsKey: "set"]) { rawSet = s; let f = jsReceiving(s); set = { this, v in _ = try f(this, [v]) } }
73
77
  if has("enumerable") { enumerable = jsIsTruthy(o[jsKey: "enumerable"]) }
74
78
  if has("writable") { writable = jsIsTruthy(o[jsKey: "writable"]) }
75
79
  if has("configurable") { configurable = jsIsTruthy(o[jsKey: "configurable"]) }
@@ -187,7 +187,9 @@ public final class JSFunctionAlias {
187
187
 
188
188
  /// A typed view of an untyped function value: the function object itself is the identity.
189
189
  public init(of source: Any?) {
190
- if let f = jsFlat(source) as? JSFunctionObject { target = .object(f.identity ?? f) } else { target = .none }
190
+ if let f = jsFlat(source) as? JSFunctionObject { target = .object(f.identity ?? f) }
191
+ else if let m = jsFlat(source) as? JSMethod { target = .object(JSFunctionObject("", 0, m)) }
192
+ else { target = .none }
191
193
  }
192
194
 
193
195
  /// An adapter of a typed closure, which the adapter calls (and so keeps alive).
@@ -861,10 +861,12 @@ public final class JSObject: JSDynamic, JSSymbolKeyed, JSAccessorKeyed, JSReacti
861
861
  if d.get != nil || d.set != nil {
862
862
  slot.get = d.get
863
863
  slot.set = d.set
864
+ slot.rawGet = d.rawGet
865
+ slot.rawSet = d.rawSet
864
866
  slot.isAccessor = true
865
867
  slot.writable = false
866
868
  } else if d.value != nil || d.writable != nil {
867
- if slot.isAccessor { slot.get = nil; slot.set = nil; slot.isAccessor = false }
869
+ if slot.isAccessor { slot.get = nil; slot.set = nil; slot.rawGet = nil; slot.rawSet = nil; slot.isAccessor = false }
868
870
  }
869
871
  if let v = d.enumerable { slot.enumerable = v }
870
872
  if let v = d.writable { slot.writable = v }
@@ -884,7 +886,8 @@ public final class JSObject: JSDynamic, JSSymbolKeyed, JSAccessorKeyed, JSReacti
884
886
  guard storage[k] != nil else { return nil }
885
887
  let slot = slots[k] ?? JSPropertySlot()
886
888
  if slot.isAccessor {
887
- return JSObject([("get", slot.get.map { g in { (_: [Any?]) throws -> Any? in try g(self) } as JSFunction } as Any?), ("set", slot.set.map { s in { (a: [Any?]) throws -> Any? in try s(self, jsArg(a, 0)); return nil } as JSFunction } as Any?),
889
+ return JSObject([("get", slot.rawGet ?? (slot.get.map { g in ({ (selfArg: Any?, _: [Any?]) throws -> Any? in try g(selfArg) } as JSMethod) } as Any?)),
890
+ ("set", slot.rawSet ?? (slot.set.map { s in ({ (selfArg: Any?, a: [Any?]) throws -> Any? in try s(selfArg, jsArg(a, 0)); return nil } as JSMethod) } as Any?)),
888
891
  ("enumerable", slot.enumerable), ("configurable", slot.configurable)])
889
892
  }
890
893
  return JSObject([("value", storage[k] ?? nil), ("writable", slot.writable), ("enumerable", slot.enumerable), ("configurable", slot.configurable)])
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nstudio/nativescript-compiler",
3
- "version": "9.2.0-alpha.19",
3
+ "version": "9.2.0-alpha.20",
4
4
  "description": "NativeScript CLI plugin (`ns compile`) and compiler turning a NativeScript app into native Swift (iOS) or Kotlin (Android) with no JavaScript runtime; framework adapters (`@nstudio/nativescript-compiler-<framework>`) compile each framework.",
5
5
  "type": "module",
6
6
  "keywords": [