Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/kOS.Safe.Test/Execution/BaseIntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void Setup()
shared.UpdateHandler = new UpdateHandler();
shared.VolumeMgr = new VolumeManager();

shared.FunctionManager.Load();
shared.FunctionManager.Load(new string[] { "ksp" });

Archive archive = new Archive(baseDir);
shared.VolumeMgr.Add(archive);
Expand Down
2 changes: 1 addition & 1 deletion src/kOS.Safe.Test/Opcode/FakeCpu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public Compilation.Opcode GetOpcodeAt(int instructionPtr)
throw new NotImplementedException();
}

public void Boot()
public void Boot(string[] contexts)
{
throw new NotImplementedException();
}
Expand Down
2 changes: 1 addition & 1 deletion src/kOS.Safe/Binding/IBindingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace kOS.Safe.Binding
{
public interface IBindingManager
{
void Load();
void Load(string[] contexts);
void AddBoundVariable(string name, BindingGetDlg getDelegate, BindingSetDlg setDelegate);
void AddGetter(string name, BindingGetDlg dlg);
void AddGetter(IEnumerable<string> names, BindingGetDlg dlg);
Expand Down
2 changes: 1 addition & 1 deletion src/kOS.Safe/Encapsulation/Lexicon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace kOS.Safe.Encapsulation
[kOS.Safe.Utilities.KOSNomenclature("Lex", CSharpToKOS = false) ]
public class Lexicon : SerializableStructure, IDictionary<Structure, Structure>, IIndexable
{
[Function("lex", "lexicon")]
[Function("lex", "lexicon", Contexts=new string[] { "ksp", "archive" })]
public class FunctionLexicon : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down
2 changes: 1 addition & 1 deletion src/kOS.Safe/Encapsulation/ListValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void Insert(int index, T item)
[kOS.Safe.Utilities.KOSNomenclature("List", KOSToCSharp = false)] // one-way because the generic templated ListValue<T> is the canonical one.
public class ListValue : ListValue<Structure>
{
[Function("list")]
[Function("list", Contexts=new string[] { "ksp", "archive" })]
public class FunctionList : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down
2 changes: 1 addition & 1 deletion src/kOS.Safe/Encapsulation/PIDLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace kOS.Safe.Encapsulation
[kOS.Safe.Utilities.KOSNomenclature("PIDLoop")]
public class PIDLoop : SerializableStructure
{
[Function("pidloop")]
[Function("pidloop", Contexts=new string[] { "ksp", "archive" })]
public class PIDLoopConstructor : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down
2 changes: 1 addition & 1 deletion src/kOS.Safe/Encapsulation/QueueValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static QueueValue<T> CreateQueue<TU>(IEnumerable<TU> list)
[kOS.Safe.Utilities.KOSNomenclature("Queue", KOSToCSharp = false)] // one-way because the generic templated QueueValue<T> is the canonical one.
public class QueueValue : QueueValue<Structure>
{
[Function("queue")]
[Function("queue", Contexts=new string[] { "ksp", "archive" })]
public class FunctionQueue : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down
2 changes: 1 addition & 1 deletion src/kOS.Safe/Encapsulation/StackValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static StackValue<T> CreateStack<TU>(IEnumerable<TU> list)
[kOS.Safe.Utilities.KOSNomenclature("Stack", KOSToCSharp = false)] // one-way because the generic templated StackValue<T> is the canonical one.
public class StackValue : StackValue<Structure>
{
[Function("stack")]
[Function("stack", Contexts=new string[] { "ksp", "archive" })]
public class FunctionStack : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down
2 changes: 1 addition & 1 deletion src/kOS.Safe/Encapsulation/UniqueSetValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void SetInitializeSuffixes()
[kOS.Safe.Utilities.KOSNomenclature("UniqueSet", KOSToCSharp = false)] // one-way because the generic templated UniqueSetValue<T> is the canonical one.
public class UniqueSetValue : UniqueSetValue<Structure>
{
[Function("uniqueset")]
[Function("uniqueset", Contexts=new string[] { "ksp", "archive" })]
public class FunctionSet : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down
13 changes: 9 additions & 4 deletions src/kOS.Safe/Execution/CPU.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ public CPU(SafeSharedObjects shared)
popContextNotifyees = new List<WeakReference>();
}

public void Boot()
public void Boot(string[] ctxs = null)
{
if (ctxs == null)
ctxs = new string[] { "ksp" };
// break all running programs
currentContext = null;
contexts.Clear();
Expand All @@ -107,9 +109,9 @@ public void Boot()
// clear interpreter
if (shared.Interpreter != null) shared.Interpreter.Reset();
// load functions
if (shared.FunctionManager != null) shared.FunctionManager.Load();
if (shared.FunctionManager != null) shared.FunctionManager.Load(ctxs);
// load bindings
if (shared.BindingMgr != null) shared.BindingMgr.Load();
if (shared.BindingMgr != null) shared.BindingMgr.Load(ctxs);

// Booting message
if (shared.Screen != null)
Expand Down Expand Up @@ -400,6 +402,8 @@ public IUserDelegate MakeUserDelegate(int entryPoint, bool withClosure)
// only two contexts exist now, one for the interpreter and one for the programs
public IProgramContext GetInterpreterContext()
{
if (contexts.Count == 0)
return null;
return contexts[0];
}

Expand Down Expand Up @@ -507,7 +511,8 @@ public void BreakExecution(bool manual)
{
PopFirstContext();
shared.Screen.Print("Program aborted.");
shared.SoundMaker.StopAllVoices(); // stop voices if execution was manually broken, but not if the program ends normally
if (shared.SoundMaker != null)
shared.SoundMaker.StopAllVoices(); // stop voices if execution was manually broken, but not if the program ends normally
PrintStatistics();
stack.Clear();
}
Expand Down
2 changes: 1 addition & 1 deletion src/kOS.Safe/Execution/ICpu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public interface ICpu : IFixedUpdateObserver
void RemovePopContextNotifyee(IPopContextNotifyee notifyee);
Opcode GetCurrentOpcode();
Opcode GetOpcodeAt(int instructionPtr);
void Boot();
void Boot(string[] contexts = null);
int InstructionsThisUpdate { get; }
void StartCompileStopwatch();
void StopCompileStopwatch();
Expand Down
4 changes: 3 additions & 1 deletion src/kOS.Safe/Function/FunctionAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System;
using System;

namespace kOS.Safe.Function
{
public class FunctionAttribute : Attribute
{
public string[] Names { get; set; }
public string[] Contexts { get; set; }

public FunctionAttribute(params string[] names)
{
Names = names;
Contexts = new string[] { "ksp" };
}
}
}
7 changes: 4 additions & 3 deletions src/kOS.Safe/Function/FunctionManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using kOS.Safe.Utilities;
using kOS.Safe.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -16,16 +16,17 @@ public class FunctionManager : IFunctionManager
public FunctionManager(SafeSharedObjects shared)
{
this.shared = shared;
Load();
Load(new string[] { "ksp" });
}

public void Load()
public void Load(string[] contexts)
{
functions = new Dictionary<string, SafeFunctionBase>(StringComparer.OrdinalIgnoreCase);
foreach (FunctionAttribute attr in rawAttributes.Keys)
{
var type = rawAttributes[attr];
if (attr == null || type == null) continue;
if (attr.Contexts.Any() && !attr.Contexts.Intersect(contexts).Any()) continue;
object functionObject = Activator.CreateInstance(type);
foreach (string functionName in attr.Names)
{
Expand Down
2 changes: 1 addition & 1 deletion src/kOS.Safe/Function/IFunctionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace kOS.Safe.Function
{
public interface IFunctionManager
{
void Load();
void Load(string[] contexts);
void CallFunction(string functionName);
bool Exists(string functionName);
}
Expand Down
28 changes: 14 additions & 14 deletions src/kOS.Safe/Function/Math.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace kOS.Safe.Function
{
[Function("abs")]
[Function("abs", Contexts = new string[] { "ksp", "archive" })]
public class FunctionAbs : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand All @@ -16,7 +16,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("mod")]
[Function("mod", Contexts = new string[] { "ksp", "archive" })]
public class FunctionMod : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand All @@ -29,7 +29,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("floor")]
[Function("floor", Contexts = new string[] { "ksp", "archive" })]
public class FunctionFloor : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand All @@ -44,7 +44,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("ceiling")]
[Function("ceiling", Contexts = new string[] { "ksp", "archive" })]
public class FunctionCeiling : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand All @@ -59,7 +59,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("round")]
[Function("round", Contexts = new string[] { "ksp", "archive" })]
public class FunctionRound : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand All @@ -86,7 +86,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("sqrt")]
[Function("sqrt", Contexts = new string[] { "ksp", "archive" })]
public class FunctionSqrt : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand All @@ -99,7 +99,7 @@ public override void Execute(SafeSharedObjects shared)
}


[Function("ln")]
[Function("ln", Contexts = new string[] { "ksp", "archive" })]
public class FunctionLn : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand All @@ -111,7 +111,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("log10")]
[Function("log10", Contexts = new string[] { "ksp", "archive" })]
public class FunctionLog10 : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand All @@ -123,7 +123,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("min")]
[Function("min", Contexts = new string[] { "ksp", "archive" })]
public class FunctionMin : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down Expand Up @@ -153,7 +153,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("max")]
[Function("max", Contexts = new string[] { "ksp", "archive" })]
public class FunctionMax : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down Expand Up @@ -183,7 +183,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("random")]
[Function("random", Contexts = new string[] { "ksp", "archive" })]
public class FunctionRandom : SafeFunctionBase
{
private readonly Random random = new Random();
Expand All @@ -201,7 +201,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("randomseed")]
[Function("randomseed", Contexts = new string[] { "ksp", "archive" })]
public class FunctionRandomSeed : SafeFunctionBase
{
private readonly Random random = new Random();
Expand All @@ -215,7 +215,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("char")]
[Function("char", Contexts = new string[] { "ksp", "archive" })]
public class FunctionChar : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand All @@ -227,7 +227,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("unchar")]
[Function("unchar", Contexts = new string[] { "ksp", "archive" })]
public class FunctionUnchar : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down
18 changes: 9 additions & 9 deletions src/kOS.Safe/Function/Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace kOS.Safe.Function
{
[Function("print")]
[Function("print", Contexts = new string[] { "ksp", "archive" })]
public class FunctionPrint : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand All @@ -21,7 +21,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("printat")]
[Function("printat", Contexts = new string[] { "ksp", "archive" })]
public class FunctionPrintAt : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down Expand Up @@ -57,7 +57,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("run")]
[Function("run", Contexts = new string[] { "ksp", "archive" })]
public class FunctionRun : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down Expand Up @@ -128,7 +128,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[FunctionAttribute("load")]
[FunctionAttribute("load", Contexts = new string[] { "ksp", "archive" })]
public class FunctionLoad : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down Expand Up @@ -251,7 +251,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("logfile")]
[Function("logfile", Contexts = new string[] { "ksp", "archive" })]
public class FunctionLogFile : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down Expand Up @@ -289,7 +289,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("reboot")]
[Function("reboot", Contexts = new string[] { "ksp", "archive" })]
public class FunctionReboot : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand All @@ -315,7 +315,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("debugdump")]
[Function("debugdump", Contexts = new string[] { "ksp", "archive" })]
public class DebugDump : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down Expand Up @@ -372,7 +372,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("makebuiltindelegate")]
[Function("makebuiltindelegate", Contexts = new string[] { "ksp", "archive" })]
public class MakeBuiltinDelegate : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand All @@ -384,7 +384,7 @@ public override void Execute(SafeSharedObjects shared)
}
}

[Function("droppriority")]
[Function("droppriority", Contexts = new string[] { "ksp", "archive" })]
public class AllowInterrupt : SafeFunctionBase
{
public override void Execute(SafeSharedObjects shared)
Expand Down
Loading