|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2022 The Emscripten Authors |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | + |
| 7 | +mergeInto(LibraryManager.library, { |
| 8 | + // Returns the C function with a specified identifier (for C++, you need to do manual name mangling) |
| 9 | + $getCFunc: function(ident) { |
| 10 | + var func = Module['_' + ident]; // closure exported function |
| 11 | +#if ASSERTIONS |
| 12 | + assert(func, 'Cannot call unknown function ' + ident + ', make sure it is exported'); |
| 13 | +#endif |
| 14 | + return func; |
| 15 | + }, |
| 16 | + |
| 17 | + // C calling interface. |
| 18 | + $ccall__deps: ['$getCFunc'], |
| 19 | + $ccall__docs: ` |
| 20 | + /** |
| 21 | + * @param {string|null=} returnType |
| 22 | + * @param {Array=} argTypes |
| 23 | + * @param {Arguments|Array=} args |
| 24 | + * @param {Object=} opts |
| 25 | + */`, |
| 26 | + $ccall: function(ident, returnType, argTypes, args, opts) { |
| 27 | + // For fast lookup of conversion functions |
| 28 | + var toC = { |
| 29 | +#if MEMORY64 |
| 30 | + 'pointer': (p) => {{{ to64('p') }}}, |
| 31 | +#endif |
| 32 | + 'string': function(str) { |
| 33 | + var ret = 0; |
| 34 | + if (str !== null && str !== undefined && str !== 0) { // null string |
| 35 | + // at most 4 bytes per UTF-8 code point, +1 for the trailing '\0' |
| 36 | + var len = (str.length << 2) + 1; |
| 37 | + ret = stackAlloc(len); |
| 38 | + stringToUTF8(str, ret, len); |
| 39 | + } |
| 40 | + return {{{ to64('ret') }}}; |
| 41 | + }, |
| 42 | + 'array': function(arr) { |
| 43 | + var ret = stackAlloc(arr.length); |
| 44 | + writeArrayToMemory(arr, ret); |
| 45 | + return {{{ to64('ret') }}}; |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + function convertReturnValue(ret) { |
| 50 | + if (returnType === 'string') { |
| 51 | + {{{ from64('ret') }}} |
| 52 | + return UTF8ToString(ret); |
| 53 | + } |
| 54 | +#if MEMORY64 |
| 55 | + if (returnType === 'pointer') return Number(ret); |
| 56 | +#endif |
| 57 | + if (returnType === 'boolean') return Boolean(ret); |
| 58 | + return ret; |
| 59 | + } |
| 60 | + |
| 61 | + var func = getCFunc(ident); |
| 62 | + var cArgs = []; |
| 63 | + var stack = 0; |
| 64 | +#if ASSERTIONS |
| 65 | + assert(returnType !== 'array', 'Return type should not be "array".'); |
| 66 | +#endif |
| 67 | + if (args) { |
| 68 | + for (var i = 0; i < args.length; i++) { |
| 69 | + var converter = toC[argTypes[i]]; |
| 70 | + if (converter) { |
| 71 | + if (stack === 0) stack = stackSave(); |
| 72 | + cArgs[i] = converter(args[i]); |
| 73 | + } else { |
| 74 | + cArgs[i] = args[i]; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +#if ASYNCIFY |
| 79 | + // Data for a previous async operation that was in flight before us. |
| 80 | + var previousAsync = Asyncify.currData; |
| 81 | +#endif |
| 82 | + var ret = func.apply(null, cArgs); |
| 83 | + function onDone(ret) { |
| 84 | +#if ASYNCIFY |
| 85 | + runtimeKeepalivePop(); |
| 86 | +#endif |
| 87 | + if (stack !== 0) stackRestore(stack); |
| 88 | + return convertReturnValue(ret); |
| 89 | + } |
| 90 | +#if ASYNCIFY |
| 91 | + // Keep the runtime alive through all calls. Note that this call might not be |
| 92 | + // async, but for simplicity we push and pop in all calls. |
| 93 | + runtimeKeepalivePush(); |
| 94 | + var asyncMode = opts && opts.async; |
| 95 | + if (Asyncify.currData != previousAsync) { |
| 96 | +#if ASSERTIONS |
| 97 | + // A change in async operation happened. If there was already an async |
| 98 | + // operation in flight before us, that is an error: we should not start |
| 99 | + // another async operation while one is active, and we should not stop one |
| 100 | + // either. The only valid combination is to have no change in the async |
| 101 | + // data (so we either had one in flight and left it alone, or we didn't have |
| 102 | + // one), or to have nothing in flight and to start one. |
| 103 | + assert(!(previousAsync && Asyncify.currData), 'We cannot start an async operation when one is already flight'); |
| 104 | + assert(!(previousAsync && !Asyncify.currData), 'We cannot stop an async operation in flight'); |
| 105 | +#endif |
| 106 | + // This is a new async operation. The wasm is paused and has unwound its stack. |
| 107 | + // We need to return a Promise that resolves the return value |
| 108 | + // once the stack is rewound and execution finishes. |
| 109 | +#if ASSERTIONS |
| 110 | + assert(asyncMode, 'The call to ' + ident + ' is running asynchronously. If this was intended, add the async option to the ccall/cwrap call.'); |
| 111 | +#endif |
| 112 | + return Asyncify.whenDone().then(onDone); |
| 113 | + } |
| 114 | +#endif |
| 115 | + |
| 116 | + ret = onDone(ret); |
| 117 | +#if ASYNCIFY |
| 118 | + // If this is an async ccall, ensure we return a promise |
| 119 | + if (asyncMode) return Promise.resolve(ret); |
| 120 | +#endif |
| 121 | + return ret; |
| 122 | + }, |
| 123 | + |
| 124 | + $cwrap__docs: ` |
| 125 | + /** |
| 126 | + * @param {string=} returnType |
| 127 | + * @param {Array=} argTypes |
| 128 | + * @param {Object=} opts |
| 129 | + */`, |
| 130 | + $cwrap__deps: ['$getCFunc', '$ccall'], |
| 131 | + $cwrap: function(ident, returnType, argTypes, opts) { |
| 132 | +#if !ASSERTIONS |
| 133 | + argTypes = argTypes || []; |
| 134 | + // When the function takes numbers and returns a number, we can just return |
| 135 | + // the original function |
| 136 | + var numericArgs = argTypes.every((type) => type === 'number'); |
| 137 | + var numericRet = returnType !== 'string'; |
| 138 | + if (numericRet && numericArgs && !opts) { |
| 139 | + return getCFunc(ident); |
| 140 | + } |
| 141 | +#endif |
| 142 | + return function() { |
| 143 | + return ccall(ident, returnType, argTypes, arguments, opts); |
| 144 | + } |
| 145 | + }, |
| 146 | +}); |
0 commit comments