| 23 | EM_JS_DEPS(main, "$addFunction,$removeFunction"); |
| 24 | |
| 25 | int main(int argc, char **argv) { |
| 26 | #if defined(GROWTH) |
| 27 | EM_ASM({ |
| 28 | // Get an export that isn't in the table (we never took its address in C). |
| 29 | var baz = wasmExports["baz"]; |
| 30 | var tableSizeBefore = wasmTable.length; |
| 31 | var bazIndex = addFunction(baz); |
| 32 | assert(bazIndex >= tableSizeBefore, "we actually added it"); |
| 33 | assert(addFunction(baz) === bazIndex, "we never add it again"); |
| 34 | }); |
| 35 | #endif |
| 36 | |
| 37 | int fp = atoi(argv[1]); |
| 38 | printf("fp: %d\n", fp); |
| 39 | void (*f)(int) = reinterpret_cast<void (*)(int)>(fp); |
| 40 | f(7); |
| 41 | EM_ASM({ |
| 42 | removeFunction($0) |
| 43 | }, f); |
| 44 | |
| 45 | // We can reuse indexes |
| 46 | EM_ASM({ |
| 47 | var beforeLength = wasmTable.length; |
| 48 | for (var i = 0; i < 10; i++) { |
| 49 | var index = addFunction(function(){}, 'v'); |
| 50 | removeFunction(index); |
| 51 | } |
| 52 | assert(wasmTable.length === beforeLength); |
| 53 | }); |
| 54 | |
| 55 | // We guarantee index uniqueness for each function. |
| 56 | EM_ASM({ |
| 57 | assert(wasmTable.length >= 3); |
| 58 | assert(addFunction(wasmTable.get(1)) === 1); |
| 59 | assert(addFunction(wasmTable.get(2)) === 2); |
| 60 | }, &foo, &bar); // taking the addresses here ensures they are in the table |
| 61 | // (the optimizer can't remove these uses) which then lets |
| 62 | // us assume the table is of a certain size in the test. |
| 63 | printf("ok\n"); |
| 64 | return 0; |
| 65 | } |