Пример #1
0
        public UserAddedScript Read(Stream input)
        {
            var header = ReadStruct <ModuleHeader>(input);

            var userScript = new UserAddedScript();

            var nameBytes = new byte[header.NameLen];

            input.Read(nameBytes, 0, nameBytes.Length);

            userScript.Symbol = Encoding.UTF8.GetString(nameBytes);

            var moduleImage = (ModuleImage)_formatter.Deserialize(input);

            var path = Assembly.GetEntryAssembly().Location;

            moduleImage.ModuleInfo = new ModuleInformation
            {
                CodeIndexer = new CompiledCodeIndexer(),
                ModuleName  = string.Format("{0}:{1}", Path.GetFileName(path), userScript.Symbol),
                Origin      = path
            };

            userScript.Image = moduleImage;
            userScript.Type  = header.IsClass ? UserAddedScriptType.Class : UserAddedScriptType.Module;

            return(userScript);
        }
Пример #2
0
 private void WriteImage(UserAddedScript script, JsonTextWriter writer, JsonSerializer serializer)
 {
     writer.WriteStartObject();
     writer.WritePropertyName("name");
     writer.WriteValue(script.Symbol);
     writer.WritePropertyName("type");
     writer.WriteValue(script.Type.ToString());
     writer.WritePropertyName("image");
     serializer.Serialize(writer, script.Image);
     writer.WriteEndObject();
 }
Пример #3
0
 public void LoadUserScript(UserAddedScript script)
 {
     if (script.Type == UserAddedScriptType.Class)
     {
         _engine.AttachedScriptsFactory.LoadAndRegister(script.Symbol, script.Image);
     }
     else
     {
         var loaded   = _engine.LoadModuleImage(script.Image);
         var instance = (IValue)_engine.NewObject(loaded);
         _env.InjectGlobalProperty(instance, script.Symbol, true);
     }
 }
Пример #4
0
        public void rr(string source)
        {
            ICodeSource sourceToCompile = Engine.Loader.FromFile(source);
            ModuleImage module          = Engine.GetCompilerService().Compile(sourceToCompile);

            UserAddedScript script = new UserAddedScript
            {
                Type   = UserAddedScriptType.Class,
                Image  = module,
                Symbol = source
            };

            Engine.LoadUserScript(script);

            //var builder = new ClassBuilder<UserScriptContextInstance>();
            //var reflected = builder.SetModule(module)
            //                       .SetTypeName("Dummy")
            //                       .ExportDefaults()
            //                       .Build();
        }
Пример #5
0
        public void Save(UserAddedScript script, Stream output)
        {
            var nameBytes  = Encoding.UTF8.GetBytes(script.Symbol);
            var bodyStream = new MemoryStream(8 * 1024);

            _formatter.Serialize(bodyStream, script.Image);

            var header = new ModuleHeader
            {
                NameLen = nameBytes.Length,
                BodyLen = bodyStream.Length,
                IsClass = script.Type == UserAddedScriptType.Class
            };

            WriteStruct(output, header);

            output.Write(nameBytes, 0, nameBytes.Length);
            bodyStream.Position = 0;
            bodyStream.WriteTo(output);
            bodyStream.Dispose();
        }