{"id":1800,"date":"2014-12-25T14:54:24","date_gmt":"2014-12-25T14:54:24","guid":{"rendered":"http:\/\/41j.com\/blog\/?p=1800"},"modified":"2014-12-23T14:57:33","modified_gmt":"2014-12-23T14:57:33","slug":"emscripten-calling-js-c-returning-reference-pointer","status":"publish","type":"post","link":"https:\/\/41j.com\/blog\/2014\/12\/emscripten-calling-js-c-returning-reference-pointer\/","title":{"rendered":"Emscripten calling JS from C and returning by reference (pointer)"},"content":{"rendered":"<p>JS code can manipuate memory directly using setValue, here&#8217;s a quick example of returning by reference. The C code is as follows:<\/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#include &lt;string.h&gt;\r\n\r\nint main() {\r\n\r\n  char buffer&#x5B;10];\r\n  strcpy(buffer,&quot;nothing&quot;);\r\n\r\n  int ret = EM_ASM_INT({\r\n    return js_receive($0,$1,$2,$3);\r\n  }, 1,buffer,3,4);\r\n\r\n  printf(&quot;returned: %d\\n&quot;,ret);\r\n  printf(&quot;buffer  : %s\\n&quot;,buffer);\r\n\r\n  char call&#x5B;100];\r\n  strcpy(call,&quot;alert('&quot;);\r\n  strcat(call,buffer);\r\n  strcat(call,&quot;')&quot;);\r\n  emscripten_run_script(call);\r\n}\r\n<\/pre>\n<p>And can be called using the following JS which modifies the contents of buffer:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\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\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  setValue(b  ,  84, 'i8');\r\n  setValue(b+1,  87, 'i8');\r\n  setValue(b+2,  79, 'i8');\r\n  setValue(b+3,   0, 'i8');\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\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JS code can manipuate memory directly using setValue, here&#8217;s a quick example of returning by reference. The C code is as follows: #include &lt;stdio.h&gt; #include &lt;emscripten.h&gt; #include &lt;string.h&gt; int main() { char buffer&#x5B;10]; strcpy(buffer,&quot;nothing&quot;); int ret = EM_ASM_INT({ return js_receive($0,$1,$2,$3); }, 1,buffer,3,4); printf(&quot;returned: %d\\n&quot;,ret); printf(&quot;buffer : %s\\n&quot;,buffer); char call&#x5B;100]; strcpy(call,&quot;alert(&#8216;&quot;); strcat(call,buffer); strcat(call,&quot;&#8217;)&quot;); emscripten_run_script(call); } And [&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-1800","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1RRoU-t2","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1800","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=1800"}],"version-history":[{"count":2,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1800\/revisions"}],"predecessor-version":[{"id":1802,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/posts\/1800\/revisions\/1802"}],"wp:attachment":[{"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/media?parent=1800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/categories?post=1800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/41j.com\/blog\/wp-json\/wp\/v2\/tags?post=1800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}