-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
323 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,67 @@ | ||
mergeInto(LibraryManager.library, { | ||
$bindDynCall: function(funcPtr) { | ||
{{{ (function() { global.wbind = function() { return SHRINK_LEVEL == 0 ? 'wbind' : 'wasmTable.get'; }; return null; })(); }}} | ||
{{{ (function() { global.getDynCaller = function(sig) { return MINIMAL_RUNTIME ? `dynCalls[${sig}]` : `Module["dynCall_${sig}]`; }; return null; })(); }}} | ||
|
||
#if SHRINK_LEVEL == 0 | ||
// A mirror copy of contents of wasmTable in JS side, to avoid relatively | ||
// slow wasmTable.get() call. Only used when not compiling with -Os or -Oz. | ||
_wasmTableMirror: [], | ||
|
||
$wbind__deps: ['_wasmTableMirror'], | ||
$wbind: function(funcPtr) { | ||
var func = __wasmTableMirror[funcPtr]; | ||
if (!func) { | ||
if (funcPtr >= __wasmTableMirror.length) __wasmTableMirror.length = funcPtr + 1; | ||
__wasmTableMirror[funcPtr] = func = wasmTable.get(funcPtr); | ||
} | ||
return func; | ||
}, | ||
|
||
$dynCall__deps: ['$wbind'], | ||
$bindDynCall__deps: ['$wbind'], | ||
$wbindArray__deps: ['$wbind'], | ||
#else | ||
$wbind: function(funcPtr) { | ||
// In -Os and -Oz builds, do not implement a JS side wasm table mirror for small | ||
// code size, but directly access wasmTable, which is a bit slower. | ||
return wasmTable.get(funcPtr); | ||
}, | ||
#endif | ||
|
||
$bindDynCallArray: function(funcPtr) { | ||
var func = wasmTable.get(funcPtr); | ||
return func.length ? function(args) { | ||
return func.apply(null, args); | ||
} : function() { return func(); } | ||
// A helper that binds a wasm function into a form that can be called by passing all | ||
// the parameters in an array, e.g. wbindArray(func)([param1, param2, ..., paramN]). | ||
$wbindArray: function(funcPtr) { | ||
var func = {{{wbind()}}}(funcPtr); | ||
return func.length | ||
? function(args) { return func.apply(null, args); } | ||
: function() { return func(); } | ||
}, | ||
|
||
#if USE_LEGACY_DYNCALLS || !WASM_BIGINT | ||
$dynCallLegacy: function(sig, ptr, args) { | ||
#if ASSERTIONS | ||
assert(('dynCall_' + sig) in Module, 'bad function pointer type - no table for sig \'' + sig + '\''); | ||
if (args && args.length) { | ||
// j (64-bit integer) must be passed in as two numbers [low 32, high 32]. | ||
assert(args.length === sig.substring(1).replace(/j/g, '--').length); | ||
} else { | ||
assert(sig.length == 1); | ||
} | ||
#endif | ||
if (args && args.length) { | ||
return Module['dynCall_' + sig].apply(null, [ptr].concat(args)); | ||
} | ||
return Module['dynCall_' + sig].call(null, ptr); | ||
// A helper that returns a function that can be used to invoke function pointers, i.e. | ||
// getDynCaller('vi')(funcPtr, myInt); | ||
$getDynCaller: function(sig, funcPtr) { | ||
return {{{getDynCaller('sig')}}}; | ||
}, | ||
|
||
// Used in library code to get JS function from wasm function pointer. | ||
// All callers should use direct table access where possible and only fall | ||
// back to this function if needed. | ||
$getDynCaller__deps: ['$dynCall'], | ||
$getDynCaller: function(sig, ptr) { | ||
#if !USE_LEGACY_DYNCALLS | ||
assert(sig.indexOf('j') >= 0, 'getDynCaller should only be called with i64 sigs') | ||
#endif | ||
var argCache = []; | ||
return function() { | ||
argCache.length = arguments.length; | ||
for (var i = 0; i < arguments.length; i++) { | ||
argCache[i] = arguments[i]; | ||
} | ||
return dynCall(sig, ptr, argCache); | ||
}; | ||
$bindDynCall: function(sig, funcPtr) { | ||
// For int64 signatures, use the dynCall_sig dispatch mechanism. | ||
if (sig.includes('j')) return function(args) { | ||
return {{{getDynCaller('sig')}}}.apply(null, [funcPtr].concat(args)); | ||
} | ||
// For non-int64 signatures, invoke via the wasm table. | ||
var func = {{{wbind()}}}(funcPtr); | ||
return func.length | ||
? function(args) { return func.apply(null, args); } | ||
: function() { return func(); } | ||
}, | ||
#endif | ||
|
||
// $dynCall__deps: ['$dynCallLegacy'], | ||
$dynCall: function(sig, ptr, args) { | ||
#if USE_LEGACY_DYNCALLS | ||
#if MINIMAL_RUNTIME | ||
var func = dynCalls[sig]; | ||
#else | ||
var func = Module['dynCall_'+sig]; | ||
#endif | ||
return args ? func.apply(null, [ptr].concat(args)) : func(ptr); | ||
#else | ||
#if !WASM_BIGINT | ||
// Without WASM_BIGINT support we cannot directly call function with i64 as | ||
// part of thier signature, so we rely the dynCall functions generated by | ||
// wasm-emscripten-finalize | ||
if (sig.indexOf('j') != -1) { | ||
#if MINIMAL_RUNTIME | ||
var func = dynCalls[sig]; | ||
#else | ||
var func = Module['dynCall_'+sig]; | ||
#endif | ||
return args ? func.apply(null, [ptr].concat(args)) : func(ptr); | ||
$dynCall: function(sig, funcPtr, args) { | ||
// For int64 signatures, use the dynCall_sig dispatch mechanism. | ||
if (sig.includes('j')) { | ||
return {{{getDynCaller('sig')}}}.apply(null, [funcPtr].concat(args)); | ||
} | ||
#endif | ||
#if ASSERTIONS | ||
assert(wasmTable.get(ptr), 'missing table entry in dynCall: ' + ptr); | ||
#endif | ||
return wasmTable.get(ptr).apply(null, args) | ||
#endif | ||
|
||
// For non-int64 signatures, invoke via the wasm table. | ||
return {{{wbind()}}}(funcPtr).apply(null, args); | ||
} | ||
}); |
Oops, something went wrong.