From 10d1343d682c2982827c78365350697bfded09f7 Mon Sep 17 00:00:00 2001 From: winetree94 Date: Fri, 2 Aug 2024 04:00:39 +0900 Subject: [PATCH] feat: modernize wasi example codes --- .../htdocs/browser_wasi_shim/index.js | 1 - .../htdocs/browser_wasi_shim/wasi_defs.js | 1 - examples/wasi-browser/htdocs/index.html | 4 +- examples/wasi-browser/htdocs/stack-worker.js | 87 ++++++++------ examples/wasi-browser/htdocs/wasi-util.js | 26 ++-- examples/wasi-browser/htdocs/worker-util.js | 112 ++++++++++-------- examples/wasi-browser/htdocs/worker.js | 61 +++++----- 7 files changed, 159 insertions(+), 133 deletions(-) delete mode 100644 examples/wasi-browser/htdocs/browser_wasi_shim/index.js delete mode 100644 examples/wasi-browser/htdocs/browser_wasi_shim/wasi_defs.js diff --git a/examples/wasi-browser/htdocs/browser_wasi_shim/index.js b/examples/wasi-browser/htdocs/browser_wasi_shim/index.js deleted file mode 100644 index 82a60320..00000000 --- a/examples/wasi-browser/htdocs/browser_wasi_shim/index.js +++ /dev/null @@ -1 +0,0 @@ -!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r=e();for(var s in r)("object"==typeof exports?exports:t)[s]=r[s]}}(self,(()=>(()=>{"use strict";var t={759:(t,e,r)=>{r.d(e,{CLOCKID_MONOTONIC:()=>n,CLOCKID_REALTIME:()=>s,Ciovec:()=>_,Dirent:()=>c,ERRNO_BADF:()=>i,ERRNO_INVAL:()=>f,FILETYPE_DIRECTORY:()=>u,FILETYPE_REGULAR_FILE:()=>h,Fdstat:()=>p,Filestat:()=>b,Iovec:()=>l,OFLAGS_CREAT:()=>y,OFLAGS_DIRECTORY:()=>m,OFLAGS_EXCL:()=>w,OFLAGS_TRUNC:()=>g,Prestat:()=>E,WHENCE_CUR:()=>d,WHENCE_END:()=>o,WHENCE_SET:()=>a});const s=0,n=1,i=8,f=28;class l{static read_bytes(t,e){let r=new l;return r.buf=t.getUint32(e,!0),r.buf_len=t.getUint32(e+4,!0),r}static read_bytes_array(t,e,r){let s=[];for(let n=0;n{for(var s in e)r.o(e,s)&&!r.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:e[s]})},r.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var s={};return(()=>{r.r(s),r.d(s,{Directory:()=>f,Fd:()=>n,File:()=>i,OpenDirectory:()=>_,OpenFile:()=>l,PreopenDirectory:()=>a,WASI:()=>e,strace:()=>d});var t=r(759);let e=class{start(t){this.inst=t,t.exports._start()}initialize(t){this.inst=t,t.exports._initialize()}constructor(e,r,s){this.args=[],this.env=[],this.fds=[],this.args=e,this.env=r,this.fds=s;let n=this;this.wasiImport={args_sizes_get(t,e){let r=new DataView(n.inst.exports.memory.buffer);r.setUint32(t,n.args.length,!0);let s=0;for(let t of n.args)s+=t.length+1;return r.setUint32(e,s,!0),0},args_get(t,e){let r=new DataView(n.inst.exports.memory.buffer),s=new Uint8Array(n.inst.exports.memory.buffer);for(let i=0;inull!=n.fds[e]?n.fds[e].fd_advise(r,s,i):t.ERRNO_BADF,fd_allocate:(e,r,s)=>null!=n.fds[e]?n.fds[e].fd_allocate(r,s):t.ERRNO_BADF,fd_close(e){if(null!=n.fds[e]){let t=n.fds[e].fd_close();return n.fds[e]=void 0,t}return t.ERRNO_BADF},fd_datasync:e=>null!=n.fds[e]?n.fds[e].fd_datasync():t.ERRNO_BADF,fd_fdstat_get(e,r){if(null!=n.fds[e]){let{ret:t,fdstat:s}=n.fds[e].fd_fdstat_get();return null!=s&&s.write_bytes(new DataView(n.inst.exports.memory.buffer),r),t}return t.ERRNO_BADF},fd_fdstat_set_flags:(e,r)=>null!=n.fds[e]?n.fds[e].fd_fdstat_set_flags(r):t.ERRNO_BADF,fd_fdstat_set_rights:(e,r,s)=>null!=n.fds[e]?n.fds[e].fd_fdstat_set_rights(r,s):t.ERRNO_BADF,fd_filestat_get(e,r){if(null!=n.fds[e]){let{ret:t,filestat:s}=n.fds[e].fd_filestat_get();return null!=s&&s.write_bytes(new DataView(n.inst.exports.memory.buffer),r),t}return t.ERRNO_BADF},fd_filestat_set_size:(e,r)=>null!=n.fds[e]?n.fds[e].fd_filestat_set_size(r):t.ERRNO_BADF,fd_filestat_set_times:(e,r,s,i)=>null!=n.fds[e]?n.fds[e].fd_filestat_set_times(r,s,i):t.ERRNO_BADF,fd_pread(e,r,s,i,f){let l=new DataView(n.inst.exports.memory.buffer),_=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[e]){let a=t.Iovec.read_bytes_array(l,r,s),{ret:d,nread:o}=n.fds[e].fd_pread(_,a,i);return l.setUint32(f,o,!0),d}return t.ERRNO_BADF},fd_prestat_get(e,r){let s=new DataView(n.inst.exports.memory.buffer);if(null!=n.fds[e]){let{ret:t,prestat:i}=n.fds[e].fd_prestat_get();return null!=i&&i.write_bytes(s,r),t}return t.ERRNO_BADF},fd_prestat_dir_name(e,r,s){if(null!=n.fds[e]){let{ret:t,prestat_dir_name:s}=n.fds[e].fd_prestat_dir_name();return null!=s&&new Uint8Array(n.inst.exports.memory.buffer).set(s,r),t}return t.ERRNO_BADF},fd_pwrite(e,r,s,i,f){let l=new DataView(n.inst.exports.memory.buffer),_=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[e]){let a=t.Ciovec.read_bytes_array(l,r,s),{ret:d,nwritten:o}=n.fds[e].fd_pwrite(_,a,i);return l.setUint32(f,o,!0),d}return t.ERRNO_BADF},fd_read(e,r,s,i){let f=new DataView(n.inst.exports.memory.buffer),l=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[e]){let _=t.Iovec.read_bytes_array(f,r,s),{ret:a,nread:d}=n.fds[e].fd_read(l,_);return f.setUint32(i,d,!0),a}return t.ERRNO_BADF},fd_readdir(e,r,s,i,f){let l=new DataView(n.inst.exports.memory.buffer),_=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[e]){let t=0;for(;;){let{ret:a,dirent:d}=n.fds[e].fd_readdir_single(i);if(0!=a)return l.setUint32(f,t,!0),a;if(null==d)break;let o=d.length();if(s-tnull!=n.fds[e]?n.fds[e].fd_sync():t.ERRNO_BADF,fd_tell(e,r){let s=new DataView(n.inst.exports.memory.buffer);if(null!=n.fds[e]){let{ret:t,offset:i}=n.fds[e].fd_tell();return s.setUint32(r,i,!0),t}return t.ERRNO_BADF},fd_write(e,r,s,i){let f=new DataView(n.inst.exports.memory.buffer),l=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[e]){let _=t.Ciovec.read_bytes_array(f,r,s),{ret:a,nwritten:d}=n.fds[e].fd_write(l,_);return f.setUint32(i,d,!0),a}return t.ERRNO_BADF},path_create_directory(t,e,r){let s=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[t]){let i=new TextDecoder("utf-8").decode(s.slice(e,e+r));return n.fds[t].path_create_directory(i)}},path_filestat_get(e,r,s,i,f){let l=new DataView(n.inst.exports.memory.buffer),_=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[e]){let t=new TextDecoder("utf-8").decode(_.slice(s,s+i)),{ret:a,filestat:d}=n.fds[e].path_filestat_get(r,t);return null!=d&&d.write_bytes(l,f),a}return t.ERRNO_BADF},path_filestat_set_times(e,r,s,i,f,l,_){let a=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[e]){let t=new TextDecoder("utf-8").decode(a.slice(s,s+i));return n.fds[e].path_filestat_set_times(r,t,f,l,_)}return t.ERRNO_BADF},path_link(e,r,s,i,f,l,_){let a=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[e]&&null!=n.fds[f]){let t=new TextDecoder("utf-8").decode(a.slice(s,s+i)),d=new TextDecoder("utf-8").decode(a.slice(l,l+_));return n.fds[f].path_link(e,r,t,d)}return t.ERRNO_BADF},path_open(e,r,s,i,f,l,_,a,d){let o=new DataView(n.inst.exports.memory.buffer),u=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[e]){let t=new TextDecoder("utf-8").decode(u.slice(s,s+i)),{ret:h,fd_obj:c}=n.fds[e].path_open(r,t,f,l,_,a);if(0!=h)return h;n.fds.push(c);let p=n.fds.length-1;return o.setUint32(d,p,!0),0}return t.ERRNO_BADF},path_readlink(e,r,s,i,f,l){let _=new DataView(n.inst.exports.memory.buffer),a=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[e]){let d=new TextDecoder("utf-8").decode(a.slice(r,r+s)),{ret:o,data:u}=n.fds[e].path_readlink(d);if(null!=u){if(u.length>f)return _.setUint32(l,0,!0),t.ERRNO_BADF;a.set(u,i),_.setUint32(l,u.length,!0)}return o}return t.ERRNO_BADF},path_remove_directory(e,r,s){let i=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[e]){let t=new TextDecoder("utf-8").decode(i.slice(r,r+s));return n.fds[e].path_remove_directory(t)}return t.ERRNO_BADF},path_rename(t,e,r,s,n,i){throw"FIXME what is the best abstraction for this?"},path_symlink(e,r,s,i,f){let l=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[s]){let t=new TextDecoder("utf-8").decode(l.slice(e,e+r)),_=new TextDecoder("utf-8").decode(l.slice(i,i+f));return n.fds[s].path_symlink(t,_)}return t.ERRNO_BADF},path_unlink_file(e,r,s){let i=new Uint8Array(n.inst.exports.memory.buffer);if(null!=n.fds[e]){let t=new TextDecoder("utf-8").decode(i.slice(r,r+s));return n.fds[e].path_unlink_file(t)}return t.ERRNO_BADF},poll_oneoff(t,e,r){throw"async io not supported"},proc_exit(t){throw"exit with exit code "+t},proc_raise(t){throw"raised signal "+t},sched_yield(){},random_get(t,e){let r=new Uint8Array(n.inst.exports.memory.buffer);for(let s=0;s"/"!=t));for(let t=0;tthis.file.size){let t=this.file.data;this.file.data=new Uint8Array(Number(this.file_pos+BigInt(e.byteLength))),this.file.data.set(t)}this.file.data.set(e.slice(0,this.file.size-Number(this.file_pos)),Number(this.file_pos)),this.file_pos+=BigInt(e.byteLength),r+=s.buf_len}return{ret:0,nwritten:r}}fd_filestat_get(){return{ret:0,filestat:this.file.stat()}}constructor(t){super(),this.file_pos=0n,this.file=t}}class _ extends n{fd_fdstat_get(){return{ret:0,fdstat:new t.Fdstat(t.FILETYPE_DIRECTORY,0)}}fd_readdir_single(e){if(e>=BigInt(Object.keys(this.dir.contents).length))return{ret:0,dirent:null};let r=Object.keys(this.dir.contents)[Number(e)],s=this.dir.contents[r];return new TextEncoder("utf-8").encode(r),{ret:0,dirent:new t.Dirent(e+1n,r,s.stat().filetype)}}path_filestat_get(t,e){let r=this.dir.get_entry_for_path(e);return null==r?{ret:-1,filestat:null}:{ret:0,filestat:r.stat()}}path_open(e,r,s,n,a,d){let o=this.dir.get_entry_for_path(r);if(null==o){if((s&t.OFLAGS_CREAT)!=t.OFLAGS_CREAT)return{ret:-1,fd_obj:null};o=this.dir.create_entry_for_path(r)}else if((s&t.OFLAGS_EXCL)==t.OFLAGS_EXCL)return{ret:-1,fd_obj:null};if((s&t.OFLAGS_DIRECTORY)==t.OFLAGS_DIRECTORY&&o.stat().filetype!=t.FILETYPE_DIRECTORY)return{ret:-1,fd_obj:null};if((s&t.OFLAGS_TRUNC)==t.OFLAGS_TRUNC&&o.truncate(),o instanceof i)return{ret:0,fd_obj:new l(o)};if(o instanceof f)return{ret:0,fd_obj:new _(o)};throw"dir entry neither file nor dir"}constructor(t){super(),this.dir=t}}class a extends _{fd_prestat_get(){return{ret:0,prestat:t.Prestat.dir(this.prestat_name.length)}}fd_prestat_dir_name(){return{ret:0,prestat_dir_name:this.prestat_name}}constructor(t,e){super(new f(e)),this.prestat_name=new TextEncoder("utf-8").encode(t)}}function d(t,e){return new Proxy(t,{get(t,r,s){let n=Reflect.get(t,r,s);return e.includes(r)?n:function(...t){return console.log(r,"(",...t,")"),Reflect.apply(n,s,t)}}})}})(),s})())); \ No newline at end of file diff --git a/examples/wasi-browser/htdocs/browser_wasi_shim/wasi_defs.js b/examples/wasi-browser/htdocs/browser_wasi_shim/wasi_defs.js deleted file mode 100644 index 1d7cf709..00000000 --- a/examples/wasi-browser/htdocs/browser_wasi_shim/wasi_defs.js +++ /dev/null @@ -1 +0,0 @@ -!function(_,R){if("object"==typeof exports&&"object"==typeof module)module.exports=R();else if("function"==typeof define&&define.amd)define([],R);else{var E=R();for(var N in E)("object"==typeof exports?exports:_)[N]=E[N]}}(self,(()=>(()=>{"use strict";var _={d:(R,E)=>{for(var N in E)_.o(E,N)&&!_.o(R,N)&&Object.defineProperty(R,N,{enumerable:!0,get:E[N]})},o:(_,R)=>Object.prototype.hasOwnProperty.call(_,R),r:_=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(_,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(_,"__esModule",{value:!0})}},R={};_.r(R),_.d(R,{ADVICE_DONTNEED:()=>hR,ADVICE_NOREUSE:()=>lR,ADVICE_NORMAL:()=>UR,ADVICE_RANDOM:()=>oR,ADVICE_SEQUENTIAL:()=>HR,ADVICE_WILLNEED:()=>aR,CLOCKID_MONOTONIC:()=>T,CLOCKID_PROCESS_CPUTIME_ID:()=>S,CLOCKID_REALTIME:()=>O,CLOCKID_THREAD_CPUTIME_ID:()=>e,Ciovec:()=>eR,Dirent:()=>PR,ERRNO_2BIG:()=>A,ERRNO_ACCES:()=>i,ERRNO_ADDRINUSE:()=>s,ERRNO_ADDRNOTAVAIL:()=>L,ERRNO_AFNOSUPPORT:()=>n,ERRNO_AGAIN:()=>D,ERRNO_ALREADY:()=>G,ERRNO_BADF:()=>C,ERRNO_BADMSG:()=>r,ERRNO_BUSY:()=>F,ERRNO_CANCELED:()=>P,ERRNO_CHILD:()=>U,ERRNO_CONNABORTED:()=>H,ERRNO_CONNREFUSED:()=>o,ERRNO_CONNRESET:()=>a,ERRNO_DEADLK:()=>h,ERRNO_DESTADDRREQ:()=>l,ERRNO_DOM:()=>c,ERRNO_DQUOT:()=>M,ERRNO_EXIST:()=>d,ERRNO_FAULT:()=>f,ERRNO_FBIG:()=>y,ERRNO_HOSTUNREACH:()=>g,ERRNO_IDRM:()=>b,ERRNO_ILSEQ:()=>Y,ERRNO_INPROGRESS:()=>B,ERRNO_INTR:()=>u,ERRNO_INVAL:()=>K,ERRNO_IO:()=>p,ERRNO_ISCONN:()=>V,ERRNO_ISDIR:()=>m,ERRNO_LOOP:()=>W,ERRNO_MFILE:()=>w,ERRNO_MLINK:()=>v,ERRNO_MSGSIZE:()=>X,ERRNO_MULTIHOP:()=>j,ERRNO_NAMETOOLONG:()=>x,ERRNO_NETDOWN:()=>Q,ERRNO_NETRESET:()=>Z,ERRNO_NETUNREACH:()=>k,ERRNO_NFILE:()=>z,ERRNO_NOBUFS:()=>q,ERRNO_NODEV:()=>J,ERRNO_NOENT:()=>$,ERRNO_NOEXEC:()=>__,ERRNO_NOLCK:()=>R_,ERRNO_NOLINK:()=>E_,ERRNO_NOMEM:()=>N_,ERRNO_NOMSG:()=>t_,ERRNO_NOPROTOOPT:()=>O_,ERRNO_NOSPC:()=>T_,ERRNO_NOSYS:()=>S_,ERRNO_NOTCAPABLE:()=>y_,ERRNO_NOTCONN:()=>e_,ERRNO_NOTDIR:()=>I_,ERRNO_NOTEMPTY:()=>A_,ERRNO_NOTRECOVERABLE:()=>i_,ERRNO_NOTSOCK:()=>s_,ERRNO_NOTSUP:()=>L_,ERRNO_NOTTY:()=>n_,ERRNO_NXIO:()=>D_,ERRNO_OVERFLOW:()=>G_,ERRNO_OWNERDEAD:()=>C_,ERRNO_PERM:()=>r_,ERRNO_PIPE:()=>F_,ERRNO_PROTO:()=>P_,ERRNO_PROTONOSUPPORT:()=>U_,ERRNO_PROTOTYPE:()=>H_,ERRNO_RANGE:()=>o_,ERRNO_ROFS:()=>a_,ERRNO_SPIPE:()=>h_,ERRNO_SRCH:()=>l_,ERRNO_STALE:()=>c_,ERRNO_SUCCESS:()=>I,ERRNO_TIMEDOUT:()=>M_,ERRNO_TXTBSY:()=>d_,ERRNO_XDEV:()=>f_,EVENTRWFLAGS_FD_READWRITE_HANGUP:()=>jR,EVENTTYPE_CLOCK:()=>wR,EVENTTYPE_FD_READ:()=>vR,EVENTTYPE_FD_WRITE:()=>XR,FDFLAGS_APPEND:()=>cR,FDFLAGS_DSYNC:()=>MR,FDFLAGS_NONBLOCK:()=>dR,FDFLAGS_RSYNC:()=>fR,FDFLAGS_SYNC:()=>yR,FD_STDERR:()=>t,FD_STDIN:()=>E,FD_STDOUT:()=>N,FILETYPE_BLOCK_DEVICE:()=>LR,FILETYPE_CHARACTER_DEVICE:()=>nR,FILETYPE_DIRECTORY:()=>DR,FILETYPE_REGULAR_FILE:()=>GR,FILETYPE_SOCKET_DGRAM:()=>CR,FILETYPE_SOCKET_STREAM:()=>rR,FILETYPE_SYMBOLIC_LINK:()=>FR,FILETYPE_UNKNOWN:()=>sR,FSTFLAGS_ATIM:()=>bR,FSTFLAGS_ATIM_NOW:()=>YR,FSTFLAGS_MTIM:()=>BR,FSTFLAGS_MTIM_NOW:()=>uR,Fdstat:()=>gR,Filestat:()=>WR,Iovec:()=>SR,OFLAGS_CREAT:()=>KR,OFLAGS_DIRECTORY:()=>pR,OFLAGS_EXCL:()=>VR,OFLAGS_TRUNC:()=>mR,PREOPENTYPE_DIR:()=>dE,Prestat:()=>yE,PrestatDir:()=>fE,RIFLAGS_RECV_PEEK:()=>aE,RIFLAGS_RECV_WAITALL:()=>hE,RIGHTS_FD_ADVISE:()=>V_,RIGHTS_FD_ALLOCATE:()=>m_,RIGHTS_FD_DATASYNC:()=>g_,RIGHTS_FD_FDSTAT_SET_FLAGS:()=>B_,RIGHTS_FD_FILESTAT_GET:()=>$_,RIGHTS_FD_FILESTAT_SET_SIZE:()=>_R,RIGHTS_FD_FILESTAT_SET_TIMES:()=>RR,RIGHTS_FD_READ:()=>b_,RIGHTS_FD_READDIR:()=>x_,RIGHTS_FD_SEEK:()=>Y_,RIGHTS_FD_SYNC:()=>u_,RIGHTS_FD_TELL:()=>K_,RIGHTS_FD_WRITE:()=>p_,RIGHTS_PATH_CREATE_DIRECTORY:()=>W_,RIGHTS_PATH_CREATE_FILE:()=>w_,RIGHTS_PATH_FILESTAT_GET:()=>z_,RIGHTS_PATH_FILESTAT_SET_SIZE:()=>q_,RIGHTS_PATH_FILESTAT_SET_TIMES:()=>J_,RIGHTS_PATH_LINK_SOURCE:()=>v_,RIGHTS_PATH_LINK_TARGET:()=>X_,RIGHTS_PATH_OPEN:()=>j_,RIGHTS_PATH_READLINK:()=>Q_,RIGHTS_PATH_REMOVE_DIRECTORY:()=>NR,RIGHTS_PATH_RENAME_SOURCE:()=>Z_,RIGHTS_PATH_RENAME_TARGET:()=>k_,RIGHTS_PATH_SYMLINK:()=>ER,RIGHTS_PATH_UNLINK_FILE:()=>tR,RIGHTS_POLL_FD_READWRITE:()=>OR,RIGHTS_SOCK_SHUTDOWN:()=>TR,ROFLAGS_RECV_DATA_TRUNCATED:()=>lE,SDFLAGS_RD:()=>cE,SDFLAGS_WR:()=>ME,SIGNAL_ABRT:()=>$R,SIGNAL_ALRM:()=>SE,SIGNAL_BUS:()=>_E,SIGNAL_CHLD:()=>IE,SIGNAL_CONT:()=>AE,SIGNAL_FPE:()=>RE,SIGNAL_HUP:()=>ZR,SIGNAL_ILL:()=>qR,SIGNAL_INT:()=>kR,SIGNAL_KILL:()=>EE,SIGNAL_NONE:()=>QR,SIGNAL_PIPE:()=>TE,SIGNAL_POLL:()=>UE,SIGNAL_PROF:()=>FE,SIGNAL_PWR:()=>HE,SIGNAL_QUIT:()=>zR,SIGNAL_SEGV:()=>tE,SIGNAL_STOP:()=>iE,SIGNAL_SYS:()=>oE,SIGNAL_TERM:()=>eE,SIGNAL_TRAP:()=>JR,SIGNAL_TSTP:()=>sE,SIGNAL_TTIN:()=>LE,SIGNAL_TTOU:()=>nE,SIGNAL_URG:()=>DE,SIGNAL_USR1:()=>NE,SIGNAL_USR2:()=>OE,SIGNAL_VTALRM:()=>rE,SIGNAL_WINCH:()=>PE,SIGNAL_XCPU:()=>GE,SIGNAL_XFSZ:()=>CE,SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME:()=>xR,WHENCE_CUR:()=>AR,WHENCE_END:()=>iR,WHENCE_SET:()=>IR});const E=0,N=1,t=2,O=0,T=1,S=2,e=3,I=0,A=1,i=2,s=3,L=4,n=5,D=6,G=7,C=8,r=9,F=10,P=11,U=12,H=13,o=14,a=15,h=16,l=17,c=18,M=19,d=20,f=21,y=22,g=23,b=24,Y=25,B=26,u=27,K=28,p=29,V=30,m=31,W=32,w=33,v=34,X=35,j=36,x=37,Q=38,Z=39,k=40,z=41,q=42,J=43,$=44,__=45,R_=46,E_=47,N_=48,t_=49,O_=50,T_=51,S_=52,e_=53,I_=54,A_=55,i_=56,s_=57,L_=58,n_=59,D_=60,G_=61,C_=62,r_=63,F_=64,P_=65,U_=66,H_=67,o_=68,a_=69,h_=70,l_=71,c_=72,M_=73,d_=74,f_=75,y_=76,g_=1,b_=2,Y_=4,B_=8,u_=16,K_=32,p_=64,V_=128,m_=256,W_=512,w_=1024,v_=2048,X_=4096,j_=8192,x_=16384,Q_=32768,Z_=65536,k_=1<<17,z_=1<<18,q_=1<<19,J_=1<<20,$_=1<<21,_R=1<<22,RR=1<<23,ER=1<<24,NR=1<<25,tR=1<<26,OR=1<<27,TR=1<<28;class SR{static read_bytes(_,R){let E=new SR;return E.buf=_.getUint32(R,!0),E.buf_len=_.getUint32(R+4,!0),E}static read_bytes_array(_,R,E){let N=[];for(let t=0;t { serveIfInitMsg(msg); @@ -18,16 +29,16 @@ onmessage = (msg) => { var listenfd = 4; var args = ['arg0', '--certfd='+certfd, '--net-listenfd='+listenfd, '--debug']; var env = []; - var wasi = new WASI(args, env, fds); - wasiHack(wasi, certfd, 5); - wasiHackSocket(wasi, listenfd, 5); + var wasiInstance = new WASI(args, env, fds); + wasiHack(wasiInstance, certfd, 5); + wasiHackSocket(wasiInstance, listenfd, 5); fetch(getImagename(), { credentials: 'same-origin' }).then((resp) => { resp['arrayBuffer']().then((wasm) => { WebAssembly.instantiate(wasm, { - "wasi_snapshot_preview1": wasi.wasiImport, - "env": envHack(wasi), + "wasi_snapshot_preview1": wasiInstance.wasiImport, + "env": envHack(wasiInstance), }).then((inst) => { - wasi.start(inst.instance); + wasiInstance.start(inst.instance); }); }) }); @@ -37,35 +48,35 @@ onmessage = (msg) => { const ERRNO_INVAL = 28; const ERRNO_AGAIN= 6; -function wasiHack(wasi, certfd, connfd) { +function wasiHack(wasiInstance, certfd, connfd) { var certbuf = new Uint8Array(0); - var _fd_close = wasi.wasiImport.fd_close; - wasi.wasiImport.fd_close = (fd) => { + var _fd_close = wasiInstance.wasiImport.fd_close; + wasiInstance.wasiImport.fd_close = (fd) => { if (fd == certfd) { sendCert(certbuf); return 0; } - return _fd_close.apply(wasi.wasiImport, [fd]); + return _fd_close.apply(wasiInstance.wasiImport, [fd]); } - var _fd_fdstat_get = wasi.wasiImport.fd_fdstat_get; - wasi.wasiImport.fd_fdstat_get = (fd, fdstat_ptr) => { + var _fd_fdstat_get = wasiInstance.wasiImport.fd_fdstat_get; + wasiInstance.wasiImport.fd_fdstat_get = (fd, fdstat_ptr) => { if (fd == certfd) { return 0; } - return _fd_fdstat_get.apply(wasi.wasiImport, [fd, fdstat_ptr]); + return _fd_fdstat_get.apply(wasiInstance.wasiImport, [fd, fdstat_ptr]); } - wasi.wasiImport.fd_fdstat_set_flags = (fd, fdflags) => { + wasiInstance.wasiImport.fd_fdstat_set_flags = (fd, fdflags) => { // TODO return 0; } - var _fd_write = wasi.wasiImport.fd_write; - wasi.wasiImport.fd_write = (fd, iovs_ptr, iovs_len, nwritten_ptr) => { + var _fd_write = wasiInstance.wasiImport.fd_write; + wasiInstance.wasiImport.fd_write = (fd, iovs_ptr, iovs_len, nwritten_ptr) => { if ((fd == 1) || (fd == 2) || (fd == certfd)) { - var buffer = new DataView(wasi.inst.exports.memory.buffer); - var buffer8 = new Uint8Array(wasi.inst.exports.memory.buffer); - var iovecs = Ciovec.read_bytes_array(buffer, iovs_ptr, iovs_len); + var buffer = new DataView(wasiInstance.inst.exports.memory.buffer); + var buffer8 = new Uint8Array(wasiInstance.inst.exports.memory.buffer); + var iovecs = wasi.Ciovec.read_bytes_array(buffer, iovs_ptr, iovs_len); var wtotal = 0 - for (i = 0; i < iovecs.length; i++) { + for (let i = 0; i < iovecs.length; i++) { var iovec = iovecs[i]; var buf = buffer8.slice(iovec.buf, iovec.buf + iovec.buf_len); if (buf.length == 0) { @@ -81,13 +92,13 @@ function wasiHack(wasi, certfd, connfd) { return 0; } console.log("fd_write: unknown fd " + fd); - return _fd_write.apply(wasi.wasiImport, [fd, iovs_ptr, iovs_len, nwritten_ptr]); + return _fd_write.apply(wasiInstance.wasiImport, [fd, iovs_ptr, iovs_len, nwritten_ptr]); } - wasi.wasiImport.poll_oneoff = (in_ptr, out_ptr, nsubscriptions, nevents_ptr) => { + wasiInstance.wasiImport.poll_oneoff = (in_ptr, out_ptr, nsubscriptions, nevents_ptr) => { if (nsubscriptions == 0) { return ERRNO_INVAL; } - let buffer = new DataView(wasi.inst.exports.memory.buffer); + let buffer = new DataView(wasiInstance.inst.exports.memory.buffer); let in_ = Subscription.read_bytes_array(buffer, in_ptr, nsubscriptions); let isReadPollStdin = false; let isReadPollConn = false; @@ -147,12 +158,12 @@ function wasiHack(wasi, certfd, connfd) { } } -function envHack(wasi){ +function envHack(wasiInstance){ return { http_send: function(addressP, addresslen, reqP, reqlen, idP){ - var buffer = new DataView(wasi.inst.exports.memory.buffer); - var address = new Uint8Array(wasi.inst.exports.memory.buffer, addressP, addresslen); - var req = new Uint8Array(wasi.inst.exports.memory.buffer, reqP, reqlen); + var buffer = new DataView(wasiInstance.inst.exports.memory.buffer); + var address = new Uint8Array(wasiInstance.inst.exports.memory.buffer, addressP, addresslen); + var req = new Uint8Array(wasiInstance.inst.exports.memory.buffer, reqP, reqlen); streamCtrl[0] = 0; postMessage({ type: "http_send", @@ -168,8 +179,8 @@ function envHack(wasi){ return 0; }, http_writebody: function(id, bodyP, bodylen, nwrittenP, isEOF){ - var buffer = new DataView(wasi.inst.exports.memory.buffer); - var body = new Uint8Array(wasi.inst.exports.memory.buffer, bodyP, bodylen); + var buffer = new DataView(wasiInstance.inst.exports.memory.buffer); + var body = new Uint8Array(wasiInstance.inst.exports.memory.buffer, bodyP, bodylen); streamCtrl[0] = 0; postMessage({ type: "http_writebody", @@ -185,7 +196,7 @@ function envHack(wasi){ return 0; }, http_isreadable: function(id, isOKP){ - var buffer = new DataView(wasi.inst.exports.memory.buffer); + var buffer = new DataView(wasiInstance.inst.exports.memory.buffer); streamCtrl[0] = 0; postMessage({type: "http_isreadable", id: id}); Atomics.wait(streamCtrl, 0, 0); @@ -200,8 +211,8 @@ function envHack(wasi){ return 0; }, http_recv: function(id, respP, bufsize, respsizeP, isEOFP){ - var buffer = new DataView(wasi.inst.exports.memory.buffer); - var buffer8 = new Uint8Array(wasi.inst.exports.memory.buffer); + var buffer = new DataView(wasiInstance.inst.exports.memory.buffer); + var buffer8 = new Uint8Array(wasiInstance.inst.exports.memory.buffer); streamCtrl[0] = 0; postMessage({type: "http_recv", id: id, len: bufsize}); @@ -221,8 +232,8 @@ function envHack(wasi){ return 0; }, http_readbody: function(id, bodyP, bufsize, bodysizeP, isEOFP){ - var buffer = new DataView(wasi.inst.exports.memory.buffer); - var buffer8 = new Uint8Array(wasi.inst.exports.memory.buffer); + var buffer = new DataView(wasiInstance.inst.exports.memory.buffer); + var buffer8 = new Uint8Array(wasiInstance.inst.exports.memory.buffer); streamCtrl[0] = 0; postMessage({type: "http_readbody", id: id, len: bufsize}); diff --git a/examples/wasi-browser/htdocs/wasi-util.js b/examples/wasi-browser/htdocs/wasi-util.js index 7714298e..33d1bdbf 100644 --- a/examples/wasi-browser/htdocs/wasi-util.js +++ b/examples/wasi-browser/htdocs/wasi-util.js @@ -1,3 +1,5 @@ +import { wasi } from 'https://cdn.jsdelivr.net/npm/@bjorn3/browser_wasi_shim@0.3.0/+esm'; + //////////////////////////////////////////////////////////// // // event-related classes adopted from the on-going discussion @@ -6,7 +8,7 @@ // //////////////////////////////////////////////////////////// -class EventType { +export class EventType { /*:: variant: "clock" | "fd_read" | "fd_write"*/ constructor(variant/*: "clock" | "fd_read" | "fd_write"*/) { @@ -15,11 +17,11 @@ class EventType { static from_u8(data/*: number*/)/*: EventType*/ { switch (data) { - case EVENTTYPE_CLOCK: + case wasi.EVENTTYPE_CLOCK: return new EventType("clock"); - case EVENTTYPE_FD_READ: + case wasi.EVENTTYPE_FD_READ: return new EventType("fd_read"); - case EVENTTYPE_FD_WRITE: + case wasi.EVENTTYPE_FD_WRITE: return new EventType("fd_write"); default: throw "Invalid event type " + String(data); @@ -29,18 +31,18 @@ class EventType { to_u8()/*: number*/ { switch (this.variant) { case "clock": - return EVENTTYPE_CLOCK; + return wasi.EVENTTYPE_CLOCK; case "fd_read": - return EVENTTYPE_FD_READ; + return wasi.EVENTTYPE_FD_READ; case "fd_write": - return EVENTTYPE_FD_WRITE; + return wasi.EVENTTYPE_FD_WRITE; default: throw "unreachable"; } } } -class Event { +export class Event { /*:: userdata: UserData*/ /*:: error: number*/ /*:: type: EventType*/ @@ -63,7 +65,7 @@ class Event { } } -class SubscriptionClock { +export class SubscriptionClock { /*:: timeout: number*/ static read_bytes(view/*: DataView*/, ptr/*: number*/)/*: SubscriptionFdReadWrite*/ { @@ -73,7 +75,7 @@ class SubscriptionClock { } } -class SubscriptionFdReadWrite { +export class SubscriptionFdReadWrite { /*:: fd: number*/ static read_bytes(view/*: DataView*/, ptr/*: number*/)/*: SubscriptionFdReadWrite*/ { @@ -83,7 +85,7 @@ class SubscriptionFdReadWrite { } } -class SubscriptionU { +export class SubscriptionU { /*:: tag: EventType */ /*:: data: SubscriptionClock | SubscriptionFdReadWrite */ @@ -105,7 +107,7 @@ class SubscriptionU { } } -class Subscription { +export class Subscription { /*:: userdata: UserData */ /*:: u: SubscriptionU */ diff --git a/examples/wasi-browser/htdocs/worker-util.js b/examples/wasi-browser/htdocs/worker-util.js index 59f86454..a13d6e59 100644 --- a/examples/wasi-browser/htdocs/worker-util.js +++ b/examples/wasi-browser/htdocs/worker-util.js @@ -1,8 +1,16 @@ -var streamCtrl; -var streamStatus; -var streamLen; -var streamData; -function registerSocketBuffer(shared){ +import { + WASI, + File, + PreopenDirectory, + wasi, +} from 'https://cdn.jsdelivr.net/npm/@bjorn3/browser_wasi_shim@0.3.0/+esm'; + +export var streamCtrl; +export var streamStatus; +export var streamLen; +export var streamData; + +export function registerSocketBuffer(shared){ streamCtrl = new Int32Array(shared, 0, 1); streamStatus = new Int32Array(shared, 4, 1); streamLen = new Int32Array(shared, 8, 1); @@ -10,7 +18,7 @@ function registerSocketBuffer(shared){ } var imagename; -function serveIfInitMsg(msg) { +export function serveIfInitMsg(msg) { const req_ = msg.data; if (typeof req_ == "object"){ if (req_.type == "init") { @@ -26,21 +34,21 @@ function serveIfInitMsg(msg) { return false; } -function getImagename() { +export function getImagename() { return imagename; } -const errStatus = { +export const errStatus = { val: 0, }; -function sockAccept(){ +export function sockAccept(){ streamCtrl[0] = 0; postMessage({type: "accept"}); Atomics.wait(streamCtrl, 0, 0); return streamData[0] == 1; } -function sockSend(data){ +export function sockSend(data){ streamCtrl[0] = 0; postMessage({type: "send", buf: data}); Atomics.wait(streamCtrl, 0, 0); @@ -49,7 +57,7 @@ function sockSend(data){ return errStatus; } } -function sockRecv(len){ +export function sockRecv(len){ streamCtrl[0] = 0; postMessage({type: "recv", len: len}); Atomics.wait(streamCtrl, 0, 0); @@ -62,7 +70,7 @@ function sockRecv(len){ return res; } -function sockWaitForReadable(timeout){ +export function sockWaitForReadable(timeout){ streamCtrl[0] = 0; postMessage({type: "recv-is-readable", timeout: timeout}); Atomics.wait(streamCtrl, 0, 0); @@ -73,7 +81,7 @@ function sockWaitForReadable(timeout){ return streamData[0] == 1; } -function sendCert(data){ +export function sendCert(data){ streamCtrl[0] = 0; postMessage({type: "send_cert", buf: data}); Atomics.wait(streamCtrl, 0, 0); @@ -83,7 +91,7 @@ function sendCert(data){ } } -function recvCert(){ +export function recvCert(){ var buf = new Uint8Array(0); return new Promise((resolve, reject) => { function getCert(){ @@ -107,14 +115,14 @@ function recvCert(){ }); } -function appendData(data1, data2) { - buf2 = new Uint8Array(data1.byteLength + data2.byteLength); +export function appendData(data1, data2) { + var buf2 = new Uint8Array(data1.byteLength + data2.byteLength); buf2.set(new Uint8Array(data1), 0); buf2.set(new Uint8Array(data2), data1.byteLength); return buf2; } -function getCertDir(cert) { +export function getCertDir(cert) { var certDir = new PreopenDirectory("/.wasmenv", { "proxy.crt": new File(cert) }); @@ -125,12 +133,12 @@ function getCertDir(cert) { var o = ret.fd_obj; ret.fd_obj.fd_pread = (view8, iovs, offset) => { var old_offset = o.file_pos; - var r = o.fd_seek(offset, WHENCE_SET); + var r = o.fd_seek(offset, wasi.WHENCE_SET); if (r.ret != 0) { return { ret: -1, nread: 0 }; } var read_ret = o.fd_read(view8, iovs); - r = o.fd_seek(old_offset, WHENCE_SET); + r = o.fd_seek(old_offset, wasi.WHENCE_SET); if (r.ret != 0) { return { ret: -1, nread: 0 }; } @@ -143,55 +151,55 @@ function getCertDir(cert) { return certDir; } -function wasiHackSocket(wasi, listenfd, connfd) { +export function wasiHackSocket(wasiInstance, listenfd, connfd) { // definition from wasi-libc https://github.com/WebAssembly/wasi-libc/blob/wasi-sdk-19/expected/wasm32-wasi/predefined-macros.txt const ERRNO_INVAL = 28; const ERRNO_AGAIN= 6; var connfdUsed = false; var connbuf = new Uint8Array(0); - var _fd_close = wasi.wasiImport.fd_close; - wasi.wasiImport.fd_close = (fd) => { + var _fd_close = wasiInstance.wasiImport.fd_close; + wasiInstance.wasiImport.fd_close = (fd) => { if (fd == connfd) { connfdUsed = false; return 0; } - return _fd_close.apply(wasi.wasiImport, [fd]); + return _fd_close.apply(wasiInstance.wasiImport, [fd]); } - var _fd_read = wasi.wasiImport.fd_read; - wasi.wasiImport.fd_read = (fd, iovs_ptr, iovs_len, nread_ptr) => { + var _fd_read = wasiInstance.wasiImport.fd_read; + wasiInstance.wasiImport.fd_read = (fd, iovs_ptr, iovs_len, nread_ptr) => { if (fd == connfd) { - return wasi.wasiImport.sock_recv(fd, iovs_ptr, iovs_len, 0, nread_ptr, 0); + return wasiInstance.wasiImport.sock_recv(fd, iovs_ptr, iovs_len, 0, nread_ptr, 0); } - return _fd_read.apply(wasi.wasiImport, [fd, iovs_ptr, iovs_len, nread_ptr]); + return _fd_read.apply(wasiInstance.wasiImport, [fd, iovs_ptr, iovs_len, nread_ptr]); } - var _fd_write = wasi.wasiImport.fd_write; - wasi.wasiImport.fd_write = (fd, iovs_ptr, iovs_len, nwritten_ptr) => { + var _fd_write = wasiInstance.wasiImport.fd_write; + wasiInstance.wasiImport.fd_write = (fd, iovs_ptr, iovs_len, nwritten_ptr) => { if (fd == connfd) { - return wasi.wasiImport.sock_send(fd, iovs_ptr, iovs_len, 0, nwritten_ptr); + return wasiInstance.wasiImport.sock_send(fd, iovs_ptr, iovs_len, 0, nwritten_ptr); } - return _fd_write.apply(wasi.wasiImport, [fd, iovs_ptr, iovs_len, nwritten_ptr]); + return _fd_write.apply(wasiInstance.wasiImport, [fd, iovs_ptr, iovs_len, nwritten_ptr]); } - var _fd_fdstat_get = wasi.wasiImport.fd_fdstat_get; - wasi.wasiImport.fd_fdstat_get = (fd, fdstat_ptr) => { + var _fd_fdstat_get = wasiInstance.wasiImport.fd_fdstat_get; + wasiInstance.wasiImport.fd_fdstat_get = (fd, fdstat_ptr) => { if ((fd == listenfd) || (fd == connfd) && connfdUsed){ - let buffer = new DataView(wasi.inst.exports.memory.buffer); + let buffer = new DataView(wasiInstance.inst.exports.memory.buffer); // https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fdstat-struct buffer.setUint8(fdstat_ptr, 6); // filetype = 6 (socket_stream) buffer.setUint8(fdstat_ptr + 1, 2); // fdflags = 2 (nonblock) return 0; } - return _fd_fdstat_get.apply(wasi.wasiImport, [fd, fdstat_ptr]); + return _fd_fdstat_get.apply(wasiInstance.wasiImport, [fd, fdstat_ptr]); } - var _fd_prestat_get = wasi.wasiImport.fd_prestat_get; - wasi.wasiImport.fd_prestat_get = (fd, prestat_ptr) => { + var _fd_prestat_get = wasiInstance.wasiImport.fd_prestat_get; + wasiInstance.wasiImport.fd_prestat_get = (fd, prestat_ptr) => { if ((fd == listenfd) || (fd == connfd)){ // reserve socket-related fds - let buffer = new DataView(wasi.inst.exports.memory.buffer); + let buffer = new DataView(wasiInstance.inst.exports.memory.buffer); buffer.setUint8(prestat_ptr, 1); return 0; } - return _fd_prestat_get.apply(wasi.wasiImport, [fd, prestat_ptr]); + return _fd_prestat_get.apply(wasiInstance.wasiImport, [fd, prestat_ptr]); } - wasi.wasiImport.sock_accept = (fd, flags, fd_ptr) => { + wasiInstance.wasiImport.sock_accept = (fd, flags, fd_ptr) => { if (fd != listenfd) { console.log("sock_accept: unknown fd " + fd); return ERRNO_INVAL; @@ -204,20 +212,20 @@ function wasiHackSocket(wasi, listenfd, connfd) { return ERRNO_AGAIN; } connfdUsed = true; - var buffer = new DataView(wasi.inst.exports.memory.buffer); + var buffer = new DataView(wasiInstance.inst.exports.memory.buffer); buffer.setUint32(fd_ptr, connfd, true); return 0; } - wasi.wasiImport.sock_send = (fd, iovs_ptr, iovs_len, si_flags/*not defined*/, nwritten_ptr) => { + wasiInstance.wasiImport.sock_send = (fd, iovs_ptr, iovs_len, si_flags/*not defined*/, nwritten_ptr) => { if (fd != connfd) { console.log("sock_send: unknown fd " + fd); return ERRNO_INVAL; } - var buffer = new DataView(wasi.inst.exports.memory.buffer); - var buffer8 = new Uint8Array(wasi.inst.exports.memory.buffer); - var iovecs = Ciovec.read_bytes_array(buffer, iovs_ptr, iovs_len); + var buffer = new DataView(wasiInstance.inst.exports.memory.buffer); + var buffer8 = new Uint8Array(wasiInstance.inst.exports.memory.buffer); + var iovecs = wasi.Ciovec.read_bytes_array(buffer, iovs_ptr, iovs_len); var wtotal = 0 - for (i = 0; i < iovecs.length; i++) { + for (let i = 0; i < iovecs.length; i++) { var iovec = iovecs[i]; var buf = buffer8.slice(iovec.buf, iovec.buf + iovec.buf_len); if (buf.length == 0) { @@ -232,7 +240,7 @@ function wasiHackSocket(wasi, listenfd, connfd) { buffer.setUint32(nwritten_ptr, wtotal, true); return 0; } - wasi.wasiImport.sock_recv = (fd, iovs_ptr, iovs_len, ri_flags, nread_ptr, ro_flags_ptr) => { + wasiInstance.wasiImport.sock_recv = (fd, iovs_ptr, iovs_len, ri_flags, nread_ptr, ro_flags_ptr) => { if (ri_flags != 0) { console.log("ri_flags are unsupported"); // TODO } @@ -246,11 +254,11 @@ function wasiHackSocket(wasi, listenfd, connfd) { } else if (sockreadable == false) { return ERRNO_AGAIN; } - var buffer = new DataView(wasi.inst.exports.memory.buffer); - var buffer8 = new Uint8Array(wasi.inst.exports.memory.buffer); - var iovecs = Iovec.read_bytes_array(buffer, iovs_ptr, iovs_len); + var buffer = new DataView(wasiInstance.inst.exports.memory.buffer); + var buffer8 = new Uint8Array(wasiInstance.inst.exports.memory.buffer); + var iovecs = wasi.Iovec.read_bytes_array(buffer, iovs_ptr, iovs_len); var nread = 0; - for (i = 0; i < iovecs.length; i++) { + for (let i = 0; i < iovecs.length; i++) { var iovec = iovecs[i]; if (iovec.buf_len == 0) { continue; @@ -266,7 +274,7 @@ function wasiHackSocket(wasi, listenfd, connfd) { // TODO: support ro_flags_ptr return 0; } - wasi.wasiImport.sock_shutdown = (fd, sdflags) => { + wasiInstance.wasiImport.sock_shutdown = (fd, sdflags) => { if (fd == connfd) { connfdUsed = false; } diff --git a/examples/wasi-browser/htdocs/worker.js b/examples/wasi-browser/htdocs/worker.js index 7b056540..6b64920c 100644 --- a/examples/wasi-browser/htdocs/worker.js +++ b/examples/wasi-browser/htdocs/worker.js @@ -1,8 +1,15 @@ -importScripts("https://cdn.jsdelivr.net/npm/xterm-pty@0.9.4/workerTools.js"); -importScripts(location.origin + "/browser_wasi_shim/index.js"); -importScripts(location.origin + "/browser_wasi_shim/wasi_defs.js"); -importScripts(location.origin + "/worker-util.js"); -importScripts(location.origin + "/wasi-util.js"); +import { Fd, WASI, wasi } from 'https://cdn.jsdelivr.net/npm/@bjorn3/browser_wasi_shim@0.3.0/+esm'; +import { Event, EventType, Subscription } from './wasi-util.js'; +import { TtyClient } from 'https://unpkg.com/xterm-pty-esm@0.10.2/out/index.mjs'; +import { + getImagename, + errStatus, + getCertDir, + recvCert, + serveIfInitMsg, + sockWaitForReadable, + wasiHackSocket, +} from './worker-util.js'; onmessage = (msg) => { if (serveIfInitMsg(msg)) { @@ -51,29 +58,29 @@ onmessage = (msg) => { }; function startWasi(wasm, ttyClient, args, env, fds, listenfd, connfd) { - var wasi = new WASI(args, env, fds); - wasiHack(wasi, ttyClient, connfd); - wasiHackSocket(wasi, listenfd, connfd); + var wasiInstance = new WASI(args, env, fds); + wasiHack(wasiInstance, ttyClient, connfd); + wasiHackSocket(wasiInstance, listenfd, connfd); WebAssembly.instantiate(wasm, { - "wasi_snapshot_preview1": wasi.wasiImport, + "wasi_snapshot_preview1": wasiInstance.wasiImport, }).then((inst) => { - wasi.start(inst.instance); + wasiInstance.start(inst.instance); }); } // wasiHack patches wasi object for integrating it to xterm-pty. -function wasiHack(wasi, ttyClient, connfd) { +function wasiHack(wasiInstance, ttyClient, connfd) { // definition from wasi-libc https://github.com/WebAssembly/wasi-libc/blob/wasi-sdk-19/expected/wasm32-wasi/predefined-macros.txt const ERRNO_INVAL = 28; const ERRNO_AGAIN= 6; - var _fd_read = wasi.wasiImport.fd_read; - wasi.wasiImport.fd_read = (fd, iovs_ptr, iovs_len, nread_ptr) => { + var _fd_read = wasiInstance.wasiImport.fd_read; + wasiInstance.wasiImport.fd_read = (fd, iovs_ptr, iovs_len, nread_ptr) => { if (fd == 0) { - var buffer = new DataView(wasi.inst.exports.memory.buffer); - var buffer8 = new Uint8Array(wasi.inst.exports.memory.buffer); - var iovecs = Iovec.read_bytes_array(buffer, iovs_ptr, iovs_len); + var buffer = new DataView(wasiInstance.inst.exports.memory.buffer); + var buffer8 = new Uint8Array(wasiInstance.inst.exports.memory.buffer); + var iovecs = wasi.Iovec.read_bytes_array(buffer, iovs_ptr, iovs_len); var nread = 0; - for (i = 0; i < iovecs.length; i++) { + for (let i = 0; i < iovecs.length; i++) { var iovec = iovecs[i]; if (iovec.buf_len == 0) { continue; @@ -86,18 +93,18 @@ function wasiHack(wasi, ttyClient, connfd) { return 0; } else { console.log("fd_read: unknown fd " + fd); - return _fd_read.apply(wasi.wasiImport, [fd, iovs_ptr, iovs_len, nread_ptr]); + return _fd_read.apply(wasiInstance.wasiImport, [fd, iovs_ptr, iovs_len, nread_ptr]); } return ERRNO_INVAL; } - var _fd_write = wasi.wasiImport.fd_write; - wasi.wasiImport.fd_write = (fd, iovs_ptr, iovs_len, nwritten_ptr) => { + var _fd_write = wasiInstance.wasiImport.fd_write; + wasiInstance.wasiImport.fd_write = (fd, iovs_ptr, iovs_len, nwritten_ptr) => { if ((fd == 1) || (fd == 2)) { - var buffer = new DataView(wasi.inst.exports.memory.buffer); - var buffer8 = new Uint8Array(wasi.inst.exports.memory.buffer); - var iovecs = Ciovec.read_bytes_array(buffer, iovs_ptr, iovs_len); + var buffer = new DataView(wasiInstance.inst.exports.memory.buffer); + var buffer8 = new Uint8Array(wasiInstance.inst.exports.memory.buffer); + var iovecs = wasi.Ciovec.read_bytes_array(buffer, iovs_ptr, iovs_len); var wtotal = 0 - for (i = 0; i < iovecs.length; i++) { + for (let i = 0; i < iovecs.length; i++) { var iovec = iovecs[i]; var buf = buffer8.slice(iovec.buf, iovec.buf + iovec.buf_len); if (buf.length == 0) { @@ -110,15 +117,15 @@ function wasiHack(wasi, ttyClient, connfd) { return 0; } else { console.log("fd_write: unknown fd " + fd); - return _fd_write.apply(wasi.wasiImport, [fd, iovs_ptr, iovs_len, nwritten_ptr]); + return _fd_write.apply(wasiInstance.wasiImport, [fd, iovs_ptr, iovs_len, nwritten_ptr]); } return ERRNO_INVAL; } - wasi.wasiImport.poll_oneoff = (in_ptr, out_ptr, nsubscriptions, nevents_ptr) => { + wasiInstance.wasiImport.poll_oneoff = (in_ptr, out_ptr, nsubscriptions, nevents_ptr) => { if (nsubscriptions == 0) { return ERRNO_INVAL; } - let buffer = new DataView(wasi.inst.exports.memory.buffer); + let buffer = new DataView(wasiInstance.inst.exports.memory.buffer); let in_ = Subscription.read_bytes_array(buffer, in_ptr, nsubscriptions); let isReadPollStdin = false; let isReadPollConn = false;