(func, sig, scoped)
| 7698 | is not stashed there. |
| 7699 | */ |
| 7700 | const __installFunction = function f(func, sig, scoped){ |
| 7701 | if(scoped && !cache.scopedAlloc.length){ |
| 7702 | toss("No scopedAllocPush() scope is active."); |
| 7703 | } |
| 7704 | if('string'===typeof func){ |
| 7705 | const x = sig; |
| 7706 | sig = func; |
| 7707 | func = x; |
| 7708 | } |
| 7709 | if('string'!==typeof sig || !(func instanceof Function)){ |
| 7710 | toss("Invalid arguments: expecting (function,signature) "+ |
| 7711 | "or (signature,function)."); |
| 7712 | } |
| 7713 | const ft = target.functionTable(); |
| 7714 | const oldLen = __asPtrType(ft.length); |
| 7715 | let ptr; |
| 7716 | while( (ptr = cache.freeFuncIndexes.pop()) ){ |
| 7717 | if(ft.get(ptr)){ |
| 7718 | /* freeFuncIndexes's entry is stale. Table was modified via a |
| 7719 | different API */ |
| 7720 | ptr = null; |
| 7721 | continue; |
| 7722 | }else{ |
| 7723 | /* This index is free. We'll re-use it. */ |
| 7724 | break; |
| 7725 | } |
| 7726 | } |
| 7727 | if(!ptr){ |
| 7728 | ptr = __asPtrType(oldLen); |
| 7729 | ft.grow(__asPtrType(1)); |
| 7730 | } |
| 7731 | try{ |
| 7732 | /*this will only work if func is a WASM-exported function*/ |
| 7733 | ft.set(ptr, func); |
| 7734 | if(scoped){ |
| 7735 | cache.scopedAlloc.pushPtr(ptr); |
| 7736 | } |
| 7737 | return ptr; |
| 7738 | }catch(e){ |
| 7739 | if(!(e instanceof TypeError)){ |
| 7740 | if(ptr===oldLen) cache.freeFuncIndexes.push(oldLen); |
| 7741 | throw e; |
| 7742 | } |
| 7743 | } |
| 7744 | // It's not a WASM-exported function, so compile one... |
| 7745 | try { |
| 7746 | const fptr = target.jsFuncToWasm(func, sig); |
| 7747 | ft.set(ptr, fptr); |
| 7748 | if(scoped){ |
| 7749 | cache.scopedAlloc.pushPtr(ptr); |
| 7750 | } |
| 7751 | }catch(e){ |
| 7752 | if(ptr===oldLen) cache.freeFuncIndexes.push(oldLen); |
| 7753 | throw e; |
| 7754 | } |
| 7755 | return ptr; |
| 7756 | }; |
| 7757 |
no test coverage detected