(func, sig, scoped)
| 7819 | is not stashed there. |
| 7820 | */ |
| 7821 | const __installFunction = function f(func, sig, scoped){ |
| 7822 | if(scoped && !cache.scopedAlloc.length){ |
| 7823 | toss("No scopedAllocPush() scope is active."); |
| 7824 | } |
| 7825 | if('string'===typeof func){ |
| 7826 | const x = sig; |
| 7827 | sig = func; |
| 7828 | func = x; |
| 7829 | } |
| 7830 | if('string'!==typeof sig || !(func instanceof Function)){ |
| 7831 | toss("Invalid arguments: expecting (function,signature) "+ |
| 7832 | "or (signature,function)."); |
| 7833 | } |
| 7834 | const ft = target.functionTable(); |
| 7835 | const oldLen = __asPtrType(ft.length); |
| 7836 | let ptr; |
| 7837 | while( (ptr = cache.freeFuncIndexes.pop()) ){ |
| 7838 | if(ft.get(ptr)){ |
| 7839 | /* freeFuncIndexes's entry is stale. Table was modified via a |
| 7840 | different API */ |
| 7841 | ptr = null; |
| 7842 | continue; |
| 7843 | }else{ |
| 7844 | /* This index is free. We'll re-use it. */ |
| 7845 | break; |
| 7846 | } |
| 7847 | } |
| 7848 | if(!ptr){ |
| 7849 | ptr = __asPtrType(oldLen); |
| 7850 | ft.grow(__asPtrType(1)); |
| 7851 | } |
| 7852 | try{ |
| 7853 | /*this will only work if func is a WASM-exported function*/ |
| 7854 | ft.set(ptr, func); |
| 7855 | if(scoped){ |
| 7856 | cache.scopedAlloc.pushPtr(ptr); |
| 7857 | } |
| 7858 | return ptr; |
| 7859 | }catch(e){ |
| 7860 | if(!(e instanceof TypeError)){ |
| 7861 | if(ptr===oldLen) cache.freeFuncIndexes.push(oldLen); |
| 7862 | throw e; |
| 7863 | } |
| 7864 | } |
| 7865 | // It's not a WASM-exported function, so compile one... |
| 7866 | try { |
| 7867 | const fptr = target.jsFuncToWasm(func, sig); |
| 7868 | ft.set(ptr, fptr); |
| 7869 | if(scoped){ |
| 7870 | cache.scopedAlloc.pushPtr(ptr); |
| 7871 | } |
| 7872 | }catch(e){ |
| 7873 | if(ptr===oldLen) cache.freeFuncIndexes.push(oldLen); |
| 7874 | throw e; |
| 7875 | } |
| 7876 | return ptr; |
| 7877 | }; |
| 7878 |
no test coverage detected