The last time you can compile C code into a web-invoked JS file, it was exciting. Haha, I also mistakenly thought is the system level C programming!
Hey, today, to tell you a brutal fact is that just the C language level, not the system level. Because the webassembly goal is cross-platform, so, how can have the system API to mix in it. Of course, you can continue dreaming one day until each system vendor rubs its own system calls into the Webassembly SDK library. hahaha.
Today, we continue to webassembly the source C language tutorial, this time it is more complicated.
Besides the basic type, such as int, what about the string?
Hey, I'll answer you today.
*************************************************************************************************************** ******************
Call.c
#include <math.h>
#include <stdlib.h>
#include <string.h>
Char *test1 (char *instr)
{
Char *array = malloc (sizeof (char) * (strlen (instr) +1));
strcpy (ARRAY,INSTR);
int size = strlen (array);
for (int n=0;n<size;n++)
{
if ((Array[n] >= ' a ') && (array[n] <= ' z ')) array[n]-= ' a '-' a ';
}
return array;
}
int *test2 (int *in,int size)
{
int *array = malloc (sizeof (int) *size);
for (int n=0;n<size;n++)
{
Array[n] = in[n]*2;
}
return array;
}
Compiler
EMCC call.c-o call.js-s exported_functions= "[' _test1 ', ' _test2 ']"
Html
<! DOCTYPE html>
<body>
<script src= "Call.js" ></script>
<script>
function Write_1d_int32_array (Ptr,array)
{
for (i=0;i<array.length;i++)
{
Module.setvalue (Ptr,array[i], ' i32 ');
PTR + = 4;
}
}
function Read_1d_int32_array (ptr,length)
{
var array = [];
for (i=0;i<length;i++)
{
var value = Module.getvalue (ptr+ (i*4), ' i32 ');
Array.push (value);
}
return array;
}
Test1-a C function that takes, and returns a C style string (char *)
var mystring = "Test";
var strptr = Module._malloc (mystring.length);
Module.writeasciitomemory (mystring, strptr);
var retstrptr = module.ccall (' test1 ',//name of C function
' Number ',//return type
[' Number '],//argument types
[StrPtr]); Arguments
Convert the resulting string to a JS string
var retstr = pointer_stringify (retstrptr);
document.write (RETSTR);
Test2-a C function that takes, and returns a C style int array (int *)
var myarray = [10,20,30,40,50];
var arrayptr = Module._malloc (myarray.length*4);
Write_1d_int32_array (Arrayptr,myarray);
var retarrayptr = module.ccall (' test2 ',//name of C function
' Number ',//return type
[' Number '],//argument types
[Arrayptr,myarray.length]); Arguments
Convert The resulting pointer to a JS array
var Retarray = Read_1d_int32_array (retarrayptr,myarray.length);
document.write (Retarray);
</script>
</body>
Perform
Place Call.js, index.html into the Tomcat server universal, enter in the browser
Http://localhost:8080/universal/index.html
See, here's what you're looking for.
This routine is written by a foreign Daniel, we salute the hero here!
*********************************************************************
Finally:
Sorry, the enthusiasm for webassembly, I altogether TMD reduced.
I think continue to golang it, at least in Linux he can invoke the system-level functions, but also rational it is the service side of things, hahaha
Brothers and sisters on Windows, you go and find them, or you have no milk to eat! Ha ha haha
Goodbye!
Discussion on WebAssembly technology again