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() { return _engine.Operations.ConvertTo((object)Execute()); } public T Execute(ScriptScope scope) { return _engine.Operations.ConvertTo((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; } }