emscripten_async_load_script
This commit is contained in:
Родитель
ee51b2bd82
Коммит
1fc6762e31
|
@ -688,6 +688,25 @@ mergeInto(LibraryManager.library, {
|
|||
}, millis);
|
||||
},
|
||||
|
||||
emscripten_async_load_script: function(url, onload, onerror) {
|
||||
Module['noExitRuntime'] = true;
|
||||
|
||||
onload = Runtime.getFuncWrapper(onload, 'v');
|
||||
|
||||
assert(runDependencies === 0, 'async_load_script must be run when no other dependencies are active');
|
||||
var script = document.createElement('script');
|
||||
script.onload = function() {
|
||||
if (runDependencies > 0) {
|
||||
dependenciesFulfilled = onload;
|
||||
} else {
|
||||
onload();
|
||||
}
|
||||
};
|
||||
script.onerror = onerror;
|
||||
script.src = Pointer_stringify(url);
|
||||
document.body.appendChild(script);
|
||||
},
|
||||
|
||||
emscripten_set_main_loop: function(func, fps, simulateInfiniteLoop) {
|
||||
Module['noExitRuntime'] = true;
|
||||
|
||||
|
|
|
@ -45,8 +45,26 @@ extern "C" {
|
|||
extern void emscripten_run_script(const char *script);
|
||||
extern int emscripten_run_script_int(const char *script);
|
||||
extern char *emscripten_run_script_string(const char *script); // uses a single buffer - shared between calls!
|
||||
|
||||
/*
|
||||
* Asynchronously run a script, after a specified amount of
|
||||
* time.
|
||||
*/
|
||||
extern void emscripten_async_run_script(const char *script, int millis);
|
||||
|
||||
/*
|
||||
* Asynchronously loads a script from a URL.
|
||||
*
|
||||
* This integrates with the run dependencies system, so your
|
||||
* script can call addRunDependency multiple times, prepare
|
||||
* various asynchronous tasks, and call removeRunDependency
|
||||
* on them; when all are complete (or there were no run
|
||||
* dependencies to begin with), onload is called. An example use
|
||||
* for this is to load an asset module, that is, the output of the
|
||||
* file packager.
|
||||
*/
|
||||
extern void emscripten_async_load_script(const char *script, void (*onload)(), void (*onerror)());
|
||||
|
||||
/*
|
||||
* Set a C function as the main event loop. The JS environment
|
||||
* will call that function at a specified number of frames per
|
||||
|
|
|
@ -0,0 +1,53 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include<emscripten.h>
|
||||
|
||||
int value = 0;
|
||||
|
||||
extern "C" {
|
||||
void set(int x) {
|
||||
printf("set! %d\n", x);
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
|
||||
void load2() {
|
||||
printf("load2\n");
|
||||
|
||||
char buffer[10];
|
||||
memset(buffer, 0, 10);
|
||||
FILE *f = fopen("file1.txt", "r");
|
||||
fread(buffer, 1, 5, f);
|
||||
fclose(f);
|
||||
assert(strcmp(buffer, "first") == 0);
|
||||
|
||||
memset(buffer, 0, 10);
|
||||
f = fopen("file2.txt", "r");
|
||||
fread(buffer, 1, 6, f);
|
||||
fclose(f);
|
||||
assert(strcmp(buffer, "second") == 0);
|
||||
|
||||
int result = 1;
|
||||
REPORT_RESULT();
|
||||
}
|
||||
void error2() {
|
||||
printf("fail2\n");
|
||||
}
|
||||
|
||||
void load1() {
|
||||
printf("load1\n");
|
||||
assert(value == 456);
|
||||
emscripten_async_load_script("script2.js", load2, error2);
|
||||
}
|
||||
void error1() {
|
||||
printf("fail1\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
emscripten_async_load_script("script1.js", load1, error1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
|
@ -1230,6 +1230,17 @@ keydown(100);keyup(100); // trigger the end
|
|||
def test_emscripten_api(self):
|
||||
self.btest('emscripten_api_browser.cpp', '1', args=['-s', '''EXPORTED_FUNCTIONS=['_main', '_third']'''])
|
||||
|
||||
def test_emscripten_api2(self):
|
||||
open('script1.js', 'w').write('''
|
||||
Module._set(456);
|
||||
''')
|
||||
|
||||
open('file1.txt', 'w').write('first');
|
||||
open('file2.txt', 'w').write('second');
|
||||
Popen([PYTHON, FILE_PACKAGER, 'test.data', '--preload', 'file1.txt', 'file2.txt'], stdout=open('script2.js', 'w')).communicate()
|
||||
|
||||
self.btest('emscripten_api_browser2.cpp', '1', args=['-s', '''EXPORTED_FUNCTIONS=['_main', '_set']'''])
|
||||
|
||||
def test_emscripten_api_infloop(self):
|
||||
self.btest('emscripten_api_browser_infloop.cpp', '7')
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче