Skip to content

Commit

Permalink
changed exceptions to be able to handle objects as exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
bizzehdee committed Sep 28, 2020
1 parent e8b239f commit 6901dc8
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
.vs
obj
bin
*.zip
*.nupkg
13 changes: 10 additions & 3 deletions DScript.Demo/testscript.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
var MyClass = {
doSomethingComplicated: function (x, y) {
return Math.pow(--x, ++y) / 10.0;
this.doSomethingElse(x, y);
},

doSomethingElse: function (a,b) {

return Math.pow(--a, ++b) / 10.0;
}
};

Expand All @@ -23,10 +28,12 @@ console.log(p);
try {

var x = 1;
throw "this broke";
var obj = { message: "turd", doStuff: function () { } };

throw obj;
}
catch (ex) {
var pns = "Exception message: " + ex;
var pns = "Exception message: " + ex.message;
console.log(pns);
}

Expand Down
2 changes: 2 additions & 0 deletions DScript/DScript.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/bizzehdee/DScript</PackageProjectUrl>
<RepositoryUrl>https://github.com/bizzehdee/DScript</RepositoryUrl>
<Version>2.0.0</Version>
<Authors>Darren Horrocks</Authors>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 7 additions & 0 deletions DScript/JITException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,12 @@ public JITException(string message) :
{

}

public JITException(ScriptVar varObj)
{
VarObj = varObj;
}

public ScriptVar VarObj { get; }
}
}
4 changes: 3 additions & 1 deletion DScript/ScriptEngine.FunctionCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ private ScriptVarLink FunctionCall(ref bool execute, ScriptVarLink function, Scr

//callStack.PushBack(string.Format("{0} from line {1}", function.Name, currentLexer.LineNumber));

callStack.Push(function);

if (function.Var.IsNative)
{
var func = function.Var.GetCallback();
Expand Down Expand Up @@ -104,7 +106,7 @@ private ScriptVarLink FunctionCall(ref bool execute, ScriptVarLink function, Scr
}
}

//callStack.PopBack();
callStack.Pop();
scopes.PopBack();

var returnVar = new ScriptVarLink(returnVarLink.Var, null);
Expand Down
7 changes: 3 additions & 4 deletions DScript/ScriptEngine.Statement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,9 @@ private void Statement(ref bool execute)
var v = catchBlock?.Var?.FirstChild;
if(v != null)
{
catchScope.AddChild(v.Name, new ScriptVar(ex.Message));
catchScope.AddChild(v.Name, ex.VarObj);
}


var oldLex = currentLexer;
var newLex = new ScriptLex(catchBlock.Var.String);
currentLexer = newLex;
Expand Down Expand Up @@ -377,7 +376,7 @@ private void Statement(ref bool execute)
{
currentLexer.Match(ScriptLex.LexTypes.RThrow);

var message = string.Empty;
ScriptVar message = new ScriptVar();

if (currentLexer.TokenType == (ScriptLex.LexTypes)';')
{
Expand All @@ -387,7 +386,7 @@ private void Statement(ref bool execute)
{

var res = Base(ref execute);
message = res.Var.String;
message = res.Var;
}

throw new JITException(message);
Expand Down
16 changes: 8 additions & 8 deletions DScript/ScriptEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected virtual void Dispose(bool disposing)
private readonly ScriptVar objectClass;
private readonly ScriptVar arrayClass;
private List<ScriptVar> scopes;
//private List<string> callStack;
private Stack<ScriptVarLink> callStack;

private ScriptLex currentLexer;

Expand All @@ -77,7 +77,7 @@ public ScriptEngine()
currentLexer = null;

scopes = new List<ScriptVar>();
//callStack = new List<string>();
callStack = new Stack<ScriptVarLink>();

Root = (new ScriptVar(null, ScriptVar.Flags.Object)).Ref();

Expand All @@ -100,15 +100,16 @@ public void Execute(string code)
var oldLex = currentLexer;
var oldScopes = scopes;
scopes = new List<ScriptVar>();
//var oldCallStack = callStack;

scopes.Clear();
scopes.PushBack(Root);

var rootLink = new ScriptVarLink(Root, "root");

callStack.Push(rootLink);

using (currentLexer = new ScriptLex(code))
{
//callStack.Clear();

var execute = true;

while (currentLexer.TokenType != 0)
Expand All @@ -132,13 +133,12 @@ public void Execute(string code)
throw;
}
}


}

callStack.Pop();

currentLexer = oldLex;
scopes = oldScopes;
//callStack = oldCallStack;
}

public ScriptVarLink EvalComplex(string code)
Expand Down

0 comments on commit 6901dc8

Please sign in to comment.