add emscripten_async_call
This commit is contained in:
Родитель
74690aa7e9
Коммит
735637e9a1
|
@ -19,6 +19,13 @@ mergeInto(LibraryManager.library, {
|
|||
_emscripten_set_main_loop.cancel = true;
|
||||
},
|
||||
|
||||
emscripten_async_call: function(func, millis) {
|
||||
// TODO: cache these to avoid generating garbage
|
||||
setTimeout(function() {
|
||||
FUNCTION_TABLE[func]();
|
||||
}, millis);
|
||||
},
|
||||
|
||||
$Browser: {
|
||||
createContext: function(canvas, useWebGL) {
|
||||
#if !USE_TYPED_ARRAYS
|
||||
|
|
|
@ -27,6 +27,21 @@ extern int emscripten_run_script_int(const char *script);
|
|||
extern void emscripten_set_main_loop(void (*func)(), int fps);
|
||||
extern void emscripten_cancel_main_loop();
|
||||
|
||||
/*
|
||||
* Call a C function asynchronously, that is, after returning
|
||||
* control to the JS event loop. This is done by a setTimeout.
|
||||
* When building natively this becomes a simple direct call,
|
||||
* after SDL_Delay (you must include SDL.h for that).
|
||||
*/
|
||||
#if EMSCRIPTEN
|
||||
extern void emscripten_async_call(void (*func)(), int millis);
|
||||
#else
|
||||
void emscripten_async_call(void (*func)(), int millis) {
|
||||
SDL_Delay(millis);
|
||||
func();
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This macro-looking function will cause Emscripten to
|
||||
* generate a comment in the generated code.
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
#include<stdio.h>
|
||||
#include<math.h>
|
||||
#include<SDL.h>
|
||||
#include<emscripten.h>
|
||||
#include<assert.h>
|
||||
|
||||
int last = 0;
|
||||
|
||||
extern "C" {
|
||||
|
||||
void third() {
|
||||
int now = SDL_GetTicks();
|
||||
printf("thard! %d\n", now);
|
||||
assert(fabs(now - last - 1000) < 500);
|
||||
int result = 1;
|
||||
REPORT_RESULT();
|
||||
}
|
||||
|
||||
void second() {
|
||||
int now = SDL_GetTicks();
|
||||
printf("sacond! %d\n", now);
|
||||
assert(fabs(now - last - 500) < 250);
|
||||
last = now;
|
||||
emscripten_async_call(third, 1000);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
SDL_Init(0);
|
||||
last = SDL_GetTicks();
|
||||
printf("frist! %d\n", last);
|
||||
|
||||
emscripten_async_call(second, 500);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
|
@ -7163,6 +7163,11 @@ elif 'browser' in str(sys.argv):
|
|||
Popen(['python', EMCC, program, '-o', 'program.html', '--pre-js', 'reftest.js'] + args).communicate()
|
||||
self.run_browser('program.html', '', '/report_result?0')
|
||||
|
||||
def test_emscripten_api(self):
|
||||
open(os.path.join(self.get_dir(), 'main.cpp'), 'w').write(self.with_report_result(open(path_from_root('tests', 'emscripten_api_browser.cpp')).read()))
|
||||
Popen(['python', EMCC, os.path.join(self.get_dir(), 'main.cpp'), '-o', 'page.html']).communicate()
|
||||
self.run_browser('page.html', '', '/report_result?1')
|
||||
|
||||
elif 'benchmark' in str(sys.argv):
|
||||
# Benchmarks. Run them with argument |benchmark|. To run a specific test, do
|
||||
# |benchmark.test_X|.
|
||||
|
|
Загрузка…
Ссылка в новой задаче