/*global Module*/
/*global _malloc, _free, _memcpy*/
/*global FUNCTION_TABLE, HEAP32, HEAPU8*/
/*global Pointer_stringify*/
/*global __emval_register, _emval_handle_array, __emval_decref*/
/*global ___getTypeName*/
var InternalError = Module.InternalError = extendError(Error, 'InternalError');
var BindingError = Module.BindingError = extendError(Error, 'BindingError');
var UnboundTypeError = Module.UnboundTypeError = extendError(BindingError, 'UnboundTypeError');
function throwInternalError(message) {
throw new InternalError(message);
}
function throwBindingError(message) {
throw new BindingError(message);
}
function throwUnboundTypeError(message, types) {
var unboundTypes = [];
var seen = {};
function visit(type) {
if (seen[type]) {
return;
}
if (registeredTypes[type]) {
return;
}
if (typeDependencies[type]) {
typeDependencies[type].forEach(visit);
return;
}
unboundTypes.push(type);
seen[type] = true;
}
types.forEach(visit);
throw new UnboundTypeError(message + ': ' + unboundTypes.map(getTypeName).join([', ']));
}
/* Registers a symbol (function, class, enum, ...) as part of the Module JS object so that
hand-written code is able to access that symbol via 'Module.name'.
name: The name of the symbol that's being exposed.
value: The object itself to expose (function, class, ...)
numArguments: For functions, specifies the number of arguments the function takes in. For other types, unused and undefined.
To implement support for multiple overloads of a function, an 'overload selector' function is used. That selector function chooses
the appropriate overload to call from an function overload table. This selector function is only used if multiple overloads are
actually registered, since it carries a slight performance penalty. */
function exposePublicSymbol(name, value, numArguments) {
if (Module.hasOwnProperty(name)) {
if (undefined === numArguments || (undefined !== Module[name].overloadTable && undefined !== Module[name].overloadTable[numArguments])) {
throwBindingError("Cannot register public name '" + name + "' twice");
}
// We are exposing a function with the same name as an existing function. Create an overload table and a function selector
// that routes between the two.
// If we don't yet have an overload selector, install an overload selector that routes the function call to a table of overloads based on # of arguments to function.
if (undefined === Module[name].overloadTable) {
var prevFunc = Module[name];
// Inject an overload selector in place of the previous function.
Module[name] = function() {
// TODO This check can be removed in -O3 level "unsafe" optimizations.
if (!Module[name].overloadTable.hasOwnProperty(arguments.length)) {
throwBindingError("Function '" + name + "' called with an invalid number of arguments (" + arguments.length + ") - expects one of (" + Object.keys(Module[name].overloadTable