fpizlo@apple.com | 9e4dfa8 | 2017-08-30 17:33:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | "use strict"; |
| 26 | |
| 27 | // This allows you to pass structs and arrays in-place, but it's a more annoying API. |
fpizlo@apple.com | 9cb92a7 | 2017-08-31 22:22:59 +0000 | [diff] [blame] | 28 | function callFunction(program, name, typeArguments, argumentList) |
fpizlo@apple.com | 9e4dfa8 | 2017-08-30 17:33:26 +0000 | [diff] [blame] | 29 | { |
fpizlo@apple.com | 6818daa | 2017-08-31 22:15:18 +0000 | [diff] [blame] | 30 | let argumentTypes = argumentList.map(argument => argument.type); |
mmaxfield@apple.com | 728a72b | 2017-09-20 20:07:07 +0000 | [diff] [blame] | 31 | let funcOrFailures = resolveInlinedFunction(program, name, typeArguments, argumentTypes, true); |
fpizlo@apple.com | 899997f | 2017-09-03 17:46:48 +0000 | [diff] [blame] | 32 | if (!(funcOrFailures instanceof Func)) { |
| 33 | let failures = funcOrFailures; |
| 34 | throw new WTypeError("<callFunction>", "Cannot resolve function call " + name + "<" + typeArguments + ">(" + argumentList + ")" + (failures.length ? "; tried:\n" + failures.join("\n") : "")); |
| 35 | } |
| 36 | let func = funcOrFailures; |
| 37 | for (let i = 0; i < func.parameters.length; ++i) { |
| 38 | let type = argumentTypes[i].instantiatedType; |
| 39 | type.visit(new StructLayoutBuilder()); |
| 40 | func.parameters[i].ePtr.copyFrom(argumentList[i].ePtr, type.size); |
| 41 | } |
fpizlo@apple.com | 51609b6 | 2017-09-21 17:41:11 +0000 | [diff] [blame] | 42 | let result = new Evaluator(program).runFunc(func); |
fpizlo@apple.com | e34d172 | 2017-09-19 04:15:56 +0000 | [diff] [blame] | 43 | return new TypedValue(func.uninstantiatedReturnType, result); |
fpizlo@apple.com | 9e4dfa8 | 2017-08-30 17:33:26 +0000 | [diff] [blame] | 44 | } |
| 45 | |