Created
November 25, 2021 19:56
-
-
Save ryuukk/f5b877bd0e08bd2c1da00884089df8c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = string.ptr2str_len(MOD.memory.buffer, offset, len); | |
var req = new XMLHttpRequest(); | |
req.open("GET", path, true); | |
req.responseType = "arraybuffer"; | |
req.onload = function (evt) { | |
var arrayBuffer = req.response; // Note: not req.responseText | |
if (arrayBuffer) | |
{ | |
var byteArray = new Uint8Array(arrayBuffer); | |
var fileSize = byteArray.byteLength; | |
// alloc on D side | |
var ptr = MOD.WA.exports.malloc(fileSize); | |
// copy file content to our buffer | |
var mem = new Uint8Array(MOD.memory.buffer, ptr, arrayBuffer.length); | |
mem.set(byteArray); | |
// notify D side | |
MOD.WA.exports.js_cb_load_file(ctx,func, id, ptr, fileSize); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Handle | |
{ | |
bool used; | |
bool loaded; | |
void* data; | |
int data_len; | |
int id = -1; | |
void* user; | |
} | |
struct Manager | |
{ | |
Handle[64] handles; | |
Handle* next() | |
{ | |
for (int i = 0; i < handles.length; i++) | |
{ | |
Handle* handle = &handles[i]; | |
if (handle.used == false) | |
{ | |
handle.id = i; | |
return handle; | |
} | |
} | |
return null; | |
} | |
void free(Handle* handle) | |
{ | |
// unload on JS side? | |
handle.loaded = false; | |
handle.used = false; | |
handle.data = null; | |
handle.data_len = 0; | |
handle.user = null; | |
} | |
Handle* load(string path) | |
{ | |
Handle* handle = next(); | |
if (handle) | |
{ | |
handle.used = true; | |
load_file(path, handle.id, &on_cb); | |
return handle; | |
} | |
return null; | |
} | |
extern(C)void on_cb(int id, void* ptr, int len) | |
{ | |
Handle* handle = &handles[id]; | |
handle.loaded = true; | |
handle.data = ptr; | |
handle.data_len = len; | |
writeln("Id: {}:{} PTR: {} L: {}", handle.id, id, ptr, len); | |
string str = cast(string) ptr[0 .. len]; | |
writeln("Data content:\n{}", str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment