Skip to content

Commit

Permalink
Use importjs (#17422)
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumlamm authored Mar 19, 2021
1 parent 430c302 commit 9997b42
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 56 deletions.
8 changes: 4 additions & 4 deletions lib/js/asyncjs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type
future*: T
## Wraps the return type of an asynchronous procedure.

PromiseJs* {.importcpp: "Promise".} = ref object
PromiseJs* {.importjs: "Promise".} = ref object
## A JavaScript Promise.


Expand Down Expand Up @@ -113,7 +113,7 @@ proc generateJsasync(arg: NimNode): NimNode =

if len(code) > 0:
var awaitFunction = quote:
proc await[T](f: Future[T]): T {.importcpp: "(await #)", used.}
proc await[T](f: Future[T]): T {.importjs: "(await #)", used.}
result.body.add(awaitFunction)

var resolve: NimNode
Expand Down Expand Up @@ -150,11 +150,11 @@ macro async*(arg: untyped): untyped =
else:
result = generateJsasync(arg)

proc newPromise*[T](handler: proc(resolve: proc(response: T))): Future[T] {.importcpp: "(new Promise(#))".}
proc newPromise*[T](handler: proc(resolve: proc(response: T))): Future[T] {.importjs: "(new Promise(#))".}
## A helper for wrapping callback-based functions
## into promises and async procedures.

proc newPromise*(handler: proc(resolve: proc())): Future[void] {.importcpp: "(new Promise(#))".}
proc newPromise*(handler: proc(resolve: proc())): Future[void] {.importjs: "(new Promise(#))".}
## A helper for wrapping callback-based functions
## into promises and async procedures.

Expand Down
2 changes: 1 addition & 1 deletion lib/js/dom_extensions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import std/dom

{.push importcpp.}
proc elementsFromPoint*(n: DocumentOrShadowRoot; x, y: float): seq[Element]
{.pop.}
{.pop.}
98 changes: 49 additions & 49 deletions lib/js/jsffi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ runnableExamples:
var document {.importc, nodecl.}: JsObject
var console {.importc, nodecl.}: JsObject
# import the "$" function
proc jq(selector: JsObject): JsObject {.importcpp: "$$(#)".}
proc jq(selector: JsObject): JsObject {.importjs: "$$(#)".}

# Use jQuery to make the following code run, after the document is ready.
# This uses an experimental `.()` operator for `JsObject`, to emit
Expand Down Expand Up @@ -70,15 +70,15 @@ template mangleJsName(name: cstring): cstring =

# only values that can be mapped 1 to 1 with cstring should be keys: they have an injective function with cstring

proc toJsKey*[T: SomeInteger](text: cstring, t: type T): T {.importcpp: "parseInt(#)".}
proc toJsKey*[T: SomeInteger](text: cstring, t: type T): T {.importjs: "parseInt(#)".}

proc toJsKey*[T: enum](text: cstring, t: type T): T =
T(text.toJsKey(int))

proc toJsKey*(text: cstring, t: type cstring): cstring =
text

proc toJsKey*[T: SomeFloat](text: cstring, t: type T): T {.importcpp: "parseFloat(#)".}
proc toJsKey*[T: SomeFloat](text: cstring, t: type T): T {.importjs: "parseFloat(#)".}

type
JsKey* = concept a, type T
Expand All @@ -103,10 +103,10 @@ var
jsFilename* {.importc: "__filename", nodecl.}: cstring
## JavaScript's __filename pseudo-variable.

proc isNull*[T](x: T): bool {.noSideEffect, importcpp: "(# === null)".}
proc isNull*[T](x: T): bool {.noSideEffect, importjs: "(# === null)".}
## Checks if a value is exactly null.

proc isUndefined*[T](x: T): bool {.noSideEffect, importcpp: "(# === undefined)".}
proc isUndefined*[T](x: T): bool {.noSideEffect, importjs: "(# === undefined)".}
## Checks if a value is exactly undefined.

# Exceptions
Expand All @@ -121,35 +121,35 @@ type
JsURIError* {.importc: "URIError".} = object of JsError

# New
proc newJsObject*: JsObject {.importcpp: "{@}".}
proc newJsObject*: JsObject {.importjs: "{@}".}
## Creates a new empty JsObject.

proc newJsAssoc*[K: JsKey, V]: JsAssoc[K, V] {.importcpp: "{@}".}
proc newJsAssoc*[K: JsKey, V]: JsAssoc[K, V] {.importjs: "{@}".}
## Creates a new empty JsAssoc with key type `K` and value type `V`.

# Checks
proc hasOwnProperty*(x: JsObject, prop: cstring): bool
{.importcpp: "#.hasOwnProperty(#)".}
{.importjs: "#.hasOwnProperty(#)".}
## Checks, whether `x` has a property of name `prop`.

proc jsTypeOf*(x: JsObject): cstring {.importcpp: "typeof(#)".}
proc jsTypeOf*(x: JsObject): cstring {.importjs: "typeof(#)".}
## Returns the name of the JsObject's JavaScript type as a cstring.

proc jsNew*(x: auto): JsObject {.importcpp: "(new #)".}
proc jsNew*(x: auto): JsObject {.importjs: "(new #)".}
## Turns a regular function call into an invocation of the
## JavaScript's `new` operator.

proc jsDelete*(x: auto): JsObject {.importcpp: "(delete #)".}
proc jsDelete*(x: auto): JsObject {.importjs: "(delete #)".}
## JavaScript's `delete` operator.

proc require*(module: cstring): JsObject {.importc.}
## JavaScript's `require` function.

# Conversion to and from JsObject
proc to*(x: JsObject, T: typedesc): T {.importcpp: "(#)".}
proc to*(x: JsObject, T: typedesc): T {.importjs: "(#)".}
## Converts a JsObject `x` to type `T`.

proc toJs*[T](val: T): JsObject {.importcpp: "(#)".}
proc toJs*[T](val: T): JsObject {.importjs: "(#)".}
## Converts a value of any type to type JsObject.

template toJs*(s: string): JsObject = cstring(s).toJs
Expand All @@ -160,50 +160,50 @@ macro jsFromAst*(n: untyped): untyped =
result = newProc(procType = nnkDo, body = result)
return quote: toJs(`result`)

proc `&`*(a, b: cstring): cstring {.importcpp: "(# + #)".}
proc `&`*(a, b: cstring): cstring {.importjs: "(# + #)".}
## Concatenation operator for JavaScript strings.

proc `+` *(x, y: JsObject): JsObject {.importcpp: "(# + #)".}
proc `-` *(x, y: JsObject): JsObject {.importcpp: "(# - #)".}
proc `*` *(x, y: JsObject): JsObject {.importcpp: "(# * #)".}
proc `/` *(x, y: JsObject): JsObject {.importcpp: "(# / #)".}
proc `%` *(x, y: JsObject): JsObject {.importcpp: "(# % #)".}
proc `+=` *(x, y: JsObject): JsObject {.importcpp: "(# += #)", discardable.}
proc `-=` *(x, y: JsObject): JsObject {.importcpp: "(# -= #)", discardable.}
proc `*=` *(x, y: JsObject): JsObject {.importcpp: "(# *= #)", discardable.}
proc `/=` *(x, y: JsObject): JsObject {.importcpp: "(# /= #)", discardable.}
proc `%=` *(x, y: JsObject): JsObject {.importcpp: "(# %= #)", discardable.}
proc `++` *(x: JsObject): JsObject {.importcpp: "(++#)".}
proc `--` *(x: JsObject): JsObject {.importcpp: "(--#)".}
proc `>` *(x, y: JsObject): JsObject {.importcpp: "(# > #)".}
proc `<` *(x, y: JsObject): JsObject {.importcpp: "(# < #)".}
proc `>=` *(x, y: JsObject): JsObject {.importcpp: "(# >= #)".}
proc `<=` *(x, y: JsObject): JsObject {.importcpp: "(# <= #)".}
proc `**` *(x, y: JsObject): JsObject {.importcpp: "((#) ** #)".}
proc `+` *(x, y: JsObject): JsObject {.importjs: "(# + #)".}
proc `-` *(x, y: JsObject): JsObject {.importjs: "(# - #)".}
proc `*` *(x, y: JsObject): JsObject {.importjs: "(# * #)".}
proc `/` *(x, y: JsObject): JsObject {.importjs: "(# / #)".}
proc `%` *(x, y: JsObject): JsObject {.importjs: "(# % #)".}
proc `+=` *(x, y: JsObject): JsObject {.importjs: "(# += #)", discardable.}
proc `-=` *(x, y: JsObject): JsObject {.importjs: "(# -= #)", discardable.}
proc `*=` *(x, y: JsObject): JsObject {.importjs: "(# *= #)", discardable.}
proc `/=` *(x, y: JsObject): JsObject {.importjs: "(# /= #)", discardable.}
proc `%=` *(x, y: JsObject): JsObject {.importjs: "(# %= #)", discardable.}
proc `++` *(x: JsObject): JsObject {.importjs: "(++#)".}
proc `--` *(x: JsObject): JsObject {.importjs: "(--#)".}
proc `>` *(x, y: JsObject): JsObject {.importjs: "(# > #)".}
proc `<` *(x, y: JsObject): JsObject {.importjs: "(# < #)".}
proc `>=` *(x, y: JsObject): JsObject {.importjs: "(# >= #)".}
proc `<=` *(x, y: JsObject): JsObject {.importjs: "(# <= #)".}
proc `**` *(x, y: JsObject): JsObject {.importjs: "((#) ** #)".}
# (#) needed, refs https://github.com/nim-lang/Nim/pull/16409#issuecomment-760550812
proc `and`*(x, y: JsObject): JsObject {.importcpp: "(# && #)".}
proc `or` *(x, y: JsObject): JsObject {.importcpp: "(# || #)".}
proc `not`*(x: JsObject): JsObject {.importcpp: "(!#)".}
proc `in` *(x, y: JsObject): JsObject {.importcpp: "(# in #)".}
proc `and`*(x, y: JsObject): JsObject {.importjs: "(# && #)".}
proc `or` *(x, y: JsObject): JsObject {.importjs: "(# || #)".}
proc `not`*(x: JsObject): JsObject {.importjs: "(!#)".}
proc `in` *(x, y: JsObject): JsObject {.importjs: "(# in #)".}

proc `[]`*(obj: JsObject, field: cstring): JsObject {.importcpp: getImpl.}
proc `[]`*(obj: JsObject, field: cstring): JsObject {.importjs: getImpl.}
## Returns the value of a property of name `field` from a JsObject `obj`.

proc `[]`*(obj: JsObject, field: int): JsObject {.importcpp: getImpl.}
proc `[]`*(obj: JsObject, field: int): JsObject {.importjs: getImpl.}
## Returns the value of a property of name `field` from a JsObject `obj`.

proc `[]=`*[T](obj: JsObject, field: cstring, val: T) {.importcpp: setImpl.}
proc `[]=`*[T](obj: JsObject, field: cstring, val: T) {.importjs: setImpl.}
## Sets the value of a property of name `field` in a JsObject `obj` to `v`.

proc `[]=`*[T](obj: JsObject, field: int, val: T) {.importcpp: setImpl.}
proc `[]=`*[T](obj: JsObject, field: int, val: T) {.importjs: setImpl.}
## Sets the value of a property of name `field` in a JsObject `obj` to `v`.

proc `[]`*[K: JsKey, V](obj: JsAssoc[K, V], field: K): V
{.importcpp: getImpl.}
{.importjs: getImpl.}
## Returns the value of a property of name `field` from a JsAssoc `obj`.

proc `[]=`*[K: JsKey, V](obj: JsAssoc[K, V], field: K, val: V)
{.importcpp: setImpl.}
{.importjs: setImpl.}
## Sets the value of a property of name `field` in a JsAssoc `obj` to `v`.

proc `[]`*[V](obj: JsAssoc[cstring, V], field: string): V =
Expand All @@ -212,7 +212,7 @@ proc `[]`*[V](obj: JsAssoc[cstring, V], field: string): V =
proc `[]=`*[V](obj: JsAssoc[cstring, V], field: string, val: V) =
obj[cstring(field)] = val

proc `==`*(x, y: JsRoot): bool {.importcpp: "(# === #)".}
proc `==`*(x, y: JsRoot): bool {.importjs: "(# === #)".}
## Compares two JsObjects or JsAssocs. Be careful though, as this is comparison
## like in JavaScript, so if your JsObjects are in fact JavaScript Objects,
## and not strings or numbers, this is a *comparison of references*.
Expand All @@ -229,15 +229,15 @@ macro `.`*(obj: JsObject, field: untyped): JsObject =
let importString = "#." & $field
result = quote do:
proc helper(o: JsObject): JsObject
{.importcpp: `importString`, gensym.}
{.importjs: `importString`, gensym.}
helper(`obj`)
else:
if not mangledNames.hasKey($field):
mangledNames[$field] = $mangleJsName($field)
let importString = "#." & mangledNames[$field]
result = quote do:
proc helper(o: JsObject): JsObject
{.importcpp: `importString`, gensym.}
{.importjs: `importString`, gensym.}
helper(`obj`)

macro `.=`*(obj: JsObject, field, value: untyped): untyped =
Expand All @@ -247,15 +247,15 @@ macro `.=`*(obj: JsObject, field, value: untyped): untyped =
let importString = "#." & $field & " = #"
result = quote do:
proc helper(o: JsObject, v: auto)
{.importcpp: `importString`, gensym.}
{.importjs: `importString`, gensym.}
helper(`obj`, `value`)
else:
if not mangledNames.hasKey($field):
mangledNames[$field] = $mangleJsName($field)
let importString = "#." & mangledNames[$field] & " = #"
result = quote do:
proc helper(o: JsObject, v: auto)
{.importcpp: `importString`, gensym.}
{.importjs: `importString`, gensym.}
helper(`obj`, `value`)

macro `.()`*(obj: JsObject,
Expand Down Expand Up @@ -286,7 +286,7 @@ macro `.()`*(obj: JsObject,
importString = "#." & mangledNames[$field] & "(@)"
result = quote:
proc helper(o: JsObject): JsObject
{.importcpp: `importString`, gensym, discardable.}
{.importjs: `importString`, gensym, discardable.}
helper(`obj`)
for idx in 0 ..< args.len:
let paramName = newIdentNode("param" & $idx)
Expand All @@ -306,7 +306,7 @@ macro `.`*[K: cstring, V](obj: JsAssoc[K, V],
importString = "#." & mangledNames[$field]
result = quote do:
proc helper(o: type(`obj`)): `obj`.V
{.importcpp: `importString`, gensym.}
{.importjs: `importString`, gensym.}
helper(`obj`)

macro `.=`*[K: cstring, V](obj: JsAssoc[K, V],
Expand All @@ -323,7 +323,7 @@ macro `.=`*[K: cstring, V](obj: JsAssoc[K, V],
importString = "#." & mangledNames[$field] & " = #"
result = quote do:
proc helper(o: type(`obj`), v: `obj`.V)
{.importcpp: `importString`, gensym.}
{.importjs: `importString`, gensym.}
helper(`obj`, `value`)

macro `.()`*[K: cstring, V: proc](obj: JsAssoc[K, V],
Expand Down
4 changes: 2 additions & 2 deletions lib/std/jsbigints.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
when not defined(js):
{.fatal: "Module jsbigints is designed to be used with the JavaScript backend.".}

type JsBigIntImpl {.importc: "bigint".} = int # https://github.com/nim-lang/Nim/pull/16606
type JsBigInt* = distinct JsBigIntImpl ## Arbitrary precision integer for JavaScript target.
type JsBigIntImpl {.importjs: "bigint".} = int # https://github.com/nim-lang/Nim/pull/16606
type JsBigInt* = distinct JsBigIntImpl ## Arbitrary precision integer for JavaScript target.

func big*(integer: SomeInteger): JsBigInt {.importjs: "BigInt(#)".} =
## Constructor for `JsBigInt`.
Expand Down

0 comments on commit 9997b42

Please sign in to comment.