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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Microsoft Public License. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Microsoft Public License, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Microsoft Public License.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Runtime.Remoting;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Dynamic;
using Microsoft.Scripting.Runtime;
using Microsoft.Scripting.Utils;
using AstUtils = Microsoft.Scripting.Ast.Utils;
#if !SYSTEM_CORE
using dynamic = System.Object;
#endif
namespace Microsoft.Scripting.Hosting {
/// <summary>
/// A ScriptScope is a unit of execution for code. It consists of a global Scope which
/// all code executes in. A ScriptScope can have an arbitrary initializer and arbitrary
/// reloader.
///
/// ScriptScope is not thread safe. Host should either lock when multiple threads could
/// access the same module or should make a copy for each thread.
///
/// Hosting API counterpart for <see cref="Scope"/>.
/// </summary>
#if SILVERLIGHT
public sealed class ScriptScope : IDynamicMetaObjectProvider {
#else
[DebuggerTypeProxy(typeof(ScriptScope.DebugView))]
public sealed class ScriptScope : MarshalByRefObject, IDynamicMetaObjectProvider {
#endif
private readonly Scope _scope;
private readonly ScriptEngine _engine;
internal ScriptScope(ScriptEngine engine, Scope scope) {
Assert.NotNull(engine);
Assert.NotNull(scope);
_scope = scope;
_engine = engine;
}
internal Scope Scope {
get { return _scope; }
}
/// <summary>
/// Gets an engine for the language associated with this scope.
/// Returns invariant engine if the scope is language agnostic.
/// </summary>
public ScriptEngine Engine {
get {
// InvariantContext should not have an engine
// TODO: If _engine itself could be set to null, we wouldn't
// need this check
if (_engine.LanguageContext is InvariantContext) {
return null;
}
return _engine;
}
}
/// <summary>
/// Gets a value stored in the scope under the given name.
/// </summary>
/// <exception cref="MissingMemberException">The specified name is not defined in the scope.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is a <c>null</c> reference.</exception>
public dynamic GetVariable(string name) {
return _scope.GetVariable(SymbolTable.StringToId(name));
}
/// <summary>
/// Gets a value stored in the scope under the given name.
/// Converts the result to the specified type using the conversion that the language associated with the scope defines.
/// If no language is associated with the scope, the default CLR conversion is attempted.
/// </summary>
/// <exception cref="MissingMemberException">The specified name is not defined in the scope.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is a <c>null</c> reference.</exception>
public T GetVariable<T>(string name) {
return _engine.Operations.ConvertTo<T>((object)_engine.GetVariable(this, name));
}
/// <summary>
/// Tries to get a value stored in the scope under the given name.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is a <c>null</c> reference.</exception>
public bool TryGetVariable(string name, out dynamic value) {
return _scope.TryGetVariable(SymbolTable.StringToId(name), out value);
}
/// <summary>
/// Tries to get a value stored in the scope under the given name.
/// Converts the result to the specified type using the conversion that the language associated with the scope defines.
/// If no language is associated with the scope, the default CLR conversion is attempted.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is a <c>null</c> reference.</exception>
public bool TryGetVariable<T>(string name, out T value) {
object result;
if (_scope.TryGetVariable(SymbolTable.StringToId(name), out result)) {
value = _engine.Operations.ConvertTo<T>(result);
return true;
}
value = default(T);
return false;
}
/// <summary>
/// Sets the name to the specified value.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is a <c>null</c> reference.</exception>
public void SetVariable(string name, object value) {
_scope.SetVariable(SymbolTable.StringToId(name), value);
}
#if !SILVERLIGHT
/// <summary>
/// Gets a handle for a value stored in the scope under the given name.
/// </summary>
/// <exception cref="MissingMemberException">The specified name is not defined in the scope.</exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is a <c>null</c> reference.</exception>
public ObjectHandle GetVariableHandle(string name) {
return new ObjectHandle((object)GetVariable(name));
}
/// <summary>
/// Tries to get a handle for a value stored in the scope under the given name.
/// Returns <c>true</c> if there is such name, <c>false</c> otherwise.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is a <c>null</c> reference.</exception>
public bool TryGetVariableHandle(string name, out ObjectHandle handle) {
object value;
if (TryGetVariable(name, out value)) {
handle = new ObjectHandle(value);
return true;
} else {
handle = null;
return false;
}
}
/// <summary>
/// Sets the name to the specified value.
/// </summary>
/// <exception cref="SerializationException">
/// The value held by the handle isn't from the scope's app-domain and isn't serializable or MarshalByRefObject.
/// </exception>
/// <exception cref="ArgumentNullException"><paramref name="name"/> or <paramref name="handle"/> is a <c>null</c> reference.</exception>
public void SetVariable(string name, ObjectHandle handle) {
ContractUtils.RequiresNotNull(handle, "handle");
SetVariable(name, handle.Unwrap());
}
#endif
/// <summary>
/// Determines if this context or any outer scope contains the defined name.
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is a <c>null</c> reference.</exception>
public bool ContainsVariable(string name) {
return _scope.ContainsVariable(SymbolTable.StringToId(name));
}
/// <summary>
/// Removes the variable of the given name from this scope.
/// </summary>
/// <returns><c>true</c> if the value existed in the scope before it has been removed.</returns>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is a <c>null</c> reference.</exception>
public bool RemoveVariable(string name) {
return _scope.TryRemoveVariable(SymbolTable.StringToId(name));
}
/// <summary>
/// Gets a list of variable names stored in the scope.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
public IEnumerable<string> GetVariableNames() {
// Remoting: we eagerly enumerate all variables to avoid cross domain calls for each item.
var result = new List<string>();
foreach (var entry in _scope.Items) {
result.Add(SymbolTable.IdToString(entry.Key));
}
result.TrimExcess();
return result;
}
/// <summary>
/// Gets an array of variable names and their values stored in the scope.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
public IEnumerable<KeyValuePair<string, object>> GetItems() {
// Remoting: we eagerly enumerate all variables to avoid cross domain calls for each item.
var result = new List<KeyValuePair<string, object>>();
foreach (var entry in _scope.Items) {
result.Add(new KeyValuePair<string, object>(SymbolTable.IdToString(entry.Key), entry.Value));
}
result.TrimExcess();
return result;
}
#region DebugView
#if !SILVERLIGHT
internal sealed class DebugView {
private readonly ScriptScope _scope;
public DebugView(ScriptScope scope) {
Assert.NotNull(scope);
_scope = scope;
}
public ScriptEngine Language {
get { return _scope._engine; }
}
public System.Collections.Hashtable Variables {
get {
System.Collections.Hashtable result = new System.Collections.Hashtable();
foreach (KeyValuePair<string, object> variable in _scope.GetItems()) {
result[variable.Key] = variable.Value;
}
return result;
}
}
}
#endif
#endregion
#region IDynamicMetaObjectProvider implementation
DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter) {
return new Meta(parameter, this);
}
private sealed class Meta : DynamicMetaObject {
internal Meta(Expression parameter, ScriptScope scope)
: base(parameter, BindingRestrictions.Empty, scope) {
}
// TODO: support for IgnoreCase in underlying ScriptScope APIs
public override DynamicMetaObject BindGetMember(GetMemberBinder action) {
var result = Expression.Variable(typeof(object), "result");
var fallback = action.FallbackGetMember(this);
return new DynamicMetaObject(
Expression.Block(
new List<ParameterExpression>(new ParameterExpression[] { result }),
Expression.Condition(
Expression.Call(
AstUtils.Convert(Expression, typeof(ScriptScope)),
typeof(ScriptScope).GetMethod("TryGetVariable", new[] { typeof(string), typeof(object).MakeByRefType() }),
AstUtils.Constant(action.Name),
result
),
result,
AstUtils.Convert(fallback.Expression, typeof(object))
)
),
BindingRestrictionsHelpers.GetRuntimeTypeRestriction(Expression, typeof(ScriptScope)).Merge(fallback.Restrictions)
);
}
// TODO: support for IgnoreCase in underlying ScriptScope APIs
public override DynamicMetaObject BindSetMember(SetMemberBinder action, DynamicMetaObject value) {
return new DynamicMetaObject(
Expression.Block(
Expression.Call(
AstUtils.Convert(Expression, typeof(ScriptScope)),
typeof(ScriptScope).GetMethod("SetVariable", new[] { typeof(string), typeof(object) }),
AstUtils.Constant(action.Name),
AstUtils.Convert(value.Expression, typeof(object))
),
value.Expression
),
Restrictions.Merge(value.Restrictions).Merge(BindingRestrictionsHelpers.GetRuntimeTypeRestriction(Expression, typeof(ScriptScope)))
);
}
// TODO: support for IgnoreCase in underlying ScriptScope APIs
public override DynamicMetaObject BindDeleteMember(DeleteMemberBinder action) {
var fallback = action.FallbackDeleteMember(this);
return new DynamicMetaObject(
Expression.IfThenElse(
Expression.Call(
AstUtils.Convert(Expression, typeof(ScriptScope)),
typeof(ScriptScope).GetMethod("RemoveVariable"),
AstUtils.Constant(action.Name)
),
AstUtils.Empty(),
fallback.Expression
),
Restrictions.Merge(BindingRestrictionsHelpers.GetRuntimeTypeRestriction(Expression, typeof(ScriptScope))).Merge(fallback.Restrictions)
);
}
// TODO: support for IgnoreCase in underlying ScriptScope APIs
public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder action, DynamicMetaObject[] args) {
var fallback = action.FallbackInvokeMember(this, args);
var result = Expression.Variable(typeof(object), "result");
var fallbackInvoke = action.FallbackInvoke(new DynamicMetaObject(result, BindingRestrictions.Empty), args, null);
return new DynamicMetaObject(
Expression.Block(
new List<ParameterExpression>(new ParameterExpression[] { result }),
Expression.Condition(
Expression.Call(
AstUtils.Convert(Expression, typeof(ScriptScope)),
typeof(ScriptScope).GetMethod("TryGetVariable", new[] { typeof(string), typeof(object).MakeByRefType() }),
AstUtils.Constant(action.Name),
result
),
AstUtils.Convert(fallbackInvoke.Expression, typeof(object)),
AstUtils.Convert(fallback.Expression, typeof(object))
)
),
BindingRestrictions.Combine(args).Merge(BindingRestrictionsHelpers.GetRuntimeTypeRestriction(Expression, typeof(ScriptScope))).Merge(fallback.Restrictions)
);
}
public override IEnumerable<string> GetDynamicMemberNames() {
return ((ScriptScope)Value).GetVariableNames();
}
}
#endregion
#if !SILVERLIGHT
// TODO: Figure out what is the right lifetime
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
public override object InitializeLifetimeService() {
return null;
}
#endif
}
}
|