コード例 #1
0
        internal BaristaExecutionScope(BaristaContext context, IPromiseTaskQueue taskQueue, Action release)
        {
            m_context          = context;
            m_promiseTaskQueue = taskQueue;
            m_release          = release;

            //Clear and set the task queue if specified
            m_promiseContinuationCallbackDelegateHandle = default(GCHandle);
            if (m_promiseTaskQueue != null)
            {
                m_promiseTaskQueue.Clear();
                JavaScriptPromiseContinuationCallback promiseContinuationCallback = (IntPtr taskHandle, IntPtr callbackState) =>
                {
                    try
                    {
                        PromiseContinuationCallback(taskHandle, callbackState);
                    }
                    catch
                    {
                        //Do Nothing.
                    }
                };
                m_promiseContinuationCallbackDelegateHandle = GCHandle.Alloc(promiseContinuationCallback);
                m_context.Engine.JsSetPromiseContinuationCallback(promiseContinuationCallback, IntPtr.Zero);
            }
        }
コード例 #2
0
ファイル: BaristaContext.cs プロジェクト: imgits/BaristaCore
        /// <summary>
        /// Creates a new instance of a Barista Context.
        /// </summary>
        /// <param name="engine"></param>
        /// <param name="contextHandle"></param>
        public BaristaContext(IJavaScriptEngine engine, IBaristaValueFactoryBuilder valueFactoryBuilder, IBaristaConversionStrategy conversionStrategy, IBaristaModuleRecordFactory moduleRecordFactory, IPromiseTaskQueue taskQueue, JavaScriptContextSafeHandle contextHandle)
            : base(engine, contextHandle)
        {
            if (valueFactoryBuilder == null)
            {
                throw new ArgumentNullException(nameof(valueFactoryBuilder));
            }

            m_taskFactory = new TaskFactory(
                CancellationToken.None,
                TaskCreationOptions.DenyChildAttach,
                TaskContinuationOptions.None,
                new CurrentThreadTaskScheduler());

            m_conversionStrategy = conversionStrategy ?? throw new ArgumentNullException(nameof(conversionStrategy));

            m_valueFactory = valueFactoryBuilder.CreateValueFactory(this);

            m_undefinedValue = new Lazy <JsUndefined>(() => m_valueFactory.Undefined);
            m_nullValue      = new Lazy <JsNull>(() => m_valueFactory.Null);
            m_trueValue      = new Lazy <JsBoolean>(() => m_valueFactory.True);
            m_falseValue     = new Lazy <JsBoolean>(() => m_valueFactory.False);
            m_globalValue    = new Lazy <JsObject>(() => m_valueFactory.GlobalObject);
            m_jsonValue      = new Lazy <JsJSON>(() =>
            {
                var global = m_globalValue.Value;
                return(global.GetProperty <JsJSON>("JSON"));
            });
            m_objectValue = new Lazy <JsObjectConstructor>(() =>
            {
                var global = m_globalValue.Value;
                return(global.GetProperty <JsObjectConstructor>("Object"));
            });
            m_promiseValue = new Lazy <JsPromiseConstructor>(() =>
            {
                var global = m_globalValue.Value;
                return(global.GetProperty <JsPromiseConstructor>("Promise"));
            });
            m_symbolValue = new Lazy <JsSymbolConstructor>(() =>
            {
                var global = m_globalValue.Value;
                return(global.GetProperty <JsSymbolConstructor>("Symbol"));
            });

            m_promiseTaskQueue    = taskQueue;
            m_moduleRecordFactory = moduleRecordFactory ?? throw new ArgumentNullException(nameof(moduleRecordFactory));

            //Set the event that will be called prior to the engine collecting the context.
            JavaScriptObjectBeforeCollectCallback beforeCollectCallback = (IntPtr handle, IntPtr callbackState) =>
            {
                OnBeforeCollect(handle, callbackState);
            };

            m_beforeCollectCallbackDelegateHandle = GCHandle.Alloc(beforeCollectCallback);
            Engine.JsSetObjectBeforeCollectCallback(contextHandle, IntPtr.Zero, beforeCollectCallback);
        }
コード例 #3
0
        public void JsModulesWillExecuteEvenWithoutATaskQueue()
        {
            using (var rt = BaristaRuntimeFactory.CreateRuntime())
            {
                var converter               = m_provider.GetRequiredService <IBaristaConversionStrategy>();
                var valueFactoryBuilder     = m_provider.GetRequiredService <IBaristaValueFactoryBuilder>();
                var moduleRecordFactory     = m_provider.GetRequiredService <IBaristaModuleRecordFactory>();
                IPromiseTaskQueue taskQueue = null;

                var contextHandle = rt.Engine.JsCreateContext(rt.Handle);
                using (var ctx = new BaristaContext(rt.Engine, valueFactoryBuilder, converter, moduleRecordFactory, taskQueue, contextHandle))
                {
                    var result = ctx.EvaluateModule("export default 'foo';");
                    Assert.Equal("foo", result.ToString());
                }
                Assert.True(contextHandle.IsClosed);
            }
        }