js module also uses runnableExamples (#16229)

This commit is contained in:
flywind 2020-12-02 23:24:59 +08:00 • committed by GitHub
commit 148d614dff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 40 deletions

View file

@ -12,26 +12,25 @@
## and ``JsAssoc`` together with the provided macros allows for smoother
## interfacing with JavaScript, allowing for example quick and easy imports of
## JavaScript variables:
##
## .. code-block:: nim
##
## # Here, we are using jQuery for just a few calls and do not want to wrap the
## # whole library:
##
## # import the document object and the console
## var document {.importc, nodecl.}: JsObject
## var console {.importc, nodecl.}: JsObject
## # import the "$" function
## proc jq(selector: JsObject): JsObject {.importcpp: "$$(#)".}
##
## # Use jQuery to make the following code run, after the document is ready.
## # This uses an experimental ``.()`` operator for ``JsObject``, to emit
## # JavaScript calls, when no corresponding proc exists for ``JsObject``.
## proc main =
## jq(document).ready(proc() =
## console.log("Hello JavaScript!")
## )
##
runnableExamples:
# Here, we are using jQuery for just a few calls and do not want to wrap the
# whole library:
# import the document object and the console
var document {.importc, nodecl.}: JsObject
var console {.importc, nodecl.}: JsObject
# import the "$" function
proc jq(selector: JsObject): JsObject {.importcpp: "$$(#)".}
# Use jQuery to make the following code run, after the document is ready.
# This uses an experimental ``.()`` operator for ``JsObject``, to emit
# JavaScript calls, when no corresponding proc exists for ``JsObject``.
proc main =
jq(document).ready(proc() =
console.log("Hello JavaScript!")
)
when not defined(js) and not defined(nimdoc) and not defined(nimsuggest):
{.fatal: "Module jsFFI is designed to be used with the JavaScript backend.".}
@ -221,14 +220,10 @@ proc `==`*(x, y: JsRoot): bool {.importcpp: "(# === #)".}
macro `.`*(obj: JsObject, field: untyped): JsObject =
## Experimental dot accessor (get) for type JsObject.
## Returns the value of a property of name `field` from a JsObject `x`.
##
## Example:
##
## .. code-block:: nim
##
## let obj = newJsObject()
## obj.a = 20
## console.log(obj.a) # puts 20 onto the console.
runnableExamples:
let obj = newJsObject()
obj.a = 20
assert obj.a.to(int) == 20
if validJsName($field):
let importString = "#." & $field
result = quote do: