Implementation for emscripten_async_wget

This commit is contained in:
Aleksander Guryanov 2012-06-20 22:20:04 +07:00
Родитель 6d34633796
Коммит 2c86cbf25b
4 изменённых файлов: 127 добавлений и 8 удалений

Просмотреть файл

@ -142,24 +142,59 @@ mergeInto(LibraryManager.library, {
0;
},
asyncLoad: function(url, callback) {
xhrLoad: function(url, onload, onerror) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function() {
var arrayBuffer = xhr.response;
assert(arrayBuffer, 'Loading data file "' + url + '" failed (no arrayBuffer).');
callback(new Uint8Array(arrayBuffer));
removeRunDependency();
};
xhr.onerror = function(event) {
assert(arrayBuffer, 'Loading data file "' + url + '" failed.');
if (xhr.status == 200) {
onload(xhr.response);
} else {
onerror();
}
};
xhr.onerror = onerror;
xhr.send(null);
},
asyncLoad: function(url, callback) {
Browser.xhrLoad(url,
function(arrayBuffer) {
assert(arrayBuffer, 'Loading data file "' + url + '" failed (no arrayBuffer).');
callback(new Uint8Array(arrayBuffer));
removeRunDependency();
},
function(event) {
assert(arrayBuffer, 'Loading data file "' + url + '" failed.');
});
addRunDependency();
}
},
emscripten_async_wget: function(url, file, onload, onerror) {
url = Pointer_stringify(url);
Browser.xhrLoad(url,
function (response) {
var absolute = Pointer_stringify(file);
var index = absolute.lastIndexOf('/');
FS.createDataFile(
absolute.substr(0, index),
absolute.substr(index +1),
new Uint8Array(response),
true, true);
if (onload) {
FUNCTION_TABLE[onload](file);
}
},
function (event) {
if (onerror) {
FUNCTION_TABLE[onerror](file);
}
});
},
emscripten_async_run_script__deps: ['emscripten_run_script'],
emscripten_async_run_script: function(script, millis) {
Module['noExitRuntime'] = true;

Просмотреть файл

@ -102,6 +102,18 @@ float emscripten_random();
*/
//extern void EMSCRIPTEN_COMMENT(const char *text);
/*
* Emscripten file system api
*/
/*
* Load file from url in asynchronous way.
* When file is loaded then 'onload' callback will called.
* If any error occurred 'onerror' will called.
* The callbacks are called with the file as their argument.
*/
void emscripten_async_wget(const char* url, const char* file, void (*onload)(const char*), void (*onerror)(const char*));
/*
* Profiling tools.
* INIT must be called first, with the maximum identifier that

Просмотреть файл

@ -0,0 +1,69 @@
#include<stdio.h>
#include<emscripten.h>
#include<assert.h>
#include <string.h>
extern "C" {
int result = 1;
int get_count = 0;
void wait_wgets() {
if (get_count == 2) {
emscripten_cancel_main_loop();
REPORT_RESULT();
}
}
void onLoaded(const char* file) {
if (strcmp(file, "/tmp/test.html")) {
result = 0;
}
printf("loaded: %s\n", file);
if (FILE * f = fopen(file, "r")) {
printf("exists: %s\n", file);
int c = fgetc (f);
if (c == EOF) {
printf("file empty: %s\n", file);
result = 0;
}
fclose(f);
} else {
result = 0;
printf("!exists: %s\n", file);
}
get_count++;
}
void onError(const char* file) {
if (strcmp(file, "/tmp/null")) {
result = 0;
}
printf("error: %s\n", file);
get_count++;
}
int main() {
emscripten_async_wget(
"http://localhost:8888/this_is_not_a_file",
"/tmp/null",
onLoaded,
onError);
emscripten_async_wget(
"http://localhost:8888/test.html",
"/tmp/test.html",
onLoaded,
onError);
emscripten_set_main_loop(wait_wgets, 0);
return 0;
}
}

Просмотреть файл

@ -7747,6 +7747,9 @@ elif 'browser' in str(sys.argv):
def test_emscripten_api(self):
self.btest('emscripten_api_browser.cpp', '1')
def test_emscripten_fs_api(self):
self.btest('emscripten_fs_api_browser.cpp', '1')
def test_gc(self):
self.btest('browser_gc.cpp', '1')