blob: f0f039f5462d0781d82b3464c87478714c0a1737 [file] [log] [blame]
fpizlo@apple.com9e4dfa82017-08-30 17:33:26 +00001/*
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.com9cb92a72017-08-31 22:22:59 +000028function callFunction(program, name, typeArguments, argumentList)
fpizlo@apple.com9e4dfa82017-08-30 17:33:26 +000029{
fpizlo@apple.com6818daa2017-08-31 22:15:18 +000030 let argumentTypes = argumentList.map(argument => argument.type);
mmaxfield@apple.com728a72b2017-09-20 20:07:07 +000031 let funcOrFailures = resolveInlinedFunction(program, name, typeArguments, argumentTypes, true);
fpizlo@apple.com899997f2017-09-03 17:46:48 +000032 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.com51609b62017-09-21 17:41:11 +000042 let result = new Evaluator(program).runFunc(func);
fpizlo@apple.come34d1722017-09-19 04:15:56 +000043 return new TypedValue(func.uninstantiatedReturnType, result);
fpizlo@apple.com9e4dfa82017-08-30 17:33:26 +000044}
45