{"action":"opened","issue":{"url":"https://api.github.com/repos/nim-lang/Nim/issues/3132","labels_url":"https://api.github.com/repos/nim-lang/Nim/issues/3132/labels{/name}","comments_url":"https://api.github.com/repos/nim-lang/Nim/issues/3132/comments","events_url":"https://api.github.com/repos/nim-lang/Nim/issues/3132/events","html_url":"https://github.com/nim-lang/Nim/issues/3132","id":95739167,"number":3132,"title":"Need testcase for JS backend to insure closure ballbacks don't break","user":{"login":"bogen","id":5392431,"avatar_url":"https://avatars.githubusercontent.com/u/5392431?v=3","gravatar_id":"","url":"https://api.github.com/users/bogen","html_url":"https://github.com/bogen","followers_url":"https://api.github.com/users/bogen/followers","following_url":"https://api.github.com/users/bogen/following{/other_user}","gists_url":"https://api.github.com/users/bogen/gists{/gist_id}","starred_url":"https://api.github.com/users/bogen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bogen/subscriptions","organizations_url":"https://api.github.com/users/bogen/orgs","repos_url":"https://api.github.com/users/bogen/repos","events_url":"https://api.github.com/users/bogen/events{/privacy}","received_events_url":"https://api.github.com/users/bogen/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-07-17T20:35:54Z","updated_at":"2015-07-17T20:35:54Z","closed_at":null,"body":"I make extensive use of callbacks from JS code to nim nested functions.\r\n\r\nHere is a testcase to insure these don't break:\r\n```nim\r\nimport math, strutils\r\nconst consolePrefix = \"jsCallbacks\"\r\n\r\nasm \"\"\"\r\n    var callback = []\r\n    function regCallback (fn) { callback.push (fn); }\r\n    function runCallbacks () {\r\n        var result = \"\\n\"\r\n        var n = 0\r\n        for (var fn in callback) {\r\n            n += 1\r\n            result += \"(\"+String (n)+\")\"\r\n            result += callback [fn] ()\r\n            result += \"\\n\"\r\n        }\r\n        return result\r\n    }\r\n    function print (text) { console.log (text); }\r\n\"\"\"\r\n\r\nproc consoleprint (str:cstring): void {.importc: \"print\", noDecl.}\r\nproc print* (a: varargs[string, `$`]) = consoleprint \"$1: $2\" % [consolePrefix, join (a, \" \")]\r\n\r\ntype CallbackProc {.importc.} = proc () : cstring\r\n\r\nproc regCallback (fn:CallbackProc) {.importc.}\r\nproc runCallbacks ():cstring {.importc.}\r\n\r\nproc `*` (s:string, n:Natural) : string = s.repeat (n)\r\n\r\nproc outer (i:Natural) : (string, int) =\r\n    let c = $char (random (93) + 33)\r\n    let n = random (40)\r\n    let s = c * n\r\n    proc inner () : cstring = (\"[$1]\" % $n) & s & \" <--\"\r\n    regCallback (inner)\r\n    return (s, n)\r\n\r\nvar expected = \"\\n\"\r\nfor i in 1 .. 10:\r\n    let (s, n) = outer (i)\r\n    expected &= (\"($1)[$2]\" % [$i, $n]) & s & \" <--\"\r\n    expected &= \"\\n\"\r\n\r\nprint expected\r\n\r\nlet results = runCallbacks ()\r\n\r\nprint results\r\n\r\nprint \"expected == results :\", (expected == results)\r\nassert (expected == results)\r\n```\r\n```text\r\n$ nim js -d:nodejs --opt:size -d:release --hints:off --warnings:on --deadCodeElim:on --checks:on --assertions:on -o:jsCallbacks.js jsCallbacks.nim  && node jsCallbacks.js \r\njsCallbacks: \r\n(1)[0] <--\r\n(2)[23]\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\" <--\r\n(3)[10]VVVVVVVVVV <--\r\n(4)[26]}}}}}}}}}}}}}}}}}}}}}}}}}} <--\r\n(5)[24]WWWWWWWWWWWWWWWWWWWWWWWW <--\r\n(6)[2]&& <--\r\n(7)[27]/////////////////////////// <--\r\n(8)[25]@@@@@@@@@@@@@@@@@@@@@@@@@ <--\r\n(9)[18]EEEEEEEEEEEEEEEEEE <--\r\n(10)[25]IIIIIIIIIIIIIIIIIIIIIIIII <--\r\n\r\njsCallbacks: \r\n(1)[0] <--\r\n(2)[23]\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\" <--\r\n(3)[10]VVVVVVVVVV <--\r\n(4)[26]}}}}}}}}}}}}}}}}}}}}}}}}}} <--\r\n(5)[24]WWWWWWWWWWWWWWWWWWWWWWWW <--\r\n(6)[2]&& <--\r\n(7)[27]/////////////////////////// <--\r\n(8)[25]@@@@@@@@@@@@@@@@@@@@@@@@@ <--\r\n(9)[18]EEEEEEEEEEEEEEEEEE <--\r\n(10)[25]IIIIIIIIIIIIIIIIIIIIIIIII <--\r\n\r\njsCallbacks: expected == results : true\r\n```\r\nEach run will result in a different set of expected results, but the results must always be what is expected."}}