{"id":1798,"date":"2014-12-24T14:03:54","date_gmt":"2014-12-24T14:03:54","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=1798"},"modified":"2014-12-23T14:08:43","modified_gmt":"2014-12-23T14:08:43","slug":"calling-js-c-using-em_asm-emscripten","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2014\/12\/calling-js-c-using-em_asm-emscripten\/","title":{"rendered":"Calling JS from C using EM_ASM in Emscripten"},"content":{"rendered":"<p>A very simple for calling JS from C and returning a value. Create the following C program, in the file calljs.c, it makes a single call to js_receive which will be our Javascript function:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;emscripten.h&gt;\r\n\r\nint main() {\r\n  int ret = EM_ASM_INT({\r\n    return js_receive($0,$1,$2,$3);\r\n  }, 1,&quot;two&quot;,3,4);\r\n\r\n  printf(&quot;returned: %d\\n&quot;,ret);\r\n}\r\n\r\ncompile it with: &lt;pre&gt;emcc calljs.c -o calljs.js&lt;\/pre&gt;\r\n\r\nIt will then call the JS code which can access the variables and string. Here's an example complete html file:\r\n\r\n&#x5B;sourcecode language=&quot;html&quot;]\r\n &lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;body&gt;\r\n\r\n&lt;script&gt;\r\n\r\nfunction js_receive(a,b,c,d) {\r\n\r\n  var bstr = Pointer_stringify(b);\r\n  document.write(a + &quot; &quot; + bstr + &quot; &quot; + c + &quot; &quot; + d);\r\n\r\n  return 42;\r\n}\r\n\r\n&lt;\/script&gt;\r\n\r\n&lt;script src=&quot;calljs.js&quot;&gt;&lt;\/script&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A very simple for calling JS from C and returning a value. Create the following C program, in the file calljs.c, it makes a single call to js_receive which will be our Javascript function: #include &lt;stdio.h&gt; #include &lt;emscripten.h&gt; int main() { int ret = EM_ASM_INT({ return js_receive($0,$1,$2,$3); }, 1,&quot;two&quot;,3,4); printf(&quot;returned: %d\\n&quot;,ret); } compile it with: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-1798","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-t0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/comments?post=1798"}],"version-history":[{"count":1,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1798\/revisions"}],"predecessor-version":[{"id":1799,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1798\/revisions\/1799"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=1798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=1798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=1798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}