|
| 1 | +#include <emscripten.h> |
| 2 | + |
| 3 | +namespace duckdb { |
| 4 | + |
| 5 | +struct WasmResultInner { |
| 6 | + idx_t status; |
| 7 | + std::string reason; |
| 8 | + std::string body; |
| 9 | +}; |
| 10 | +struct WasmResult { |
| 11 | + operator bool() { return true; } |
| 12 | + WasmResultInner *operator->() { return &_inner; } |
| 13 | + WasmResultInner _inner; |
| 14 | +}; |
| 15 | +struct WasmClient { |
| 16 | + WasmResult Get(string path) { |
| 17 | + WasmResult res; |
| 18 | + |
| 19 | + char *exe = NULL; |
| 20 | + exe = (char *)EM_ASM_PTR( |
| 21 | + { |
| 22 | + var url = (UTF8ToString($0)); |
| 23 | + if (typeof XMLHttpRequest === "undefined") { |
| 24 | + return 0; |
| 25 | + } |
| 26 | + const xhr = new XMLHttpRequest(); |
| 27 | + xhr.open("GET", url, false); |
| 28 | + xhr.responseType = "arraybuffer"; |
| 29 | + xhr.send(null); |
| 30 | + if (xhr.status != 200) |
| 31 | + return 0; |
| 32 | + var uInt8Array = xhr.response; |
| 33 | + |
| 34 | + var len = uInt8Array.byteLength; |
| 35 | + var fileOnWasmHeap = _malloc(len + 4); |
| 36 | + |
| 37 | + var properArray = new Uint8Array(uInt8Array); |
| 38 | + |
| 39 | + for (var iii = 0; iii < len; iii++) { |
| 40 | + Module.HEAPU8[iii + fileOnWasmHeap + 4] = properArray[iii]; |
| 41 | + } |
| 42 | + var LEN123 = new Uint8Array(4); |
| 43 | + LEN123[0] = len % 256; |
| 44 | + len -= LEN123[0]; |
| 45 | + len /= 256; |
| 46 | + LEN123[1] = len % 256; |
| 47 | + len -= LEN123[1]; |
| 48 | + len /= 256; |
| 49 | + LEN123[2] = len % 256; |
| 50 | + len -= LEN123[2]; |
| 51 | + len /= 256; |
| 52 | + LEN123[3] = len % 256; |
| 53 | + len -= LEN123[3]; |
| 54 | + len /= 256; |
| 55 | + Module.HEAPU8.set(LEN123, fileOnWasmHeap); |
| 56 | + console.log(properArray); |
| 57 | + return fileOnWasmHeap; |
| 58 | + }, |
| 59 | + path.c_str()); |
| 60 | + |
| 61 | + if (!exe) { |
| 62 | + res._inner.status = 404; |
| 63 | + res._inner.reason = "Something went quack in Wasm land!"; |
| 64 | + } else { |
| 65 | + res._inner.status = 200; |
| 66 | + uint64_t LEN = 0; |
| 67 | + LEN *= 256; |
| 68 | + LEN += ((uint8_t *)exe)[3]; |
| 69 | + LEN *= 256; |
| 70 | + LEN += ((uint8_t *)exe)[2]; |
| 71 | + LEN *= 256; |
| 72 | + LEN += ((uint8_t *)exe)[1]; |
| 73 | + LEN *= 256; |
| 74 | + LEN += ((uint8_t *)exe)[0]; |
| 75 | + res._inner.body = string(exe + 4, LEN); |
| 76 | + free(exe); |
| 77 | + } |
| 78 | + |
| 79 | + return res; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +static std::pair<WasmClient, std::string> |
| 84 | +SetupHttpClient(const std::string &url) { |
| 85 | + WasmClient x; |
| 86 | + return std::make_pair(std::move(x), url); |
| 87 | +} |
| 88 | + |
| 89 | +static void HandleHttpError(const WasmResult &res, |
| 90 | + const std::string &request_type) {} |
| 91 | + |
| 92 | +} // namespace duckdb |
0 commit comments