Exemplo n.º 1
0
        public void MappingRuntimeErrorDuringOutOfMemoryIsCorrect()
        {
            // Arrange
            const string input = @"var arr = [];

for (var i = 0; i < 10000; i++) {
	arr.push('Current date: ' + new Date());
}";

            JsRuntimeException exception = null;

            // Act
            using (IJsEngine jsEngine = new ChakraCoreJsEngine(
                       new ChakraCoreSettings
            {
                MemoryLimit = new UIntPtr(2 * 1024 * 1024)
            }
                       ))
            {
                try
                {
                    jsEngine.Execute(input);
                }
                catch (JsRuntimeException e)
                {
                    exception = e;
                }
            }

            // Assert
            Assert.NotNull(exception);
            Assert.Equal("Runtime error", exception.Category);
            Assert.Equal("Out of memory", exception.Description);
        }
Exemplo n.º 2
0
        public void MappingEngineLoadErrorDuringOutOfMemoryIsCorrect()
        {
            // Arrange
            IJsEngine             jsEngine  = null;
            JsEngineLoadException exception = null;

            // Act
            try
            {
                jsEngine = new ChakraCoreJsEngine(
                    new ChakraCoreSettings
                {
                    MemoryLimit = new UIntPtr(8 * 1024)
                }
                    );
            }
            catch (JsEngineLoadException e)
            {
                exception = e;
            }
            finally
            {
                jsEngine?.Dispose();
            }

            // Assert
            Assert.NotNull(exception);
            Assert.Equal("Engine load error", exception.Category);
            Assert.Equal("Out of memory.", exception.Description);
        }
Exemplo n.º 3
0
        private ChakraCoreJsEngine EngineFactory()
        {
            var engine = new ChakraCoreJsEngine(_config.EngineSettings);

            var thisAssembly = typeof(ReactConfiguration).GetTypeInfo().Assembly;

            LoadResource(engine, "ZeroReact.Resources.shims.js", thisAssembly);

            if (_config.LoadReact)
            {
                LoadResource(
                    engine,
                    _config.UseDebugReact
                        ? "ZeroReact.Resources.react.generated.js"
                        : "ZeroReact.Resources.react.generated.min.js",
                    thisAssembly
                    );
            }

            LoadUserScripts(engine);
            if (!_config.LoadReact && _scriptLoadException == null)
            {
                EnsureReactLoaded(engine);
            }

            return(engine);
        }
Exemplo n.º 4
0
        private void MaintenanceEngine(ChakraCoreJsEngine engine)
        {
            if (_disposedFlag.IsSet()) //test
            {
                DisposeEngine(engine);
                return;
            }

            _engineMaintenance.Set();
        }
Exemplo n.º 5
0
 protected override void Prepare()
 {
     engine = new ChakraCoreJsEngine();
     engine.EmbedHostObject("log", new Action <object>(Console.WriteLine));
     engine.Execute("const window = this;");
     engine.Execute("const exports = {};");
     engine.Execute(Compiler);
     if (!engine.HasVariable("transform"))
     {
         throw new InvalidOperationException("Missing compiler");
     }
 }
Exemplo n.º 6
0
        // Note: I believe this is thread-safe based on the documentation and comments here:
        // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-constructors
        // https://github.com/dotnet/docs/issues/10243
        static MidiComposer()
        {
            scripts = new List <IPrecompiledScript>();

            // Load all required scripts from embedded resource and precompile them
            using (var compilationEngine = new ChakraCoreJsEngine())
            {
                foreach (var resource in resources)
                {
                    scripts.Add(compilationEngine.PrecompileResource(resource, typeof(MidiComposer)));
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Composes a new MIDI file based on the given seed
        /// </summary>
        /// <param name="seed"></param>
        /// <returns></returns>
        public static Composition Compose(string seed)
        {
            using (var engine = new ChakraCoreJsEngine())
            {
                foreach (var script in scripts)
                {
                    engine.Execute(script);
                }

                var result    = engine.CallFunction <string>("exportMidi", seed);
                var midiBytes = result.Split(',').Select(str => Convert.ToByte(str)).ToArray();

                return(new Composition(seed, new MemoryStream(midiBytes)));
            }
        }
Exemplo n.º 8
0
        /*
         * Get the value from `tests` in the Elm module `Main`.
         */
        static string GetTestsValueFromModuleMain(
            IImmutableDictionary <IImmutableList <string>, IImmutableList <byte> > elmAppFiles)
        {
            var javascriptFromElmMake = Kalmit.ProcessFromElm019Code.CompileElmToJavascript(
                elmAppFiles,
                ImmutableList.Create("src", "Main.elm"));

            var javascriptEngine = new ChakraCoreJsEngine(
                new ChakraCoreSettings
            {
                DisableEval = true,
                EnableExperimentalFeatures = true
            }
                );

            var javascriptPreparedToRun =
                Kalmit.ProcessFromElm019Code.PublishFunctionsFromJavascriptFromElmMake(
                    Kalmit.ProcessFromElm019Code.JavascriptMinusCrashes(javascriptFromElmMake),
                    new[]
Exemplo n.º 9
0
        private void Initialize()
        {
            if (_initialized)
            {
                return;
            }

            try
            {
                _jsEngine = new ChakraCoreJsEngine();
                OnInitialize();
            }
            catch
            {
                _jsEngine?.Dispose();
                _jsEngine = null;
                throw;
            }

            _initialized = true;
        }
Exemplo n.º 10
0
        public void DisposeEngine(ChakraCoreJsEngine engine)
        {
            _engines.TryRemove(engine, out _);

            try
            {
                engine._dispatcher?.Invoke(
                    () =>
                {
                    engine._dispatcher._sharedQueue        = null;
                    engine._dispatcher._sharedQueueEnqeued = null;
                });
            }
            catch (Exception ex)
            {
                // ignored
            }

            engine.Dispose();
            _enginePopulater?.Set();
        }
Exemplo n.º 11
0
 public void InitializeEngine()
 {
     _jsEngine = new ChakraCoreJsEngine();
 }
Exemplo n.º 12
0
 void IDisposable.Dispose()
 {
     _jsEngine?.Dispose();
     _jsEngine = null;
 }
Exemplo n.º 13
0
 public ChakraBaseProgram(ChakraRuntimeService service)
 {
     engine  = service.GetEngine();
     Console = new BasicConsoleInterface();
     InitializeEngine();
 }