jscore.nim 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2018 Nim contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module wraps core JavaScript functions.
  10. ##
  11. ## Unless your application has very
  12. ## specific requirements and solely targets JavaScript, you should be using
  13. ## the relevant functions in the `math`, `json`, and `times` stdlib
  14. ## modules instead.
  15. import std/private/[since, jsutils]
  16. when not defined(js):
  17. {.error: "This module only works on the JavaScript platform".}
  18. type
  19. MathLib* = ref object
  20. JsonLib* = ref object
  21. DateLib* = ref object
  22. DateTime* = ref object
  23. var
  24. Math* {.importc, nodecl.}: MathLib
  25. Date* {.importc, nodecl.}: DateLib
  26. JSON* {.importc, nodecl.}: JsonLib
  27. # Math library
  28. proc abs*(m: MathLib, a: SomeNumber): SomeNumber {.importcpp.}
  29. proc acos*(m: MathLib, a: SomeNumber): float {.importcpp.}
  30. proc acosh*(m: MathLib, a: SomeNumber): float {.importcpp.}
  31. proc asin*(m: MathLib, a: SomeNumber): float {.importcpp.}
  32. proc asinh*(m: MathLib, a: SomeNumber): float {.importcpp.}
  33. proc atan*(m: MathLib, a: SomeNumber): float {.importcpp.}
  34. proc atan2*(m: MathLib, a: SomeNumber): float {.importcpp.}
  35. proc atanh*(m: MathLib, a: SomeNumber): float {.importcpp.}
  36. proc cbrt*(m: MathLib, f: SomeFloat): SomeFloat {.importcpp.}
  37. proc ceil*(m: MathLib, f: SomeFloat): SomeFloat {.importcpp.}
  38. proc clz32*(m: MathLib, f: SomeInteger): int {.importcpp.}
  39. proc cos*(m: MathLib, a: SomeNumber): float {.importcpp.}
  40. proc cosh*(m: MathLib, a: SomeNumber): float {.importcpp.}
  41. proc exp*(m: MathLib, a: SomeNumber): float {.importcpp.}
  42. proc expm1*(m: MathLib, a: SomeNumber): float {.importcpp.}
  43. proc floor*(m: MathLib, f: SomeFloat): int {.importcpp.}
  44. proc fround*(m: MathLib, f: SomeFloat): float32 {.importcpp.}
  45. proc hypot*(m: MathLib, args: varargs[distinct SomeNumber]): float {.importcpp.}
  46. proc imul*(m: MathLib, a, b: int32): int32 {.importcpp.}
  47. proc log*(m: MathLib, a: SomeNumber): float {.importcpp.}
  48. proc log10*(m: MathLib, a: SomeNumber): float {.importcpp.}
  49. proc log1p*(m: MathLib, a: SomeNumber): float {.importcpp.}
  50. proc log2*(m: MathLib, a: SomeNumber): float {.importcpp.}
  51. proc max*(m: MathLib, a, b: SomeNumber): SomeNumber {.importcpp.}
  52. proc min*[T: SomeNumber | JsRoot](m: MathLib, a, b: T): T {.importcpp.}
  53. proc pow*(m: MathLib, a, b: distinct SomeNumber): float {.importcpp.}
  54. proc random*(m: MathLib): float {.importcpp.}
  55. proc round*(m: MathLib, f: SomeFloat): int {.importcpp.}
  56. proc sign*(m: MathLib, f: SomeNumber): int {.importcpp.}
  57. proc sin*(m: MathLib, a: SomeNumber): float {.importcpp.}
  58. proc sinh*(m: MathLib, a: SomeNumber): float {.importcpp.}
  59. proc sqrt*(m: MathLib, f: SomeFloat): SomeFloat {.importcpp.}
  60. proc tan*(m: MathLib, a: SomeNumber): float {.importcpp.}
  61. proc tanh*(m: MathLib, a: SomeNumber): float {.importcpp.}
  62. proc trunc*(m: MathLib, f: SomeFloat): int {.importcpp.}
  63. # Date library
  64. proc now*(d: DateLib): int {.importcpp.}
  65. proc UTC*(d: DateLib): int {.importcpp.}
  66. proc parse*(d: DateLib, s: cstring): int {.importcpp.}
  67. proc newDate*(): DateTime {.
  68. importcpp: "new Date()".}
  69. proc newDate*(date: int|string): DateTime {.
  70. importcpp: "new Date(#)".}
  71. whenJsNoBigInt64:
  72. proc newDate*(date: int64): DateTime {.
  73. importcpp: "new Date(#)".}
  74. do:
  75. proc newDate*(date: int64): DateTime {.
  76. importcpp: "new Date(Number(#))".}
  77. proc newDate*(year, month, day, hours, minutes,
  78. seconds, milliseconds: int): DateTime {.
  79. importcpp: "new Date(#,#,#,#,#,#,#)".}
  80. proc getDay*(d: DateTime): int {.importcpp.}
  81. proc getFullYear*(d: DateTime): int {.importcpp.}
  82. proc getHours*(d: DateTime): int {.importcpp.}
  83. proc getMilliseconds*(d: DateTime): int {.importcpp.}
  84. proc getMinutes*(d: DateTime): int {.importcpp.}
  85. proc getMonth*(d: DateTime): int {.importcpp.}
  86. proc getSeconds*(d: DateTime): int {.importcpp.}
  87. proc getYear*(d: DateTime): int {.importcpp.}
  88. proc getTime*(d: DateTime): int {.importcpp.}
  89. proc toString*(d: DateTime): cstring {.importcpp.}
  90. proc getUTCDate*(d: DateTime): int {.importcpp.}
  91. proc getUTCFullYear*(d: DateTime): int {.importcpp.}
  92. proc getUTCHours*(d: DateTime): int {.importcpp.}
  93. proc getUTCMilliseconds*(d: DateTime): int {.importcpp.}
  94. proc getUTCMinutes*(d: DateTime): int {.importcpp.}
  95. proc getUTCMonth*(d: DateTime): int {.importcpp.}
  96. proc getUTCSeconds*(d: DateTime): int {.importcpp.}
  97. proc getUTCDay*(d: DateTime): int {.importcpp.}
  98. proc getTimezoneOffset*(d: DateTime): int {.importcpp.}
  99. proc setFullYear*(d: DateTime, year: int) {.importcpp.}
  100. #JSON library
  101. proc stringify*(l: JsonLib, s: JsRoot): cstring {.importcpp.}
  102. proc parse*(l: JsonLib, s: cstring): JsRoot {.importcpp.}
  103. since (1, 5):
  104. func debugger*() {.importjs: "debugger@".}
  105. ## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger
  106. func copyWithin*[T](self: openArray[T]; target: int): seq[T] {.importjs: "#.copyWithin(#)".}
  107. func copyWithin*[T](self: openArray[T]; target, start: int): seq[T] {.importjs: "#.copyWithin(#, #)".}
  108. func copyWithin*[T](self: openArray[T]; target, start, ends: int): seq[T] {.importjs: "#.copyWithin(#, #, #)".} =
  109. ## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin
  110. ## `copyWithin` uses shallow copy.
  111. runnableExamples:
  112. assert ['a', 'b', 'c', 'd', 'e'].copyWithin(0, 3, 4) == @['d', 'b', 'c', 'd', 'e']
  113. assert ['a', 'b', 'c', 'd', 'e'].copyWithin(1, 3) == @['a', 'd', 'e', 'd', 'e']
  114. assert [1, 2, 3, 4, 5].copyWithin(-2) == @[1, 2, 3, 1, 2]
  115. assert [1, 2, 3, 4, 5].copyWithin(0, 3) == @[4, 5, 3, 4, 5]
  116. assert [1, 2, 3, 4, 5].copyWithin(0, 3, 4) == @[4, 2, 3, 4, 5]
  117. assert [1, 2, 3, 4, 5].copyWithin(-2, -3, -1) == @[1, 2, 3, 3, 4]
  118. since (1, 7):
  119. func shift*[T](self: seq[T]): T {.importjs: "#.$1()".} =
  120. ## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
  121. runnableExamples:
  122. var arrai = @[1, 2, 3]
  123. assert arrai.shift() == 1
  124. assert arrai == @[2, 3]
  125. func queueMicrotask*(function: proc) {.importjs: "$1(#)".} =
  126. ## * https://developer.mozilla.org/en-US/docs/Web/API/queueMicrotask
  127. ## * https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide
  128. runnableExamples"-r:off": queueMicrotask(proc() = echo "Microtask")