jscore.nim 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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
  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|int64|string): DateTime {.
  70. importcpp: "new Date(#)".}
  71. proc newDate*(year, month, day, hours, minutes,
  72. seconds, milliseconds: int): DateTime {.
  73. importcpp: "new Date(#,#,#,#,#,#,#)".}
  74. proc getDay*(d: DateTime): int {.importcpp.}
  75. proc getFullYear*(d: DateTime): int {.importcpp.}
  76. proc getHours*(d: DateTime): int {.importcpp.}
  77. proc getMilliseconds*(d: DateTime): int {.importcpp.}
  78. proc getMinutes*(d: DateTime): int {.importcpp.}
  79. proc getMonth*(d: DateTime): int {.importcpp.}
  80. proc getSeconds*(d: DateTime): int {.importcpp.}
  81. proc getYear*(d: DateTime): int {.importcpp.}
  82. proc getTime*(d: DateTime): int {.importcpp.}
  83. proc toString*(d: DateTime): cstring {.importcpp.}
  84. proc getUTCDate*(d: DateTime): int {.importcpp.}
  85. proc getUTCFullYear*(d: DateTime): int {.importcpp.}
  86. proc getUTCHours*(d: DateTime): int {.importcpp.}
  87. proc getUTCMilliseconds*(d: DateTime): int {.importcpp.}
  88. proc getUTCMinutes*(d: DateTime): int {.importcpp.}
  89. proc getUTCMonth*(d: DateTime): int {.importcpp.}
  90. proc getUTCSeconds*(d: DateTime): int {.importcpp.}
  91. proc getUTCDay*(d: DateTime): int {.importcpp.}
  92. proc getTimezoneOffset*(d: DateTime): int {.importcpp.}
  93. proc setFullYear*(d: DateTime, year: int) {.importcpp.}
  94. #JSON library
  95. proc stringify*(l: JsonLib, s: JsRoot): cstring {.importcpp.}
  96. proc parse*(l: JsonLib, s: cstring): JsRoot {.importcpp.}
  97. since (1, 5):
  98. func debugger*() {.importjs: "debugger@".}
  99. ## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger
  100. func copyWithin*[T](self: openArray[T]; target: int): seq[T] {.importjs: "#.copyWithin(#)".}
  101. func copyWithin*[T](self: openArray[T]; target, start: int): seq[T] {.importjs: "#.copyWithin(#, #)".}
  102. func copyWithin*[T](self: openArray[T]; target, start, ends: int): seq[T] {.importjs: "#.copyWithin(#, #, #)".} =
  103. ## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin
  104. ## `copyWithin` uses shallow copy.
  105. runnableExamples:
  106. assert ['a', 'b', 'c', 'd', 'e'].copyWithin(0, 3, 4) == @['d', 'b', 'c', 'd', 'e']
  107. assert ['a', 'b', 'c', 'd', 'e'].copyWithin(1, 3) == @['a', 'd', 'e', 'd', 'e']
  108. assert [1, 2, 3, 4, 5].copyWithin(-2) == @[1, 2, 3, 1, 2]
  109. assert [1, 2, 3, 4, 5].copyWithin(0, 3) == @[4, 5, 3, 4, 5]
  110. assert [1, 2, 3, 4, 5].copyWithin(0, 3, 4) == @[4, 2, 3, 4, 5]
  111. assert [1, 2, 3, 4, 5].copyWithin(-2, -3, -1) == @[1, 2, 3, 3, 4]
  112. since (1, 7):
  113. func shift*[T](self: seq[T]): T {.importjs: "#.$1()".} =
  114. ## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift
  115. runnableExamples:
  116. var arrai = @[1, 2, 3]
  117. assert arrai.shift() == 1
  118. assert arrai == @[2, 3]
  119. func queueMicrotask*(function: proc) {.importjs: "$1(#)".} =
  120. ## * https://developer.mozilla.org/en-US/docs/Web/API/queueMicrotask
  121. ## * https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide
  122. runnableExamples"-r:off": queueMicrotask(proc() = echo "Microtask")