summary refs log tree commit diff stats
path: root/tests/async/tlambda.nim
blob: 8f570689bc7d268d5d89f6bd42c84256634c7267 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# bug 2007

import asyncdispatch, asyncnet, logging, json, uri, strutils, sugar

type
  Builder = ref object
    client: Client
    build: Build

  ProgressCB* = proc (message: string): Future[void] {.closure, gcsafe.}

  Build* = ref object
    onProgress*: ProgressCB

  Client = ref ClientObj
  ClientObj = object
    onMessage: proc (client: Client, msg: JsonNode): Future[void]

proc newClient*(name: string,
                onMessage: (Client, JsonNode) -> Future[void]): Client =
  new result
  result.onMessage = onMessage

proc newBuild*(onProgress: ProgressCB): Build =
  new result
  result.onProgress = onProgress

proc start(build: Build, repo, hash: string) {.async.} =
  let path = repo.parseUri().path.toLowerAscii()

proc onProgress(builder: Builder, message: string) {.async.} =
  debug($message)

proc onMessage(builder: Builder, message: JsonNode) {.async.} =
  debug("onMessage")

proc newBuilder(): Builder =
  var cres: Builder
  new cres

  cres.client = newClient("builder", (client, msg) => (onMessage(cres, msg)))
  cres.build = newBuild(
      proc (msg: string): Future[void] {.closure, gcsafe.} = onProgress(cres, msg))
  return cres

proc main() =
  # Set up logging.
  var console = newConsoleLogger(fmtStr = verboseFmtStr)
  addHandler(console)

  var builder = newBuilder()

  # Test {.async.} pragma with do notation: #5995
  builder.client = newClient("builder") do(client: Client, msg: JsonNode) {.async.}:
    await onMessage(builder, msg)

main()
5:51 +0200 committer Araq <rumpf_a@web.de> 2018-09-08 10:45:51 +0200 some protection against injected doc comments from templates' href='/ahoang/Nim/commit/compiler/docgen2.nim?h=devel&id=769b562764d1118f18820a676db6a1224c42da39'>769b56276 ^
01ab5948a ^
813828f69 ^


0d68ef9f1 ^
db95fad6f ^
813828f69 ^
db95fad6f ^
01ab5948a ^


25e3e6db8 ^
86556ebfd ^
49d1822c8 ^
01ab5948a ^



db95fad6f ^





091c1b307 ^
db95fad6f ^
813828f69 ^
01ab5948a ^
d68181246 ^
73c6efdf6 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

 
                            








                                                                      
      
                                                          
 
                                                  
 
    
                               

                
                     

                 

                                                                                                          
                                                                                          
 
                                             
                 
                                                    
                      
        

                          
                   
             
 
                                                                  
            
                                  
 
                                                                      
            
                                      
 
                                                    

                 
                      
                            
 


                                                        
                      
                                 
 
                                             


                   
                         
                                                                                         
                                             



                 





                                                                 
                                                                                  
                                                                               
                                                    
 
                                       
         
#
#
#           The Nim Compiler
#        (c) Copyright 2012 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

# This module implements a new documentation generator that runs after
# semantic checking.

import
  options, ast, msgs, passes, docgen, lineinfos, pathutils

from modulegraphs import ModuleGraph, PPassContext

type
  TGen = object of PPassContext
    doc: PDoc
    module: PSym
    config: ConfigRef
  PGen = ref TGen

proc shouldProcess(g: PGen): bool =
  (optWholeProject in g.doc.conf.globalOptions and g.module.getnimblePkgId == g.doc.conf.mainPackageId) or
      sfMainModule in g.module.flags or g.config.projectMainIdx == g.module.info.fileIndex

template closeImpl(body: untyped) {.dirty.} =
  var g = PGen(p)
  let useWarning = sfMainModule notin g.module.flags
  if shouldProcess(g):
    body
    try:
      generateIndex(g.doc)
    except IOError:
      discard

proc close(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  closeImpl:
    writeOutput(g.doc, useWarning)

proc closeJson(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
  closeImpl:
    writeOutputJson(g.doc, useWarning)

proc processNode(c: PPassContext, n: PNode): PNode =
  result = n
  var g = PGen(c)
  if shouldProcess(g):
    generateDoc(g.doc, n, n)

proc processNodeJson(c: PPassContext, n: PNode): PNode =
  result = n
  var g = PGen(c)
  if shouldProcess(g):
    generateJson(g.doc, n, false)

template myOpenImpl(ext: untyped) {.dirty.} =
  var g: PGen
  new(g)
  g.module = module
  g.config = graph.config
  var d = newDocumentor(AbsoluteFile toFullPath(graph.config, FileIndex module.position),
      graph.cache, graph.config, ext, module)
  d.hasToc = true
  g.doc = d
  result = g

proc myOpen(graph: ModuleGraph; module: PSym): PPassContext =
  myOpenImpl(HtmlExt)

proc myOpenJson(graph: ModuleGraph; module: PSym): PPassContext =
  myOpenImpl(JsonExt)

const docgen2Pass* = makePass(open = myOpen, process = processNode, close = close)
const docgen2JsonPass* = makePass(open = myOpenJson, process = processNodeJson,
                                  close = closeJson)

proc finishDoc2Pass*(project: string) =
  discard