├── .gitignore ├── BuildAll.csproj ├── README.md ├── dist.cmd ├── install ├── build.cmd ├── dotNetFx40_Full_setup.exe ├── install.iss ├── setlocals.cmd └── vcredist_x86.exe ├── src ├── 1Script.sln ├── 1Script_Mono.sln ├── GlobalAssemblyInfo.cs ├── ScriptEngine.HostedScript │ ├── HostedScriptEngine.cs │ ├── IHostApplication.cs │ ├── Library │ │ ├── ArrayImpl.cs │ │ ├── BinaryDataContext.cs │ │ ├── CLREnumValueWrapper.cs │ │ ├── CommandLineArguments.cs │ │ ├── ConsoleContext.cs │ │ ├── FileContext.cs │ │ ├── FileOperations.cs │ │ ├── GuidWrapper.cs │ │ ├── Http │ │ │ ├── HttpConnectionContext.cs │ │ │ ├── HttpRequestBody.cs │ │ │ ├── HttpRequestBodyBinary.cs │ │ │ ├── HttpRequestBodyFile.cs │ │ │ ├── HttpRequestBodyString.cs │ │ │ ├── HttpRequestBodyUnknown.cs │ │ │ ├── HttpRequestContext.cs │ │ │ ├── HttpResponseBody.cs │ │ │ ├── HttpResponseContext.cs │ │ │ └── InternetProxyContext.cs │ │ ├── KeyAndValueImpl.cs │ │ ├── MapImpl.cs │ │ ├── MiscGlobalFunctions.cs │ │ ├── Net │ │ │ ├── TCPClient.cs │ │ │ └── TCPServer.cs │ │ ├── ProcessContext.cs │ │ ├── RandomNumberGenerator.cs │ │ ├── Reflector.cs │ │ ├── StdTextReadStream.cs │ │ ├── StdTextWriteStream.cs │ │ ├── StructureImpl.cs │ │ ├── SymbolsEnum.cs │ │ ├── SystemEnvironmentContext.cs │ │ ├── SystemGlobalContext.cs │ │ ├── TextEncodingEnum.cs │ │ ├── TextReadImpl.cs │ │ ├── TextWriteImpl.cs │ │ ├── ValueTable │ │ │ ├── CollectionIndexes.cs │ │ │ ├── ValueTable.cs │ │ │ ├── ValueTableColumn.cs │ │ │ ├── ValueTableColumnCollection.cs │ │ │ ├── ValueTableIndex.cs │ │ │ └── ValueTableRow.cs │ │ ├── Xml │ │ │ ├── XmlGlobalFunctions.cs │ │ │ ├── XmlNodeTypeEnum.cs │ │ │ ├── XmlReaderImpl.cs │ │ │ └── XmlWriterImpl.cs │ │ └── Zip │ │ │ ├── ZIPSubDirProcessingModeEnum.cs │ │ │ ├── ZipCompressionLevelEnum.cs │ │ │ ├── ZipCompressionMethodEnum.cs │ │ │ ├── ZipEncryptionMethodEnum.cs │ │ │ ├── ZipFileEntriesCollection.cs │ │ │ ├── ZipFileEntryContext.cs │ │ │ ├── ZipReader.cs │ │ │ ├── ZipRestoreFilePathsModeEnum.cs │ │ │ ├── ZipStorePathModeEnum.cs │ │ │ └── ZipWriter.cs │ ├── Process.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── ScriptEngine.HostedScript.csproj │ └── packages.config ├── ScriptEngine.Snegopat │ ├── AssemblyInfo.cpp │ ├── CriticalResourceLoader.cpp │ ├── CriticalResourceLoader.h │ ├── DispatchHelpers.cpp │ ├── DispatchHelpers.h │ ├── EventCallableSDO.cpp │ ├── EventCallableSDO.h │ ├── IAddinImpl.cpp │ ├── IAddinImpl.h │ ├── IAddinLoaderImpl.cpp │ ├── IAddinLoaderImpl.h │ ├── LibraryAttachedContext.cpp │ ├── LibraryAttachedContext.h │ ├── MarshalingHelpers.cpp │ ├── MarshalingHelpers.h │ ├── ReadMe.txt │ ├── RefCountable.cpp │ ├── RefCountable.h │ ├── ScriptDrivenAddin.cpp │ ├── ScriptDrivenAddin.h │ ├── ScriptEngine.Snegopat.vcxproj │ ├── ScriptEngine.Snegopat.vcxproj.filters │ ├── SnegAPIDefinitions.h │ ├── Snegopat.idl │ ├── SnegopatAttachedContext.cpp │ ├── SnegopatAttachedContext.h │ ├── Stdafx.cpp │ ├── Stdafx.h │ ├── app.ico │ ├── app.rc │ ├── clrfactory.cpp │ ├── resource.h │ └── snegopat.cpp ├── ScriptEngine │ ├── CodePositionInfo.cs │ ├── Compiler │ │ ├── Compiler.cs │ │ ├── CompilerContext.cs │ │ ├── CompilerExceptions.cs │ │ ├── ExpressionBuilder.cs │ │ ├── ICompilerContext.cs │ │ ├── ModuleCompilerContext.cs │ │ ├── ModulePersistor.cs │ │ ├── ModuleWriter.cs │ │ ├── ParseIterator.cs │ │ ├── Parser.cs │ │ ├── ParserException.cs │ │ ├── SourceCodeIndexer.cs │ │ ├── SymbolScope.cs │ │ └── Tokens.cs │ ├── CompilerService.cs │ ├── Environment │ │ ├── CodeSources.cs │ │ ├── FileOpener.cs │ │ ├── ICodeSource.cs │ │ ├── ICodeSourceFactory.cs │ │ ├── ModuleInformation.cs │ │ ├── ScriptModuleHandle.cs │ │ └── ScriptSourceFactory.cs │ ├── Machine │ │ ├── BuiltinFunctions.cs │ │ ├── Contexts │ │ │ ├── AttachedScriptsFactory.cs │ │ │ ├── AutoContext.cs │ │ │ ├── COMWrapperContext.cs │ │ │ ├── CollectionEnumerator.cs │ │ │ ├── ContextClassAttribute.cs │ │ │ ├── ContextDiscoverer.cs │ │ │ ├── ContextIValueImpl.cs │ │ │ ├── ContextMethodMapper.cs │ │ │ ├── ContextPropertyMapper.cs │ │ │ ├── ContextValuesMarshaller.cs │ │ │ ├── DispatchUtility.cs │ │ │ ├── DynamicPropertiesAccessor.cs │ │ │ ├── DynamicPropertiesHolder.cs │ │ │ ├── EnumContextHelper.cs │ │ │ ├── EnumValueAttribute.cs │ │ │ ├── EnumerationContext.cs │ │ │ ├── EnumerationValue.cs │ │ │ ├── ExceptionInfoContext.cs │ │ │ ├── GlobalContextAttribute.cs │ │ │ ├── GlobalContextBase.cs │ │ │ ├── ICollectionContext.cs │ │ │ ├── IObjectWrapper.cs │ │ │ ├── LibraryContextBase.cs │ │ │ ├── PropertyNameIndexAccessor.cs │ │ │ ├── ReflectableSDO.cs │ │ │ ├── ReflectedMethodInfo.cs │ │ │ ├── ReflectedParamInfo.cs │ │ │ ├── ReflectedPropertyInfo.cs │ │ │ ├── SafeArrayWrapper.cs │ │ │ ├── ScriptDrivenObject.cs │ │ │ ├── ScriptInformationContext.cs │ │ │ ├── SelfAwareEnumValue.cs │ │ │ ├── SystemEnumAttribute.cs │ │ │ └── UserScriptContextInstance.cs │ │ ├── Core.cs │ │ ├── ExecutionFrame.cs │ │ ├── GenericIValueComparer.cs │ │ ├── GenericValue.cs │ │ ├── GlobalInstancesManager.cs │ │ ├── IAttachableContext.cs │ │ ├── IReflectableContext.cs │ │ ├── IRuntimeContextInstance.cs │ │ ├── IValue.cs │ │ ├── LoadedModule.cs │ │ ├── MachineInstance.cs │ │ ├── NullValueImpl.cs │ │ ├── PropertyBag.cs │ │ ├── RuntimeExceptions.cs │ │ ├── Scope.cs │ │ ├── TypeManager.cs │ │ ├── TypeTypeValue.cs │ │ ├── ValueFactory.cs │ │ ├── ValueFormatter.cs │ │ └── Variables.cs │ ├── ModuleImage.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── RuntimeEnvironment.cs │ ├── ScriptEngine.csproj │ ├── ScriptException.cs │ ├── ScriptingEngine.cs │ └── Utils.cs ├── StandaloneRunner │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── StandaloneProcess.cs │ └── StandaloneRunner.csproj ├── TestApp │ ├── App.xaml │ ├── App.xaml.cs │ ├── Controls │ │ ├── 1CV8Syntax.xshd │ │ ├── CodeControl.xaml │ │ ├── CodeControl.xaml.cs │ │ ├── ImageArray.cs │ │ ├── MDConstants.cs │ │ ├── ProcedureListWnd.xaml │ │ └── ProcedureListWnd.xaml.cs │ ├── MainWindow.xaml │ ├── MainWindow.xaml.cs │ ├── Properties │ │ ├── AssemblyInfo.cs │ │ ├── Resources.Designer.cs │ │ ├── Resources.resx │ │ ├── Settings.Designer.cs │ │ └── Settings.settings │ ├── Resources │ │ └── module_icons.png │ ├── TestApp.csproj │ ├── packages.config │ └── script_badge.ico ├── oscript │ ├── AppBehavior.cs │ ├── BehaviorSelector.cs │ ├── CgiBehavior.cs │ ├── ExecuteScriptBehavior.cs │ ├── MakeAppBehavior.cs │ ├── MeasureBehavior.cs │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── ShowCompiledBehavior.cs │ ├── ShowUsageBehavior.cs │ ├── WebRequestContext.cs │ └── oscript.csproj └── packages │ ├── AvalonEdit.4.4.2.9744 │ ├── AvalonEdit.4.4.2.9744.nupkg │ ├── AvalonEdit.4.4.2.9744.nuspec │ └── lib │ │ ├── Net35 │ │ ├── ICSharpCode.AvalonEdit.dll │ │ └── ICSharpCode.AvalonEdit.xml │ │ └── Net40 │ │ ├── ICSharpCode.AvalonEdit.dll │ │ └── ICSharpCode.AvalonEdit.xml │ ├── DotNetZip.1.9.3 │ ├── DotNetZip.1.9.3.nupkg │ └── lib │ │ └── net20 │ │ └── Ionic.Zip.dll │ └── repositories.config └── tests ├── binarydata.os ├── engine-behaviors.os ├── example-test.os ├── file-object.os ├── formatting.os ├── global-funcs.os ├── global-new.os ├── http.os ├── process.os ├── random.os ├── readme.md ├── reflector.os ├── socket.os ├── start-all.cmd ├── start.cmd ├── sysinfo.os ├── testrunner.os ├── tests-cmd-line.os ├── text-write.os ├── types.os ├── values-test.os ├── valuetable.os ├── xmlread.os ├── xmlwrite.os └── zip.os /.gitignore: -------------------------------------------------------------------------------- 1 | /*.vssscc 2 | *.vspscc 3 | 4 | /src/*.vssscc 5 | /src/*.suo 6 | /src/oscript/oscript.csproj.user 7 | 8 | /src/ScriptEngine/bin/x86/Debug 9 | 10 | /src/ScriptEngine/bin/x86/Release 11 | 12 | /src/ScriptEngine/obj/Release/*.cache 13 | /src/ScriptEngine/obj/x86/Debug/*.cache 14 | /src/ScriptEngine/obj/x86/Debug 15 | 16 | /src/TestApp/bin/x86/Debug 17 | 18 | /src/ScriptEngine/obj/x86/Release 19 | 20 | /src/TestApp/bin/x86/Release 21 | /src/TestApp/obj/Release 22 | 23 | /src/TestApp/obj/x86/Debug 24 | 25 | /src/TestApp/obj/x86/Release 26 | 27 | /src/oscript/obj/Release 28 | 29 | /src/oscript/obj/Debug 30 | 31 | /src/oscript/obj/x86/Release 32 | 33 | /src/oscript/bin/x86/Release 34 | 35 | /src/oscript/obj/x86/Debug 36 | 37 | /src/ScriptEngine.StdLib/bin/x86/Release 38 | 39 | /src/ScriptEngine.StdLib/obj/Release 40 | 41 | /src/ScriptEngine.StdLib/obj/x86/Release 42 | 43 | /src/ScriptEngine.StdLib/obj/Debug/*.cache 44 | /src/*.sdf 45 | /src/Debug 46 | /src/ScriptEngine.HostedScript/bin/x86/Debug 47 | /src/ScriptEngine.HostedScript/obj/x86/Debug 48 | Debug 49 | /src/*.opensdf 50 | /src/ipch/scriptengine.snegopat-bbe5b63/scriptengine.ipch 51 | /src/ScriptEngine.Snegopat/Release/ScriptEngine.Snegopat.tlb 52 | /src/ScriptEngine.Snegopat/ScriptEngine.Snegopat.tlb 53 | /src/ScriptEngine.Snegopat/Release 54 | /src/Release 55 | /src/StandaloneRunner/obj/x86/Release 56 | /src/ScriptEngine.HostedScript/obj/x86/Release 57 | /src/ScriptEngine.HostedScript/bin/x86/Release 58 | /src/StandaloneRunner/*.dll 59 | /src/StandaloneRunner/bin/x86/Release 60 | /src/ipch/scriptengine.snegopat-724401f6/*.ipch 61 | /src/oscript/*.exe 62 | /src/ScriptEngine.Snegopat/dlldata.c 63 | /src/ScriptEngine.Snegopat/Snegopat_p.c 64 | install/build/ICSharpCode.AvalonEdit.dll 65 | install/build/ScriptEngine.HostedScript.dll 66 | install/build/ScriptEngine.Snegopat.dll 67 | install/build/ScriptEngine.dll 68 | install/build/TestApp.exe 69 | install/build/oscript.exe 70 | 71 | #Snegopat autogenerated interfaces 72 | src/ScriptEngine.Snegopat/Snegopat_h.h 73 | src/ScriptEngine.Snegopat/Snegopat_i.c 74 | 75 | #CI and full build|deploy 76 | /dist 77 | /install/build 78 | *.os.xml 79 | /src/packages/DotNetZip.1.9.3/lib/net20 80 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # README # 2 | 3 | ### Зачем нужен этот репозиторий? ### 4 | 5 | * Данное приложение является исполняющей средой для скриптов на языке 1С:Предприятие. 6 | * Позволяет писать скрипты WSH на языке 1С без наличия самой платформы. 7 | * Подробнее см. [WIKI проекта](/EvilBeaver/1script/wiki/Home/) 8 | 9 | ### Как установить и собрать? ### 10 | 11 | * Установите Visual Studio 2010-2013 12 | * Откройте файл 1Script.sln 13 | * Выполните сборку проекта 14 | 15 | ### Как поучаствовать в проекте? ### 16 | 17 | * Клонируйте репозиторий 18 | * Меняйте что хотите 19 | * Присылайте pull-request 20 | 21 | ### С кем я говорю? ### 22 | 23 | * http://habrahabr.ru/users/evilbeaver/ -------------------------------------------------------------------------------- /dist.cmd: -------------------------------------------------------------------------------- 1 | @echo Full distr and tests 2 | @echo add MSBuild 12 to your path 3 | 4 | MSBuild.exe ./BuildAll.csproj 5 | MSBuild.exe ./BuildAll.csproj /t:CreateZipForUpdateDll 6 | MSBuild.exe ./BuildAll.csproj /t:xUnitTest -------------------------------------------------------------------------------- /install/build.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | :build 4 | echo Build started. This CMD file will be deprecated. Use MSBUILD.exe BuildAll.csproj instead 5 | 6 | call setlocals.cmd 7 | 8 | pushd %SolutionPath% 9 | %git% pull origin master 10 | popd 11 | 12 | if NOT ERRORLEVEL == 0 GOTO bad_exit 13 | 14 | call "%vsvars%vsvars32.bat" 15 | devenv "%SolutionFilename%" /Clean 16 | devenv "%SolutionFilename%" /build Release 17 | 18 | if NOT ERRORLEVEL == 0 GOTO bad_exit 19 | 20 | if exist build ( 21 | erase /Q /S build\* 22 | rmdir /Q /S build) 23 | md build 24 | 25 | copy "%SolutionPath%\TestApp\bin\x86\Release\ScriptEngine.dll" build\ScriptEngine.dll 26 | copy "%SolutionPath%\TestApp\bin\x86\Release\ScriptEngine.HostedScript.dll" build\ScriptEngine.HostedScript.dll 27 | copy "%SolutionPath%\Release\ScriptEngine.Snegopat.dll" build\ScriptEngine.Snegopat.dll 28 | copy "%SolutionPath%\TestApp\bin\x86\Release\TestApp.exe" build\TestApp.exe 29 | copy "%SolutionPath%\TestApp\bin\x86\Release\ICSharpCode.AvalonEdit.dll" build\ICSharpCode.AvalonEdit.dll 30 | copy "%SolutionPath%\oscript\bin\x86\Release\oscript.exe" build\oscript.exe 31 | 32 | "%installer%" install.iss /o./dist 33 | if NOT ERRORLEVEL == 0 GOTO bad_exit 34 | echo Done 35 | 36 | exit /B 0 37 | 38 | :bad_exit 39 | echo Fail 40 | 41 | exit /B 1 -------------------------------------------------------------------------------- /install/dotNetFx40_Full_setup.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xDrivenDevelopment/1script/25f62879a041dce674b09a7b1e6d57f20633f21e/install/dotNetFx40_Full_setup.exe -------------------------------------------------------------------------------- /install/setlocals.cmd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xDrivenDevelopment/1script/25f62879a041dce674b09a7b1e6d57f20633f21e/install/setlocals.cmd -------------------------------------------------------------------------------- /install/vcredist_x86.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xDrivenDevelopment/1script/25f62879a041dce674b09a7b1e6d57f20633f21e/install/vcredist_x86.exe -------------------------------------------------------------------------------- /src/GlobalAssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // Этот код создан программой. 4 | // Исполняемая версия:4.0.30319.34014 5 | // 6 | // Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае 7 | // повторной генерации кода. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | [assembly: System.Reflection.AssemblyCompany("BeaverSoft")] 12 | [assembly: System.Reflection.AssemblyCopyright("Copyright (c) 2014 BeaverSoft")] 13 | [assembly: System.Reflection.AssemblyConfiguration("Commit 2a614c0")] 14 | [assembly: System.Reflection.AssemblyVersion("1.0.9.0")] 15 | [assembly: System.Reflection.AssemblyFileVersion("1.0.9.0")] 16 | [assembly: System.Reflection.AssemblyInformationalVersion("1.0.9.0")] 17 | 18 | 19 | 20 | internal sealed partial class ThisAssembly { 21 | 22 | internal const string AssemblyCompany = "BeaverSoft"; 23 | 24 | internal const string AssemblyCopyright = "Copyright (c) 2014 BeaverSoft"; 25 | 26 | internal const string AssemblyConfiguration = "Commit 2a614c0"; 27 | 28 | internal const string AssemblyVersion = "1.0.9.0"; 29 | 30 | internal const string AssemblyFileVersion = "1.0.9.0"; 31 | 32 | internal const string AssemblyInformationalVersion = "1.0.9.0"; 33 | 34 | private ThisAssembly() { 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/IHostApplication.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.HostedScript 7 | { 8 | public interface IHostApplication 9 | { 10 | void Echo(string str); 11 | void ShowExceptionInfo(Exception exc); 12 | bool InputString(out string result, int maxLen); 13 | string[] GetCommandLineArguments(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/BinaryDataContext.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using ScriptEngine.Machine.Contexts; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Text; 8 | 9 | namespace ScriptEngine.HostedScript.Library 10 | { 11 | [ContextClass("ДвоичныеДанные", "BinaryData")] 12 | public class BinaryDataContext : AutoContext, IDisposable 13 | { 14 | byte[] _buffer; 15 | 16 | public BinaryDataContext(string filename) 17 | { 18 | using(var fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) 19 | { 20 | _buffer = new byte[fs.Length]; 21 | fs.Read(_buffer, 0, _buffer.Length); 22 | } 23 | } 24 | 25 | public BinaryDataContext(byte[] buffer) 26 | { 27 | _buffer = buffer; 28 | } 29 | 30 | public void Dispose() 31 | { 32 | _buffer = null; 33 | } 34 | 35 | [ContextMethod("Размер","Size")] 36 | public int Size() 37 | { 38 | return _buffer.Length; 39 | } 40 | 41 | [ContextMethod("Записать","Write")] 42 | public void Write(string filename) 43 | { 44 | using(var fs = new FileStream(filename, FileMode.Create, FileAccess.Write)) 45 | { 46 | fs.Write(_buffer, 0, _buffer.Length); 47 | } 48 | } 49 | 50 | public byte[] Buffer 51 | { 52 | get 53 | { 54 | return _buffer; 55 | } 56 | } 57 | 58 | [ScriptConstructor(Name="На основании файла")] 59 | public static BinaryDataContext Constructor(IValue filename) 60 | { 61 | return new BinaryDataContext(filename.AsString()); 62 | } 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/CLREnumValueWrapper.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine.Contexts; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace ScriptEngine.HostedScript.Library 8 | { 9 | class CLREnumValueWrapper : EnumerationValue 10 | { 11 | T _realValue; 12 | 13 | public CLREnumValueWrapper(EnumerationContext owner, T realValue):base(owner) 14 | { 15 | _realValue = realValue; 16 | } 17 | 18 | public T UnderlyingObject 19 | { 20 | get 21 | { 22 | return _realValue; 23 | } 24 | } 25 | 26 | public override bool Equals(Machine.IValue other) 27 | { 28 | var otherWrapper = other.GetRawValue() as CLREnumValueWrapper; 29 | if (otherWrapper == null) 30 | return false; 31 | 32 | return this.UnderlyingObject.Equals(otherWrapper.UnderlyingObject); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/GuidWrapper.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using ScriptEngine.Machine.Contexts; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | 8 | namespace ScriptEngine.HostedScript.Library 9 | { 10 | [ContextClass("УникальныйИдентификатор","UUID")] 11 | public class GuidWrapper : IValue 12 | { 13 | Guid _value; 14 | 15 | public GuidWrapper() 16 | { 17 | _value = Guid.NewGuid(); 18 | } 19 | 20 | public GuidWrapper(string uuidString) 21 | { 22 | _value = Guid.Parse(uuidString); 23 | } 24 | 25 | [ScriptConstructor] 26 | public static GuidWrapper Create() 27 | { 28 | return new GuidWrapper(); 29 | } 30 | 31 | [ScriptConstructor] 32 | public static GuidWrapper Create(IValue uuidString) 33 | { 34 | return new GuidWrapper(uuidString.AsString()); 35 | } 36 | 37 | public DataType DataType 38 | { 39 | get { return Machine.DataType.GenericValue; } 40 | } 41 | 42 | public TypeDescriptor SystemType 43 | { 44 | get 45 | { 46 | return TypeManager.GetTypeByFrameworkType(typeof(GuidWrapper)); 47 | } 48 | } 49 | 50 | public decimal AsNumber() 51 | { 52 | throw RuntimeException.ConvertToNumberException(); 53 | } 54 | 55 | public DateTime AsDate() 56 | { 57 | throw RuntimeException.ConvertToDateException(); 58 | } 59 | 60 | public bool AsBoolean() 61 | { 62 | throw RuntimeException.ConvertToBooleanException(); 63 | } 64 | 65 | public string AsString() 66 | { 67 | return _value.ToString(); 68 | } 69 | 70 | public IRuntimeContextInstance AsObject() 71 | { 72 | throw RuntimeException.ValueIsNotObjectException(); 73 | } 74 | 75 | public IValue GetRawValue() 76 | { 77 | return this; 78 | } 79 | 80 | public int CompareTo(IValue other) 81 | { 82 | GuidWrapper otherUuid = other.GetRawValue() as GuidWrapper; 83 | if (otherUuid == null) 84 | throw RuntimeException.ComparisonNotSupportedException(); 85 | 86 | return _value.CompareTo(otherUuid._value); 87 | } 88 | 89 | public bool Equals(IValue other) 90 | { 91 | GuidWrapper otherUuid = other.GetRawValue() as GuidWrapper; 92 | if (otherUuid == null) 93 | return false; 94 | else 95 | return _value.Equals(otherUuid._value); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Http/HttpRequestBody.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Text; 7 | 8 | namespace ScriptEngine.HostedScript.Library.Http 9 | { 10 | interface IHttpRequestBody : IDisposable 11 | { 12 | IValue GetAsString(); 13 | IValue GetAsBinary(); 14 | IValue GetAsFilename(); 15 | 16 | Stream GetDataStream(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Http/HttpRequestBodyBinary.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using System; 3 | 4 | namespace ScriptEngine.HostedScript.Library.Http 5 | { 6 | class HttpRequestBodyBinary : IHttpRequestBody 7 | { 8 | BinaryDataContext _data; 9 | 10 | public HttpRequestBodyBinary(BinaryDataContext data) 11 | { 12 | _data = data; 13 | } 14 | 15 | public IValue GetAsString() 16 | { 17 | return ValueFactory.Create(); 18 | } 19 | 20 | public IValue GetAsBinary() 21 | { 22 | return _data; 23 | } 24 | 25 | public IValue GetAsFilename() 26 | { 27 | return ValueFactory.Create(); 28 | } 29 | 30 | public System.IO.Stream GetDataStream() 31 | { 32 | var bytes = _data.Buffer; 33 | return new System.IO.MemoryStream(bytes); 34 | } 35 | 36 | public void Dispose() 37 | { 38 | _data = null; 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Http/HttpRequestBodyFile.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using System; 3 | using System.IO; 4 | 5 | namespace ScriptEngine.HostedScript.Library.Http 6 | { 7 | class HttpRequestBodyFile : IHttpRequestBody 8 | { 9 | private FileStream _bodyOpenedFile; 10 | 11 | public HttpRequestBodyFile(string filename) 12 | { 13 | _bodyOpenedFile = new FileStream(filename, FileMode.Open); 14 | } 15 | 16 | public IValue GetAsString() 17 | { 18 | return ValueFactory.Create(); 19 | } 20 | 21 | public IValue GetAsBinary() 22 | { 23 | return ValueFactory.Create(); 24 | } 25 | 26 | public IValue GetAsFilename() 27 | { 28 | return ValueFactory.Create(_bodyOpenedFile.Name); 29 | } 30 | 31 | public Stream GetDataStream() 32 | { 33 | return _bodyOpenedFile; 34 | } 35 | 36 | public void Dispose() 37 | { 38 | _bodyOpenedFile.Dispose(); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Http/HttpRequestBodyString.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using System; 3 | using System.Text; 4 | 5 | namespace ScriptEngine.HostedScript.Library.Http 6 | { 7 | class HttpRequestBodyString : IHttpRequestBody 8 | { 9 | string _data; 10 | Encoding _encoding; 11 | 12 | public HttpRequestBodyString(string body, IValue encoding = null) 13 | { 14 | _data = body; 15 | if (encoding == null) 16 | _encoding = new UTF8Encoding(true); 17 | else 18 | _encoding = TextEncodingEnum.GetEncoding(encoding); 19 | } 20 | 21 | public IValue GetAsString() 22 | { 23 | return ValueFactory.Create(_data); 24 | } 25 | 26 | public IValue GetAsBinary() 27 | { 28 | return ValueFactory.Create(); 29 | } 30 | 31 | public IValue GetAsFilename() 32 | { 33 | return ValueFactory.Create(); 34 | } 35 | 36 | public System.IO.Stream GetDataStream() 37 | { 38 | var bytes = _encoding.GetBytes(_data); 39 | return new System.IO.MemoryStream(bytes); 40 | } 41 | 42 | public void Dispose() 43 | { 44 | _data = null; 45 | _encoding = null; 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Http/HttpRequestBodyUnknown.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | 3 | namespace ScriptEngine.HostedScript.Library.Http 4 | { 5 | class HttpRequestBodyUnknown : IHttpRequestBody 6 | { 7 | 8 | public IValue GetAsString() 9 | { 10 | return ValueFactory.Create(); 11 | } 12 | 13 | public IValue GetAsBinary() 14 | { 15 | return ValueFactory.Create(); 16 | } 17 | 18 | public IValue GetAsFilename() 19 | { 20 | return ValueFactory.Create(); 21 | } 22 | 23 | public void Dispose() 24 | { 25 | ; 26 | } 27 | 28 | public System.IO.Stream GetDataStream() 29 | { 30 | return null; 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/KeyAndValueImpl.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using ScriptEngine.Machine.Contexts; 3 | 4 | namespace ScriptEngine.HostedScript.Library 5 | { 6 | [ContextClass("КлючИЗначение", "KeyAndValue")] 7 | public class KeyAndValueImpl : AutoContext 8 | { 9 | private IValue _key; 10 | private IValue _value; 11 | 12 | public KeyAndValueImpl(IValue key, IValue value) 13 | { 14 | _key = key; 15 | _value = value; 16 | } 17 | 18 | [ContextProperty("Ключ", "Key")] 19 | public IValue Key 20 | { 21 | get 22 | { 23 | return _key; 24 | } 25 | } 26 | 27 | [ContextProperty("Значение", "Value")] 28 | public IValue Value 29 | { 30 | get 31 | { 32 | return _value; 33 | } 34 | } 35 | 36 | public override IValue GetPropValue(int propNum) 37 | { 38 | return propNum == 0 ? _key : _value; 39 | } 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/MapImpl.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using ScriptEngine.Machine; 3 | using ScriptEngine.Machine.Contexts; 4 | 5 | namespace ScriptEngine.HostedScript.Library 6 | { 7 | [ContextClass("Соответствие", "Map")] 8 | public class MapImpl : AutoContext, ICollectionContext 9 | { 10 | private Dictionary _content = new Dictionary(new GenericIValueComparer()); 11 | 12 | public override bool IsIndexed 13 | { 14 | get 15 | { 16 | return true; 17 | } 18 | } 19 | 20 | public override IValue GetIndexedValue(IValue index) 21 | { 22 | IValue result; 23 | if (!_content.TryGetValue(index, out result)) 24 | { 25 | result = ValueFactory.Create(); 26 | _content.Add(index, result); 27 | } 28 | 29 | return result; 30 | } 31 | 32 | public override void SetIndexedValue(IValue index, IValue val) 33 | { 34 | _content[index] = val; 35 | } 36 | 37 | public override bool IsPropReadable(int propNum) 38 | { 39 | return false; 40 | } 41 | 42 | public override bool IsPropWritable(int propNum) 43 | { 44 | return false; 45 | } 46 | 47 | #region ICollectionContext Members 48 | 49 | [ContextMethod("Вставить", "Insert")] 50 | public void Insert(IValue key, IValue val) 51 | { 52 | SetIndexedValue(key, val); 53 | } 54 | 55 | [ContextMethod("Получить", "Get")] 56 | public IValue Retrieve(IValue key) 57 | { 58 | return GetIndexedValue(key); 59 | } 60 | 61 | [ContextMethod("Количество", "Count")] 62 | public int Count() 63 | { 64 | return _content.Count; 65 | } 66 | 67 | [ContextMethod("Очистить", "Clear")] 68 | public void Clear() 69 | { 70 | _content.Clear(); 71 | } 72 | 73 | [ContextMethod("Удалить", "Delete")] 74 | public void Delete(IValue key) 75 | { 76 | _content.Remove(key); 77 | } 78 | 79 | public CollectionEnumerator GetManagedIterator() 80 | { 81 | return new CollectionEnumerator(GetEnumerator()); 82 | } 83 | 84 | #endregion 85 | 86 | #region IEnumerable Members 87 | 88 | public IEnumerator GetEnumerator() 89 | { 90 | foreach (var item in _content) 91 | { 92 | yield return new KeyAndValueImpl(item.Key, item.Value); 93 | } 94 | } 95 | 96 | #endregion 97 | 98 | #region IEnumerable Members 99 | 100 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 101 | { 102 | return GetEnumerator(); 103 | } 104 | 105 | #endregion 106 | 107 | [ScriptConstructor] 108 | public static MapImpl Constructor() 109 | { 110 | return new MapImpl(); 111 | } 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/MiscGlobalFunctions.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine.Contexts; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace ScriptEngine.HostedScript.Library 8 | { 9 | [GlobalContext(Category="Прочие функции")] 10 | public class MiscGlobalFunctions : GlobalContextBase 11 | { 12 | [ContextMethod("Base64Строка", "Base64String")] 13 | public string Base64String(BinaryDataContext data) 14 | { 15 | return Convert.ToBase64String(data.Buffer); 16 | } 17 | 18 | [ContextMethod("Base64Значение", "Base64Value")] 19 | public BinaryDataContext Base64String(string data) 20 | { 21 | byte[] bytes = Convert.FromBase64String(data); 22 | return new BinaryDataContext(bytes); 23 | } 24 | 25 | public static MiscGlobalFunctions CreateInstance() 26 | { 27 | return new MiscGlobalFunctions(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Net/TCPServer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Net; 5 | using System.Net.Sockets; 6 | using System.Text; 7 | using ScriptEngine.Machine; 8 | using ScriptEngine.Machine.Contexts; 9 | 10 | namespace ScriptEngine.HostedScript.Library.Net 11 | { 12 | [ContextClass("TCPСервер", "TCPServer")] 13 | public class TCPServer : AutoContext 14 | { 15 | private TcpListener _listener; 16 | 17 | public TCPServer(int port) 18 | { 19 | _listener = new TcpListener(IPAddress.Any, port); 20 | } 21 | 22 | [ContextMethod("Запустить", "Start")] 23 | public void Start() 24 | { 25 | _listener.Start(); 26 | } 27 | 28 | [ContextMethod("Остановить", "Stop")] 29 | public void Stop() 30 | { 31 | _listener.Stop(); 32 | } 33 | 34 | [ContextMethod("ОжидатьСоединения","WaitForConnection")] 35 | public TCPClient WaitForConnection() 36 | { 37 | var client = _listener.AcceptTcpClient(); 38 | return new TCPClient(client); 39 | } 40 | 41 | [ScriptConstructor] 42 | public static TCPServer ConstructByPort(IValue port) 43 | { 44 | return new TCPServer((int)port.AsNumber()); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/RandomNumberGenerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ScriptEngine.Machine; 3 | using ScriptEngine.Machine.Contexts; 4 | 5 | namespace ScriptEngine.HostedScript.Library 6 | { 7 | [ContextClass("ГенераторСлучайныхЧисел", "RandomNumberGenerator")] 8 | class RandomNumberGenerator : AutoContext 9 | { 10 | private Random _random; 11 | 12 | public RandomNumberGenerator(int seed = 0) 13 | { 14 | if (seed == 0) 15 | _random = new Random(); 16 | else 17 | _random = new Random(seed); 18 | } 19 | 20 | [ContextMethod("СлучайноеЧисло", "RandomNumber")] 21 | public IValue RandomNumber(IValue low = null, IValue high = null) 22 | { 23 | long lo64 = 0, hi64 = UInt32.MaxValue; 24 | 25 | if (low != null) 26 | lo64 = decimal.ToInt64(low.AsNumber()); 27 | 28 | if (high != null) 29 | hi64 = decimal.ToInt64(high.AsNumber()); 30 | 31 | if (lo64 < 0 || lo64 > 4294967295) 32 | throw RuntimeException.InvalidArgumentValue(); 33 | 34 | if (hi64 < 0 || hi64 > 4294967295) 35 | throw RuntimeException.InvalidArgumentValue(); 36 | 37 | if (hi64 < lo64) 38 | throw RuntimeException.InvalidArgumentValue(); 39 | 40 | // Приводим к рабочему диапазону 41 | lo64 += Int32.MinValue; 42 | hi64 += Int32.MinValue; 43 | 44 | int lo = (int)lo64, hi = (int)hi64; 45 | 46 | int v = _random.Next(lo, hi); 47 | long v64 = v; 48 | v64 -= Int32.MinValue; 49 | 50 | return ValueFactory.Create( v64 ); 51 | } 52 | 53 | /// 54 | /// Формирует ГСЧ с возможностью указания начального числа. 55 | /// 56 | /// Начальное число. Последовательность случайных чисел для одного и того же начального числа будет одинакова 57 | /// 58 | [ScriptConstructor] 59 | public static IRuntimeContextInstance Constructor(IValue seed) 60 | { 61 | seed = seed.GetRawValue(); 62 | if (seed.DataType == DataType.Number) 63 | return new RandomNumberGenerator(decimal.ToInt32(seed.AsNumber())); 64 | 65 | return new RandomNumberGenerator(); 66 | } 67 | 68 | [ScriptConstructor(Name="Формирование неинициализированного объекта")] 69 | public static IRuntimeContextInstance Constructor() 70 | { 71 | return new RandomNumberGenerator(); 72 | } 73 | 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Reflector.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using ScriptEngine.Machine; 3 | using ScriptEngine.Machine.Contexts; 4 | 5 | namespace ScriptEngine.HostedScript.Library 6 | { 7 | /// 8 | /// Рефлектор предназначен для получения метаданных объектов во время выполнения. 9 | /// Как правило, рефлексия используется для проверки наличия у объекта определенных свойств/методов. 10 | /// В OneScript рефлексию можно применять для вызова методов объектов по именам методов. 11 | /// 12 | [ContextClass("Рефлектор","Reflector")] 13 | public class ReflectorContext : AutoContext 14 | { 15 | public ReflectorContext() 16 | { 17 | 18 | } 19 | 20 | /// 21 | /// Вызывает метод по его имени. 22 | /// 23 | /// Объект, метод которого нужно вызвать. 24 | /// Имя метода для вызова 25 | /// Массив аргументов, передаваемых методу 26 | /// Если вызывается функция, то возвращается ее результат. В противном случае возвращается Неопределено. 27 | [ContextMethod("ВызватьМетод", "CallMethod")] 28 | public IValue CallMethod(IRuntimeContextInstance target, string methodName, IRuntimeContextInstance arguments = null) 29 | { 30 | ArrayImpl argArray; 31 | if (arguments != null) 32 | { 33 | argArray = arguments as ArrayImpl; 34 | if (argArray == null) 35 | throw RuntimeException.InvalidArgumentType(); 36 | } 37 | else 38 | { 39 | argArray = new ArrayImpl(); 40 | } 41 | 42 | var methodIdx = target.FindMethod(methodName); 43 | 44 | var methInfo = target.GetMethodInfo(methodIdx); 45 | IValue retValue = ValueFactory.Create(); 46 | if (methInfo.IsFunction) 47 | { 48 | target.CallAsFunction(methodIdx, argArray.ToArray(), out retValue); 49 | } 50 | else 51 | { 52 | target.CallAsProcedure(methodIdx, argArray.ToArray()); 53 | } 54 | 55 | return retValue; 56 | } 57 | 58 | [ScriptConstructor] 59 | public static IRuntimeContextInstance CreateNew() 60 | { 61 | return new ReflectorContext(); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/StdTextReadStream.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using ScriptEngine.Machine.Contexts; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Text; 8 | 9 | namespace ScriptEngine.HostedScript.Library 10 | { 11 | [ContextClass("ПотокВыводаТекста","TextOutputStream")] 12 | public class StdTextReadStream : AutoContext, IDisposable 13 | { 14 | private StreamReader _reader; 15 | 16 | public StdTextReadStream(StreamReader source) 17 | { 18 | _reader = source; 19 | } 20 | 21 | [ContextProperty("ЕстьДанные", "HasData")] 22 | public bool HasData 23 | { 24 | get 25 | { 26 | return !_reader.EndOfStream; 27 | } 28 | } 29 | 30 | [ContextMethod("Прочитать", "Read")] 31 | public string Read() 32 | { 33 | return _reader.ReadToEnd(); 34 | } 35 | 36 | [ContextMethod("ПрочитатьСтроку", "ReadLine")] 37 | public string ReadLine() 38 | { 39 | return _reader.ReadLine(); 40 | } 41 | 42 | [ContextMethod("Закрыть", "Close")] 43 | public void Close() 44 | { 45 | _reader.Close(); 46 | } 47 | 48 | public void Dispose() 49 | { 50 | _reader.Dispose(); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/StdTextWriteStream.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using ScriptEngine.Machine.Contexts; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Text; 8 | 9 | namespace ScriptEngine.HostedScript.Library 10 | { 11 | [ContextClass("ПотокВводаТекста", "TextInputStream")] 12 | public class StdTextWriteStream : AutoContext, IDisposable 13 | { 14 | private StreamWriter _writer; 15 | 16 | public StdTextWriteStream(StreamWriter writer) 17 | { 18 | _writer = writer; 19 | } 20 | 21 | [ContextMethod("Записать","Write")] 22 | public void Write(string data) 23 | { 24 | _writer.Write(data); 25 | } 26 | 27 | [ContextMethod("ЗаписатьСтроку", "WriteLine")] 28 | public void WriteLine(string data) 29 | { 30 | _writer.WriteLine(data); 31 | } 32 | 33 | [ContextMethod("Закрыть", "Close")] 34 | public void Close() 35 | { 36 | _writer.Close(); 37 | } 38 | 39 | public void Dispose() 40 | { 41 | _writer.Dispose(); 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/SymbolsEnum.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using ScriptEngine.Machine.Contexts; 3 | 4 | namespace ScriptEngine.HostedScript.Library 5 | { 6 | 7 | [SystemEnum("Символы", "Symbols")] 8 | public class SymbolsEnum : EnumerationContext 9 | { 10 | private SymbolsEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType) 11 | :base(typeRepresentation, valuesType) 12 | { 13 | 14 | } 15 | 16 | class SymbolsEnumValue : EnumerationValue 17 | { 18 | string _val; 19 | 20 | public SymbolsEnumValue(EnumerationContext owner, string val) 21 | : base(owner) 22 | { 23 | _val = val; 24 | } 25 | 26 | public override string AsString() 27 | { 28 | return _val; 29 | } 30 | 31 | public override DataType DataType 32 | { 33 | get 34 | { 35 | return DataType.String; 36 | } 37 | } 38 | 39 | public override TypeDescriptor SystemType 40 | { 41 | get 42 | { 43 | return TypeDescriptor.FromDataType(DataType); 44 | } 45 | } 46 | 47 | public override int CompareTo(IValue other) 48 | { 49 | return _val.CompareTo(other.AsString()); 50 | } 51 | 52 | public override bool Equals(IValue other) 53 | { 54 | return _val == other.AsString(); 55 | } 56 | } 57 | 58 | public static SymbolsEnum CreateInstance() 59 | { 60 | 61 | var type = TypeManager.RegisterType("Символы", typeof(SymbolsEnum)); 62 | var stringType = TypeDescriptor.FromDataType(DataType.String); 63 | var instance = new SymbolsEnum(type, stringType); 64 | 65 | instance.AddValue("ПС", new SymbolsEnumValue(instance, "\n")); 66 | instance.AddValue("ВК", new SymbolsEnumValue(instance, "\r")); 67 | instance.AddValue("ВТаб", new SymbolsEnumValue(instance, "\v")); 68 | instance.AddValue("Таб", new SymbolsEnumValue(instance, "\t")); 69 | instance.AddValue("ПФ", new SymbolsEnumValue(instance, "\f")); 70 | instance.AddValue("НПП", new SymbolsEnumValue(instance, "\u00A0")); 71 | 72 | return instance; 73 | } 74 | 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/TextReadImpl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Text; 4 | using ScriptEngine.Machine; 5 | using ScriptEngine.Machine.Contexts; 6 | 7 | namespace ScriptEngine.HostedScript.Library 8 | { 9 | [ContextClass("ЧтениеТекста", "TextReader")] 10 | class TextReadImpl : AutoContext, IDisposable 11 | { 12 | StreamReader _reader; 13 | 14 | [ContextMethod("Открыть", "Open")] 15 | public void Open(string path, IValue encoding = null) 16 | { 17 | if (encoding == null) 18 | { 19 | _reader = Environment.FileOpener.OpenReader(path); 20 | } 21 | else 22 | { 23 | var enc = TextEncodingEnum.GetEncoding(encoding); 24 | _reader = Environment.FileOpener.OpenReader(path, enc); 25 | } 26 | } 27 | 28 | [ContextMethod("Прочитать", "Read")] 29 | public IValue ReadAll() 30 | { 31 | RequireOpen(); 32 | if (_reader.EndOfStream) 33 | return ValueFactory.Create(); 34 | 35 | return ValueFactory.Create(_reader.ReadToEnd()); 36 | } 37 | 38 | [ContextMethod("ПрочитатьСтроку", "ReadLine")] 39 | public IValue ReadLine() 40 | { 41 | RequireOpen(); 42 | if (_reader.EndOfStream) 43 | return ValueFactory.Create(); 44 | 45 | return ValueFactory.Create(_reader.ReadLine()); 46 | } 47 | 48 | [ContextMethod("Закрыть", "Close")] 49 | public void Close() 50 | { 51 | Dispose(); 52 | } 53 | 54 | private void RequireOpen() 55 | { 56 | if (_reader == null) 57 | { 58 | throw new RuntimeException("Файл не открыт"); 59 | } 60 | } 61 | 62 | [ScriptConstructor(Name="По имени файла и кодировке")] 63 | public static IRuntimeContextInstance Constructor(IValue path, IValue encoding) 64 | { 65 | var reader = new TextReadImpl(); 66 | reader.Open(path.AsString(), encoding); 67 | return reader; 68 | } 69 | 70 | [ScriptConstructor(Name = "По имени файла")] 71 | public static IRuntimeContextInstance Constructor(IValue path) 72 | { 73 | var reader = new TextReadImpl(); 74 | reader.Open(path.AsString(), null); 75 | return reader; 76 | } 77 | 78 | [ScriptConstructor] 79 | public static IRuntimeContextInstance Constructor() 80 | { 81 | var reader = new TextReadImpl(); 82 | return reader; 83 | } 84 | 85 | #region IDisposable Members 86 | 87 | public void Dispose() 88 | { 89 | if (_reader != null) 90 | { 91 | _reader.Dispose(); 92 | _reader = null; 93 | } 94 | } 95 | 96 | #endregion 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/ValueTable/CollectionIndexes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ScriptEngine.Machine.Contexts; 6 | using ScriptEngine.Machine; 7 | 8 | namespace ScriptEngine.HostedScript.Library.ValueTable 9 | { 10 | [ContextClass("ИндексыКоллекции", "CollectionIndexes")] 11 | class CollectionIndexes : AutoContext, ICollectionContext 12 | { 13 | 14 | List _indexes = new List(); 15 | 16 | [ContextMethod("Добавить", "Add")] 17 | public CollectionIndex Add(string columns) 18 | { 19 | CollectionIndex newIndex = new CollectionIndex(); 20 | _indexes.Add(newIndex); 21 | return newIndex; 22 | } 23 | 24 | [ContextMethod("Количество", "Count")] 25 | public int Count() 26 | { 27 | return _indexes.Count(); 28 | } 29 | 30 | [ContextMethod("Удалить", "Delete")] 31 | public void Delete(IValue Index) 32 | { 33 | Index = Index.GetRawValue(); 34 | if (Index is CollectionIndex) 35 | _indexes.Remove(Index as CollectionIndex); 36 | else 37 | _indexes.RemoveAt(Decimal.ToInt32(Index.AsNumber())); 38 | } 39 | 40 | [ContextMethod("Очистить", "Clear")] 41 | public void Clear() 42 | { 43 | _indexes.Clear(); 44 | } 45 | 46 | public IEnumerator GetEnumerator() 47 | { 48 | foreach (var item in _indexes) 49 | { 50 | yield return item; 51 | } 52 | } 53 | 54 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 55 | { 56 | return GetEnumerator(); 57 | } 58 | 59 | public CollectionEnumerator GetManagedIterator() 60 | { 61 | return new CollectionEnumerator(GetEnumerator()); 62 | } 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/ValueTable/ValueTableColumn.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ScriptEngine.Machine.Contexts; 6 | using ScriptEngine.Machine; 7 | 8 | 9 | namespace ScriptEngine.HostedScript.Library.ValueTable 10 | { 11 | [ContextClass("КолонкаТаблицыЗначений", "ValueTableColumn")] 12 | class ValueTableColumn : AutoContext 13 | { 14 | private string _title; 15 | private string _name; 16 | private IValue _valueType; 17 | private int _width; 18 | private WeakReference _owner; 19 | 20 | // id нужен для правильной работы функции FindProperty. 21 | // Порядковый номер колонки не может быть использовать из-за своей изменчивости. 22 | private int _id; 23 | 24 | public ValueTableColumn(ValueTableColumnCollection Owner, int id, string Name, string Title, IValue Type, int Width) 25 | { 26 | _name = Name; 27 | _title = Title; 28 | _valueType = Type; 29 | _width = Width; 30 | 31 | _owner = new WeakReference(Owner); 32 | _id = id; 33 | 34 | } 35 | 36 | public int ID 37 | { 38 | get { return _id; } 39 | } 40 | 41 | [ContextProperty("Заголовок", "Title")] 42 | public string Title 43 | { 44 | get { return _title == null ? _name : _title; } 45 | set { _title = value; } 46 | } 47 | 48 | [ContextProperty("Имя", "Name")] 49 | public string Name 50 | { 51 | get { return _name; } 52 | set 53 | { 54 | ValueTableColumnCollection Owner = _owner.Target as ValueTableColumnCollection; 55 | if (Owner.FindColumnByName(value) != null) 56 | throw new RuntimeException("Неверное имя колонки!"); 57 | 58 | if (_title == _name) 59 | _title = value; 60 | 61 | _name = value; 62 | 63 | } 64 | } 65 | [ContextProperty("ТипЗначения", "ValueType")] 66 | public IValue ValueType 67 | { 68 | get { return _valueType; } 69 | set { _valueType = value; } // TODO: Проверить тип 70 | } 71 | [ContextProperty("Ширина", "Width")] 72 | public int Width 73 | { 74 | get { return Width; } 75 | set { _width = value; } // TOOD: Проверить неотрицательность значения 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/ValueTable/ValueTableIndex.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ScriptEngine.Machine.Contexts; 6 | using ScriptEngine.Machine; 7 | 8 | namespace ScriptEngine.HostedScript.Library.ValueTable 9 | { 10 | [ContextClass("ИндексКоллекции", "CollectionIndex")] 11 | class CollectionIndex : AutoContext 12 | { 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Xml/XmlGlobalFunctions.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using ScriptEngine.Machine.Contexts; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Xml; 8 | 9 | namespace ScriptEngine.HostedScript.Library.Xml 10 | { 11 | [GlobalContext(Category="Функции работы с XML")] 12 | public class XmlGlobalFunctions : GlobalContextBase 13 | { 14 | [ContextMethod("XMLСтрока", "XMLString")] 15 | public string XMLString(IValue value) 16 | { 17 | switch(value.DataType) 18 | { 19 | case DataType.Undefined: 20 | return ""; 21 | case DataType.Boolean: 22 | return XmlConvert.ToString(value.AsBoolean()); 23 | case DataType.Date: 24 | return XmlConvert.ToString(value.AsDate(), XmlDateTimeSerializationMode.Unspecified); 25 | case DataType.Number: 26 | return XmlConvert.ToString(value.AsNumber()); 27 | default: 28 | 29 | if(value.SystemType.Equals(TypeManager.GetTypeByFrameworkType(typeof(BinaryDataContext)))) 30 | { 31 | var bdc = value.GetRawValue() as BinaryDataContext; 32 | System.Diagnostics.Debug.Assert(bdc != null); 33 | 34 | return Convert.ToBase64String(bdc.Buffer, Base64FormattingOptions.InsertLineBreaks); 35 | } 36 | else 37 | { 38 | return value.GetRawValue().AsString(); 39 | } 40 | 41 | } 42 | } 43 | 44 | [ContextMethod("XMLЗначение", "XMLValue")] 45 | public IValue XMLValue(IValue givenType, string presentation) 46 | { 47 | var typeValue = TypeManager.GetTypeDescriptorFor(givenType.GetRawValue()); 48 | 49 | if(typeValue.Equals(TypeDescriptor.FromDataType(DataType.Boolean))) 50 | { 51 | return ValueFactory.Create(XmlConvert.ToBoolean(presentation)); 52 | } 53 | else if (typeValue.Equals(TypeDescriptor.FromDataType(DataType.Date))) 54 | { 55 | return ValueFactory.Create(XmlConvert.ToDateTime(presentation, XmlDateTimeSerializationMode.Unspecified)); 56 | } 57 | else if (typeValue.Equals(TypeDescriptor.FromDataType(DataType.Number))) 58 | { 59 | return ValueFactory.Create(XmlConvert.ToDecimal(presentation)); 60 | } 61 | else if (typeValue.Equals(TypeDescriptor.FromDataType(DataType.String))) 62 | { 63 | return ValueFactory.Create(presentation); 64 | } 65 | else if (typeValue.Equals(TypeDescriptor.FromDataType(DataType.Undefined)) && presentation == "") 66 | { 67 | return ValueFactory.Create(); 68 | } 69 | else 70 | { 71 | throw RuntimeException.InvalidArgumentValue(); 72 | } 73 | 74 | } 75 | 76 | public static IAttachableContext CreateInstance() 77 | { 78 | return new XmlGlobalFunctions(); 79 | } 80 | 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Zip/ZIPSubDirProcessingModeEnum.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using ScriptEngine.Machine.Contexts; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | 8 | namespace ScriptEngine.HostedScript.Library.Zip 9 | { 10 | [SystemEnum("РежимОбработкиПодкаталоговZIP", "ZIPSubDirProcessingMode")] 11 | public class ZIPSubDirProcessingModeEnum : EnumerationContext 12 | { 13 | private const string EV_DONT_RECURSE = "НеОбрабатывать"; 14 | private const string EV_RECURSE = "ОбрабатыватьРекурсивно"; 15 | 16 | private ZIPSubDirProcessingModeEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType) 17 | : base(typeRepresentation, valuesType) 18 | { 19 | } 20 | 21 | [EnumValue(EV_DONT_RECURSE)] 22 | public EnumerationValue DontRecurse 23 | { 24 | get 25 | { 26 | return this[EV_DONT_RECURSE]; 27 | } 28 | } 29 | 30 | [EnumValue(EV_RECURSE)] 31 | public EnumerationValue Recurse 32 | { 33 | get 34 | { 35 | return this[EV_RECURSE]; 36 | } 37 | } 38 | 39 | public static ZIPSubDirProcessingModeEnum CreateInstance() 40 | { 41 | ZIPSubDirProcessingModeEnum instance; 42 | 43 | TypeDescriptor enumType; 44 | TypeDescriptor enumValType; 45 | 46 | EnumContextHelper.RegisterEnumType(out enumType, out enumValType); 47 | 48 | instance = new ZIPSubDirProcessingModeEnum(enumType, enumValType); 49 | 50 | EnumContextHelper.RegisterValues(instance); 51 | 52 | return instance; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Zip/ZipCompressionLevelEnum.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using ScriptEngine.Machine.Contexts; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | 8 | namespace ScriptEngine.HostedScript.Library.Zip 9 | { 10 | [SystemEnum("УровеньСжатияZIP", "ZIPCompressionLevel")] 11 | public class ZipCompressionLevelEnum : EnumerationContext 12 | { 13 | private const string EV_MINIMAL_NAME = "Минимальный"; 14 | private const string EV_OPTIMAL_NAME = "Оптимальный"; 15 | private const string EV_MAXIMAL_NAME = "Максимальный"; 16 | 17 | private ZipCompressionLevelEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType) 18 | : base(typeRepresentation, valuesType) 19 | { 20 | } 21 | 22 | [EnumValue(EV_MINIMAL_NAME)] 23 | public EnumerationValue Minimal 24 | { 25 | get 26 | { 27 | return this[EV_MINIMAL_NAME]; 28 | } 29 | } 30 | 31 | [EnumValue(EV_OPTIMAL_NAME)] 32 | public EnumerationValue Optimal 33 | { 34 | get 35 | { 36 | return this[EV_OPTIMAL_NAME]; 37 | } 38 | } 39 | 40 | [EnumValue(EV_MAXIMAL_NAME)] 41 | public EnumerationValue Maximal 42 | { 43 | get 44 | { 45 | return this[EV_MAXIMAL_NAME]; 46 | } 47 | } 48 | 49 | public static ZipCompressionLevelEnum CreateInstance() 50 | { 51 | ZipCompressionLevelEnum instance; 52 | 53 | TypeDescriptor enumType; 54 | TypeDescriptor enumValType; 55 | 56 | EnumContextHelper.RegisterEnumType(out enumType, out enumValType); 57 | 58 | instance = new ZipCompressionLevelEnum(enumType, enumValType); 59 | 60 | EnumContextHelper.RegisterValues(instance); 61 | 62 | return instance; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Zip/ZipCompressionMethodEnum.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using ScriptEngine.Machine.Contexts; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | 8 | namespace ScriptEngine.HostedScript.Library.Zip 9 | { 10 | [SystemEnum("МетодСжатияZIP", "ZIPCompressionMethod")] 11 | public class ZipCompressionMethodEnum : EnumerationContext 12 | { 13 | private const string EV_COPY_NAME = "Копирование"; 14 | private const string EV_DEFLATE_NAME = "Сжатие"; 15 | 16 | private ZipCompressionMethodEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType) 17 | : base(typeRepresentation, valuesType) 18 | { 19 | } 20 | 21 | [EnumValue(EV_COPY_NAME)] 22 | public EnumerationValue Copy 23 | { 24 | get 25 | { 26 | return this[EV_COPY_NAME]; 27 | } 28 | } 29 | 30 | [EnumValue(EV_DEFLATE_NAME)] 31 | public EnumerationValue Deflate 32 | { 33 | get 34 | { 35 | return this[EV_DEFLATE_NAME]; 36 | } 37 | } 38 | 39 | public static ZipCompressionMethodEnum CreateInstance() 40 | { 41 | ZipCompressionMethodEnum instance; 42 | 43 | TypeDescriptor enumType; 44 | TypeDescriptor enumValType; 45 | 46 | EnumContextHelper.RegisterEnumType(out enumType, out enumValType); 47 | 48 | instance = new ZipCompressionMethodEnum(enumType, enumValType); 49 | 50 | EnumContextHelper.RegisterValues(instance); 51 | 52 | return instance; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Zip/ZipEncryptionMethodEnum.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using ScriptEngine.Machine.Contexts; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | 8 | namespace ScriptEngine.HostedScript.Library.Zip 9 | { 10 | [SystemEnum("МетодШифрованияZIP", "ZIPEncryptionMethod")] 11 | public class ZipEncryptionMethodEnum : EnumerationContext 12 | { 13 | private const string EV_AES128 = "AES128"; 14 | private const string EV_AES192 = "AES192"; 15 | private const string EV_AES256 = "AES256"; 16 | private const string EV_ZIP20 = "Zip20"; 17 | 18 | private ZipEncryptionMethodEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType) 19 | : base(typeRepresentation, valuesType) 20 | { 21 | } 22 | 23 | [EnumValue(EV_AES128)] 24 | public EnumerationValue Aes128 25 | { 26 | get 27 | { 28 | return this[EV_AES128]; 29 | } 30 | } 31 | 32 | [EnumValue(EV_AES192)] 33 | public EnumerationValue Aes192 34 | { 35 | get 36 | { 37 | return this[EV_AES192]; 38 | } 39 | } 40 | 41 | [EnumValue(EV_AES256)] 42 | public EnumerationValue Aes256 43 | { 44 | get 45 | { 46 | return this[EV_AES256]; 47 | } 48 | } 49 | 50 | [EnumValue(EV_ZIP20)] 51 | public EnumerationValue Zip20 52 | { 53 | get 54 | { 55 | return this[EV_ZIP20]; 56 | } 57 | } 58 | 59 | public static ZipEncryptionMethodEnum CreateInstance() 60 | { 61 | ZipEncryptionMethodEnum instance; 62 | 63 | TypeDescriptor enumType; 64 | TypeDescriptor enumValType; 65 | 66 | EnumContextHelper.RegisterEnumType(out enumType, out enumValType); 67 | 68 | instance = new ZipEncryptionMethodEnum(enumType, enumValType); 69 | 70 | EnumContextHelper.RegisterValues(instance); 71 | 72 | return instance; 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Zip/ZipFileEntriesCollection.cs: -------------------------------------------------------------------------------- 1 | using Ionic.Zip; 2 | using ScriptEngine.Machine; 3 | using ScriptEngine.Machine.Contexts; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | 9 | namespace ScriptEngine.HostedScript.Library.Zip 10 | { 11 | [ContextClass("ЭлементыZipФайла", "ZipFileEntries")] 12 | public class ZipFileEntriesCollection : AutoContext, ICollectionContext 13 | { 14 | List _entries; 15 | 16 | public ZipFileEntriesCollection(IEnumerable entries) 17 | { 18 | _entries = entries.Select(x => new ZipFileEntryContext(x)).ToList(); 19 | } 20 | 21 | [ContextMethod("Количество", "Count")] 22 | public int Count() 23 | { 24 | return _entries.Count; 25 | } 26 | 27 | [ContextMethod("Получить", "Get")] 28 | public IValue Get(IValue index) 29 | { 30 | return GetIndexedValue(index); 31 | } 32 | 33 | [ContextMethod("Найти", "Find")] 34 | public IValue Find(string name) 35 | { 36 | var entry = _entries.FirstOrDefault(x => System.IO.Path.GetFileName(x.GetZipEntry().FileName) == name); 37 | 38 | if (entry == null) 39 | return ValueFactory.Create(); 40 | 41 | return entry; 42 | } 43 | 44 | public override bool IsIndexed 45 | { 46 | get 47 | { 48 | return true; 49 | } 50 | } 51 | 52 | public override IValue GetIndexedValue(IValue index) 53 | { 54 | int idx = (int)index.AsNumber(); 55 | return _entries[idx]; 56 | } 57 | 58 | public CollectionEnumerator GetManagedIterator() 59 | { 60 | return new CollectionEnumerator(GetEnumerator()); 61 | } 62 | 63 | public IEnumerator GetEnumerator() 64 | { 65 | return _entries.GetEnumerator(); 66 | } 67 | 68 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 69 | { 70 | return GetEnumerator(); 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Zip/ZipRestoreFilePathsModeEnum.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using ScriptEngine.Machine.Contexts; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | 8 | namespace ScriptEngine.HostedScript.Library.Zip 9 | { 10 | [SystemEnum("РежимВосстановленияПутейФайловZIP", "ZIPRestoreFilePathsMode")] 11 | public class ZipRestoreFilePathsModeEnum : EnumerationContext 12 | { 13 | private const string RESTORE_PATHS_NAME = "Восстанавливать"; 14 | private const string DONT_RESTORE_PATHS_NAME = "НеВосстанавливать"; 15 | 16 | private ZipRestoreFilePathsModeEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType) 17 | : base(typeRepresentation, valuesType) 18 | { 19 | } 20 | 21 | [EnumValue(RESTORE_PATHS_NAME)] 22 | public EnumerationValue Restore 23 | { 24 | get 25 | { 26 | return this[RESTORE_PATHS_NAME]; 27 | } 28 | } 29 | 30 | [EnumValue(DONT_RESTORE_PATHS_NAME)] 31 | public EnumerationValue DoNotRestore 32 | { 33 | get 34 | { 35 | return this[DONT_RESTORE_PATHS_NAME]; 36 | } 37 | } 38 | 39 | public static ZipRestoreFilePathsModeEnum CreateInstance() 40 | { 41 | ZipRestoreFilePathsModeEnum instance; 42 | 43 | TypeDescriptor enumType; 44 | TypeDescriptor enumValType; 45 | 46 | EnumContextHelper.RegisterEnumType(out enumType, out enumValType); 47 | 48 | instance = new ZipRestoreFilePathsModeEnum(enumType, enumValType); 49 | 50 | EnumContextHelper.RegisterValues(instance); 51 | 52 | return instance; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Library/Zip/ZipStorePathModeEnum.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | using ScriptEngine.Machine.Contexts; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | 8 | namespace ScriptEngine.HostedScript.Library.Zip 9 | { 10 | [SystemEnum("РежимСохраненияПутейZIP", "ZIPStorePathsMode")] 11 | public class ZipStorePathModeEnum : EnumerationContext 12 | { 13 | const string DONT_SAVE = "НеСохранятьПути"; 14 | const string SAVE_RELATIVE = "СохранятьОтносительныеПути"; 15 | const string SAVE_FULL = "СохранятьПолныеПути"; 16 | 17 | public ZipStorePathModeEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType) 18 | : base(typeRepresentation, valuesType) 19 | { 20 | 21 | } 22 | 23 | [EnumValue(DONT_SAVE)] 24 | public EnumerationValue DontStorePath 25 | { 26 | get 27 | { 28 | return this[DONT_SAVE]; 29 | } 30 | } 31 | 32 | [EnumValue(SAVE_RELATIVE)] 33 | public EnumerationValue StoreRelativePath 34 | { 35 | get 36 | { 37 | return this[SAVE_RELATIVE]; 38 | } 39 | } 40 | 41 | [EnumValue(SAVE_FULL)] 42 | public EnumerationValue StoreFullPath 43 | { 44 | get 45 | { 46 | return this[SAVE_FULL]; 47 | } 48 | } 49 | 50 | public static ZipStorePathModeEnum CreateInstance() 51 | { 52 | ZipStorePathModeEnum instance; 53 | 54 | TypeDescriptor enumType; 55 | TypeDescriptor enumValType; 56 | 57 | EnumContextHelper.RegisterEnumType(out enumType, out enumValType); 58 | 59 | instance = new ZipStorePathModeEnum(enumType, enumValType); 60 | 61 | EnumContextHelper.RegisterValues(instance); 62 | 63 | return instance; 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Process.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ScriptEngine.Environment; 6 | using ScriptEngine.Machine; 7 | 8 | namespace ScriptEngine.HostedScript 9 | { 10 | public class Process 11 | { 12 | ScriptingEngine _engine; 13 | IHostApplication _host; 14 | LoadedModuleHandle _module; 15 | 16 | internal Process(IHostApplication host, LoadedModuleHandle src, ScriptingEngine runtime) 17 | { 18 | _host = host; 19 | _engine = runtime; 20 | _module = src; 21 | } 22 | 23 | public int Start() 24 | { 25 | try 26 | { 27 | _engine.NewObject(_module); 28 | return 0; 29 | } 30 | catch (ScriptInterruptionException e) 31 | { 32 | return e.ExitCode; 33 | } 34 | catch (Exception e) 35 | { 36 | _host.ShowExceptionInfo(e); 37 | return 1; 38 | } 39 | finally 40 | { 41 | //AttachedScriptsFactory.Dispose(); 42 | _engine = null; 43 | } 44 | } 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("ScriptEngine.HostedScript")] 9 | [assembly: AssemblyProduct("ScriptEngine.HostedScript")] 10 | 11 | // Setting ComVisible to false makes the types in this assembly not visible 12 | // to COM components. If you need to access a type in this assembly from 13 | // COM, set the ComVisible attribute to true on that type. 14 | [assembly: ComVisible(false)] 15 | 16 | // The following GUID is for the ID of the typelib if this project is exposed to COM 17 | [assembly: Guid("1b057f7e-4a28-4c51-8620-3c90a75408bf")] 18 | 19 | // Version information for an assembly consists of the following four values: 20 | // 21 | // Major Version 22 | // Minor Version 23 | // Build Number 24 | // Revision 25 | // 26 | // You can specify all the values or you can default the Build and Revision Numbers 27 | // by using the '*' as shown below: 28 | // [assembly: AssemblyVersion("1.0.*")] 29 | -------------------------------------------------------------------------------- /src/ScriptEngine.HostedScript/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/AssemblyInfo.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | 3 | using namespace System; 4 | using namespace System::Reflection; 5 | using namespace System::Runtime::CompilerServices; 6 | using namespace System::Runtime::InteropServices; 7 | using namespace System::Security::Permissions; 8 | 9 | // 10 | // General Information about an assembly is controlled through the following 11 | // set of attributes. Change these attribute values to modify the information 12 | // associated with an assembly. 13 | // 14 | [assembly:AssemblyTitleAttribute("ScriptEngineSnegopat")]; 15 | [assembly:AssemblyDescriptionAttribute("")]; 16 | [assembly:AssemblyConfigurationAttribute("")]; 17 | [assembly:AssemblyCompanyAttribute("")]; 18 | [assembly:AssemblyProductAttribute("ScriptEngineSnegopat")]; 19 | [assembly:AssemblyCopyrightAttribute("Copyright BeaverSoft(c) 2014")]; 20 | [assembly:AssemblyTrademarkAttribute("")]; 21 | [assembly:AssemblyCultureAttribute("")]; 22 | 23 | // 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the value or you can default the Revision and Build Numbers 32 | // by using the '*' as shown below: 33 | 34 | [assembly:AssemblyVersionAttribute("1.0.9.0")]; 35 | 36 | [assembly:ComVisible(false)]; 37 | 38 | [assembly:CLSCompliantAttribute(true)]; 39 | 40 | [assembly:SecurityPermission(SecurityAction::RequestMinimum, UnmanagedCode = true)]; 41 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/CriticalResourceLoader.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "CriticalResourceLoader.h" 3 | 4 | CriticalResourceLoader::CriticalResourceLoader(HMODULE mod) 5 | { 6 | m_module = mod; 7 | m_modulePath = new WCHAR[MAX_PATH+1]; 8 | memset(m_modulePath, 0, (MAX_PATH+1) * sizeof(WCHAR)); 9 | GetModuleFileName(mod, m_modulePath, MAX_PATH); 10 | 11 | System::AppDomain::CurrentDomain->AssemblyResolve += 12 | gcnew System::ResolveEventHandler(this, &CriticalResourceLoader::DependencyHandler); 13 | 14 | } 15 | 16 | Reflection::Assembly^ CriticalResourceLoader::DependencyHandler(Object^ sender, ResolveEventArgs^ args) 17 | { 18 | System::String^ str = args->Name; 19 | if(str->IndexOf(L"ScriptEngine",0) >= 0) 20 | { 21 | 22 | System::IntPtr^ ptr = gcnew System::IntPtr(m_modulePath); 23 | System::String^ pathBuild = System::Runtime::InteropServices::Marshal::PtrToStringUni(*ptr, MAX_PATH); 24 | 25 | int idx = pathBuild->LastIndexOf('\\'); 26 | System::String^ dir = pathBuild->Substring(0, idx + 1); 27 | 28 | idx = str->IndexOf(','); 29 | System::String^ dll = str->Substring(0, idx) + ".dll"; 30 | pathBuild = System::IO::Path::Combine(dir, dll); 31 | 32 | return System::Reflection::Assembly::LoadFrom(pathBuild); 33 | 34 | } 35 | else 36 | { 37 | return nullptr; 38 | } 39 | } 40 | 41 | CriticalResourceLoader::~CriticalResourceLoader(void) 42 | { 43 | delete[] m_modulePath; 44 | } 45 | 46 | bool CriticalResourceLoader::PrepareTypeInfo() 47 | { 48 | return true; 49 | } 50 | 51 | IUnknown* CriticalResourceLoader::GetLoader(IDispatch* pDesigner) 52 | { 53 | IAddinLoader* loader = new IAddinLoaderImpl(pDesigner); 54 | return loader; 55 | } -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/CriticalResourceLoader.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "StdAfx.h" 3 | #include 4 | #include "IAddinImpl.h" 5 | #include "IAddinLoaderImpl.h" 6 | 7 | using namespace System; 8 | 9 | ref class CriticalResourceLoader 10 | { 11 | private: 12 | HMODULE m_module; 13 | WCHAR* m_modulePath; 14 | 15 | Reflection::Assembly^ DependencyHandler(Object^ sender, ResolveEventArgs^ args); 16 | 17 | public: 18 | CriticalResourceLoader(HMODULE); 19 | bool PrepareTypeInfo(); 20 | IUnknown* GetLoader(IDispatch* pDesigner); 21 | ~CriticalResourceLoader(void); 22 | }; 23 | 24 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/DispatchHelpers.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | HRESULT invoke(LPDISPATCH pdisp, 6 | WORD wFlags, 7 | LPVARIANT pvRet, 8 | ::EXCEPINFO FAR* pexcepinfo, 9 | UINT FAR* pnArgErr, 10 | LPOLESTR pszName, 11 | LPCTSTR pszFmt, 12 | ...); 13 | 14 | LPCTSTR getNextVarType(LPCTSTR pszFmt, VARTYPE FAR* pvt); 15 | 16 | HRESULT countArgsInFormat(LPCTSTR pszFmt, UINT FAR *pn); -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/EventCallableSDO.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "EventCallableSDO.h" 3 | 4 | EventCallableSDO::EventCallableSDO(ScriptDrivenObject^ instance, LoadedModuleHandle module) 5 | : ReflectableSDO(instance, module) 6 | { 7 | } 8 | 9 | Object^ EventCallableSDO::InvokeInternal(String^ name, 10 | System::Reflection::BindingFlags invokeAttr, 11 | Binder^ binder, 12 | Object^ target, 13 | array^ args, 14 | System::Globalization::CultureInfo^ culture) 15 | { 16 | array^ passedArgs = gcnew array(args->Length); 17 | array^ wrappers = gcnew array(args->Length); 18 | 19 | try 20 | { 21 | Object^ result; 22 | 23 | for (int i = 0; i < args->Length; i++) 24 | { 25 | IParamsWrapper^ wrapper = dynamic_cast(args[i]); 26 | IValue^ argValue; 27 | if(wrapper != nullptr) 28 | { 29 | wrappers[i] = wrapper; 30 | argValue = COMWrapperContext::CreateIValue(wrapper->val); 31 | } 32 | else 33 | { 34 | argValue = COMWrapperContext::CreateIValue(args[i]); 35 | } 36 | 37 | passedArgs[i] = Variable::CreateReference(Variable::Create(argValue)); 38 | 39 | } 40 | 41 | result = ReflectableSDO::InvokeInternal(name, invokeAttr, binder, target, passedArgs, culture); 42 | 43 | for (int i = 0; i < args->Length; i++) 44 | { 45 | IParamsWrapper^ wrapper = wrappers[i]; 46 | Object^ argValue = COMWrapperContext::MarshalIValue(safe_cast(passedArgs[i])); 47 | if(wrapper != nullptr) 48 | { 49 | wrapper->val = argValue; 50 | } 51 | else 52 | { 53 | args[i] = passedArgs[i]; 54 | } 55 | } 56 | 57 | return result; 58 | 59 | } 60 | catch(Exception^ exc) 61 | { 62 | auto buf = stringBuf(exc->ToString()); 63 | MessageBox(0, buf, L"Error", MB_ICONERROR); 64 | delete[] buf; 65 | 66 | throw; 67 | 68 | } 69 | finally 70 | { 71 | delete[] passedArgs; 72 | delete[] wrappers; 73 | } 74 | } -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/EventCallableSDO.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "MarshalingHelpers.h" 5 | #include "SnegAPIDefinitions.h" 6 | 7 | using namespace System; 8 | using namespace System::Reflection; 9 | using namespace ScriptEngine; 10 | using namespace ScriptEngine::Environment; 11 | using namespace ScriptEngine::Machine; 12 | using namespace ScriptEngine::Machine::Contexts; 13 | 14 | public ref class EventCallableSDO : public ReflectableSDO 15 | { 16 | public: 17 | EventCallableSDO(ScriptDrivenObject^ instance, LoadedModuleHandle module); 18 | 19 | protected: 20 | 21 | virtual Object^ InvokeInternal(String^ name, 22 | System::Reflection::BindingFlags invokeAttr, 23 | Binder^ binder, 24 | Object^ target, 25 | array^ args, 26 | System::Globalization::CultureInfo^ culture) override; 27 | }; 28 | 29 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/IAddinImpl.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xDrivenDevelopment/1script/25f62879a041dce674b09a7b1e6d57f20633f21e/src/ScriptEngine.Snegopat/IAddinImpl.cpp -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/IAddinImpl.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include "Snegopat_h.h" 5 | #include "RefCountable.h" 6 | #include 7 | 8 | using namespace System; 9 | using namespace ScriptEngine; 10 | 11 | class IAddinImpl : 12 | public RefCountable, 13 | public IAddinMacroses 14 | { 15 | private: 16 | gcroot m_innerObject; 17 | 18 | BSTR m_uniqueName; 19 | BSTR m_displayName; 20 | BSTR m_fullPath; 21 | 22 | public: 23 | 24 | IAddinImpl(Machine::Contexts::ScriptDrivenObject^ innerObject); 25 | 26 | void SetNames(BSTR uniqueName, BSTR displayName, BSTR fullPath) 27 | { 28 | m_uniqueName = uniqueName; 29 | m_displayName = displayName; 30 | m_fullPath = fullPath; 31 | } 32 | 33 | ScriptEngine::Machine::Contexts::ScriptDrivenObject^ 34 | GetManagedInstance() 35 | { 36 | return m_innerObject; 37 | } 38 | 39 | //IUnknown interface 40 | virtual HRESULT __stdcall QueryInterface( 41 | REFIID riid, 42 | void **ppObj); 43 | virtual ULONG __stdcall AddRef(); 44 | virtual ULONG __stdcall Release(); 45 | 46 | // IAddIn interface 47 | 48 | virtual HRESULT STDMETHODCALLTYPE macroses(SAFEARRAY **result); 49 | 50 | virtual HRESULT STDMETHODCALLTYPE invokeMacros(BSTR MacrosName, VARIANT *result); 51 | 52 | virtual void OnZeroCount(); 53 | 54 | ~IAddinImpl(void); 55 | 56 | }; 57 | 58 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/IAddinLoaderImpl.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xDrivenDevelopment/1script/25f62879a041dce674b09a7b1e6d57f20633f21e/src/ScriptEngine.Snegopat/IAddinLoaderImpl.cpp -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/IAddinLoaderImpl.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Stdafx.h" 4 | #include "Snegopat_h.h" 5 | #include "RefCountable.h" 6 | #include "IAddinImpl.h" 7 | #include "SnegopatAttachedContext.h" 8 | #include "LibraryAttachedContext.h" 9 | #include "ScriptDrivenAddin.h" 10 | 11 | #include 12 | 13 | using namespace System; 14 | using namespace ScriptEngine; 15 | using namespace ScriptEngine::Machine; 16 | using namespace ScriptEngine::Compiler; 17 | using namespace ScriptEngine::Environment; 18 | using namespace System::Runtime::InteropServices; 19 | 20 | class IAddinLoaderImpl : 21 | public RefCountable, 22 | public IAddinLoader 23 | { 24 | private: 25 | 26 | IDispatch* m_pDesigner; 27 | gcroot m_engine; 28 | 29 | struct addinNames 30 | { 31 | BSTR uniqueName; 32 | BSTR displayName; 33 | }; 34 | 35 | ScriptDrivenAddin^ LoadFromScriptFile(String^ path, addinNames* names); 36 | ScriptDrivenAddin^ LoadFromDialog(String^ path, addinNames* names); 37 | 38 | protected: 39 | virtual void OnZeroCount(); 40 | 41 | public: 42 | IAddinLoaderImpl(IDispatch* pDesigner); 43 | 44 | //IUnknown interface 45 | virtual HRESULT __stdcall QueryInterface( 46 | REFIID riid, 47 | void **ppObj); 48 | virtual ULONG __stdcall AddRef(); 49 | virtual ULONG __stdcall Release(); 50 | 51 | virtual HRESULT __stdcall proto( 52 | BSTR *result); 53 | 54 | virtual HRESULT __stdcall load( 55 | BSTR uri, 56 | BSTR *fullPath, 57 | BSTR *uniqueName, 58 | BSTR *displayName, 59 | IUnknown **result); 60 | 61 | virtual HRESULT __stdcall canUnload( 62 | BSTR fullPath, 63 | IUnknown *addin, 64 | VARIANT_BOOL *result); 65 | 66 | virtual HRESULT __stdcall unload( 67 | BSTR fullPath, 68 | IUnknown *addin, 69 | VARIANT_BOOL *result); 70 | 71 | virtual HRESULT __stdcall loadCommandName( 72 | BSTR *result); 73 | 74 | virtual HRESULT __stdcall selectLoadURI( 75 | BSTR *result); 76 | 77 | virtual ~IAddinLoaderImpl(void); 78 | }; 79 | 80 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/LibraryAttachedContext.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xDrivenDevelopment/1script/25f62879a041dce674b09a7b1e6d57f20633f21e/src/ScriptEngine.Snegopat/LibraryAttachedContext.cpp -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/LibraryAttachedContext.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "ScriptDrivenAddin.h" 4 | 5 | using namespace System; 6 | using namespace System::Collections::Generic; 7 | using namespace ScriptEngine; 8 | using namespace ScriptEngine::Machine; 9 | using namespace ScriptEngine::Machine::Contexts; 10 | using namespace System::Runtime::InteropServices; 11 | 12 | ref class LibraryAttachedContext : public IAttachableContext, public IRuntimeContextInstance 13 | { 14 | private: 15 | 16 | ScriptingEngine^ m_engine; 17 | array^ m_methods; 18 | Dictionary^ m_methodIndexes; 19 | 20 | void DisposeObject(IValue^ object); 21 | 22 | public: 23 | LibraryAttachedContext(ScriptingEngine^ engine); 24 | 25 | virtual void OnAttach(MachineInstance^ machine, 26 | [Out] cli::array^% variables, 27 | [Out] cli::array^% methods, 28 | [Out] IRuntimeContextInstance^% instance); 29 | 30 | virtual IEnumerable^ GetProperties(); 31 | virtual IEnumerable^ GetMethods(); 32 | 33 | property bool IsIndexed 34 | { 35 | virtual bool get() { return false; } 36 | }; 37 | 38 | property bool DynamicMethodSignatures 39 | { 40 | virtual bool get() { return false; } 41 | }; 42 | 43 | virtual IValue^ GetIndexedValue(IValue^ index){ return nullptr; } 44 | virtual void SetIndexedValue(IValue^ index, IValue^ val){} 45 | 46 | virtual int FindProperty(String^ name); 47 | virtual bool IsPropReadable(int propNum); 48 | virtual bool IsPropWritable(int propNum); 49 | virtual IValue^ GetPropValue(int propNum); 50 | virtual void SetPropValue(int propNum, IValue^ val); 51 | virtual int FindMethod(String^ mName); 52 | virtual ScriptEngine::Machine::MethodInfo GetMethodInfo(int mNum); 53 | virtual void CallAsProcedure(int mNum, array^ args); 54 | virtual void CallAsFunction(int mNum, array^ args, [Out] IValue^% retVal); 55 | 56 | }; 57 | 58 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/MarshalingHelpers.cpp: -------------------------------------------------------------------------------- 1 | #include "Stdafx.h" 2 | #include "MarshalingHelpers.h" 3 | 4 | WCHAR* stringBuf(System::String^ str) 5 | { 6 | int len = str->Length; 7 | WCHAR* buf = new WCHAR[len+1]; 8 | memset(buf, 0, (len+1) * sizeof(WCHAR)); 9 | for(int i = 0; i < len; i++) 10 | { 11 | buf[i] = str[i]; 12 | } 13 | 14 | return buf; 15 | } 16 | 17 | BSTR stringToBSTR(System::String^ str) 18 | { 19 | WCHAR* buf = stringBuf(str); 20 | BSTR ret = SysAllocString(buf); 21 | delete[] buf; 22 | 23 | return ret; 24 | } -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/MarshalingHelpers.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | WCHAR* stringBuf(System::String^ str); 6 | 7 | BSTR stringToBSTR(System::String^ str); 8 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/ReadMe.txt: -------------------------------------------------------------------------------- 1 | ======================================================================== 2 | DYNAMIC LINK LIBRARY : ScriptEngine.Snegopat Project Overview 3 | ======================================================================== 4 | 5 | AppWizard has created this ScriptEngine.Snegopat DLL for you. 6 | 7 | This file contains a summary of what you will find in each of the files that 8 | make up your ScriptEngine.Snegopat application. 9 | 10 | ScriptEngine.Snegopat.vcxproj 11 | This is the main project file for VC++ projects generated using an Application Wizard. 12 | It contains information about the version of Visual C++ that generated the file, and 13 | information about the platforms, configurations, and project features selected with the 14 | Application Wizard. 15 | 16 | ScriptEngine.Snegopat.vcxproj.filters 17 | This is the filters file for VC++ projects generated using an Application Wizard. 18 | It contains information about the association between the files in your project 19 | and the filters. This association is used in the IDE to show grouping of files with 20 | similar extensions under a specific node (for e.g. ".cpp" files are associated with the 21 | "Source Files" filter). 22 | 23 | ScriptEngine.Snegopat.cpp 24 | This is the main DLL source file. 25 | 26 | ScriptEngine.Snegopat.h 27 | This file contains a class declaration. 28 | 29 | AssemblyInfo.cpp 30 | Contains custom attributes for modifying assembly metadata. 31 | 32 | ///////////////////////////////////////////////////////////////////////////// 33 | Other notes: 34 | 35 | AppWizard uses "TODO:" to indicate parts of the source code you 36 | should add to or customize. 37 | 38 | ///////////////////////////////////////////////////////////////////////////// 39 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/RefCountable.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "RefCountable.h" 3 | 4 | RefCountable::RefCountable(void) 5 | { 6 | m_refCount = 0; 7 | } 8 | 9 | ULONG __stdcall RefCountable::AddRef() 10 | { 11 | return InterlockedIncrement(&m_refCount) ; 12 | } 13 | 14 | ULONG __stdcall RefCountable::Release() 15 | { 16 | long nRefCount = 0; 17 | nRefCount = InterlockedDecrement(&m_refCount) ; 18 | if (nRefCount == 0) 19 | { 20 | OnZeroCount(); 21 | delete this; 22 | } 23 | 24 | return nRefCount; 25 | } 26 | 27 | void RefCountable::OnZeroCount() 28 | { 29 | } 30 | 31 | RefCountable::~RefCountable(void) 32 | { 33 | } 34 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/RefCountable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Snegopat_h.h" 3 | 4 | class RefCountable 5 | { 6 | private: 7 | ULONG m_refCount; 8 | 9 | protected: 10 | 11 | virtual void OnZeroCount(); 12 | 13 | public: 14 | 15 | RefCountable(void); 16 | virtual ~RefCountable(void); 17 | 18 | virtual ULONG __stdcall AddRef(); 19 | virtual ULONG __stdcall Release(); 20 | 21 | }; 22 | 23 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/ScriptDrivenAddin.cpp: -------------------------------------------------------------------------------- 1 | #include "stdafx.h" 2 | #include "ScriptDrivenAddin.h" 3 | 4 | 5 | ScriptDrivenAddin::ScriptDrivenAddin(LoadedModuleHandle module) 6 | : ScriptDrivenObject(module, true) 7 | { 8 | m_marshalledReference = gcnew EventCallableSDO(this, module); 9 | m_valuesByIndex = gcnew List(); 10 | m_names = gcnew Dictionary(StringComparer::InvariantCultureIgnoreCase); 11 | } 12 | 13 | ScriptDrivenAddin::~ScriptDrivenAddin() 14 | { 15 | if(m_valuesByIndex != nullptr) 16 | { 17 | for (int i = 0; i < m_valuesByIndex->Count; i++) 18 | { 19 | if(Marshal::IsComObject(m_valuesByIndex[i])) 20 | { 21 | Marshal::ReleaseComObject(m_valuesByIndex[i]); 22 | } 23 | } 24 | m_valuesByIndex->Clear(); 25 | m_names->Clear(); 26 | m_valuesByIndex = nullptr; 27 | } 28 | } 29 | 30 | Object^ ScriptDrivenAddin::UnderlyingObject::get() 31 | { 32 | return m_marshalledReference; 33 | } 34 | 35 | void ScriptDrivenAddin::AddProperty(String^ name, IValue^ value) 36 | { 37 | int newIndex = m_valuesByIndex->Count; 38 | m_valuesByIndex->Add(value); 39 | m_names->Add(name, newIndex); 40 | } 41 | 42 | int ScriptDrivenAddin::FindOwnProperty(String^ name) 43 | { 44 | int index = -1; 45 | if(m_names->TryGetValue(name, index)) 46 | { 47 | return index; 48 | } 49 | else 50 | { 51 | return -1; 52 | } 53 | } 54 | 55 | IValue^ ScriptDrivenAddin::GetOwnPropValue(int index) 56 | { 57 | return m_valuesByIndex[index]; 58 | } -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/ScriptDrivenAddin.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "EventCallableSDO.h" 4 | 5 | using namespace System; 6 | using namespace System::Collections::Generic; 7 | using namespace ScriptEngine; 8 | using namespace ScriptEngine::Machine; 9 | using namespace ScriptEngine::Machine::Contexts; 10 | 11 | ref class ScriptDrivenAddin : public ScriptDrivenObject, public IObjectWrapper 12 | { 13 | private: 14 | 15 | EventCallableSDO^ m_marshalledReference; 16 | List^ m_valuesByIndex; 17 | Dictionary^ m_names; 18 | 19 | public: 20 | ScriptDrivenAddin(LoadedModuleHandle module); 21 | virtual ~ScriptDrivenAddin(); 22 | 23 | virtual property Object^ UnderlyingObject 24 | { 25 | Object^ get(); 26 | } 27 | 28 | void AddProperty(String^ name, IValue^ value); 29 | 30 | protected: 31 | 32 | virtual int GetMethodCount() override 33 | { 34 | return 0; 35 | } 36 | 37 | virtual int GetVariableCount() override 38 | { 39 | return m_valuesByIndex->Count; 40 | } 41 | 42 | virtual int FindOwnProperty(String^ name) override; 43 | virtual bool IsOwnPropReadable(int index) override 44 | { 45 | return true; 46 | } 47 | virtual bool IsOwnPropWritable(int index) override 48 | { 49 | return false; 50 | } 51 | virtual IValue^ GetOwnPropValue(int index) override; 52 | 53 | virtual void UpdateState() override { }; 54 | 55 | }; 56 | 57 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/SnegAPIDefinitions.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xDrivenDevelopment/1script/25f62879a041dce674b09a7b1e6d57f20633f21e/src/ScriptEngine.Snegopat/SnegAPIDefinitions.h -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/Snegopat.idl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xDrivenDevelopment/1script/25f62879a041dce674b09a7b1e6d57f20633f21e/src/ScriptEngine.Snegopat/Snegopat.idl -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/SnegopatAttachedContext.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xDrivenDevelopment/1script/25f62879a041dce674b09a7b1e6d57f20633f21e/src/ScriptEngine.Snegopat/SnegopatAttachedContext.cpp -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/SnegopatAttachedContext.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | using namespace System; 6 | using namespace System::Collections::Generic; 7 | using namespace ScriptEngine; 8 | using namespace ScriptEngine::Machine; 9 | using namespace ScriptEngine::Compiler; 10 | using namespace ScriptEngine::Environment; 11 | using namespace System::Runtime::InteropServices; 12 | 13 | /* Defines global context for snegopat script 14 | */ 15 | ref class SnegopatAttachedContext : public IAttachableContext, public IRuntimeContextInstance 16 | { 17 | private: 18 | 19 | IRuntimeContextInstance^ m_DesignerWrapper; 20 | List^ m_varList; 21 | List^ m_nameList; 22 | List^ m_methods; 23 | Dictionary^ m_propDispIdMap; 24 | Dictionary^ m_methDispIdMap; 25 | Dictionary^ m_propIndexes; 26 | Dictionary^ m_methIndexes; 27 | 28 | void InsertProperty(String^ name); 29 | void PropertyAlias(String^ name, String^ alias); 30 | void InsertMethod(String^ name); 31 | void MethodAlias(String^ name, String^ alias); 32 | 33 | static SnegopatAttachedContext^ s_instance; 34 | 35 | public: 36 | SnegopatAttachedContext(IRuntimeContextInstance^ Designer); 37 | 38 | virtual void OnAttach(MachineInstance^ machine, 39 | [Out] cli::array^% variables, 40 | [Out] cli::array^% methods, 41 | [Out] IRuntimeContextInstance^% instance); 42 | 43 | virtual IEnumerable^ GetProperties(); 44 | virtual IEnumerable^ GetMethods(); 45 | 46 | property bool IsIndexed 47 | { 48 | virtual bool get() { return false; } 49 | }; 50 | 51 | property bool DynamicMethodSignatures 52 | { 53 | virtual bool get() { return true; } 54 | }; 55 | 56 | virtual IValue^ GetIndexedValue(IValue^ index){ return nullptr; } 57 | virtual void SetIndexedValue(IValue^ index, IValue^ val){} 58 | 59 | virtual int FindProperty(String^ name); 60 | virtual bool IsPropReadable(int propNum); 61 | virtual bool IsPropWritable(int propNum); 62 | virtual IValue^ GetPropValue(int propNum); 63 | virtual void SetPropValue(int propNum, IValue^ val); 64 | virtual int FindMethod(String^ mName); 65 | virtual ScriptEngine::Machine::MethodInfo GetMethodInfo(int mNum); 66 | virtual void CallAsProcedure(int mNum, array^ args); 67 | virtual void CallAsFunction(int mNum, array^ args, [Out] IValue^% retVal); 68 | 69 | [ScriptEngine::Machine::Contexts::ScriptConstructor(ParametrizeWithClassName = true)] 70 | static IRuntimeContextInstance^ CreateV8New(String^ className, array^ args); 71 | 72 | static void SetInstance(SnegopatAttachedContext^ instance) 73 | { 74 | s_instance = instance; 75 | } 76 | 77 | }; 78 | 79 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/Stdafx.cpp: -------------------------------------------------------------------------------- 1 | // stdafx.cpp : source file that includes just the standard includes 2 | // ScriptEngine.Snegopat.pch will be the pre-compiled header 3 | // stdafx.obj will contain the pre-compiled type information 4 | 5 | #include "stdafx.h" 6 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/Stdafx.h: -------------------------------------------------------------------------------- 1 | // stdafx.h : include file for standard system include files, 2 | // or project specific include files that are used frequently, 3 | // but are changed infrequently 4 | 5 | #pragma once 6 | 7 | 8 | -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/app.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xDrivenDevelopment/1script/25f62879a041dce674b09a7b1e6d57f20633f21e/src/ScriptEngine.Snegopat/app.ico -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/app.rc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xDrivenDevelopment/1script/25f62879a041dce674b09a7b1e6d57f20633f21e/src/ScriptEngine.Snegopat/app.rc -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/clrfactory.cpp: -------------------------------------------------------------------------------- 1 | #include "StdAfx.h" 2 | #include 3 | #include 4 | 5 | #include "CriticalResourceLoader.h" 6 | 7 | gcroot g_ResLoader; 8 | 9 | void InitLibrary(HMODULE module) 10 | { 11 | g_ResLoader = gcnew CriticalResourceLoader(module); 12 | } 13 | 14 | IUnknown* GetLoader(IDispatch* pDesigner) 15 | { 16 | return g_ResLoader->GetLoader(pDesigner); 17 | } 18 | 19 | bool PrepareTypeInfo() 20 | { 21 | return g_ResLoader->PrepareTypeInfo(); 22 | } -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/resource.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xDrivenDevelopment/1script/25f62879a041dce674b09a7b1e6d57f20633f21e/src/ScriptEngine.Snegopat/resource.h -------------------------------------------------------------------------------- /src/ScriptEngine.Snegopat/snegopat.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xDrivenDevelopment/1script/25f62879a041dce674b09a7b1e6d57f20633f21e/src/ScriptEngine.Snegopat/snegopat.cpp -------------------------------------------------------------------------------- /src/ScriptEngine/CodePositionInfo.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine 7 | { 8 | class CodePositionInfo 9 | { 10 | public string ModuleName { get; set; } 11 | public int LineNumber { get; set; } 12 | public string Code { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/ScriptEngine/Compiler/ICompilerContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using ScriptEngine.Machine; 3 | 4 | namespace ScriptEngine.Compiler 5 | { 6 | interface ICompilerContext 7 | { 8 | SymbolBinding DefineMethod(ScriptEngine.Machine.MethodInfo method); 9 | SymbolBinding DefineProperty(string name); 10 | SymbolBinding DefineVariable(string name); 11 | SymbolBinding GetMethod(string name); 12 | SymbolScope GetScope(int scopeIndex); 13 | VariableBinding GetVariable(string name); 14 | SymbolScope Peek(); 15 | SymbolScope PopScope(); 16 | void PushScope(SymbolScope scope); 17 | int ScopeIndex(SymbolScope scope); 18 | int TopIndex(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/ScriptEngine/Compiler/ModulePersistor.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Environment; 2 | using ScriptEngine.Machine; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Runtime.Serialization; 8 | using System.Text; 9 | 10 | namespace ScriptEngine.Compiler 11 | { 12 | public class ModulePersistor 13 | { 14 | IFormatter _formatter; 15 | 16 | public ModulePersistor (IFormatter format) 17 | { 18 | _formatter = format; 19 | } 20 | 21 | public void Save(ScriptModuleHandle module, Stream output) 22 | { 23 | _formatter.Serialize(output, FromHandle(module)); 24 | } 25 | 26 | public ScriptModuleHandle Read(Stream input) 27 | { 28 | var moduleImage = (ModuleImage)_formatter.Deserialize(input); 29 | return new ScriptModuleHandle() 30 | { 31 | Module = moduleImage 32 | }; 33 | } 34 | 35 | private ModuleImage FromHandle(ScriptModuleHandle module) 36 | { 37 | return module.Module; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/ScriptEngine/Compiler/ParserException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ScriptEngine.Compiler 4 | { 5 | public class ParserException : ScriptException 6 | { 7 | internal ParserException(CodePositionInfo posInfo, string message) 8 | :base(posInfo, message) 9 | { 10 | } 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /src/ScriptEngine/Compiler/SourceCodeIndexer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Compiler 7 | { 8 | class SourceCodeIndexer 9 | { 10 | private string _code; 11 | private IList _lineBounds; 12 | 13 | public SourceCodeIndexer(string code, IList lineBounds) 14 | { 15 | _code = code; 16 | _lineBounds = lineBounds; 17 | } 18 | 19 | public string GetCodeLine(int index) 20 | { 21 | int start = GetLineBound(index); 22 | int end = _code.IndexOf('\n', start); 23 | if (end >= 0) 24 | { 25 | return _code.Substring(start, end - start); 26 | } 27 | else 28 | { 29 | return _code.Substring(start); 30 | } 31 | } 32 | 33 | private int GetLineBound(int lineNumber) 34 | { 35 | return _lineBounds[lineNumber - 1]; 36 | } 37 | 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/ScriptEngine/Environment/CodeSources.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ScriptEngine.Compiler; 6 | 7 | namespace ScriptEngine.Environment 8 | { 9 | class StringBasedSource : ICodeSource 10 | { 11 | string _src; 12 | 13 | public StringBasedSource(string src) 14 | { 15 | _src = src; 16 | } 17 | 18 | #region ICodeSource Members 19 | 20 | string ICodeSource.Code 21 | { 22 | get 23 | { 24 | return _src; 25 | } 26 | } 27 | 28 | string ICodeSource.SourceDescription 29 | { 30 | get 31 | { 32 | return ""; 33 | } 34 | } 35 | 36 | #endregion 37 | 38 | } 39 | 40 | class FileBasedSource : ICodeSource 41 | { 42 | string _path; 43 | string _code; 44 | 45 | public FileBasedSource(string path) 46 | { 47 | _path = path; 48 | } 49 | 50 | private string GetCodeString() 51 | { 52 | if (_code == null) 53 | { 54 | using (var reader = FileOpener.OpenReader(_path)) 55 | { 56 | _code = reader.ReadToEnd(); 57 | } 58 | } 59 | 60 | return _code; 61 | } 62 | 63 | #region ICodeSource Members 64 | 65 | string ICodeSource.Code 66 | { 67 | get 68 | { 69 | return GetCodeString(); 70 | } 71 | } 72 | 73 | string ICodeSource.SourceDescription 74 | { 75 | get { return _path; } 76 | } 77 | 78 | #endregion 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/ScriptEngine/Environment/FileOpener.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace ScriptEngine.Environment 8 | { 9 | public static class FileOpener 10 | { 11 | 12 | public static StreamReader OpenReader(string filename) 13 | { 14 | FileStream input = new FileStream(filename, FileMode.Open, FileAccess.Read); 15 | Encoding enc = AssumeEncoding(input); 16 | 17 | var reader = new StreamReader(input, enc, true); 18 | 19 | #if __MonoCS__ 20 | bool skipFirstLine = IsLinuxScript (input); 21 | if(skipFirstLine) 22 | reader.ReadLine(); 23 | #endif 24 | return reader; 25 | 26 | } 27 | 28 | public static StreamReader OpenReader(string filename, Encoding encoding) 29 | { 30 | return new StreamReader(filename, encoding); 31 | } 32 | 33 | public static StreamWriter OpenWriter(string filename) 34 | { 35 | return new StreamWriter(filename, false, Encoding.UTF8); 36 | } 37 | 38 | public static StreamWriter OpenWriter(string filename, Encoding encoding) 39 | { 40 | return new StreamWriter(filename, false, encoding); 41 | } 42 | 43 | public static StreamWriter OpenWriter(string filename, Encoding encoding, bool append) 44 | { 45 | return new StreamWriter(filename, append, encoding); 46 | } 47 | 48 | public static Encoding AssumeEncoding(Stream inputStream) 49 | { 50 | Encoding enc; 51 | // *** Use Default of Encoding.Default (Ansi CodePage) 52 | enc = Encoding.Default; 53 | 54 | // *** Detect byte order mark if any - otherwise assume default 55 | byte[] buffer = new byte[5]; 56 | 57 | inputStream.Read(buffer, 0, 5); 58 | inputStream.Position = 0; 59 | 60 | if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf) 61 | enc = Encoding.UTF8; 62 | else if (buffer[0] == 0xfe && buffer[1] == 0xff) 63 | enc = Encoding.Unicode; 64 | else if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff) 65 | enc = Encoding.UTF32; 66 | else if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76) 67 | enc = Encoding.UTF7; 68 | 69 | return enc; 70 | } 71 | 72 | #if __MonoCS__ 73 | 74 | static bool IsLinuxScript (FileStream input) 75 | { 76 | byte[] buf = new byte[2]; 77 | bool skipLine = false; 78 | if (input.Read (buf, 0, 2) > 0) { 79 | if (buf [0] == 0x23 && buf [1] == 0x21) 80 | skipLine = true; 81 | } 82 | 83 | input.Position = 0; 84 | return skipLine; 85 | } 86 | #endif 87 | 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/ScriptEngine/Environment/ICodeSource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ScriptEngine.Machine; 6 | using ScriptEngine.Machine.Contexts; 7 | 8 | namespace ScriptEngine.Environment 9 | { 10 | public interface ICodeSource 11 | { 12 | string Code { get; } 13 | string SourceDescription { get; } 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/ScriptEngine/Environment/ICodeSourceFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Environment 7 | { 8 | public interface ICodeSourceFactory 9 | { 10 | ICodeSource FromFile(string path); 11 | ICodeSource FromString(string code); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/ScriptEngine/Environment/ModuleInformation.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Compiler; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace ScriptEngine.Environment 8 | { 9 | class ModuleInformation 10 | { 11 | public string ModuleName { get; set; } 12 | public SourceCodeIndexer CodeIndexer { get; set; } 13 | public string Origin { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/ScriptEngine/Environment/ScriptModuleHandle.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Machine; 2 | 3 | namespace ScriptEngine.Environment 4 | { 5 | public struct ScriptModuleHandle 6 | { 7 | internal ModuleImage Module { get; set; } 8 | } 9 | 10 | public struct LoadedModuleHandle 11 | { 12 | internal LoadedModule Module { get; set; } 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/ScriptEngine/Environment/ScriptSourceFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ScriptEngine.Compiler; 6 | 7 | namespace ScriptEngine.Environment 8 | { 9 | class ScriptSourceFactory : ICodeSourceFactory 10 | { 11 | public ICodeSource FromString(string source) 12 | { 13 | return new StringBasedSource(source); 14 | } 15 | 16 | public ICodeSource FromFile(string path) 17 | { 18 | return new FileBasedSource(path); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/CollectionEnumerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | public class CollectionEnumerator : IValue, IEnumerator 9 | { 10 | private IEnumerator _iterator; 11 | 12 | public CollectionEnumerator(IEnumerator realEnumerator) 13 | { 14 | _iterator = realEnumerator; 15 | } 16 | 17 | #region IEnumerator Members 18 | 19 | public IValue Current 20 | { 21 | get { return _iterator.Current; } 22 | } 23 | 24 | #endregion 25 | 26 | #region IDisposable Members 27 | 28 | public void Dispose() 29 | { 30 | Dispose(true); 31 | } 32 | 33 | protected void Dispose(bool disposing) 34 | { 35 | if (disposing) 36 | { 37 | _iterator.Dispose(); 38 | _iterator = null; 39 | GC.SuppressFinalize(this); 40 | } 41 | } 42 | 43 | #endregion 44 | 45 | #region IEnumerator Members 46 | 47 | object System.Collections.IEnumerator.Current 48 | { 49 | get { return _iterator.Current; } 50 | } 51 | 52 | public bool MoveNext() 53 | { 54 | return _iterator.MoveNext(); 55 | } 56 | 57 | public void Reset() 58 | { 59 | _iterator.Reset(); 60 | } 61 | 62 | #endregion 63 | 64 | #region IValue Members 65 | 66 | public DataType DataType 67 | { 68 | get { throw new NotImplementedException(); } 69 | } 70 | 71 | public TypeDescriptor SystemType 72 | { 73 | get { throw new NotImplementedException(); } 74 | } 75 | 76 | public decimal AsNumber() 77 | { 78 | throw new NotImplementedException(); 79 | } 80 | 81 | public DateTime AsDate() 82 | { 83 | throw new NotImplementedException(); 84 | } 85 | 86 | public bool AsBoolean() 87 | { 88 | throw new NotImplementedException(); 89 | } 90 | 91 | public string AsString() 92 | { 93 | throw new NotImplementedException(); 94 | } 95 | 96 | public IRuntimeContextInstance AsObject() 97 | { 98 | throw new NotImplementedException(); 99 | } 100 | 101 | public IValue GetRawValue() 102 | { 103 | throw new NotImplementedException(); 104 | } 105 | 106 | #endregion 107 | 108 | #region IComparable Members 109 | 110 | public int CompareTo(IValue other) 111 | { 112 | throw new NotImplementedException(); 113 | } 114 | 115 | #endregion 116 | 117 | #region IEquatable Members 118 | 119 | public bool Equals(IValue other) 120 | { 121 | return false; 122 | } 123 | 124 | #endregion 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/ContextClassAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ScriptEngine.Machine.Contexts 4 | { 5 | [AttributeUsage(AttributeTargets.Class)] 6 | public class ContextClassAttribute : Attribute 7 | { 8 | string _name; 9 | string _alias; 10 | 11 | public ContextClassAttribute(string typeName, string typeAlias = "") 12 | { 13 | _name = typeName; 14 | _alias = typeAlias; 15 | } 16 | 17 | public string GetName() 18 | { 19 | return _name; 20 | } 21 | 22 | public string GetAlias() 23 | { 24 | return _alias; 25 | } 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/DynamicPropertiesAccessor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | public abstract class DynamicPropertiesAccessor : PropertyNameIndexAccessor, IReflectableContext 9 | { 10 | private DynamicPropertiesHolder _propHolder; 11 | 12 | public DynamicPropertiesAccessor() 13 | { 14 | _propHolder = new DynamicPropertiesHolder(); 15 | } 16 | 17 | protected int RegisterProperty(string name) 18 | { 19 | return _propHolder.RegisterProperty(name); 20 | } 21 | 22 | protected void RemoveProperty(string name) 23 | { 24 | _propHolder.RemoveProperty(name); 25 | } 26 | 27 | protected void ReorderPropertyNumbers() 28 | { 29 | _propHolder.ReorderPropertyNumbers(); 30 | } 31 | 32 | protected void ClearProperties() 33 | { 34 | _propHolder.ClearProperties(); 35 | } 36 | 37 | protected virtual IEnumerable> GetProperties() 38 | { 39 | return _propHolder.GetProperties(); 40 | } 41 | 42 | #region IRuntimeContextInstance Members 43 | 44 | public override bool IsIndexed 45 | { 46 | get { return true; } 47 | } 48 | 49 | public override int FindProperty(string name) 50 | { 51 | try 52 | { 53 | return _propHolder.GetPropertyNumber(name); 54 | } 55 | catch (KeyNotFoundException) 56 | { 57 | throw RuntimeException.PropNotFoundException(name); 58 | } 59 | } 60 | 61 | public override bool IsPropReadable(int propNum) 62 | { 63 | return true; 64 | } 65 | 66 | public override bool IsPropWritable(int propNum) 67 | { 68 | return true; 69 | } 70 | 71 | #endregion 72 | 73 | 74 | IEnumerable IReflectableContext.GetProperties() 75 | { 76 | var props = this.GetProperties(); 77 | 78 | var result = new List(); 79 | 80 | foreach (var prop in props) 81 | { 82 | result.Add(new VariableInfo() 83 | { 84 | Identifier = prop.Key, 85 | Index = prop.Value, 86 | Type = SymbolType.ContextProperty 87 | }); 88 | } 89 | 90 | return result; 91 | } 92 | 93 | public IEnumerable GetMethods() 94 | { 95 | throw new NotImplementedException(); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/DynamicPropertiesHolder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | public class DynamicPropertiesHolder 9 | { 10 | private Dictionary _propNumbers = new Dictionary(StringComparer.InvariantCultureIgnoreCase); 11 | 12 | public int RegisterProperty(string name) 13 | { 14 | if (_propNumbers.ContainsKey(name)) 15 | { 16 | return _propNumbers[name]; 17 | } 18 | else 19 | { 20 | if (!IsValidIdentifier(name)) 21 | { 22 | throw new RuntimeException("Неверное значение аргумента"); 23 | } 24 | 25 | var idx = _propNumbers.Count; 26 | _propNumbers.Add(name, idx); 27 | return idx; 28 | } 29 | } 30 | 31 | public void RemoveProperty(string name) 32 | { 33 | _propNumbers.Remove(name); 34 | } 35 | 36 | public void ReorderPropertyNumbers() 37 | { 38 | var sorted = _propNumbers.OrderBy(x => x.Value).Select(x => x.Key).ToArray(); 39 | _propNumbers.Clear(); 40 | for (int i = 0; i < sorted.Length; i++) 41 | { 42 | _propNumbers.Add(sorted[i], i); 43 | } 44 | } 45 | 46 | public void ClearProperties() 47 | { 48 | _propNumbers.Clear(); 49 | } 50 | 51 | public int GetPropertyNumber(string name) 52 | { 53 | try 54 | { 55 | return _propNumbers[name]; 56 | } 57 | catch (KeyNotFoundException) 58 | { 59 | throw RuntimeException.PropNotFoundException(name); 60 | } 61 | } 62 | 63 | public IEnumerable> GetProperties() 64 | { 65 | return _propNumbers.AsEnumerable(); 66 | } 67 | 68 | private bool IsValidIdentifier(string name) 69 | { 70 | return Utils.IsValidIdentifier(name); 71 | } 72 | 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/EnumContextHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | public static class EnumContextHelper 9 | { 10 | public static void RegisterValues(T instance) where T : EnumerationContext 11 | { 12 | var enumType = typeof(T); 13 | var values = enumType.GetProperties() 14 | .Where(x => x.GetCustomAttributes(typeof(EnumValueAttribute), false).Any()) 15 | .Select(x => (EnumValueAttribute)x.GetCustomAttributes(typeof(EnumValueAttribute), false)[0]); 16 | 17 | foreach (var enumProperty in values) 18 | { 19 | instance.AddValue(enumProperty.GetName(), new SelfAwareEnumValue(instance)); 20 | } 21 | } 22 | 23 | public static void RegisterEnumType(out TypeDescriptor enumType, out TypeDescriptor enumValueType) where T : EnumerationContext 24 | { 25 | var enumClassType = typeof(T); 26 | var attribs = enumClassType.GetCustomAttributes(typeof(SystemEnumAttribute), false); 27 | 28 | if (attribs.Length == 0) 29 | throw new InvalidOperationException("Enum is not marked as SystemEnum"); 30 | 31 | var enumMetadata = (SystemEnumAttribute)attribs[0]; 32 | 33 | enumType = TypeManager.RegisterType("Перечисление" + enumMetadata.GetName(), typeof(T)); 34 | enumValueType = TypeManager.RegisterType(enumMetadata.GetName(), typeof(SelfAwareEnumValue)); 35 | } 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/EnumValueAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | [AttributeUsage(AttributeTargets.Property)] 9 | public class EnumValueAttribute : Attribute 10 | { 11 | string _name; 12 | 13 | public EnumValueAttribute(string name) 14 | { 15 | _name = name; 16 | } 17 | 18 | public string GetName() 19 | { 20 | return _name; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/EnumerationContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | public class EnumerationContext : PropertyNameIndexAccessor 9 | { 10 | private Dictionary _nameIndexes = new Dictionary(StringComparer.InvariantCultureIgnoreCase); 11 | private List _values = new List(); 12 | private TypeDescriptor _valuesType; 13 | 14 | public EnumerationContext(TypeDescriptor typeRepresentation, TypeDescriptor valuesType) : base(typeRepresentation) 15 | { 16 | _valuesType = valuesType; 17 | } 18 | 19 | public void AddValue(string name, EnumerationValue val) 20 | { 21 | System.Diagnostics.Debug.Assert(val != null); 22 | 23 | if(!ScriptEngine.Utils.IsValidIdentifier(name)) 24 | { 25 | throw new ArgumentException("Name must be a valid identifier", "name"); 26 | } 27 | 28 | int id = _values.Count; 29 | _nameIndexes.Add(name, id); 30 | _values.Add(val); 31 | 32 | val.ValuePresentation = name; 33 | 34 | } 35 | 36 | public TypeDescriptor ValuesType 37 | { 38 | get 39 | { 40 | return _valuesType; 41 | } 42 | } 43 | 44 | public EnumerationValue this[string name] 45 | { 46 | get 47 | { 48 | int id = FindProperty(name); 49 | return _values[id]; 50 | } 51 | } 52 | 53 | public int IndexOf(EnumerationValue enumVal) 54 | { 55 | return _values.IndexOf(enumVal); 56 | } 57 | 58 | public override int FindProperty(string name) 59 | { 60 | int id; 61 | if (_nameIndexes.TryGetValue(name, out id)) 62 | { 63 | return id; 64 | } 65 | else 66 | return base.FindProperty(name); 67 | } 68 | 69 | public override bool IsPropReadable(int propNum) 70 | { 71 | return true; 72 | } 73 | 74 | public override IValue GetPropValue(int propNum) 75 | { 76 | return _values[propNum]; 77 | } 78 | 79 | protected IList ValuesInternal 80 | { 81 | get 82 | { 83 | return _values; 84 | } 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/EnumerationValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | abstract public class EnumerationValue : IValue 9 | { 10 | EnumerationContext _owner; 11 | 12 | public EnumerationValue(EnumerationContext owner) 13 | { 14 | _owner = owner; 15 | } 16 | 17 | public EnumerationContext Owner 18 | { 19 | get 20 | { 21 | return _owner; 22 | } 23 | } 24 | 25 | public string ValuePresentation 26 | { 27 | get;set; 28 | } 29 | 30 | public virtual DataType DataType 31 | { 32 | get { return Machine.DataType.GenericValue; } 33 | } 34 | 35 | public virtual TypeDescriptor SystemType 36 | { 37 | get { return _owner.ValuesType; } 38 | } 39 | 40 | public virtual decimal AsNumber() 41 | { 42 | throw RuntimeException.ConvertToNumberException(); 43 | } 44 | 45 | public virtual DateTime AsDate() 46 | { 47 | throw RuntimeException.ConvertToDateException(); 48 | } 49 | 50 | public virtual bool AsBoolean() 51 | { 52 | throw RuntimeException.ConvertToBooleanException(); 53 | } 54 | 55 | public virtual string AsString() 56 | { 57 | return ValuePresentation == null ? SystemType.Name : ValuePresentation; 58 | } 59 | 60 | public virtual IRuntimeContextInstance AsObject() 61 | { 62 | throw RuntimeException.ValueIsNotObjectException(); 63 | } 64 | 65 | public IValue GetRawValue() 66 | { 67 | return this; 68 | } 69 | 70 | public virtual int CompareTo(IValue other) 71 | { 72 | if (other != null) 73 | { 74 | if (other is EnumerationValue) 75 | { 76 | int thisIdx = _owner.IndexOf(this); 77 | int otherIdx = _owner.IndexOf((EnumerationValue)other); 78 | return thisIdx - otherIdx; 79 | } 80 | else 81 | { 82 | return SystemType.ID - other.SystemType.ID; 83 | } 84 | } 85 | else 86 | { 87 | return 1; 88 | } 89 | } 90 | 91 | public virtual bool Equals(IValue other) 92 | { 93 | return other.GetRawValue() == this; 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/ExceptionInfoContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | /// 9 | /// Класс позволяет узнать информацию о произошедшем исключении. 10 | /// 11 | [ContextClass("ИнформацияОбОшибке", "ErrorInfo")] 12 | public class ExceptionInfoContext : AutoContext 13 | { 14 | ScriptException _exc; 15 | public ExceptionInfoContext(ScriptException source) 16 | { 17 | if (source == null) 18 | throw new ArgumentNullException(); 19 | 20 | _exc = source; 21 | } 22 | 23 | /// 24 | /// Содержит краткое описание ошибки. Эквивалент Exception.Message в C# 25 | /// 26 | [ContextProperty("Описание", "Description")] 27 | public string Description 28 | { 29 | get { return _exc.ErrorDescription; } 30 | } 31 | 32 | public string MessageWithoutCodeFragment 33 | { 34 | get { return _exc.MessageWithoutCodeFragment; } 35 | } 36 | 37 | public string DetailedDescription 38 | { 39 | get { return _exc.Message; } 40 | } 41 | 42 | /// 43 | /// Содержит вложенное исключение, если таковое было. Эквивалент Exception.InnerException в C# 44 | /// 45 | [ContextProperty("Причина", "Cause")] 46 | public IValue InnerException 47 | { 48 | get 49 | { 50 | if (_exc.InnerException != null) 51 | { 52 | ScriptException inner; 53 | inner = _exc.InnerException as ScriptException; 54 | if(inner == null) 55 | { 56 | inner = new ExternalSystemException(_exc.InnerException); 57 | } 58 | 59 | return new ExceptionInfoContext(inner); 60 | } 61 | else 62 | return ValueFactory.Create(); 63 | } 64 | } 65 | 66 | /// 67 | /// Содержит подробное описание исключения, включая стек вызовов среды исполнения CLR. 68 | /// т.е. не стек вызовов скрипта, а стек вызовов скриптового движка. 69 | /// Эквивалентно функции Exception.ToString() в C#. 70 | /// 71 | /// Строка. 72 | [ContextMethod("ПодробноеОписаниеОшибки", "DetailedDescription")] 73 | public string GetDescription() 74 | { 75 | return _exc.ToString(); 76 | } 77 | 78 | public override string ToString() 79 | { 80 | return Description; 81 | } 82 | 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/GlobalContextAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | [AttributeUsage(AttributeTargets.Class)] 9 | public class GlobalContextAttribute : Attribute 10 | { 11 | public string Category { get; set; } 12 | public bool ManualRegistration { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/ICollectionContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | public interface ICollectionContext : IEnumerable 9 | { 10 | int Count(); 11 | CollectionEnumerator GetManagedIterator(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/IObjectWrapper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace ScriptEngine.Machine.Contexts 3 | { 4 | public interface IObjectWrapper 5 | { 6 | object UnderlyingObject { get; } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/LibraryContextBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | // костыльный класс для реализации ЗаполнитьЗначенияСвойств. 9 | // в версии 2.0 используется другой подход к иерархии контекстов. 10 | 11 | public class LibraryContextBase : PropertyNameIndexAccessor, IReflectableContext 12 | { 13 | 14 | public virtual IEnumerable GetProperties() 15 | { 16 | throw new NotImplementedException(); 17 | } 18 | 19 | public virtual IEnumerable GetMethods() 20 | { 21 | throw new NotImplementedException(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/PropertyNameIndexAccessor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | public abstract class PropertyNameIndexAccessor : ContextIValueImpl 9 | { 10 | public PropertyNameIndexAccessor() 11 | { 12 | 13 | } 14 | 15 | public PropertyNameIndexAccessor(TypeDescriptor type):base(type) 16 | { 17 | } 18 | 19 | public override bool IsIndexed 20 | { 21 | get { return true; } 22 | } 23 | 24 | public override bool IsPropReadable(int propNum) 25 | { 26 | return false; 27 | } 28 | 29 | public override bool IsPropWritable(int propNum) 30 | { 31 | return false; 32 | } 33 | 34 | public override IValue GetIndexedValue(IValue index) 35 | { 36 | if (index.DataType != DataType.String) 37 | { 38 | throw RuntimeException.InvalidArgumentType(); 39 | } 40 | 41 | var n = FindProperty(index.AsString()); 42 | if (IsPropReadable(n)) 43 | return GetPropValue(n); 44 | else 45 | throw RuntimeException.PropIsNotReadableException(index.AsString()); 46 | } 47 | 48 | public override void SetIndexedValue(IValue index, IValue val) 49 | { 50 | if (index.DataType != DataType.String) 51 | { 52 | throw RuntimeException.InvalidArgumentType(); 53 | } 54 | 55 | var n = FindProperty(index.AsString()); 56 | if (IsPropWritable(n)) 57 | SetPropValue(n, val); 58 | else 59 | throw RuntimeException.PropIsNotWritableException(index.AsString()); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/ReflectedParamInfo.cs: -------------------------------------------------------------------------------- 1 | #if !__MonoCS__ 2 | using System.Reflection; 3 | 4 | namespace ScriptEngine.Machine.Contexts 5 | { 6 | class ReflectedParamInfo : ParameterInfo 7 | { 8 | public ReflectedParamInfo(string name, bool isByVal) 9 | { 10 | NameImpl = name; 11 | AttrsImpl = ParameterAttributes.In; 12 | if (!isByVal) 13 | { 14 | AttrsImpl |= ParameterAttributes.Out; 15 | } 16 | 17 | ClassImpl = typeof(IValue); 18 | 19 | } 20 | 21 | public void SetOwner(MemberInfo owner) 22 | { 23 | MemberImpl = owner; 24 | } 25 | 26 | public void SetPosition(int index) 27 | { 28 | PositionImpl = index; 29 | } 30 | 31 | } 32 | } 33 | #endif -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/SafeArrayWrapper.cs: -------------------------------------------------------------------------------- 1 | #if !__MonoCS__ 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | 7 | namespace ScriptEngine.Machine.Contexts 8 | { 9 | /// 10 | /// Вспомогательный класс для работы с объектами COMSafeArray, получаемыми из COM-объектов. 11 | /// На данный момент класс не является полноценной заменой для COMSafeArray и его нельзя создать вручную. 12 | /// 13 | [ContextClass("SafeArrayWrapper")] 14 | public class SafeArrayWrapper : AutoContext, ICollectionContext, IObjectWrapper 15 | { 16 | private object[] _array; 17 | 18 | public SafeArrayWrapper(object safearray) 19 | { 20 | _array = (object[])safearray; 21 | } 22 | 23 | public SafeArrayWrapper(object[] safearray) 24 | { 25 | _array = safearray; 26 | } 27 | 28 | [ContextMethod("Количество", "Count")] 29 | public int Count() 30 | { 31 | return _array.Length; 32 | } 33 | 34 | public override bool IsIndexed 35 | { 36 | get 37 | { 38 | return true; 39 | } 40 | } 41 | 42 | public override IValue GetIndexedValue(IValue index) 43 | { 44 | var intIndex = (int)index.AsNumber(); 45 | return COMWrapperContext.CreateIValue(_array[intIndex]); 46 | } 47 | 48 | public override void SetIndexedValue(IValue index, IValue val) 49 | { 50 | var intIndex = (int)index.AsNumber(); 51 | var newValue = COMWrapperContext.MarshalIValue(val); 52 | _array[intIndex] = newValue; 53 | } 54 | 55 | public CollectionEnumerator GetManagedIterator() 56 | { 57 | return new CollectionEnumerator(GetEnumerator()); 58 | } 59 | 60 | public IEnumerator GetEnumerator() 61 | { 62 | for (int i = 0; i < _array.Length; i++) 63 | { 64 | yield return COMWrapperContext.CreateIValue(_array[i]); 65 | } 66 | } 67 | 68 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 69 | { 70 | return GetEnumerator(); 71 | } 72 | 73 | public object UnderlyingObject 74 | { 75 | get { return _array; } 76 | } 77 | } 78 | } 79 | #endif -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/ScriptInformationContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ScriptEngine.Environment; 6 | 7 | namespace ScriptEngine.Machine.Contexts 8 | { 9 | /// 10 | /// Информация о выполняемом сценарии. 11 | /// 12 | [ContextClass("ИнформацияОСценарии", "ScriptInformation")] 13 | public class ScriptInformationContext : AutoContext 14 | { 15 | private string _origin; 16 | 17 | internal ScriptInformationContext(ModuleInformation info) 18 | { 19 | _origin = info.Origin; 20 | } 21 | 22 | public ScriptInformationContext(ICodeSource codeSrc) 23 | { 24 | _origin = codeSrc.SourceDescription; 25 | } 26 | 27 | /// 28 | /// Путь к файлу сценария, если выполняется сценарий из файла. Для всех прочих сценариев возвращаемое значение определяется хост-приложением. 29 | /// 30 | [ContextProperty("Источник", "Source")] 31 | public string Source 32 | { 33 | get 34 | { 35 | return _origin; 36 | } 37 | } 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/SelfAwareEnumValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | public class SelfAwareEnumValue : EnumerationValue where TOwner : EnumerationContext 9 | { 10 | public SelfAwareEnumValue(TOwner owner) : base(owner) 11 | { 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/SystemEnumAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine.Contexts 7 | { 8 | [AttributeUsage(AttributeTargets.Class)] 9 | public class SystemEnumAttribute : Attribute 10 | { 11 | private string _name; 12 | private string _alias; 13 | 14 | public SystemEnumAttribute(string name, string alias = "") 15 | { 16 | _name = name; 17 | _alias = alias; 18 | } 19 | 20 | public string GetName() 21 | { 22 | return _name; 23 | } 24 | 25 | public string GetAlias() 26 | { 27 | return _alias; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Contexts/UserScriptContextInstance.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ScriptEngine.Machine.Contexts; 6 | 7 | namespace ScriptEngine.Machine.Contexts 8 | { 9 | public class UserScriptContextInstance : ScriptDrivenObject 10 | { 11 | LoadedModule _module; 12 | Dictionary _ownPropertyIndexes; 13 | List _ownProperties; 14 | 15 | internal UserScriptContextInstance(LoadedModule module) : base(module) 16 | { 17 | _module = module; 18 | } 19 | 20 | internal UserScriptContextInstance(LoadedModule module, string asObjectOfType) 21 | : base(module, true) 22 | { 23 | DefineType(TypeManager.GetTypeByName(asObjectOfType)); 24 | _module = module; 25 | } 26 | 27 | public void AddProperty(string name, IValue value) 28 | { 29 | if(_ownProperties == null) 30 | { 31 | _ownProperties = new List(); 32 | _ownPropertyIndexes = new Dictionary(); 33 | } 34 | 35 | var newIndex = _ownProperties.Count; 36 | _ownPropertyIndexes.Add(name, newIndex); 37 | _ownProperties.Add(value); 38 | 39 | } 40 | 41 | protected override int GetMethodCount() 42 | { 43 | return 0; 44 | } 45 | 46 | protected override int GetVariableCount() 47 | { 48 | if (_ownProperties == null) 49 | return 0; 50 | else 51 | return _ownProperties.Count; 52 | } 53 | 54 | protected override void UpdateState() 55 | { 56 | } 57 | 58 | protected override bool IsOwnPropReadable(int index) 59 | { 60 | if (_ownProperties == null) 61 | return false; 62 | 63 | if (index >= 0 && index < _ownProperties.Count) 64 | return true; 65 | else 66 | return false; 67 | } 68 | 69 | protected override IValue GetOwnPropValue(int index) 70 | { 71 | return _ownProperties[index]; 72 | } 73 | 74 | #region IReflectableContext Members 75 | 76 | public override IEnumerable GetProperties() 77 | { 78 | foreach (var item in _module.ExportedProperies) 79 | { 80 | var vi = new VariableInfo(); 81 | vi.Identifier = item.SymbolicName; 82 | vi.Index = item.Index; 83 | vi.Type = SymbolType.ContextProperty; 84 | 85 | yield return vi; 86 | } 87 | } 88 | 89 | public override IEnumerable GetMethods() 90 | { 91 | foreach (var item in _module.ExportedMethods) 92 | { 93 | yield return GetMethodInfo(item.Index); 94 | } 95 | } 96 | 97 | #endregion 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/ExecutionFrame.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine 7 | { 8 | class ExecutionFrame 9 | { 10 | public IVariable[] Locals; 11 | public int InstructionPointer; 12 | public int LineNumber; 13 | public bool DiscardReturnValue; 14 | public string MethodName; 15 | public RuntimeException LastException; 16 | 17 | public Stack LocalFrameStack = new Stack(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/GenericIValueComparer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ScriptEngine.Machine.Contexts; 6 | 7 | namespace ScriptEngine.Machine 8 | { 9 | public class GenericIValueComparer : IEqualityComparer 10 | { 11 | 12 | public bool Equals(IValue x, IValue y) 13 | { 14 | return x.Equals(y); 15 | } 16 | 17 | public int GetHashCode(IValue obj) 18 | { 19 | var CLR_obj = ContextValuesMarshaller.ConvertToCLRObject(obj); 20 | return CLR_obj.GetHashCode(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/GlobalInstancesManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine 7 | { 8 | public static class GlobalsManager 9 | { 10 | static Dictionary _instances = new Dictionary(); 11 | 12 | internal static void Reset() 13 | { 14 | _instances.Clear(); 15 | } 16 | 17 | internal static void RegisterInstance(object instance) 18 | { 19 | _instances.Add(instance.GetType(), instance); 20 | } 21 | 22 | public static T GetGlobalContext() 23 | { 24 | return InternalGetInstance(); 25 | } 26 | 27 | public static T GetEnum() 28 | { 29 | return InternalGetInstance(); 30 | } 31 | 32 | private static T InternalGetInstance() 33 | { 34 | return (T)_instances[typeof(T)]; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/IAttachableContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine 7 | { 8 | public interface IAttachableContext : IReflectableContext 9 | { 10 | void OnAttach(MachineInstance machine, 11 | out IVariable[] variables, 12 | out MethodInfo[] methods, 13 | out IRuntimeContextInstance instance); 14 | 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/IReflectableContext.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using ScriptEngine.Compiler; 3 | using ScriptEngine.Machine; 4 | 5 | namespace ScriptEngine.Machine 6 | { 7 | public interface IReflectableContext 8 | { 9 | IEnumerable GetProperties(); 10 | IEnumerable GetMethods(); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/IRuntimeContextInstance.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine 7 | { 8 | public interface IRuntimeContextInstance 9 | { 10 | bool IsIndexed { get; } 11 | bool DynamicMethodSignatures { get; } 12 | 13 | IValue GetIndexedValue(IValue index); 14 | void SetIndexedValue(IValue index, IValue val); 15 | 16 | int FindProperty(string name); 17 | bool IsPropReadable(int propNum); 18 | bool IsPropWritable(int propNum); 19 | IValue GetPropValue(int propNum); 20 | void SetPropValue(int propNum, IValue newVal); 21 | 22 | int FindMethod(string name); 23 | MethodInfo GetMethodInfo(int methodNumber); 24 | void CallAsProcedure(int methodNumber, IValue[] arguments); 25 | void CallAsFunction(int methodNumber, IValue[] arguments, out IValue retValue); 26 | 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/IValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine 7 | { 8 | public interface IValue : IComparable, IEquatable 9 | { 10 | DataType DataType { get; } 11 | TypeDescriptor SystemType { get; } 12 | 13 | decimal AsNumber(); 14 | DateTime AsDate(); 15 | bool AsBoolean(); 16 | string AsString(); 17 | IRuntimeContextInstance AsObject(); 18 | IValue GetRawValue(); 19 | 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/LoadedModule.cs: -------------------------------------------------------------------------------- 1 | using ScriptEngine.Compiler; 2 | using ScriptEngine.Environment; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | 8 | namespace ScriptEngine.Machine 9 | { 10 | class LoadedModule 11 | { 12 | internal LoadedModule(ModuleImage image) 13 | { 14 | this.Code = image.Code.ToArray(); 15 | this.EntryMethodIndex = image.EntryMethodIndex; 16 | this.MethodRefs = image.MethodRefs.ToArray(); 17 | this.VariableRefs = image.VariableRefs.ToArray(); 18 | this.Methods = image.Methods.ToArray(); 19 | this.Constants = new IValue[image.Constants.Count]; 20 | this.VariableFrameSize = image.VariableFrameSize; 21 | this.ExportedProperies = image.ExportedProperties.ToArray(); 22 | this.ExportedMethods = image.ExportedMethods.ToArray(); 23 | this.ModuleInfo = image.ModuleInfo; 24 | for (int i = 0; i < image.Constants.Count; i++) 25 | { 26 | var def = image.Constants[i]; 27 | this.Constants[i] = ValueFactory.Parse(def.Presentation, def.Type); 28 | } 29 | } 30 | 31 | public int VariableFrameSize { get; private set; } 32 | public int EntryMethodIndex { get; private set; } 33 | public Command[] Code { get; private set;} 34 | public SymbolBinding[] VariableRefs { get; private set; } 35 | public SymbolBinding[] MethodRefs { get; private set; } 36 | public MethodDescriptor[] Methods { get; private set; } 37 | public IValue[] Constants { get; private set; } 38 | public ExportedSymbol[] ExportedProperies { get; private set; } 39 | public ExportedSymbol[] ExportedMethods { get; private set; } 40 | public ModuleInformation ModuleInfo { get; private set; } 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/NullValueImpl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine 7 | { 8 | class NullValueImpl : IValue 9 | { 10 | 11 | public DataType DataType 12 | { 13 | get { return Machine.DataType.GenericValue; } 14 | } 15 | 16 | public TypeDescriptor SystemType 17 | { 18 | get { return TypeManager.GetTypeByFrameworkType(typeof(NullValueImpl)); } 19 | } 20 | 21 | public decimal AsNumber() 22 | { 23 | throw RuntimeException.ConvertToNumberException(); 24 | } 25 | 26 | public DateTime AsDate() 27 | { 28 | throw RuntimeException.ConvertToDateException(); 29 | } 30 | 31 | public bool AsBoolean() 32 | { 33 | throw RuntimeException.ConvertToBooleanException(); 34 | } 35 | 36 | public string AsString() 37 | { 38 | return ""; 39 | } 40 | 41 | public IRuntimeContextInstance AsObject() 42 | { 43 | throw RuntimeException.ValueIsNotObjectException(); 44 | } 45 | 46 | public IValue GetRawValue() 47 | { 48 | return this; 49 | } 50 | 51 | public int CompareTo(IValue other) 52 | { 53 | throw RuntimeException.ComparisonNotSupportedException(); 54 | } 55 | 56 | public bool Equals(IValue other) 57 | { 58 | return other.GetRawValue() == Instance; 59 | } 60 | 61 | public static readonly NullValueImpl Instance; 62 | 63 | static NullValueImpl() 64 | { 65 | Instance = new NullValueImpl(); 66 | } 67 | 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/PropertyBag.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ScriptEngine.Machine.Contexts; 6 | 7 | namespace ScriptEngine.Machine 8 | { 9 | class PropertyBag : DynamicPropertiesAccessor, IAttachableContext 10 | { 11 | private struct PropertyAccessFlags 12 | { 13 | public bool CanRead; 14 | public bool CanWrite; 15 | } 16 | 17 | private List _values = new List(); 18 | private List _accessFlags = new List(); 19 | 20 | public void Insert(IValue value, string identifier) 21 | { 22 | Insert(value, identifier, true, true); 23 | } 24 | 25 | public void Insert(IValue value, string identifier, bool canRead, bool canWrite) 26 | { 27 | var num = RegisterProperty(identifier); 28 | 29 | if (num == _values.Count) 30 | { 31 | _values.Add(null); 32 | _accessFlags.Add(new PropertyAccessFlags() { CanRead = canRead, CanWrite = canWrite }); 33 | } 34 | 35 | if (value == null) 36 | { 37 | value = ValueFactory.Create(); 38 | } 39 | 40 | SetPropValue(num, value); 41 | 42 | } 43 | 44 | public override bool IsPropReadable(int propNum) 45 | { 46 | return _accessFlags[propNum].CanRead; 47 | } 48 | 49 | public override bool IsPropWritable(int propNum) 50 | { 51 | return _accessFlags[propNum].CanWrite; 52 | } 53 | 54 | public override IValue GetPropValue(int propNum) 55 | { 56 | return _values[propNum]; 57 | } 58 | 59 | public override void SetPropValue(int propNum, IValue newVal) 60 | { 61 | _values[propNum] = newVal; 62 | } 63 | 64 | public int Count 65 | { 66 | get 67 | { 68 | return _values.Count; 69 | } 70 | } 71 | 72 | #region IAttachableContext Members 73 | 74 | public void OnAttach(MachineInstance machine, out IVariable[] variables, out MethodInfo[] methods, out IRuntimeContextInstance instance) 75 | { 76 | variables = new IVariable[this.Count]; 77 | for (int i = 0; i < variables.Length; i++) 78 | { 79 | variables[i] = Variable.CreateContextPropertyReference(this, i); 80 | } 81 | 82 | methods = new MethodInfo[0]; 83 | instance = this; 84 | } 85 | 86 | #endregion 87 | 88 | #region IReflectableContext Members 89 | 90 | IEnumerable IReflectableContext.GetProperties() 91 | { 92 | throw new NotImplementedException(); 93 | } 94 | 95 | IEnumerable IReflectableContext.GetMethods() 96 | { 97 | throw new NotImplementedException(); 98 | } 99 | 100 | #endregion 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/Scope.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine 7 | { 8 | struct Scope 9 | { 10 | public IVariable[] Variables; 11 | public MethodInfo[] Methods; 12 | public IRuntimeContextInstance Instance; 13 | public bool Detachable; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/ScriptEngine/Machine/TypeTypeValue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine.Machine 7 | { 8 | class TypeTypeValue : IValue 9 | { 10 | TypeDescriptor _instance; 11 | 12 | public TypeTypeValue(string name) 13 | { 14 | _instance = TypeManager.GetTypeByName(name); 15 | } 16 | 17 | public TypeTypeValue(TypeDescriptor type) 18 | { 19 | _instance = type; 20 | } 21 | 22 | public DataType DataType 23 | { 24 | get { return Machine.DataType.Type; } 25 | } 26 | 27 | public TypeDescriptor SystemType 28 | { 29 | get { return TypeDescriptor.FromDataType(DataType.Type); } 30 | } 31 | 32 | public decimal AsNumber() 33 | { 34 | throw RuntimeException.ConvertToNumberException(); 35 | } 36 | 37 | public DateTime AsDate() 38 | { 39 | throw RuntimeException.ConvertToDateException(); 40 | } 41 | 42 | public bool AsBoolean() 43 | { 44 | throw RuntimeException.ConvertToBooleanException(); 45 | } 46 | 47 | public string AsString() 48 | { 49 | return _instance.ToString(); 50 | } 51 | 52 | public IRuntimeContextInstance AsObject() 53 | { 54 | throw RuntimeException.ValueIsNotObjectException(); 55 | } 56 | 57 | public IValue GetRawValue() 58 | { 59 | return this; 60 | } 61 | 62 | public int CompareTo(IValue other) 63 | { 64 | throw RuntimeException.ComparisonNotSupportedException(); 65 | } 66 | 67 | public bool Equals(IValue other) 68 | { 69 | if(other.DataType == this.DataType) 70 | { 71 | var otherVal = other.GetRawValue() as TypeTypeValue; 72 | return otherVal._instance.ID == this._instance.ID; 73 | } 74 | else 75 | { 76 | return false; 77 | } 78 | } 79 | 80 | public TypeDescriptor Value 81 | { 82 | get 83 | { 84 | return _instance; 85 | } 86 | } 87 | 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/ScriptEngine/ModuleImage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ScriptEngine.Machine; 6 | using ScriptEngine.Environment; 7 | 8 | namespace ScriptEngine 9 | { 10 | [Serializable] 11 | class ModuleImage 12 | { 13 | public ModuleImage() 14 | { 15 | EntryMethodIndex = -1; 16 | Code = new List(); 17 | VariableRefs = new List(); 18 | MethodRefs = new List(); 19 | Methods = new List(); 20 | Constants = new List(); 21 | ExportedProperties = new List(); 22 | ExportedMethods = new List(); 23 | } 24 | 25 | public int VariableFrameSize { get; set; } 26 | public int EntryMethodIndex { get; set; } 27 | public IList Code { get; set; } 28 | public IList VariableRefs { get; set; } 29 | public IList MethodRefs { get; set; } 30 | public IList Methods { get; set; } 31 | public IList Constants { get; set; } 32 | public IList ExportedProperties { get; set; } 33 | public IList ExportedMethods { get; set; } 34 | 35 | // Привязка к исходному коду для отладочной информации в RuntimeException 36 | [NonSerialized] 37 | private ModuleInformation _source; 38 | 39 | internal ModuleInformation ModuleInfo 40 | { 41 | get 42 | { 43 | return _source; 44 | } 45 | set 46 | { 47 | _source = value; 48 | } 49 | } 50 | } 51 | 52 | [Serializable] 53 | struct MethodDescriptor 54 | { 55 | public MethodInfo Signature; 56 | public int VariableFrameSize; 57 | public int EntryPoint; 58 | } 59 | 60 | [Serializable] 61 | struct ExportedSymbol 62 | { 63 | public string SymbolicName; 64 | public int Index; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/ScriptEngine/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("ScriptEngine")] 9 | [assembly: AssemblyDescription("1C language runtime")] 10 | [assembly: AssemblyProduct("ScriptEngine")] 11 | #if DEBUG 12 | [assembly: InternalsVisibleTo("EngineTests")] 13 | #endif 14 | 15 | 16 | // Setting ComVisible to false makes the types in this assembly not visible 17 | // to COM components. If you need to access a type in this assembly from 18 | // COM, set the ComVisible attribute to true on that type. 19 | [assembly: ComVisible(false)] 20 | 21 | // The following GUID is for the ID of the typelib if this project is exposed to COM 22 | [assembly: Guid("d40a0ecf-c2ee-460f-bc90-05ead73f7a58")] 23 | 24 | // Version information for an assembly consists of the following four values: 25 | // 26 | // Major Version 27 | // Minor Version 28 | // Build Number 29 | // Revision 30 | // 31 | // You can specify all the values or you can default the Build and Revision Numbers 32 | // by using the '*' as shown below: 33 | // [assembly: AssemblyVersion("1.0.*")] 34 | -------------------------------------------------------------------------------- /src/ScriptEngine/RuntimeEnvironment.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using ScriptEngine.Compiler; 6 | using ScriptEngine.Environment; 7 | using ScriptEngine.Machine; 8 | 9 | namespace ScriptEngine 10 | { 11 | public class RuntimeEnvironment 12 | { 13 | private List _objects = new List(); 14 | private CompilerContext _symbolScopes = new CompilerContext(); 15 | private SymbolScope _globalScope; 16 | private PropertyBag _injectedProperties; 17 | 18 | public void InjectObject(IAttachableContext context) 19 | { 20 | InjectObject(context, false); 21 | } 22 | 23 | public void InjectObject(IAttachableContext context, bool asDynamicScope) 24 | { 25 | RegisterSymbolScope(context, asDynamicScope); 26 | RegisterObject(context); 27 | } 28 | 29 | public void InjectGlobalProperty(IValue value, string identifier, bool readOnly) 30 | { 31 | if(!Utils.IsValidIdentifier(identifier)) 32 | { 33 | throw new ArgumentException("Invalid identifier", "identifier"); 34 | } 35 | 36 | if (_globalScope == null) 37 | { 38 | _globalScope = new SymbolScope(); 39 | TypeManager.RegisterType("__globalPropertiesHolder", typeof(PropertyBag)); 40 | _injectedProperties = new PropertyBag(); 41 | _symbolScopes.PushScope(_globalScope); 42 | RegisterObject(_injectedProperties); 43 | } 44 | 45 | _globalScope.DefineVariable(identifier, SymbolType.ContextProperty); 46 | _injectedProperties.Insert(value, identifier, true, !readOnly); 47 | } 48 | 49 | internal CompilerContext SymbolsContext 50 | { 51 | get 52 | { 53 | return _symbolScopes; 54 | } 55 | } 56 | 57 | internal IList AttachedContexts 58 | { 59 | get 60 | { 61 | return _objects; 62 | } 63 | } 64 | 65 | private void RegisterSymbolScope(IReflectableContext provider, bool asDynamicScope) 66 | { 67 | var scope = new SymbolScope(); 68 | scope.IsDynamicScope = asDynamicScope; 69 | 70 | _symbolScopes.PushScope(scope); 71 | foreach (var item in provider.GetProperties()) 72 | { 73 | if (item.Type == SymbolType.Variable) 74 | { 75 | _symbolScopes.DefineVariable(item.Identifier); 76 | } 77 | else 78 | { 79 | _symbolScopes.DefineProperty(item.Identifier); 80 | } 81 | } 82 | 83 | foreach (var item in provider.GetMethods()) 84 | { 85 | _symbolScopes.DefineMethod(item); 86 | } 87 | } 88 | 89 | private void RegisterObject(IAttachableContext context) 90 | { 91 | _objects.Add(context); 92 | } 93 | 94 | 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/ScriptEngine/ScriptException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine 7 | { 8 | public class ScriptException : ApplicationException 9 | { 10 | private CodePositionInfo _codePosition; 11 | 12 | internal ScriptException() 13 | { 14 | _codePosition = new CodePositionInfo(); 15 | _codePosition.LineNumber = -1; 16 | } 17 | 18 | internal ScriptException(string message) 19 | : this(new CodePositionInfo(), message, null) 20 | { 21 | 22 | } 23 | 24 | internal ScriptException(CodePositionInfo codeInfo, string message) 25 | : this(codeInfo, message, null) 26 | { 27 | 28 | } 29 | 30 | internal ScriptException(CodePositionInfo codeInfo, string message, Exception innerException) 31 | : base(message, innerException) 32 | { 33 | _codePosition = codeInfo; 34 | } 35 | 36 | public int LineNumber 37 | { 38 | get 39 | { 40 | return _codePosition.LineNumber; 41 | } 42 | internal set 43 | { 44 | _codePosition.LineNumber = value; 45 | } 46 | } 47 | 48 | public string Code 49 | { 50 | get 51 | { 52 | return _codePosition.Code; 53 | } 54 | internal set 55 | { 56 | _codePosition.Code = value; 57 | } 58 | } 59 | 60 | public string ModuleName 61 | { 62 | get 63 | { 64 | return _codePosition.ModuleName; 65 | } 66 | internal set 67 | { 68 | _codePosition.ModuleName = value; 69 | } 70 | } 71 | 72 | public string ErrorDescription 73 | { 74 | get 75 | { 76 | return base.Message; 77 | } 78 | } 79 | 80 | public string MessageWithoutCodeFragment 81 | { 82 | get 83 | { 84 | return String.Format("{{Модуль {0} / Ошибка в строке: {1} / {2}}}", 85 | this.ModuleName, 86 | this.LineNumber, 87 | base.Message); 88 | } 89 | } 90 | 91 | public override string Message 92 | { 93 | get 94 | { 95 | var sb = new StringBuilder(MessageWithoutCodeFragment); 96 | sb.AppendLine(" "); 97 | sb.Append(Code); 98 | 99 | return sb.ToString(); 100 | } 101 | } 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /src/ScriptEngine/Utils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace ScriptEngine 7 | { 8 | public static class Utils 9 | { 10 | public static bool IsValidIdentifier(string name) 11 | { 12 | if (name == null || name.Length == 0) 13 | return false; 14 | 15 | if (!(Char.IsLetter(name[0]) || name[0] == '_')) 16 | return false; 17 | 18 | for (int i = 1; i < name.Length; i++) 19 | { 20 | if (!(Char.IsLetterOrDigit(name[i]) || name[i] == '_')) 21 | return false; 22 | } 23 | 24 | return true; 25 | } 26 | 27 | public static IEnumerable SplitCommandLine(string commandLine) 28 | { 29 | bool inQuotes = false; 30 | 31 | return SplitString(commandLine, c => 32 | { 33 | if (c == '\"') 34 | inQuotes = !inQuotes; 35 | 36 | return !inQuotes && c == ' '; 37 | }) 38 | .Select(arg => arg.Trim().TrimMatchingQuotes('\"')) 39 | .Where(arg => !string.IsNullOrEmpty(arg)); 40 | } 41 | 42 | private static IEnumerable SplitString(this string str, 43 | Func controller) 44 | { 45 | int nextPiece = 0; 46 | 47 | for (int c = 0; c < str.Length; c++) 48 | { 49 | if (controller(str[c])) 50 | { 51 | yield return str.Substring(nextPiece, c - nextPiece); 52 | nextPiece = c + 1; 53 | } 54 | } 55 | 56 | yield return str.Substring(nextPiece); 57 | } 58 | 59 | private static string TrimMatchingQuotes(this string input, char quote) 60 | { 61 | if ((input.Length >= 2) && 62 | (input[0] == quote) && (input[input.Length - 1] == quote)) 63 | return input.Substring(1, input.Length - 2); 64 | 65 | return input; 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/StandaloneRunner/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Text; 7 | 8 | namespace StandaloneRunner 9 | { 10 | class Program 11 | { 12 | static int Main(string[] args) 13 | { 14 | AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; 15 | 16 | return Run(args); 17 | 18 | } 19 | 20 | private static int Run(string[] args) 21 | { 22 | var sp = new StandaloneProcess(); 23 | sp.CommandLineArguments = args; 24 | return sp.Run(); 25 | } 26 | 27 | static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) 28 | { 29 | string resourceName = "StandaloneRunner." + new AssemblyName(args.Name).Name + ".dll"; 30 | 31 | using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) 32 | { 33 | byte[] asmData = new byte[stream.Length]; 34 | stream.Read(asmData, 0, asmData.Length); 35 | return Assembly.Load(asmData); 36 | } 37 | 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/StandaloneRunner/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("StandaloneRunner")] 9 | [assembly: AssemblyProduct("StandaloneRunner")] 10 | 11 | // Setting ComVisible to false makes the types in this assembly not visible 12 | // to COM components. If you need to access a type in this assembly from 13 | // COM, set the ComVisible attribute to true on that type. 14 | [assembly: ComVisible(false)] 15 | 16 | // The following GUID is for the ID of the typelib if this project is exposed to COM 17 | [assembly: Guid("374878a5-347c-46e3-8651-5f83e708c43d")] 18 | 19 | // Version information for an assembly consists of the following four values: 20 | // 21 | // Major Version 22 | // Minor Version 23 | // Build Number 24 | // Revision 25 | // 26 | // You can specify all the values or you can default the Build and Revision Numbers 27 | // by using the '*' as shown below: 28 | // [assembly: AssemblyVersion("1.0.*")] 29 | -------------------------------------------------------------------------------- /src/TestApp/App.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/TestApp/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Configuration; 4 | using System.Data; 5 | using System.Linq; 6 | using System.Windows; 7 | 8 | namespace TestApp 9 | { 10 | /// 11 | /// Interaction logic for App.xaml 12 | /// 13 | public partial class App : Application 14 | { 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/TestApp/Controls/ProcedureListWnd.xaml: -------------------------------------------------------------------------------- 1 |  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 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 47 | 48 | 49 | 50 | 51 | Сортировка 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /src/TestApp/MainWindow.xaml: -------------------------------------------------------------------------------- 1 |  7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | Файл 26 | 27 | 28 | 29 | M1,4 L5,4 L3,8 Z 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 |