aspclassic-core/AspClassic.Scripting/Hosting/CompiledCode.cs
Jelle Luteijn 484dbfc9d9 progress
2022-05-15 11:19:49 +02:00

103 lines
2 KiB
C#

using System;
using System.Runtime.Remoting;
using System.Security.Permissions;
using System.Threading;
using AspClassic.Scripting.Utils;
namespace AspClassic.Scripting.Hosting;
public sealed class CompiledCode : MarshalByRefObject
{
private readonly ScriptEngine _engine;
private readonly ScriptCode _code;
private ScriptScope _defaultScope;
internal ScriptCode ScriptCode => _code;
public ScriptEngine Engine => _engine;
public ScriptScope DefaultScope
{
get
{
if (_defaultScope == null)
{
Interlocked.CompareExchange(ref _defaultScope, new ScriptScope(_engine, _code.CreateScope()), null);
}
return _defaultScope;
}
}
internal CompiledCode(ScriptEngine engine, ScriptCode code)
{
_engine = engine;
_code = code;
}
public dynamic Execute()
{
return _code.Run(DefaultScope.Scope);
}
public dynamic Execute(ScriptScope scope)
{
ContractUtils.RequiresNotNull(scope, "scope");
return _code.Run(scope.Scope);
}
public T Execute<T>()
{
return _engine.Operations.ConvertTo<T>((object)Execute());
}
public T Execute<T>(ScriptScope scope)
{
return _engine.Operations.ConvertTo<T>((object)Execute(scope));
}
public ObjectHandle ExecuteAndWrap()
{
return new ObjectHandle((object)Execute());
}
public ObjectHandle ExecuteAndWrap(ScriptScope scope)
{
return new ObjectHandle((object)Execute(scope));
}
public ObjectHandle ExecuteAndWrap(out ObjectHandle exception)
{
exception = null;
try
{
return new ObjectHandle((object)Execute());
}
catch (Exception o)
{
exception = new ObjectHandle(o);
return null;
}
}
public ObjectHandle ExecuteAndWrap(ScriptScope scope, out ObjectHandle exception)
{
exception = null;
try
{
return new ObjectHandle((object)Execute(scope));
}
catch (Exception o)
{
exception = new ObjectHandle(o);
return null;
}
}
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
public override object InitializeLifetimeService()
{
return null;
}
}