From 8cf2b6adf180af075ae2802dfe48862f364d3751 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Brammer?= Date: Sun, 19 Aug 2012 17:34:53 +0200 Subject: [PATCH] Script: Call can execute functions directly Call(this["foo"],...) is now the same as Call("foo",...). --- docs/sdk/script/fn/Call.xml | 6 ++++-- src/gamescript/C4GameScript.cpp | 8 -------- src/script/C4AulDefFunc.h | 3 ++- src/script/C4PropList.cpp | 12 ------------ src/script/C4PropList.h | 1 - src/script/C4Script.cpp | 23 +++++++++++++++++++++++ 6 files changed, 29 insertions(+), 24 deletions(-) diff --git a/docs/sdk/script/fn/Call.xml b/docs/sdk/script/fn/Call.xml index c1afb3dba..a41943dff 100644 --- a/docs/sdk/script/fn/Call.xml +++ b/docs/sdk/script/fn/Call.xml @@ -8,11 +8,12 @@ Script Function call 5.1 OC + 5.4 OC any - string + string or function function Function to be called. @@ -23,11 +24,12 @@ - Calls the local function function. If "~" is prepended to the function name then the call does not fail if the function does not exist. + Calls the specified function. If given a string, the function is looked up in the context object (this). For example, obj->Call("Foo") is the same as obj->Foo(). Using Call like this is primarily useful when the name of the function can vary. If "~" is prepended to the function name then the call does not fail if the function does not exist. GameCall eval jwk2002-04 + Günther2012 diff --git a/src/gamescript/C4GameScript.cpp b/src/gamescript/C4GameScript.cpp index e92ff153d..af0747657 100644 --- a/src/gamescript/C4GameScript.cpp +++ b/src/gamescript/C4GameScript.cpp @@ -1059,13 +1059,6 @@ C4Object* FnObject(C4PropList * _this, long iNumber) // See FnObjectNumber } -static C4Value FnCall(C4PropList * _this, C4Value * Pars) -{ - if (!_this) return C4Value(); - C4AulParSet ParSet(&Pars[1], 9); - return _this->CallOrThrow(FnStringPar(Pars[0].getStr()), &ParSet); -} - static C4Value FnGameCall(C4PropList * _this, C4Value * Pars) { C4String * fn = Pars[0].getStr(); @@ -2588,7 +2581,6 @@ C4ScriptFnDef C4ScriptGameFnMap[]= { "ObjectCount", 1, C4V_Int, { C4V_Array ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnObjectCount }, { "GameCall", 1, C4V_Any, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnGameCall }, { "GameCallEx", 1, C4V_Any, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnGameCallEx }, - { "Call", 1, C4V_Any, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnCall }, { "PlayerMessage", 1, C4V_Int, { C4V_Int ,C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnPlayerMessage }, { "Message", 1, C4V_Bool, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnMessage }, { "AddMessage", 1, C4V_Bool, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnAddMessage }, diff --git a/src/script/C4AulDefFunc.h b/src/script/C4AulDefFunc.h index f8eebc020..598b49387 100644 --- a/src/script/C4AulDefFunc.h +++ b/src/script/C4AulDefFunc.h @@ -41,7 +41,8 @@ inline C4Object * Object(C4PropList * _this) StdStrBuf FnStringFormat(C4PropList * _this, C4String *szFormatPar, C4Value * Pars, int ParCount); template struct C4ValueConv; -// Allow parameters to be nil +// Allow integer and boolean parameters to be nil +// pointer parameters represent nil via plain NULL template class Nillable { diff --git a/src/script/C4PropList.cpp b/src/script/C4PropList.cpp index f5193aa78..68ab2c3fe 100644 --- a/src/script/C4PropList.cpp +++ b/src/script/C4PropList.cpp @@ -490,18 +490,6 @@ C4Value C4PropList::Call(const char * s, C4AulParSet *Pars) return pFn->Exec(this, Pars); } -C4Value C4PropList::CallOrThrow(const char * s, C4AulParSet *Pars) -{ - C4AulFunc *pFn = Status ? GetFunc(s) : NULL; - if (!pFn) - { - if (s[0] == '~') - return C4Value(); - throw new C4AulExecError(FormatString("Call: no function \"%s\"", s).getData()); - } - return pFn->Exec(this, Pars); -} - C4PropertyName C4PropList::GetPropertyP(C4PropertyName n) const { C4String * k = &Strings.P[n]; diff --git a/src/script/C4PropList.h b/src/script/C4PropList.h index 79b0d848e..f4305c8c1 100644 --- a/src/script/C4PropList.h +++ b/src/script/C4PropList.h @@ -98,7 +98,6 @@ public: { return Call(&Strings.P[k], pPars); } C4Value Call(C4String * k, C4AulParSet *pPars=0); C4Value Call(const char * k, C4AulParSet *pPars=0); - C4Value CallOrThrow(const char * k, C4AulParSet *pPars=0); C4PropertyName GetPropertyP(C4PropertyName k) const; int32_t GetPropertyInt(C4PropertyName k) const; bool HasProperty(C4String * k) { return Properties.Has(k); } diff --git a/src/script/C4Script.cpp b/src/script/C4Script.cpp index c85e5faac..48e6a392f 100644 --- a/src/script/C4Script.cpp +++ b/src/script/C4Script.cpp @@ -215,6 +215,28 @@ static C4ValueArray * FnGetProperties(C4PropList * _this, C4PropList * p) return r; } +static C4Value FnCall(C4PropList * _this, C4Value * Pars) +{ + if (!_this) return C4Value(); + C4AulParSet ParSet(&Pars[1], 9); + C4AulFunc * fn = Pars[0].getFunction(); + if (!fn) + fn = _this->GetFunc(Pars[0].getStr()); + if (!fn) + { + const char * s = FnStringPar(Pars[0].getStr()); + if (s[0] == '~') + { + fn = _this->GetFunc(&s[1]); + if (!fn) + return C4Value(); + } + } + if (!fn) + throw new C4AulExecError(FormatString("Call: no function %s", Pars[0].GetDataString().getData()).getData()); + return fn->Exec(_this, &ParSet, true); +} + static C4Value FnLog(C4PropList * _this, C4Value * Pars) { Log(FnStringFormat(_this, Pars[0].getStr(), &Pars[1], 9).getData()); @@ -579,6 +601,7 @@ C4ScriptConstDef C4ScriptConstMap[]= C4ScriptFnDef C4ScriptFnMap[]= { + { "Call", 1, C4V_Any, { C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnCall }, { "Log", 1, C4V_Bool, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnLog }, { "DebugLog", 1, C4V_Bool, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnDebugLog }, { "Format", 1, C4V_String, { C4V_String ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any ,C4V_Any}, FnFormat },