|
165 | 165 | :wasi-stderr (b "wasi_config_inherit_stderr" nil ADDR) |
166 | 166 | :wasi-stdin (b "wasi_config_inherit_stdin" nil ADDR) |
167 | 167 | :wasi-env (b "wasi_config_inherit_env" nil ADDR) |
| 168 | + :wasi-argv (b "wasi_config_set_argv" ValueLayout/JAVA_BOOLEAN ADDR I64 ADDR) |
| 169 | + :wasi-setenv (b "wasi_config_set_env" ValueLayout/JAVA_BOOLEAN ADDR I64 ADDR ADDR) |
168 | 170 | ;; 0017 C: wasmtime frees what an import callback writes, so a string |
169 | 171 | ;; result cannot come from an Arena. libc's malloc is the allocator it |
170 | 172 | ;; expects, and this is the only place in the library that needs one. |
|
916 | 918 | "Adds every WASI 0.2 interface to the linker and gives the store a context. |
917 | 919 | Deny-by-default: a capability is inherited only when named, because the |
918 | 920 | alternative leaks the host's environment into the guest." |
919 | | - [api ^MemorySegment ctx clink {:keys [inherit-stdout inherit-stderr |
920 | | - inherit-stdin inherit-env]}] |
| 921 | + [api ^Arena arena ^MemorySegment ctx clink |
| 922 | + {:keys [inherit-stdout inherit-stderr inherit-stdin inherit-env args env]}] |
921 | 923 | (ok! api "add_wasip2" (invoke (:add-wasip2 api) clink)) |
922 | | - (let [cfg (invoke (:wasi-new api))] |
| 924 | + (let [cfg (invoke (:wasi-new api)) |
| 925 | + strs (fn [xs] |
| 926 | + (let [a ^MemorySegment (.allocate arena (long (* 8 (max 1 (count xs)))))] |
| 927 | + (dotimes [i (count xs)] |
| 928 | + (.set a ADDR (long (* 8 i)) ^MemorySegment (cstr arena (str (nth xs i))))) |
| 929 | + a))] |
923 | 930 | (when inherit-stdout (invoke (:wasi-stdout api) cfg)) |
924 | 931 | (when inherit-stderr (invoke (:wasi-stderr api) cfg)) |
925 | 932 | (when inherit-stdin (invoke (:wasi-stdin api) cfg)) |
926 | 933 | (when inherit-env (invoke (:wasi-env api) cfg)) |
| 934 | + (when args |
| 935 | + (when-not (invoke (:wasi-argv api) cfg (long (count args)) (strs args)) |
| 936 | + (throw (ex-info "wasi_config_set_argv refused the arguments" |
| 937 | + {:cljwit/error :wasi-config})))) |
| 938 | + (when env |
| 939 | + (when-not (invoke (:wasi-setenv api) cfg (long (count env)) |
| 940 | + (strs (map key env)) (strs (map val env))) |
| 941 | + (throw (ex-info "wasi_config_set_env refused the environment" |
| 942 | + {:cljwit/error :wasi-config})))) |
927 | 943 | ;; The config is consumed even on error, so it is per-instantiate. |
928 | 944 | (ok! api "set_wasi" (invoke (:set-wasi api) ctx cfg)))) |
929 | 945 |
|
|
946 | 962 | ;; report a trap with the cause gone. |
947 | 963 | dead (atom nil) |
948 | 964 | _ (define-imports! api arena e ctx clink ct imports dead) |
949 | | - _ (when wasi (enable-wasi! api ctx clink wasi)) |
| 965 | + _ (when wasi (enable-wasi! api arena ctx clink wasi)) |
950 | 966 | inst ^MemorySegment (.allocate arena (long INSTANCE)) |
951 | 967 | _ (ok! api "linker_instantiate" |
952 | 968 | (invoke (:instantiate api) clink ctx (.-ptr art) inst)) |
|
0 commit comments