Skip to content

Commit

Permalink
Refactor|Scripting: Renamed Context and function call "instance scope"
Browse files Browse the repository at this point in the history
A clearer name is to just call this the "self" instance, since that's
what it is being used for.
  • Loading branch information
skyjake committed Oct 13, 2015
1 parent fc2c1ed commit 36d337b
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 79 deletions.
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/include/de/data/recordvalue.h
Expand Up @@ -114,7 +114,7 @@ class DENG2_PUBLIC RecordValue : public Value, DENG2_OBSERVES(Record, Deletion)
bool contains(Value const &value) const;
bool isTrue() const;
dint compare(Value const &value) const;
void call(Process &process, Value const &arguments, Value *instanceScope = 0) const;
void call(Process &process, Value const &arguments, Value *self = 0) const;

// Implements ISerializable.
void operator >> (Writer &to) const;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/include/de/data/refvalue.h
Expand Up @@ -81,7 +81,7 @@ class DENG2_PUBLIC RefValue : public Value,
void multiply(Value const &value);
void modulo(Value const &divisor);
void assign(Value *value);
void call(Process &process, Value const &arguments, Value *instanceScope) const;
void call(Process &process, Value const &arguments, Value *self) const;

// Implements ISerializable.
void operator >> (Writer &to) const;
Expand Down
12 changes: 6 additions & 6 deletions doomsday/sdk/libcore/include/de/data/value.h
Expand Up @@ -300,13 +300,13 @@ class DENG2_PUBLIC Value : public String::IPatternArg, public ISerializable
/**
* Applies the call operator on the value.
*
* @param process Process where the call is made.
* @param arguments Arguments of the call.
* @param instanceScope Optional scope that becomes the value of the "self"
* variable in the called function's local namespace.
* Ownership taken.
* @param process Process where the call is made.
* @param arguments Arguments of the call.
* @param self Optional scope that becomes the value of the "self"
* variable in the called function's local namespace.
* Ownership taken.
*/
virtual void call(Process &process, Value const &arguments, Value *instanceScope = 0) const;
virtual void call(Process &process, Value const &arguments, Value *self = 0) const;

public:
/**
Expand Down
20 changes: 15 additions & 5 deletions doomsday/sdk/libcore/include/de/scriptsys/context.h
Expand Up @@ -14,7 +14,7 @@
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDENG2_CONTEXT_H
Expand Down Expand Up @@ -139,18 +139,28 @@ class DENG2_PUBLIC Context
void setIterationValue(Value *value);

/**
* Sets the instance scope of the context. This is equivalent to the value
* of the "self" variable, however only used for native function calls.
* Sets the instance scope of the context, i.e., the "self" object whose
* contents are being accessed. This is used when executing native
* functions and it is comparable to the "self" variable that gets added to
* script contexts.
*
* @param scope Value that specifies the instance whose scope the context
* is being evaluated in. Ownership taken.
*/
void setInstanceScope(Value *scope);
void setNativeSelf(Value *nativeSelf);

/**
* Returns the current instance scope. A scope must exist if this is called.
*/
Value &instanceScope() const;
Value &nativeSelf() const;

/**
* Returns the self instance. This makes the assumption that "self" is a
* RecordValue pointing to the instance record.
*
* @return Instance currently designated as "self".
*/
Record &selfInstance() const;

/**
* Returns the throwaway variable. This can be used for dumping
Expand Down
6 changes: 3 additions & 3 deletions doomsday/sdk/libcore/include/de/scriptsys/function.h
Expand Up @@ -261,9 +261,9 @@ class DENG2_PUBLIC NativeFunctionSpec
de::Function::Arguments() << Args, Defaults)

/**
* Utility that keeps track of which entry points have been bound and unregisters
* them when the instance is destroyed. Use as a member in a class that registers
* native entry points.
* Utility that keeps track of which entry points have been bound and
* unregisters them when the Binder instance is destroyed. For example, use as
* a member in a class that registers native entry points.
*
* @ingroup script
*/
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/include/de/scriptsys/functionvalue.h
Expand Up @@ -46,7 +46,7 @@ class FunctionValue : public Value
bool isTrue() const;
bool isFalse() const;
dint compare(Value const &value) const;
void call(Process &process, Value const &arguments, Value *instanceScope) const;
void call(Process &process, Value const &arguments, Value *self) const;

// Implements ISerializable.
void operator >> (Writer &to) const;
Expand Down
6 changes: 3 additions & 3 deletions doomsday/sdk/libcore/include/de/scriptsys/process.h
Expand Up @@ -14,7 +14,7 @@
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBDENG2_PROCESS_H
Expand Down Expand Up @@ -183,11 +183,11 @@ class DENG2_PUBLIC Process
* must be a DictionaryValue containing values for the
* named arguments of the call. The rest of the array
* are the unnamed arguments.
* @param instanceScope Optional scope that becomes the value of the "self"
* @param self Optional scope that becomes the value of the "self"
* variable. Ownership given to Process.
*/
void call(Function const &function, ArrayValue const &arguments,
Value *instanceScope = 0);
Value *self = 0);

/**
* Collects the namespaces currently visible. This includes the process's
Expand Down
4 changes: 2 additions & 2 deletions doomsday/sdk/libcore/src/data/refvalue.cpp
Expand Up @@ -166,9 +166,9 @@ void RefValue::assign(Value *value)
_variable->set(value);
}

void RefValue::call(Process &process, Value const &arguments, Value *instanceScope) const
void RefValue::call(Process &process, Value const &arguments, Value *self) const
{
dereference().call(process, arguments, instanceScope);
dereference().call(process, arguments, self);
}

void RefValue::operator >> (Writer &to) const
Expand Down
26 changes: 10 additions & 16 deletions doomsday/sdk/libcore/src/scriptsys/bindings_core.cpp
Expand Up @@ -34,32 +34,32 @@ namespace de {

static Value *Function_String_FileNamePath(Context &ctx, Function::ArgumentValues const &)
{
return new TextValue(ctx.instanceScope().asText().fileNamePath());
return new TextValue(ctx.nativeSelf().asText().fileNamePath());
}

static Value *Function_String_FileNameExtension(Context &ctx, Function::ArgumentValues const &)
{
return new TextValue(ctx.instanceScope().asText().fileNameExtension());
return new TextValue(ctx.nativeSelf().asText().fileNameExtension());
}

static Value *Function_String_FileNameWithoutExtension(Context &ctx, Function::ArgumentValues const &)
{
return new TextValue(ctx.instanceScope().asText().fileNameWithoutExtension());
return new TextValue(ctx.nativeSelf().asText().fileNameWithoutExtension());
}

static Value *Function_String_FileNameAndPathWithoutExtension(Context &ctx, Function::ArgumentValues const &)
{
return new TextValue(ctx.instanceScope().asText().fileNameAndPathWithoutExtension());
return new TextValue(ctx.nativeSelf().asText().fileNameAndPathWithoutExtension());
}

static Value *Function_String_Upper(Context &ctx, Function::ArgumentValues const &)
{
return new TextValue(ctx.instanceScope().asText().upper());
return new TextValue(ctx.nativeSelf().asText().upper());
}

static Value *Function_String_Lower(Context &ctx, Function::ArgumentValues const &)
{
return new TextValue(ctx.instanceScope().asText().lower());
return new TextValue(ctx.nativeSelf().asText().lower());
}

static Value *Function_Path_WithoutFileName(Context &, Function::ArgumentValues const &args)
Expand All @@ -69,24 +69,18 @@ static Value *Function_Path_WithoutFileName(Context &, Function::ArgumentValues

static Value *Function_Dictionary_Keys(Context &ctx, Function::ArgumentValues const &)
{
return ctx.instanceScope().as<DictionaryValue>().contentsAsArray(DictionaryValue::Keys);
return ctx.nativeSelf().as<DictionaryValue>().contentsAsArray(DictionaryValue::Keys);
}

static Value *Function_Dictionary_Values(Context &ctx, Function::ArgumentValues const &)
{
return ctx.instanceScope().as<DictionaryValue>().contentsAsArray(DictionaryValue::Values);
return ctx.nativeSelf().as<DictionaryValue>().contentsAsArray(DictionaryValue::Values);
}

static File const &fileInstance(Context &ctx)
{
Record const *obj = ctx.instanceScope().as<RecordValue>().record();
if(!obj)
{
throw Value::IllegalError("ScriptSystem::fileInstance", "No File instance available");
}

// The record is expected to have a path (e.g., File info record).
return App::rootFolder().locate<File>(obj->gets("path", "/"));
return App::rootFolder().locate<File>(ctx.selfInstance().gets("path", "/"));
}

static Value *Function_File_Locate(Context &ctx, Function::ArgumentValues const &args)
Expand Down Expand Up @@ -118,7 +112,7 @@ static Value *Function_File_ReadUtf8(Context &ctx, Function::ArgumentValues cons

static Animation &animationInstance(Context &ctx)
{
Animation *obj = ctx.instanceScope().as<NativeValue>().nativeObject<Animation>();
Animation *obj = ctx.nativeSelf().as<NativeValue>().nativeObject<Animation>();
if(!obj)
{
throw Value::IllegalError("ScriptSystem::animationInstance", "No Animation instance available");
Expand Down
37 changes: 24 additions & 13 deletions doomsday/sdk/libcore/src/scriptsys/context.cpp
Expand Up @@ -14,12 +14,13 @@
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
* http://www.gnu.org/licenses</small>
*/

#include "de/Context"
#include "de/Statement"
#include "de/Process"
#include "de/RecordValue"

namespace de {

Expand Down Expand Up @@ -83,7 +84,7 @@ DENG2_PIMPL(Context)
/// The local namespace of this context.
Record *names;

QScopedPointer<Value> instanceScope;
QScopedPointer<Value> nativeSelf;

Variable throwaway;

Expand Down Expand Up @@ -167,7 +168,7 @@ bool Context::hasExternalGlobalNamespace() const
return !d->ownsNamespace;
}

Record &Context::names()
Record &Context::names()
{
return *d->names;
}
Expand All @@ -190,7 +191,7 @@ void Context::reset()
while(!d->controlFlow.empty())
{
d->popFlow();
}
}
d->evaluator.reset();
}

Expand Down Expand Up @@ -241,7 +242,7 @@ void Context::jumpBreak(duint count)
{
throw JumpError("Context::jumpBreak", "Invalid number of nested breaks");
}

Statement const *st = NULL;
while((!st || count > 0) && d->controlFlow.size())
{
Expand Down Expand Up @@ -282,7 +283,7 @@ Value *Context::iterationValue()
void Context::setIterationValue(Value *value)
{
DENG2_ASSERT(d->controlFlow.size());

Instance::ControlFlow &fl = d->flow();
if(fl.iteration)
{
Expand All @@ -291,20 +292,30 @@ void Context::setIterationValue(Value *value)
fl.iteration = value;
}

void Context::setInstanceScope(Value *scope)
void Context::setNativeSelf(Value *scope)
{
d->instanceScope.reset(scope);
d->nativeSelf.reset(scope);
}

Value &Context::instanceScope() const
Value &Context::nativeSelf() const
{
DENG2_ASSERT(!d->instanceScope.isNull());
if(d->instanceScope.isNull())
DENG2_ASSERT(!d->nativeSelf.isNull());
if(d->nativeSelf.isNull())
{
throw UndefinedScopeError("Context::instanceScope",
throw UndefinedScopeError("Context::nativeSelf",
"Context is not executing in scope of any instance");
}
return *d->instanceScope;
return *d->nativeSelf;
}

Record &Context::selfInstance() const
{
Record *obj = nativeSelf().as<RecordValue>().record();
if(!obj)
{
throw UndefinedScopeError("Context::selfInstance", "No \"self\" instance has been set");
}
return *obj;
}

Variable &Context::throwaway()
Expand Down
4 changes: 2 additions & 2 deletions doomsday/sdk/libcore/src/scriptsys/functionvalue.cpp
Expand Up @@ -77,15 +77,15 @@ dint FunctionValue::compare(Value const &value) const
return -1;
}

void FunctionValue::call(Process &process, Value const &arguments, Value *instanceScope) const
void FunctionValue::call(Process &process, Value const &arguments, Value *self) const
{
ArrayValue const *array = dynamic_cast<ArrayValue const *>(&arguments);
if(!array)
{
/// @throw IllegalError The call arguments must be an array value.
throw IllegalError("FunctionValue::call", "Arguments is not an array");
}
process.call(*_func, *array, instanceScope);
process.call(*_func, *array, self);
}

void FunctionValue::operator >> (Writer &to) const
Expand Down

0 comments on commit 36d337b

Please sign in to comment.