Skip to content

Instantly share code, notes, and snippets.

@junjihashimoto
Created December 1, 2016 20:50
Show Gist options
  • Save junjihashimoto/a44d7067e3d326fe739f9c79f8d16a89 to your computer and use it in GitHub Desktop.
Save junjihashimoto/a44d7067e3d326fe739f9c79f8d16a89 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -x
cat <<EOF > write.c
#include <stdio.h>
int
write_test(){
{
FILE* out = fopen("/hoge0.txt","w");
char buf[1024];
int len;
if(out==NULL){
printf("write:open failed\n");
return -1;
}
fprintf(out,"write test\n");
fclose(out);
}
return 0;
}
int
read_test(){
{
FILE* in = fopen("/hoge0.txt","r");
char buf[1024];
int len;
if(in==NULL){
printf("read:open failed\n");
return -1;
}
while(! feof(in)){
if(fgets(buf,sizeof(buf),in)==buf){
printf("%s",buf);
}
}
fclose(in);
}
return 0;
}
EOF
cat <<EOF > read.c
#include <stdio.h>
int
read_test(){
{
FILE* in = fopen("/hoge0.txt","r");
char buf[1024];
int len;
if(in==NULL){
printf("read:open failed\n");
return -1;
}
while(! feof(in)){
if(fgets(buf,sizeof(buf),in)==buf){
printf("%s",buf);
}
}
fclose(in);
}
return 0;
}
EOF
cat <<EOF > pre_w.js
var Module = {
"noExitRuntime" : true
}
EOF
cat <<EOF > pre_r.js
var Module = {
"noExitRuntime" : true,
FS: SHARED_FS
}
EOF
cat <<EOF > test0.js
SHARED_FS = {};
var w = require('./write.js');
SHARED_FS = w.FS;
var r = require('./read.js');
w.ccall('write_test','number',[],[]);
w.ccall('read_test','number',[],[]);
r.ccall('read_test','number',[],[]);
EOF
emcc -o read.js read.c --pre-js pre_r.js -s MAIN_MODULE=1
emcc -o write.js write.c --pre-js pre_w.js -s MAIN_MODULE=1
node ./test0.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment