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
|
/* ****************************************************************************
*
* 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.Runtime.Remoting;
using System.Dynamic;
using Microsoft.Scripting.Utils;
using System.Security.Permissions;
using System.Threading;
#if !SYSTEM_CORE
using dynamic = System.Object;
#endif
namespace Microsoft.Scripting.Hosting {
/// <summary>
/// Hosting API counterpart for <see cref="ScriptCode"/>.
/// </summary>
public sealed class CompiledCode
#if !SILVERLIGHT
: MarshalByRefObject
#endif
{
private readonly ScriptEngine _engine;
private readonly ScriptCode _code;
private ScriptScope _defaultScope;
internal ScriptCode ScriptCode { get { return _code; } }
internal CompiledCode(ScriptEngine engine, ScriptCode code) {
Assert.NotNull(engine);
Assert.NotNull(code);
_engine = engine;
_code = code;
}
/// <summary>
/// Engine that compiled this code.
/// </summary>
public ScriptEngine Engine {
get { return _engine; }
}
/// <summary>
/// Default scope for this code.
/// </summary>
public ScriptScope DefaultScope {
get {
if (_defaultScope == null) {
Interlocked.CompareExchange(ref _defaultScope, new ScriptScope(_engine, _code.CreateScope()), null);
}
return _defaultScope;
}
}
/// <summary>
/// Executes code in a default scope.
/// </summary>
public dynamic Execute() {
return _code.Run(DefaultScope.Scope);
}
/// <summary>
/// Execute code within a given scope and returns the result.
/// </summary>
public dynamic Execute(ScriptScope scope) {
ContractUtils.RequiresNotNull(scope, "scope");
return _code.Run(scope.Scope);
}
/// <summary>
/// Executes code in in a default scope and converts to a given type.
/// </summary>
public T Execute<T>() {
return _engine.Operations.ConvertTo<T>((object)Execute());
}
/// <summary>
/// Execute code within a given scope and converts result to a given type.
/// </summary>
public T Execute<T>(ScriptScope scope) {
return _engine.Operations.ConvertTo<T>((object)Execute(scope));
}
#if !SILVERLIGHT
public ObjectHandle ExecuteAndWrap() {
return new ObjectHandle((object)Execute());
}
public ObjectHandle ExecuteAndWrap(ScriptScope scope) {
return new ObjectHandle((object)Execute(scope));
}
// TODO: Figure out what is the right lifetime
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
public override object InitializeLifetimeService() {
return null;
}
#endif
}
}
|