77 lines
1.5 KiB
C++
77 lines
1.5 KiB
C++
|
/*
|
||
|
* Context.cpp
|
||
|
*
|
||
|
* Created on: Dec 7, 2012
|
||
|
* Author: attero
|
||
|
*/
|
||
|
|
||
|
#include "ContextManager.h"
|
||
|
|
||
|
ContextManager::ContextManager() {
|
||
|
index = 0;
|
||
|
}
|
||
|
|
||
|
ContextManager::~ContextManager() {
|
||
|
}
|
||
|
|
||
|
void ContextManager::register_function(std::string name, PointerToNativeFunction f)
|
||
|
{
|
||
|
registered_functions[name] = f;
|
||
|
}
|
||
|
|
||
|
SenchaObject ContextManager::execute_native_function(std::string name, std::vector<ASTExpression *> arguments)
|
||
|
{
|
||
|
SenchaObject result;
|
||
|
if(registered_functions.count(name) == 1)
|
||
|
result = registered_functions[name](arguments);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
ObjectIndex ContextManager::add_to_store(SenchaObject & object)
|
||
|
{
|
||
|
index++;
|
||
|
object_store[index] = object;
|
||
|
|
||
|
return index;
|
||
|
}
|
||
|
|
||
|
SenchaObject ContextManager::get_from_store(ObjectIndex index)
|
||
|
{
|
||
|
return object_store[index];
|
||
|
}
|
||
|
|
||
|
void ContextManager::add(std::string name, SenchaObject object)
|
||
|
{
|
||
|
interpreter_context[name] = add_to_store(object);
|
||
|
}
|
||
|
|
||
|
void ContextManager::set(std::string name, SenchaObject object)
|
||
|
{
|
||
|
if(interpreter_context[name] != 0)
|
||
|
{
|
||
|
object_store[interpreter_context[name]] = object;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
add(name, object);
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
SenchaObject ContextManager::get(std::string name)
|
||
|
{
|
||
|
return get_from_store(interpreter_context[name]);
|
||
|
}
|
||
|
|
||
|
std::string ContextManager::debug() {
|
||
|
std::string debug_note = "";
|
||
|
|
||
|
for( auto iter = this->interpreter_context.begin(); iter != this->interpreter_context.end(); iter++)
|
||
|
{
|
||
|
debug_note += "Context: " + to_string((*iter).second) + ": " + (*iter).first + " " + object_store[(*iter).second].repr() + "\n";
|
||
|
}
|
||
|
return debug_note;
|
||
|
|
||
|
|
||
|
}
|