Пример #1
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;
            }));
        }
        public void JsCanStartDebugging()
        {
            using (var runtimeHandle = Engine.JsCreateRuntime(JavaScriptRuntimeAttributes.None, null))
            {
                using (var contextHandle = Engine.JsCreateContext(runtimeHandle))
                {
                    Engine.JsSetCurrentContext(contextHandle);

                    bool called = false;
                    JavaScriptDiagDebugEventCallback callback = (JavaScriptDiagDebugEventType eventType, IntPtr eventData, IntPtr callbackState) =>
                    {
                        called = true;
                        return(true);
                    };

                    Engine.JsDiagStartDebugging(runtimeHandle, callback, IntPtr.Zero);

                    //We didn't specify any breakpoints so...
                    Assert.False(called);
                }
            }
        }
Пример #3
0
        public void JsValueSafeHandlesAreNotSingletons()
        {
            using (var runtimeHandle = Engine.JsCreateRuntime(JavaScriptRuntimeAttributes.None, null))
            {
                using (var contextHandle = Engine.JsCreateContext(runtimeHandle))
                {
                    Engine.JsSetCurrentContext(contextHandle);

                    var trueHandle        = Engine.JsGetTrueValue();
                    var anotherTrueHandle = Engine.JsBoolToBoolean(true);

                    Assert.Equal(trueHandle, anotherTrueHandle);
                    Assert.NotSame(trueHandle, anotherTrueHandle);
                }
            }
        }
Пример #4
0
        public void JsDiagEvaluateReturnsAValue()
        {
            if (DebugWindowsEngine == null)
            {
                return;
            }

            string iterate = @"
var moose = 0;
for(var i = 0; i < 50; i++)
{
    moose++;
}

moose;
";
            int    iPod    = 0;

            using (var runtimeHandle = Engine.JsCreateRuntime(JavaScriptRuntimeAttributes.None, null))
            {
                using (var contextHandle = Engine.JsCreateContext(runtimeHandle))
                {
                    Engine.JsSetCurrentContext(contextHandle);

                    //Callback that is run for each breakpoint.
                    JavaScriptDiagDebugEventCallback callback = (JavaScriptDiagDebugEventType eventType, IntPtr eventData, IntPtr callbackState) =>
                    {
                        var evalScript       = Engine.JsCreateString("i", (ulong)"i".Length);
                        var evalResultHandle = Engine.JsDiagEvaluate(evalScript, 0, JavaScriptParseScriptAttributes.None, false);

                        var handleType = Engine.JsGetValueType(evalResultHandle);
                        Assert.Equal(JsValueType.Object, handleType);

                        var valuePropertyHandle = CommonWindowsEngine.JsGetPropertyIdFromName("value");
                        var valueHandle         = Engine.JsGetProperty(evalResultHandle, valuePropertyHandle);
                        iPod = Engine.JsNumberToInt(valueHandle);
                        evalScript.Dispose();
                        return(true);
                    };

                    var callbackHandle = GCHandle.Alloc(callback);
                    try
                    {
                        using (var ss = new ScriptSource(Engine, iterate))
                        {
                            Engine.JsDiagStartDebugging(runtimeHandle, callback, IntPtr.Zero);

                            var scripts = Engine.JsDiagGetScripts();
                            Assert.NotEqual(JavaScriptValueSafeHandle.Invalid, scripts);

                            var ix = Engine.JsIntToNumber(0);
                            var objScriptHandle        = Engine.JsGetIndexedProperty(scripts, ix);
                            var scriptIdPropertyHandle = CommonWindowsEngine.JsGetPropertyIdFromName("scriptId");
                            var scriptIdHandle         = Engine.JsGetProperty(objScriptHandle, scriptIdPropertyHandle);

                            var scriptId = Engine.JsNumberToInt(scriptIdHandle);

                            //Set a breakpoint with a knkown position
                            var breakPointHandle = Engine.JsDiagSetBreakpoint((uint)scriptId, 4, 0);
                            Assert.NotEqual(JavaScriptValueSafeHandle.Invalid, breakPointHandle);

                            var finalResult = Engine.JsCallFunction(ss.FunctionHandle, new IntPtr[] { ss.FunctionHandle.DangerousGetHandle() }, 1);
                            var handleType  = Engine.JsGetValueType(finalResult);
                            Assert.Equal(JsValueType.Number, handleType);

                            Engine.JsDiagStopDebugging(runtimeHandle);
                        }

                        Assert.Equal(49, iPod);
                    }
                    finally
                    {
                        callbackHandle.Free();
                    }
                }
            }
        }
        public void JsParseScriptParsesAndReturnsAFunction()
        {
            if (CommonWindowsEngine == null)
            {
                return;
            }

            var    script    = "(()=>{return 6*7;})()";
            string sourceUrl = "[eval code]";

            using (var runtimeHandle = Engine.JsCreateRuntime(JavaScriptRuntimeAttributes.None, null))
            {
                using (var contextHandle = Engine.JsCreateContext(runtimeHandle))
                {
                    Engine.JsSetCurrentContext(contextHandle);

                    var resultHandle = CommonWindowsEngine.JsParseScript(script, JavaScriptSourceContext.GetNextSourceContext(), sourceUrl);

                    Assert.True(resultHandle != JavaScriptValueSafeHandle.Invalid);

                    var handleType = Engine.JsGetValueType(resultHandle);
                    Assert.True(handleType == JsValueType.Function);

                    resultHandle.Dispose();
                }
            }
        }
Пример #6
0
        public void JsCreateStringTest()
        {
            var str = "Hello, World!";

            using (var runtimeHandle = Engine.JsCreateRuntime(JavaScriptRuntimeAttributes.None, null))
            {
                using (var contextHandle = Engine.JsCreateContext(runtimeHandle))
                {
                    Engine.JsSetCurrentContext(contextHandle);

                    var stringHandle = Engine.JsCreateString(str, (ulong)str.Length);

                    Assert.True(stringHandle != JavaScriptValueSafeHandle.Invalid);

                    var handleType = Engine.JsGetValueType(stringHandle);
                    Assert.True(handleType == JsValueType.String);

                    stringHandle.Dispose();
                }
            }
        }