Script: Add GetPrototype and SetPrototype functions

This should eventually replace the Prototype property, so that proplists
can be used as a key-value-storage without any hidden gotchas.

The Prototype is such a magical property that any code dealing with all
properties has to special-case it anyway, and isn't even returned by
GetProperties().
liquid_container
Günther Brammer 2016-04-27 20:42:59 +02:00
parent 525cdc11a3
commit 3760363a3b
4 changed files with 77 additions and 1 deletions

View File

@ -28,7 +28,7 @@ Stand = {
<row>
<col><code>Prototype</code></col>
<col>proplist</col>
<col></col>
<col>Deprecated. Use <funclink>SetPrototype</funclink> and <funclink>GetPrototype</funclink>.</col>
</row>
<row>
<col><code>Name</code></col>

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE funcs
SYSTEM '../../../clonk.dtd'>
<?xml-stylesheet type="text/xsl" href="../../../clonk.xsl"?>
<funcs>
<func>
<title>GetPrototype</title>
<category>Objects</category>
<subcat>Properties</subcat>
<version>8.0 OC</version>
<syntax>
<rtype>nil</rtype>
<params>
<param>
<type>proplist</type>
<name>obj</name>
<desc>The Object whose prototype is returned. Can be <code>nil</code> in local calls.</desc>
<optional />
</param>
</params>
</syntax>
<desc>When properties of a proplist are read and not set on that proplist, the property is looked up in the proplist's prototype(s). The immediate prototype is returned by this function.</desc>
<related><funclink>SetPrototype</funclink></related>
</func>
<author>Günther</author><date>2016-04</date>
</funcs>

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE funcs
SYSTEM '../../../clonk.dtd'>
<?xml-stylesheet type="text/xsl" href="../../../clonk.xsl"?>
<funcs>
<func>
<title>SetPrototype</title>
<category>Objects</category>
<subcat>Properties</subcat>
<version>8.0 OC</version>
<syntax>
<rtype>nil</rtype>
<params>
<param>
<type>proplist</type>
<name>prototype</name>
<desc>The new prototype.</desc>
<optional />
</param>
<param>
<type>proplist</type>
<name>obj</name>
<desc>Object to be changed. Can be <code>nil</code> in local calls.</desc>
<optional />
</param>
</params>
</syntax>
<desc>This function changes the prototype of a proplist.
When properties of a proplist are read and not set on that proplist, the property is looked up in the proplist's prototype(s).
This can be used for inheritance.</desc>
<related><funclink>GetPrototype</funclink></related>
</func>
<author>Günther</author><date>2016-04</date>
</funcs>

View File

@ -300,6 +300,20 @@ static C4ValueArray * FnGetProperties(C4PropList * _this, C4PropList * p)
return r;
}
static C4PropList * FnGetPrototype(C4PropList * _this, C4PropList * p)
{
if (!p) p = _this;
if (!p) throw NeedNonGlobalContext("GetPrototype");
return p->GetPrototype();
}
static void FnSetPrototype(C4PropList * _this, C4PropList * prototype, C4PropList * p)
{
if (!p) p = _this;
if (!p) throw NeedNonGlobalContext("GetPrototype");
p->SetProperty(P_Prototype, C4Value(prototype));
}
static C4Value FnCall(C4PropList * _this, C4Value * Pars)
{
if (!_this) _this = ::ScriptEngine.GetPropList();
@ -955,6 +969,8 @@ void InitCoreFunctionMap(C4AulScriptEngine *pEngine)
F(GetProperties);
F(GetProperty);
F(SetProperty);
F(GetPrototype);
F(SetPrototype);
F(ResetProperty);
F(GetName);
F(AddEffect);