forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pthread_run_script.cpp
More file actions
72 lines (63 loc) · 2.46 KB
/
test_pthread_run_script.cpp
File metadata and controls
72 lines (63 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <emscripten/emscripten.h>
#include <emscripten/threading.h>
extern "C"
{
void EMSCRIPTEN_KEEPALIVE FinishTest(int result)
{
printf("Test finished, result: %d\n", result);
#ifdef REPORT_RESULT
REPORT_RESULT(result);
#endif
}
}
void TestAsyncRunScript()
{
// 5. Test emscripten_async_run_script() runs in a pthread.
#if __EMSCRIPTEN_PTHREADS__
emscripten_async_run_script("Module['_FinishTest'](ENVIRONMENT_IS_PTHREAD && (typeof ENVIRONMENT_IS_WORKER !== 'undefined' && ENVIRONMENT_IS_WORKER));", 1);
#else
emscripten_async_run_script("Module['_FinishTest'](!(typeof ENVIRONMENT_IS_WORKER !== 'undefined' && ENVIRONMENT_IS_WORKER));", 1);
#endif
}
void AsyncScriptLoaded()
{
printf("async script load succeeded!\n");
TestAsyncRunScript();
}
void AsyncScriptFailed()
{
printf("async script load failed!\n");
TestAsyncRunScript();
}
int main() {
// 1. Test that emscripten_run_script() works in a pthread, and it gets executed in the web worker and not on the main thread.
#if __EMSCRIPTEN_PTHREADS__
emscripten_run_script("Module['ranScript'] = ENVIRONMENT_IS_PTHREAD && (typeof ENVIRONMENT_IS_WORKER !== 'undefined' && ENVIRONMENT_IS_WORKER);");
#else
emscripten_run_script("Module['ranScript'] = true;");
#endif
// 2. Test that emscripten_run_script_int() works in a pthread and it gets executed in the web worker and not on the main thread.
#if __EMSCRIPTEN_PTHREADS__
int result = emscripten_run_script_int("Module['ranScript'] && ENVIRONMENT_IS_PTHREAD && (typeof ENVIRONMENT_IS_WORKER !== 'undefined' && ENVIRONMENT_IS_WORKER);");
#else
int result = emscripten_run_script_int("Module['ranScript'];");
#endif
printf("Module['ranScript']=%d\n", result);
assert(result);
// 3. Test emscripten_run_script_string() runs in a pthread.
#if __EMSCRIPTEN_PTHREADS__
char *data = emscripten_run_script_string("ENVIRONMENT_IS_PTHREAD && (typeof ENVIRONMENT_IS_WORKER !== 'undefined' && ENVIRONMENT_IS_WORKER) ? 'in pthread' : 'not in pthread';");
printf("%s\n", data);
assert(!strcmp(data, "in pthread"));
#else
char *data = emscripten_run_script_string("(typeof ENVIRONMENT_IS_WORKER !== 'undefined' && ENVIRONMENT_IS_WORKER) ? 'in worker' : 'not in worker';");
printf("%s\n", data);
assert(!strcmp(data, "not in worker"));
#endif
// 4. Test emscripten_async_load_script() runs in a pthread.
emscripten_async_load_script("foo.js", AsyncScriptLoaded, AsyncScriptFailed);
}