Markdown code blocks migration part 7 (#20547)

This commit is contained in:
Andrey Makarov 2022-10-12 17:13:43 +03:00 • committed by GitHub
commit 19ff746916
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 426 additions and 411 deletions

View file

@ -17,37 +17,42 @@
##
## This is roughly equivalent to the `async` keyword in JavaScript code.
##
## .. code-block:: nim
## proc loadGame(name: string): Future[Game] {.async.} =
## # code
## ```nim
## proc loadGame(name: string): Future[Game] {.async.} =
## # code
## ```
##
## should be equivalent to
##
## .. code-block:: javascript
## ```javascript
## async function loadGame(name) {
## // code
## }
## ```
##
## A call to an asynchronous procedure usually needs `await` to wait for
## the completion of the `Future`.
##
## .. code-block:: nim
## ```nim
## var game = await loadGame(name)
## ```
##
## Often, you might work with callback-based API-s. You can wrap them with
## asynchronous procedures using promises and `newPromise`:
##
## .. code-block:: nim
## ```nim
## proc loadGame(name: string): Future[Game] =
## var promise = newPromise() do (resolve: proc(response: Game)):
## cbBasedLoadGame(name) do (game: Game):
## resolve(game)
## return promise
## ```
##
## Forward definitions work properly, you just need to always add the `{.async.}` pragma:
##
## .. code-block:: nim
## ```nim
## proc loadGame(name: string): Future[Game] {.async.}
## ```
##
## JavaScript compatibility
## ========================
@ -57,7 +62,7 @@
## If you need to use this module with older versions of JavaScript, you can
## use a tool that backports the resulting JavaScript code, as babel.
# xxx code-block:: javascript above gives `LanguageXNotSupported` warning.
# xxx code: javascript above gives `LanguageXNotSupported` warning.
when not defined(js) and not defined(nimsuggest):
{.fatal: "Module asyncjs is designed to be used with the JavaScript backend.".}

View file

@ -1335,9 +1335,10 @@ since (1, 3):
## DOM Parser object (defined on browser only, may not be on NodeJS).
## * https://developer.mozilla.org/en-US/docs/Web/API/DOMParser
##
## .. code-block:: nim
## ```nim
## let prsr = newDomParser()
## discard prsr.parseFromString("<html><marquee>Hello World</marquee></html>".cstring, "text/html".cstring)
## ```
DomException* = ref DOMExceptionObj
## The DOMException interface represents an abnormal event (called an exception)

View file

@ -268,15 +268,14 @@ macro `.()`*(obj: JsObject,
## so be careful when using this.)
##
## Example:
##
## .. code-block:: nim
##
## # Let's get back to the console example:
## var console {.importc, nodecl.}: JsObject
## let res = console.log("I return undefined!")
## console.log(res) # This prints undefined, as console.log always returns
## # undefined. Thus one has to be careful, when using
## # JsObject calls.
## ```nim
## # Let's get back to the console example:
## var console {.importc, nodecl.}: JsObject
## let res = console.log("I return undefined!")
## console.log(res) # This prints undefined, as console.log always returns
## # undefined. Thus one has to be careful, when using
## # JsObject calls.
## ```
var importString: string
if validJsName($field):
importString = "#." & $field & "(@)"
@ -407,21 +406,20 @@ macro `{}`*(typ: typedesc, xs: varargs[untyped]): auto =
##
## Example:
##
## .. code-block:: nim
## ```nim
## # Let's say we have a type with a ton of fields, where some fields do not
## # need to be set, and we do not want those fields to be set to `nil`:
## type
## ExtremelyHugeType = ref object
## a, b, c, d, e, f, g: int
## h, i, j, k, l: cstring
## # And even more fields ...
##
## # Let's say we have a type with a ton of fields, where some fields do not
## # need to be set, and we do not want those fields to be set to `nil`:
## type
## ExtremelyHugeType = ref object
## a, b, c, d, e, f, g: int
## h, i, j, k, l: cstring
## # And even more fields ...
##
## let obj = ExtremelyHugeType{ a: 1, k: "foo".cstring, d: 42 }
##
## # This generates roughly the same JavaScript as:
## {.emit: "var obj = {a: 1, k: "foo", d: 42};".}
## let obj = ExtremelyHugeType{ a: 1, k: "foo".cstring, d: 42 }
##
## # This generates roughly the same JavaScript as:
## {.emit: "var obj = {a: 1, k: "foo", d: 42};".}
## ```
let a = ident"a"
var body = quote do:
var `a` {.noinit.}: `typ`
@ -471,24 +469,25 @@ macro bindMethod*(procedure: typed): auto =
## Example:
##
## We want to generate roughly this JavaScript:
##
## .. code-block:: js
## var obj = {a: 10};
## obj.someMethod = function() {
## return this.a + 42;
## };
## ```js
## var obj = {a: 10};
## obj.someMethod = function() {
## return this.a + 42;
## };
## ```
##
## We can achieve this using the `bindMethod` macro:
##
## .. code-block:: nim
## let obj = JsObject{ a: 10 }
## proc someMethodImpl(that: JsObject): int =
## that.a.to(int) + 42
## obj.someMethod = bindMethod someMethodImpl
## ```nim
## let obj = JsObject{ a: 10 }
## proc someMethodImpl(that: JsObject): int =
## that.a.to(int) + 42
## obj.someMethod = bindMethod someMethodImpl
##
## # Alternatively:
## obj.someMethod = bindMethod
## proc(that: JsObject): int = that.a.to(int) + 42
## # Alternatively:
## obj.someMethod = bindMethod
## proc(that: JsObject): int = that.a.to(int) + 42
## ```
if not (procedure.kind == nnkSym or procedure.kind == nnkLambda):
error("Argument has to be a proc or a symbol corresponding to a proc.")
var