Skip to content

Commit

Permalink
Fixed|Scripting: Functions' global namespaces
Browse files Browse the repository at this point in the history
Each function remembers the global namespace it was defined in, in case the function accesses those globals when it's run.
  • Loading branch information
skyjake committed Nov 24, 2019
1 parent 748f9dd commit a6e1c03
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/src/scriptsys/function.cpp
Expand Up @@ -331,7 +331,7 @@ void Function::recordBeingDeleted(Record &DENG2_DEBUG_ONLY(record))
// The namespace of the record is being deleted.
DENG2_ASSERT(d->globals == &record);

d->globals = 0;
d->globals = nullptr;
}

void Function::registerNativeEntryPoint(String const &name, Function::NativeEntryPoint entryPoint)
Expand Down
3 changes: 2 additions & 1 deletion doomsday/sdk/libcore/src/scriptsys/functionstatement.cpp
Expand Up @@ -63,7 +63,8 @@ void FunctionStatement::execute(Context &context) const
Evaluator &eval = context.evaluator();

// Set the function's namespace.
_function->setGlobals(&context.process().globals());
Record &globals = context.process().globals();
_function->setGlobals(&globals);

// Variable that will store the function.
eval.evaluateTo<RefValue>(_identifier);
Expand Down
19 changes: 13 additions & 6 deletions doomsday/sdk/libcore/src/scriptsys/process.cpp
Expand Up @@ -106,23 +106,21 @@ DENG2_PIMPL(Process)

/// Fast forward to a suitable catch statement for @a err.
/// @return @c true, if suitable catch statement found.
bool jumpIntoCatch(Error const &err)
bool jumpIntoCatch(const Error &err)
{
dint level = 0;

// Proceed along default flow.
for (context().proceed(); context().current(); context().proceed())
{
Statement const *statement = context().current();
TryStatement const *tryStatement = dynamic_cast<TryStatement const *>(statement);
if (tryStatement)
const Statement *statement = context().current();
if (const auto *tryStatement = dynamic_cast<const TryStatement *>(statement))
{
// Encountered a nested try statement.
++level;
continue;
}
CatchStatement const *catchStatement = dynamic_cast<CatchStatement const *>(statement);
if (catchStatement)
if (const auto *catchStatement = dynamic_cast<const CatchStatement *>(statement))
{
if (!level)
{
Expand Down Expand Up @@ -437,6 +435,15 @@ void Process::namespaces(Namespaces &spaces) const

Record &Process::globals()
{
// Find the global namespace currently in effect.
DENG2_FOR_EACH_CONST_REVERSE(Impl::ContextStack, i, d->stack)
{
Context &context = **i;
if (context.type() == Context::GlobalNamespace || context.type() == Context::BaseProcess)
{
return context.names();
}
}
return d->stack[0]->names();
}

Expand Down

0 comments on commit a6e1c03

Please sign in to comment.