1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001, 2007, Sven Eberhardt
5  * Copyright (c) 2011-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 
17 #include "C4Include.h"
18 #include "script/C4Value.h"
19 #include "script/C4AulFunc.h"
20 #include "script/C4Aul.h"
21 
C4AulFunc(C4PropListStatic * Parent,const char * pName)22 C4AulFunc::C4AulFunc(C4PropListStatic * Parent, const char *pName):
23 		Parent(Parent),
24 		Name(pName ? Strings.RegString(pName) : nullptr),
25 		MapNext(nullptr)
26 {
27 	// add to global lookuptable with this name
28 	if (GetName())
29 		::ScriptEngine.FuncLookUp.Add(this);
30 }
31 
~C4AulFunc()32 C4AulFunc::~C4AulFunc()
33 {
34 	if (GetName())
35 		::ScriptEngine.FuncLookUp.Remove(this);
36 }
37 
GetFullName() const38 StdStrBuf C4AulFunc::GetFullName() const
39 {
40 	StdStrBuf r;
41 	// "lost" function?
42 	if (!Parent)
43 	{
44 		r.Ref("(unowned) ");
45 	}
46 	else
47 	{
48 		r.Take(Parent->GetDataString());
49 		r.AppendChar('.');
50 	}
51 	if (GetName())
52 		r.Append(Name->GetData());
53 	else
54 		r.Append("(unnamed)");
55 	return r;
56 }
57 
CheckParTypes(const C4Value pPars[],bool fPassErrors) const58 bool C4AulFunc::CheckParTypes(const C4Value pPars[], bool fPassErrors) const {
59 	// Convert parameters (typecheck)
60 	const C4V_Type *pTypes = GetParType();
61 	int parcount = GetParCount();
62 	for (int i = 0; i < parcount; i++) {
63 		if (!pPars[i].CheckParConversion(pTypes[i]))
64 		{
65 			C4AulExecError e(FormatString(
66 				R"(call to "%s" parameter %d: passed %s, but expected %s)",
67 				GetName(), i + 1, pPars[i].GetTypeName(), GetC4VName(pTypes[i])).getData());
68 			if (fPassErrors)
69 				throw e;
70 			else
71 			{
72 				::ScriptEngine.GetErrorHandler()->OnError(e.what());
73 				return false;
74 			}
75 		}
76 	}
77 	return true;
78 }
79