diff --git a/src/OneScript.Core/Contexts/AttachedContext.cs b/src/OneScript.Core/Contexts/AttachedContext.cs index 981a645a2..0b5c4fc97 100644 --- a/src/OneScript.Core/Contexts/AttachedContext.cs +++ b/src/OneScript.Core/Contexts/AttachedContext.cs @@ -15,13 +15,18 @@ namespace OneScript.Contexts /// public sealed class AttachedContext { - private readonly IVariable[] _variables; - private readonly BslMethodInfo[] _methods; + private IVariable[] _variables; + private BslMethodInfo[] _methods; public AttachedContext(IAttachableContext source) { - source.OnAttach(out _variables, out _methods); Instance = source; + Attach(); + } + + public void Attach() + { + Instance.OnAttach(out _variables, out _methods); } public AttachedContext(IAttachableContext target, IVariable[] vars) diff --git a/src/ScriptEngine.HostedScript/LibraryLoader.cs b/src/ScriptEngine.HostedScript/LibraryLoader.cs index 505250d86..a01075c75 100644 --- a/src/ScriptEngine.HostedScript/LibraryLoader.cs +++ b/src/ScriptEngine.HostedScript/LibraryLoader.cs @@ -113,6 +113,7 @@ public void AddModule(string file, string moduleName) $"en = 'Load module ={moduleName}= in to context from file {file}'") ); _env.InjectGlobalProperty(null, moduleName, true); + MachineInstance.Current.UpdateGlobals(); } catch (InvalidOperationException e) { diff --git a/src/ScriptEngine/Machine/MachineInstance.cs b/src/ScriptEngine/Machine/MachineInstance.cs index c85aadf00..e342cf4aa 100644 --- a/src/ScriptEngine/Machine/MachineInstance.cs +++ b/src/ScriptEngine/Machine/MachineInstance.cs @@ -69,6 +69,11 @@ public void SetMemory(ExecutionContext memory) _globalContexts = _mem.GlobalNamespace.AttachedContexts.Select(x => new AttachedContext(x)) .ToArray(); } + + public void UpdateGlobals() + { + _globalContexts.ForEach(x => x.Attach()); + } public bool IsRunning => _callStack.Count != 0;