Пример #1
0
        /// <summary>
        /// Creates a new JavaScript Runtime.
        /// </summary>
        /// <param name="attributes"></param>
        /// <returns></returns>
        public BaristaRuntime CreateRuntime(JavaScriptRuntimeAttributes attributes = JavaScriptRuntimeAttributes.None)
        {
            var contextService = m_serviceProvider.GetRequiredService <IBaristaContextFactory>();

            var runtimeHandle = m_engine.JsCreateRuntime(attributes, null);
            var result        = m_runtimePool.GetOrAdd(runtimeHandle, () => {
                var rt = new BaristaRuntime(m_engine, contextService, runtimeHandle);
                //Do not wire up a runtime's BeforeCollect handler with the factory as this will
                //remove and dispose of the runtime on ANY garbage collect, which will eventually
                //crash the engine.

                //After a runtime handle is disposed, remove the handle from the pool.
                void afterDispose(object sender, EventArgs args)
                {
                    rt.AfterDispose -= afterDispose;
                    m_runtimePool.RemoveHandle(runtimeHandle);
                }

                rt.AfterDispose += afterDispose;
                return(rt);
            });

            if (result == null)
            {
                throw new InvalidOperationException("Unable to create or retrieve a new Barista Runtime.");
            }

            return(result);
        }
Пример #2
0
        public BaristaContext CreateContext(BaristaRuntime runtime)
        {
            if (runtime == null)
            {
                throw new ArgumentNullException(nameof(runtime));
            }

            if (runtime.IsDisposed)
            {
                throw new ObjectDisposedException(nameof(runtime));
            }

            var contextHandle = m_engine.JsCreateContext(runtime.Handle);

            return(m_contextPool.GetOrAdd(contextHandle, () =>
            {
                var moduleRecordFactory = m_serviceProvider.GetRequiredService <IBaristaModuleRecordFactory>();
                var valueFactoryBuilder = m_serviceProvider.GetRequiredService <IBaristaValueFactoryBuilder>();
                var conversionStrategy = m_serviceProvider.GetRequiredService <IBaristaConversionStrategy>();

                //For flexability, a promise task queue is not required.
                var promiseTaskQueue = m_serviceProvider.GetService <IPromiseTaskQueue>();

                //Set the handle that will be called prior to the engine collecting the context.
                var context = new BaristaContext(m_engine, valueFactoryBuilder, conversionStrategy, moduleRecordFactory, promiseTaskQueue, contextHandle);

                void beforeCollect(object sender, BaristaObjectBeforeCollectEventArgs args)
                {
                    context.BeforeCollect -= beforeCollect;
                    m_contextPool.RemoveHandle(new JavaScriptContextSafeHandle(args.Handle));
                }
                context.BeforeCollect += beforeCollect;
                return context;
            }));
        }