{"action":"created","issue":{"url":"https://api.github.com/repos/kripken/emscripten/issues/3896","labels_url":"https://api.github.com/repos/kripken/emscripten/issues/3896/labels{/name}","comments_url":"https://api.github.com/repos/kripken/emscripten/issues/3896/comments","events_url":"https://api.github.com/repos/kripken/emscripten/issues/3896/events","html_url":"https://github.com/kripken/emscripten/issues/3896","id":115155787,"number":3896,"title":"macro expansion EM_ASM_INT have issues with javascript array","user":{"login":"claudiodaffra","id":4798613,"avatar_url":"https://avatars.githubusercontent.com/u/4798613?v=3","gravatar_id":"","url":"https://api.github.com/users/claudiodaffra","html_url":"https://github.com/claudiodaffra","followers_url":"https://api.github.com/users/claudiodaffra/followers","following_url":"https://api.github.com/users/claudiodaffra/following{/other_user}","gists_url":"https://api.github.com/users/claudiodaffra/gists{/gist_id}","starred_url":"https://api.github.com/users/claudiodaffra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/claudiodaffra/subscriptions","organizations_url":"https://api.github.com/users/claudiodaffra/orgs","repos_url":"https://api.github.com/users/claudiodaffra/repos","events_url":"https://api.github.com/users/claudiodaffra/events{/privacy}","received_events_url":"https://api.github.com/users/claudiodaffra/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-11-04T21:47:57Z","updated_at":"2015-12-09T18:09:39Z","closed_at":null,"body":" macro expansion EM_ASM_INT have issues with javascript array\r\nnot for EM_ASM this macro works perfectly\r\n\r\nemcsriten 1.34 on windows 8 TESTED\r\nclang version 3.7.0  \r\nTarget: x86_64-pc-windows-msvc\r\nThread model: posix\r\nemscripten 1.35 on Linux Mint 17.1 NOT TESTED\r\n\r\n----------------------------- source code\r\n#include <emscripten.h>\r\n#include <stdio.h>\r\n\r\nint main ( void ) \r\n{\r\n\tint ret = EM_ASM_INT  ( {\r\n\t\tvar x = $0 ;\r\n\t\tvar a = [1,2,3] ; // <---- issue with javascript array\r\n\t\treturn 1 ;\r\n\t},10) ;\t\r\n\tprintf (\"%d\",ret );\r\n\treturn 0 ;\r\n}\r\n------------------------------- Error\r\n\r\nprova.cpp:9:17: error: expected ')'\r\n                var a = [1,2,3] ;\r\n                              ^\r\nC:\\Program Files\\Emscripten\\emscripten\\1.34.1\\system\\include\\emscripten\\emscripten.h:54:63: note: expanded from macro 'EM_ASM_INT'\r\n#define EM_ASM_INT(code, ...) emscripten_asm_const_int(#code, __VA_ARGS__)\r\n                                                              ^\r\nprova.cpp:7:12: note: to match this '('\r\n        int ret = EM_ASM_INT  ( {\r\n                  ^\r\nC:\\Program Files\\Emscripten\\emscripten\\1.34.1\\system\\include\\emscripten\\emscripten.h:54:55: note: expanded from macro 'EM_ASM_INT'\r\n#define EM_ASM_INT(code, ...) emscripten_asm_const_int(#code, __VA_ARGS__)\r\n                                                      ^\r\nprova.cpp:11:3: error: expected unqualified-id\r\n        },10) ; \r\n         ^\r\nC:\\Program Files\\Emscripten\\emscripten\\1.34.1\\system\\include\\emscripten\\emscripten.h:54:63: note: expanded from macro 'EM_ASM_INT'\r\n#define EM_ASM_INT(code, ...) emscripten_asm_const_int(#code, __VA_ARGS__)\r\n                                                              ^\r\nprova.cpp:13:2: error: C++ requires a type specifier for all declarations\r\n        printf (\"%d\",ret );\r\n        ^\r\nprova.cpp:13:15: error: use of undeclared identifier 'ret'\r\n        printf (\"%d\",ret );\r\n                     ^\r\nprova.cpp:15:2: error: expected unqualified-id\r\n        return 0 ;\r\n        ^\r\nprova.cpp:16:1: error: extraneous closing brace ('}')\r\n}\r\n^\r\n6 errors generated.\r\nERROR    root: compiler frontend failed to generate LLVM bitcode, halting\r\n\r\ninstead, in this way works :\r\n\r\nint main ( void ) \r\n{\r\n\tint ret = EM_ASM_INT ( {\r\n\t\tvar a = new Array($0) ;\r\n\t\ta[0] = 10 ; //  \r\n\t\ta[1] = 11 ;\r\n\t\treturn a[1] ; ;\r\n\t},10) ;\t\r\n \r\n\tprintf (\"%d\",ret );\r\n\t\r\n\treturn 0 ;\r\n}\r\n\r\nregards\r\nclaudio\r\n\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/kripken/emscripten/issues/comments/163343561","html_url":"https://github.com/kripken/emscripten/issues/3896#issuecomment-163343561","issue_url":"https://api.github.com/repos/kripken/emscripten/issues/3896","id":163343561,"user":{"login":"evanw","id":406394,"avatar_url":"https://avatars.githubusercontent.com/u/406394?v=3","gravatar_id":"","url":"https://api.github.com/users/evanw","html_url":"https://github.com/evanw","followers_url":"https://api.github.com/users/evanw/followers","following_url":"https://api.github.com/users/evanw/following{/other_user}","gists_url":"https://api.github.com/users/evanw/gists{/gist_id}","starred_url":"https://api.github.com/users/evanw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/evanw/subscriptions","organizations_url":"https://api.github.com/users/evanw/orgs","repos_url":"https://api.github.com/users/evanw/repos","events_url":"https://api.github.com/users/evanw/events{/privacy}","received_events_url":"https://api.github.com/users/evanw/received_events","type":"User","site_admin":false},"created_at":"2015-12-09T18:09:39Z","updated_at":"2015-12-09T18:09:39Z","body":"The C preprocessor assumes top-level commas mean additional macro arguments. The preprocessor is only aware of nesting due to parentheses, not curly braces or square brackets which are just treated like any other token. To get around this, just make sure the commas are surrounded by parentheses:\r\n\r\n```\r\nint main ( void ) \r\n{\r\n  int ret = EM_ASM_INT ( {\r\n    var x = $0 ;\r\n    var a = ([1,2,3]); // <---- make sure commas appear inside parentheses\r\n    return 1 ;\r\n  },10) ; \r\n  printf (\"%d\",ret );\r\n  return 0 ;\r\n}\r\n```\r\n\r\n@kripken this is probably something that's useful to have in the documentation near EM_ASM_INT and friends.\r\n"}}