forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimeEnvironment.cs
More file actions
147 lines (127 loc) · 4.5 KB
/
RuntimeEnvironment.cs
File metadata and controls
147 lines (127 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScriptEngine.Compiler;
using ScriptEngine.Environment;
using ScriptEngine.Machine;
namespace ScriptEngine
{
public class RuntimeEnvironment
{
private readonly List<IAttachableContext> _objects = new List<IAttachableContext>();
private readonly CompilerContext _symbolScopes = new CompilerContext();
private SymbolScope _globalScope;
private PropertyBag _injectedProperties;
private readonly List<UserAddedScript> _externalScripts = new List<UserAddedScript>();
public void InjectObject(IAttachableContext context)
{
InjectObject(context, false);
}
public void InjectObject(IAttachableContext context, bool asDynamicScope)
{
RegisterSymbolScope(context, asDynamicScope);
RegisterObject(context);
}
public void InjectGlobalProperty(IValue value, string identifier, bool readOnly)
{
if(!Utils.IsValidIdentifier(identifier))
{
throw new ArgumentException("Invalid identifier", "identifier");
}
if (_globalScope == null)
{
_globalScope = new SymbolScope();
TypeManager.RegisterType("__globalPropertiesHolder", typeof(PropertyBag));
_injectedProperties = new PropertyBag();
_symbolScopes.PushScope(_globalScope);
RegisterObject(_injectedProperties);
}
_globalScope.DefineVariable(identifier, SymbolType.ContextProperty);
_injectedProperties.Insert(value, identifier, true, !readOnly);
}
public void SetGlobalProperty(string propertyName, IValue value)
{
int propId = _injectedProperties.FindProperty(propertyName);
_injectedProperties.SetPropValue(propId, value);
}
internal CompilerContext SymbolsContext
{
get
{
return _symbolScopes;
}
}
internal IList<IAttachableContext> AttachedContexts
{
get
{
return _objects;
}
}
public void NotifyClassAdded(ScriptModuleHandle module, string symbol)
{
_externalScripts.Add(new UserAddedScript()
{
Type = UserAddedScriptType.Class,
Symbol = symbol,
Module = module
});
}
public void NotifyModuleAdded(ScriptModuleHandle module, string symbol)
{
_externalScripts.Add(new UserAddedScript()
{
Type = UserAddedScriptType.Module,
Symbol = symbol,
Module = module
});
}
public IEnumerable<UserAddedScript> GetUserAddedScripts()
{
return _externalScripts;
}
private void RegisterSymbolScope(IReflectableContext provider, bool asDynamicScope)
{
var scope = new SymbolScope();
scope.IsDynamicScope = asDynamicScope;
_symbolScopes.PushScope(scope);
foreach (var item in provider.GetProperties())
{
if (item.Type == SymbolType.Variable)
{
_symbolScopes.DefineVariable(item.Identifier);
}
else
{
_symbolScopes.DefineProperty(item.Identifier);
}
}
foreach (var item in provider.GetMethods())
{
_symbolScopes.DefineMethod(item);
}
}
private void RegisterObject(IAttachableContext context)
{
_objects.Add(context);
}
}
public struct UserAddedScript
{
public UserAddedScriptType Type;
public ScriptModuleHandle Module;
public string Symbol;
}
public enum UserAddedScriptType
{
Module,
Class
}
}