Skip to content

Commit

Permalink
Merge pull request #10 from nibblebits/dev
Browse files Browse the repository at this point in the history
Network Changes + Bug Fix
  • Loading branch information
nibblebits authored Aug 6, 2019
2 parents c8f3acd + 91c44f8 commit 563cd7a
Show file tree
Hide file tree
Showing 38 changed files with 1,492 additions and 79 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.z
*.o
*.so
/.vscode
/bin
/docs
/debs
/debs
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SYSTEM_OBJECT_FILES = ./build/system/token.o ./build/system/splitter.o ./build/system/interpreter.o ./build/system/lexer.o ./build/system/parser.o ./build/system/node.o ./build/system/expnode.o ./build/system/varnode.o ./build/system/literalnode.o ./build/system/identifiernode.o ./build/system/debug.o ./build/system/scope.o ./build/system/variable.o ./build/system/fcnode.o ./build/system/keywordnode.o ./build/system/nodefactory.o ./build/system/tokenfactory.o ./build/system/einode.o ./build/system/value.o ./build/system/function.o ./build/system/nativefunction.o ./build/system/functionsystem.o ./build/system/stringnode.o ./build/system/negnode.o ./build/system/statement.o ./build/system/ifstmtnode.o ./build/system/bodynode.o ./build/system/castnode.o ./build/system/arraynode.o ./build/system/newnode.o ./build/system/array.o ./build/system/object.o ./build/system/logger.o ./build/system/posinfo.o ./build/system/class.o ./build/system/csystem.o ./build/system/fnode.o ./build/system/inode.o ./build/system/writtenfunction.o ./build/system/retnode.o ./build/system/classnode.o ./build/system/validator.o ./build/system/scopehandler.o ./build/system/systemhandler.o ./build/system/evaluatingnode.o ./build/system/datatype.o ./build/system/groupedfunction.o ./build/system/singlefunction.o ./build/system/vartype.o ./build/system/exceptions/systemexception.o ./build/system/exceptions/breakexception.o ./build/system/trynode.o ./build/system/thrownode.o ./build/system/exceptionobject.o ./build/system/whilenode.o ./build/system/breaknode.o ./build/system/continuenode.o ./build/system/dowhilenode.o ./build/system/fornode.o ./build/system/listnode.o ./build/system/modulesystem.o ./build/system/module.o ./build/system/includenode.o ./build/system/purefunction.o ./build/system/includeoncenode.o ./build/system/misc.o ./build/system/permissionobject.o ./build/system/permissionsobject.o ./build/system/permissionnode.o ./build/system/permissionpropertyobject.o ./build/system/basesystemhandler.o ./build/system/modulehandlingpermissionobject.o ./build/system/nullnode.o ./build/system/requirenode.o ./build/system/booleanliteralnode.o ./build/system/operator.o ./build/system/outputnode.o ./build/system/limitscopenode.o ./build/system/filepermission.o ./build/system/outputfilternode.o ./build/system/notnode.o
SYSTEM_OBJECT_FILES = ./build/system/token.o ./build/system/splitter.o ./build/system/interpreter.o ./build/system/lexer.o ./build/system/parser.o ./build/system/node.o ./build/system/expnode.o ./build/system/varnode.o ./build/system/literalnode.o ./build/system/identifiernode.o ./build/system/debug.o ./build/system/scope.o ./build/system/variable.o ./build/system/referencevariable.o ./build/system/fcnode.o ./build/system/keywordnode.o ./build/system/nodefactory.o ./build/system/tokenfactory.o ./build/system/einode.o ./build/system/value.o ./build/system/function.o ./build/system/nativefunction.o ./build/system/functionsystem.o ./build/system/stringnode.o ./build/system/negnode.o ./build/system/statement.o ./build/system/ifstmtnode.o ./build/system/bodynode.o ./build/system/castnode.o ./build/system/arraynode.o ./build/system/newnode.o ./build/system/array.o ./build/system/object.o ./build/system/logger.o ./build/system/posinfo.o ./build/system/class.o ./build/system/csystem.o ./build/system/fnode.o ./build/system/inode.o ./build/system/writtenfunction.o ./build/system/retnode.o ./build/system/classnode.o ./build/system/validator.o ./build/system/scopehandler.o ./build/system/systemhandler.o ./build/system/evaluatingnode.o ./build/system/datatype.o ./build/system/groupedfunction.o ./build/system/singlefunction.o ./build/system/vartype.o ./build/system/exceptions/systemexception.o ./build/system/exceptions/breakexception.o ./build/system/trynode.o ./build/system/thrownode.o ./build/system/exceptionobject.o ./build/system/whilenode.o ./build/system/breaknode.o ./build/system/continuenode.o ./build/system/dowhilenode.o ./build/system/fornode.o ./build/system/listnode.o ./build/system/modulesystem.o ./build/system/module.o ./build/system/includenode.o ./build/system/purefunction.o ./build/system/includeoncenode.o ./build/system/misc.o ./build/system/permissionobject.o ./build/system/permissionsobject.o ./build/system/permissionnode.o ./build/system/permissionpropertyobject.o ./build/system/basesystemhandler.o ./build/system/modulehandlingpermissionobject.o ./build/system/nullnode.o ./build/system/requirenode.o ./build/system/booleanliteralnode.o ./build/system/operator.o ./build/system/outputnode.o ./build/system/limitscopenode.o ./build/system/shellpermission.o ./build/system/filepermission.o ./build/system/outputfilternode.o ./build/system/notnode.o
EXTERNAL_OBJECT_FILES = ./src/commonmod/bin/marble_commonmod.o
APACHE_OBJECT_FILES = ./src/apache/build/webmod.o ./src/apache/build/responseobject.o ./src/apache/build/cookiepermission.o ./src/apache/build/headerpermission.o ./src/apache/build/requestargumentsobject.o ./src/apache/build/multipartfileobject.o ./src/apache/build/requestobject.o
OBJECT_FILE_FLAGS = -c -g -fPIC -std=c++14 -g
Expand Down Expand Up @@ -49,6 +49,8 @@ system: ${SYSTEM_OBJECT_FILES}
g++ -I ${INCLUDES} ./src/system/scope.cpp -o ./build/system/scope.o ${OBJECT_FILE_FLAGS}
./build/system/variable.o: ./src/system/variable.cpp
g++ -I ${INCLUDES} ./src/system/variable.cpp -o ./build/system/variable.o ${OBJECT_FILE_FLAGS}
./build/system/referencevariable.o: ./src/system/referencevariable.cpp
g++ -I ${INCLUDES} ./src/system/referencevariable.cpp -o ./build/system/referencevariable.o ${OBJECT_FILE_FLAGS}
./build/system/keywordnode.o: ./src/system/keywordnode.cpp
g++ -I ${INCLUDES} ./src/system/keywordnode.cpp -o ./build/system/keywordnode.o ${OBJECT_FILE_FLAGS}
./build/system/fcnode.o: ./src/system/fcnode.cpp
Expand Down Expand Up @@ -177,6 +179,8 @@ system: ${SYSTEM_OBJECT_FILES}
g++ -I ${INCLUDES} ./src/system/limitscopenode.cpp -o ./build/system/limitscopenode.o ${OBJECT_FILE_FLAGS}
./build/system/filepermission.o: ./src/system/filepermission.cpp
g++ -I ${INCLUDES} ./src/system/filepermission.cpp -o ./build/system/filepermission.o ${OBJECT_FILE_FLAGS}
./build/system/shellpermission.o: ./src/system/shellpermission.cpp
g++ -I ${INCLUDES} ./src/system/shellpermission.cpp -o ./build/system/shellpermission.o ${OBJECT_FILE_FLAGS}
./build/system/outputfilternode.o: ./src/system/outputfilternode.cpp
g++ -I ${INCLUDES} ./src/system/outputfilternode.cpp -o ./build/system/outputfilternode.o ${OBJECT_FILE_FLAGS}
./build/system/notnode.o: ./src/system/notnode.cpp
Expand Down
2 changes: 2 additions & 0 deletions config_files/permissionconfig.marble
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Permission networkPermission = new @NetworkPermission();
networkPermission.setCanSendEmail(true);
networkPermission.setCanUseCurl(true);
networkPermission.setCanLookup(true);
networkPermission.setCanSocket(true);
networkPermission.setCanSocketServer(true);
allowed_permissions.add(networkPermission);

Permission timeoutPermission = new @TimeoutPermission();
Expand Down
267 changes: 267 additions & 0 deletions include/acsp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
/*
Marble scripting language interpreter
Copyright (C) 2018 Daniel McCarthy
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

/*
Anti Circle Shared Pointer is a shared pointer implementation that can break out of circular references
*/
#ifndef ACSP_H
#define ACSP_H

#include <stdio.h>
#include <iostream>
#include <map>
#include <memory>
#include <exception>

class RC
{
private:
int count; // Reference count
int stuck_count; // The total cycles we are stuck in
public:
void markStuck()
{
stuck_count += 1;
}

void markUnstuck()
{
stuck_count -= 1;
}

bool isStuck()
{
return stuck_count > 0;
}

int getStuckCount()
{
return stuck_count;
}

void AddRef()
{
// Increment the reference count
count++;
}

int Release()
{
// Decrement the reference count and
// return the reference count.
int c = --count;
if (c == stuck_count)
{
count = 0;
return count;
}
return c;
}

int getRef()
{
return this->count;
}
};

template <typename T>
class ACSP
{
private:
T *pData; // pointer
std::shared_ptr<RC> reference; // Reference count
std::shared_ptr<std::map<T *, int>> circle; // The referencing circle
protected:
void createCircle()
{
if (circle != NULL)
throw std::logic_error("Circle is not NULL");
circle = std::make_shared<std::map<T *, int>>();
addToCircle(pData);
}

bool hasInCircle(T* pValue)
{
return ((*circle).find(pValue) != (*circle).end());
}

void addToCircle(T *pValue, int count_to_add = 1, bool exactly=false)
{
// Ignore NULL's
if (pValue == NULL)
return;

if (exactly)
{
(*circle)[pValue] = count_to_add;
return;
}

int total_count = 0;
if (hasInCircle(pValue))
{
total_count = (*circle)[pValue];
}

total_count += count_to_add;
(*circle)[pValue] = total_count;
}


void decrementCircleElement(T *pValue)
{
(*circle)[pValue] = (*circle)[pValue] - 1;
if ((*circle)[pValue] <= 0)
{
(*circle).erase(pValue);
}

if (reference->getRef() == 1 && (*circle).size() > 1)
{
// throw std::logic_error("Circular reference problem detected");
}
}

public:
std::shared_ptr<std::map<T *, int>> getCircle()
{
return circle;
}

ACSP() : pData(0), reference(0)
{
// Create inner circle
createCircle();
// Create a new reference
reference = std::make_shared<RC>();
// Increment the reference count
reference->AddRef();
}

ACSP(T *pValue) : pData(pValue), reference(0)
{
// Create inner circle
createCircle();

// Create a new reference
reference = std::make_shared<RC>();
// Increment the reference count
reference->AddRef();
}

ACSP(const ACSP<T>&sp) : pData(sp.pData), reference(sp.reference), circle(sp.circle)
{
// Copy constructor
// Copy the data and reference pointer
// and increment the reference count
reference->AddRef();
}

~ACSP()
{
// Destructor
// Decrement the reference count
// if reference become zero delete the data

int ref_count = reference->Release();
decrementCircleElement(pData);

if (ref_count == 0)
{
delete pData;
}

}

T *get()
{
return pData;
}

T &operator*()
{
return *pData;
}

T *operator->()
{
return pData;
}

RC* getReference()
{
return this->reference.get();
}

ACSP<T>&operator=(const ACSP<T>&sp)
{
// Assignment operator
if (this != &sp) // Avoid self assignment
{
// Decrement the old reference count
// if reference become zero delete the old data
int ref_count = reference->Release();
decrementCircleElement(pData);
if (ref_count == 0)
{
delete pData;
}

// Copy the data and reference pointer
// and increment the reference count
pData = sp.pData;
reference = sp.reference;
circle = sp.circle;
reference->AddRef();
}
return *this;
}

static void CircleMerge(ACSP<T>&dst, ACSP<T>&other)
{
bool is_stuck = false;
if (other.hasInCircle(dst.get()))
{
dst.getReference()->markStuck();
other.getReference()->markStuck();
}

for (auto const &x : *other.circle)
{
dst.addToCircle(x.first, x.second, true);
}

other.circle = dst.circle;
}

static void CircleRelease(ACSP<T>&dst, ACSP<T>&other)
{
if (other.hasInCircle(dst.get()))
{
dst.getReference()->markUnstuck();
other.getReference()->markUnstuck();
}

for (auto const &x : *other.circle)
{
dst.decrementCircleElement(x.first);
}
}
};
#endif
5 changes: 5 additions & 0 deletions include/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class Array : public Object
Array(Class* c);
Array(Class* c, Variable* variables, int count);
virtual ~Array();

static Class* registerClass(SystemHandler* systemHandler);
static void Array_toString(Interpreter* interpreter, std::vector<Value> values, Value* return_value, std::shared_ptr<Object> object, Scope* caller_scope);
static void Array_size(Interpreter* interpreter, std::vector<Value> values, Value* return_value, std::shared_ptr<Object> object, Scope* caller_scope);

virtual void setup();
Variable* variables;
int count;
Expand Down
2 changes: 1 addition & 1 deletion include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

// Marble versioning information
#define MARBLE_MAJOR_CODENAME "Clearies"
#define MARBLE_VERSION "0.7.0"
#define MARBLE_VERSION "0.10.0"

#define MAX_KEYWORD_SIZE 15
#define MAX_OPERATORS_SIZE 3
Expand Down
42 changes: 42 additions & 0 deletions include/referencevariable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Marble scripting language interpreter
Copyright (C) 2018 Daniel McCarthy
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifndef REFERENCEVARIABLE_H
#define REFERENCEVARIABLE_H
#include <string>
#include <memory>
#include "value.h"
#include "variable.h"
class Scope;
class Object;
class ReferenceVariable : public Variable
{
public:
ReferenceVariable(Variable* variable_to_ref, int ref_index=-1);
virtual ~ReferenceVariable();

virtual void setValue(Value value, Interpreter* interpreter);
bool isReferencingIndex();

// The index this reference variable is pointing to in the string or array
int ref_index;

Variable* variable_to_ref;
};
#endif
8 changes: 8 additions & 0 deletions include/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <string>
#include <memory>
#include "variable.h"
#include "referencevariable.h"
#include "statics.h"
class Object;
class PermissionsObject;
Expand All @@ -38,6 +39,13 @@ class Scope
Scope(std::shared_ptr<PermissionsObject> permissions);
virtual ~Scope();
virtual void registerVariable(Variable* variable);
/**
* Warning you must ensure that the reference variable scope is compatible with the variable scope provided
* otherwise this can result in the memory being freed and it referencing deleted memory
*
* if a -1 index is provided then this reference will not be binded to an array or string index
*/
ReferenceVariable* createReferenceVariable(Variable* variable, int index_to_ref=-1);
Variable* createVariable();
Variable* createVariable(std::string name, double value, MODIFIER_ACCESS modifier_access = MODIFIER_ACCESS_PUBLIC);
Variable* createVariable(std::string name, std::string type, std::shared_ptr<Object> value, MODIFIER_ACCESS modifier_access = MODIFIER_ACCESS_PUBLIC);
Expand Down
File renamed without changes.
Loading

0 comments on commit 563cd7a

Please sign in to comment.