Skip to content

Commit

Permalink
Cleanup|libcore: Accessing built-in script classes; setting super record
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Oct 30, 2015
1 parent 40de768 commit 1885e89
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 13 deletions.
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/include/de/data/nativevalue.h
Expand Up @@ -40,7 +40,7 @@ class DENG2_PUBLIC NativeValue : public Value
typedef Deletable Object;

public:
NativeValue(Object *object, Record const *memberScope);
NativeValue(Object *object, Record const *memberScope = nullptr);

Object *object() const;
void setObject(Object *object);
Expand Down
10 changes: 10 additions & 0 deletions doomsday/sdk/libcore/include/de/data/record.h
Expand Up @@ -73,6 +73,8 @@ class DENG2_PUBLIC Record
/// Name of the special variable that identifies the source file.
static String const VAR_FILE;

static String const VAR_NATIVE_SELF;

typedef QMap<String, Variable *> Members;
typedef QMap<String, Record *> Subrecords;
typedef std::pair<String, String> KeyValue;
Expand Down Expand Up @@ -464,6 +466,14 @@ class DENG2_PUBLIC Record
*/
void addSuperRecord(Value *superValue);

/**
* Adds a new record to be used as a superclass of this record.
*
* @param superRecord Record to use as super record. A new RecordValue is
* created to refer to this record.
*/
void addSuperRecord(Record const &superRecord);

/**
* Adds a new native function to the record according to the specification.
*
Expand Down
14 changes: 14 additions & 0 deletions doomsday/sdk/libcore/include/de/scriptsys/scriptsystem.h
Expand Up @@ -108,8 +108,22 @@ class DENG2_PUBLIC ScriptSystem : public System
void timeChanged(Clock const &);

public:
/**
* Returns a built-in Doomsday Script class from the Core module.
* @param name Name of the class.
* @return Class record.
*/
static Record &builtInClass(String const &name);

/**
* Returns a built-in Doomsday Script class from the specified module.
* @param nativeModuleName Name of the module where the class is located.
* @param className Name of the class.
* @return Class record.
*/
static Record &builtInClass(String const &nativeModuleName,
String const &className);

static ScriptSystem &get();

private:
Expand Down
6 changes: 6 additions & 0 deletions doomsday/sdk/libcore/src/data/record.cpp
Expand Up @@ -44,6 +44,7 @@ int const SUBRECORD_CONTENT_EXCERPT_THRESHOLD = 100; // lines

String const Record::VAR_SUPER = "__super__";
String const Record::VAR_FILE = "__file__";
String const Record::VAR_NATIVE_SELF = "__self__";

/**
* Each record is given a unique identifier, so that serialized record
Expand Down Expand Up @@ -694,6 +695,11 @@ void Record::addSuperRecord(Value *superValue)
(*this)[VAR_SUPER].array().add(superValue);
}

void Record::addSuperRecord(Record const &superRecord)
{
addSuperRecord(new RecordValue(superRecord));
}

void Record::operator >> (Writer &to) const
{
to << d->uniqueId << duint32(d->members.size());
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/src/data/recordvalue.cpp
Expand Up @@ -257,7 +257,7 @@ void RecordValue::call(Process &process, Value const &arguments, Value *) const
// initialized as a member of the class.
QScopedPointer<RecordValue> instance(new RecordValue(new Record, RecordValue::OwnsRecord));

instance->record()->addSuperRecord(new RecordValue(d->record));
instance->record()->addSuperRecord(*d->record);

// If there is an initializer method, call it now.
if(dereference().hasMember("__init__"))
Expand Down
18 changes: 9 additions & 9 deletions doomsday/sdk/libcore/src/filesys/file.cpp
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>
*/

#include "de/File"
Expand Down Expand Up @@ -60,8 +60,8 @@ File::File(String const &fileName) : Node(fileName), d(new Instance)
d->source = this;

// Core.File provides the member functions for files.
d->info.addSuperRecord(new RecordValue(ScriptSystem::get().builtInClass("File")));
d->info.addSuperRecord(ScriptSystem::builtInClass("File"));

// Create the default set of info variables common to all files.
d->info.add(new Variable("name", new Accessor(*this, Accessor::NAME), Accessor::VARIABLE_MODE));
d->info.add(new Variable("path", new Accessor(*this, Accessor::PATH), Accessor::VARIABLE_MODE));
Expand Down Expand Up @@ -184,7 +184,7 @@ Feed *File::originFeed() const
{
return d->originFeed;
}

void File::setSource(File *source)
{
DENG2_GUARD(this);
Expand All @@ -195,8 +195,8 @@ void File::setSource(File *source)
delete d->source;
}
d->source = source;
}
}

File const *File::source() const
{
DENG2_GUARD(this);
Expand Down Expand Up @@ -234,7 +234,7 @@ void File::setStatus(Status const &status)
}
}

File::Status const &File::status() const
File::Status const &File::status() const
{
DENG2_GUARD(this);

Expand Down Expand Up @@ -396,13 +396,13 @@ void File::Accessor::update() const

// We need to alter the value content.
Accessor *nonConst = const_cast<Accessor *>(this);

switch(_prop)
{
case NAME:
nonConst->setValue(_owner.name());
break;

case PATH:
nonConst->setValue(_owner.path());
break;
Expand Down
9 changes: 7 additions & 2 deletions doomsday/sdk/libcore/src/scriptsys/scriptsystem.cpp
Expand Up @@ -287,8 +287,13 @@ File const &ScriptSystem::findModuleSource(String const &name, String const &loc

Record &ScriptSystem::builtInClass(String const &name)
{
return const_cast<Record &>(ScriptSystem::get().nativeModule("Core")
.getAs<RecordValue>(name).dereference());
return builtInClass(QStringLiteral("Core"), name);
}

Record &ScriptSystem::builtInClass(String const &nativeModuleName, String const &className)
{
return const_cast<Record &>(ScriptSystem::get().nativeModule(nativeModuleName)
.getr(className).dereference());
}

ScriptSystem &ScriptSystem::get()
Expand Down

0 comments on commit 1885e89

Please sign in to comment.