예제 #1
0
        public void Reset()
        {
            if (_context == null)
            {
                return;
            }

            _context.TerminateExecution();
            _context.Dispose();

            _context = new JavascriptContext();
        }
예제 #2
0
        /// <summary>
        /// Retrieves the CLR representation, consisting of a dictionary of strings to objects, 
        /// of a JSON string.
        /// </summary>
        /// <param name="o">JSON representation, as string</param>
        /// <returns>CLR object, consisting of a dictionary 
        /// of strings (attributes) to objects (values).</returns>
        public static object DeserializeJson(string jsonString)
        {
            JavascriptContext context = null;
            try
            {
                if (string.IsNullOrEmpty(jsonString))
                    return null;

                context = new JavascriptContext();

                context.SetParameter("arg1", jsonString);
                context.SetParameter("obj", null);
                context.Run(@" obj = JSON.parse(arg1, null); ");

                return context.GetParameter("obj");
            }
            catch (AccessViolationException)
            {
                throw new Exception("Access Violation Exception. Probably you should restart the app ...");
            }
            catch (Exception ex)
            {
                throw new Exception("Error in DeserializeJson.", ex);
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
        }
예제 #3
0
        public string encrypt(string dKey, string data, string iv, string adata)
        {
            JavascriptContext context = new JavascriptContext();

            context.SetParameter("derivedk", dKey);
            context.SetParameter("data", data);
            context.SetParameter("iv", iv);
            context.SetParameter("adata", adata);

            string js = string.Concat(jssjcl,
                                      "var c = new sjcl.cipher.aes(sjcl.codec.hex.toBits(derivedk));",
                                      "var ct = sjcl.mode.ccm.encrypt(c, sjcl.codec.utf8String.toBits(data), sjcl.codec.hex.toBits(iv), sjcl.codec.hex.toBits(adata));",
                                      "ct = sjcl.codec.hex.fromBits(ct);");

            context.Run(js);

            string response = context.GetParameter("ct") as string;

            context.TerminateExecution();
            context.Dispose();
            context = null;
            js      = null;

            return(response);
        }
예제 #4
0
 /// <summary>
 /// 资源释放
 /// </summary>
 public override void Release()
 {
     if (context != null)
     {
         context.Dispose();
         context = null;
     }
 }
예제 #5
0
 private void DisposeJSContext()
 {
     if (contextNeedsDisposale)
     {
         JsContext.TerminateExecution();
         JsContext.Dispose();
     }
 }
예제 #6
0
        private void FreeContext()
        {
            if (m_Context != null)
            {
                m_Context.Dispose();
            }

            if (m_Elements != null)
            {
                m_Elements.Dispose();
            }
        }
예제 #7
0
        private static void ResourceEnsurace()
        {
            usedCount++;

            if (usedCount >= reGenerateJSContextPeek)
            {
                if (context != null)
                {
                    context.Dispose();
                    context = null;
                }

                context = new JavascriptContext();

                usedCount = 1;
            }
        }
        private void Dispose(bool disposeManagedResources)
        {
            var disposedAlready = Interlocked.Exchange(ref alreadyDisposed, 1);

            if (disposedAlready != 0)
            {
                return;
            }

            if (!disposeManagedResources)
            {
                return;
            }

            // Dispose managed resources.
            ScriptEngine.Dispose();
        }
예제 #9
0
        static void Main(string[] args)
        {
            SubProcessClient sp = new SubProcessClient(args, AsyncHandleServerRequest);

            Console.WriteLine("Client: setting up javascript");

            JSContext.SetParameter("console", Console.Out);
            JSContext.SetParameter("sp_request", sp);

            string filename    = "Scripts.js";
            string filecontent = File.ReadAllText(filename);

            JSContext.Run(filecontent, filename);

            Console.WriteLine("Client: starting");
            sp.Start();
            quitEvent.WaitOne();
            Console.WriteLine("Client: quitting");
            JSContext.Dispose();
        }
예제 #10
0
        public override void Dispose()
        {
            Trace.Assert(ctx != null);

            ctx.Dispose();
        }
 public void TearDown()
 {
     _context.Dispose();
 }
예제 #12
0
 static void TestFunctionScope()
 {
     JavascriptContext ctx = new JavascriptContext();
     ctx.SetParameter("XB", 0);
     ctx.RegisterMethod("\"use strict\";var cyclic; var one_time; (function(){ /* XBuildr-globals: cyclic one_time */ var mathy = $.require(\"math\"); XB += 1; var kyle = 5; this.c = function() { return 2; }}).call(this);", "a");
     ctx.RegisterMethod("markus = 4; var a = 5; var b = a*b*999; XB += c();", "b");
     try
     {
         ctx.CallMethodFromDict("a");
         ctx.CallMethodFromDict("b");
     }
     catch (JavascriptException e)
     {
         Console.WriteLine("Threw error: {0} at code {1} at line {2}", (string)e.Data["V8StackTrace"], e.Source, e.Line);
         Console.WriteLine("**********");
     }
     Console.WriteLine("Parameter XB = {0}", ctx.GetParameter("XB"));
     ctx.Dispose();
 }
예제 #13
0
 static void RegisterScripts(int iterations)
 {
     JavascriptContext ctx = new JavascriptContext();
     object[] methodArgs = new object[] {};
     ctx.SetParameter("XB", 0);
     ctx.Run("function a() {XB += 1;}", "a");
     ctx.Run("function b() {var a = 5; var b = a*b*999; XB += 1;}", "b");
     var watch = new Stopwatch();
     watch.Start();
     for (int i = 0; i < iterations; i++)
     {
         ctx.CallMethod("a", methodArgs);
         ctx.CallMethod("b", methodArgs);
     }
     watch.Stop();
     Console.WriteLine("Took {0}ms to run {1} iterations of simple methods using RegisterScripts.", watch.ElapsedMilliseconds, iterations);
     ctx.Dispose();
 }
예제 #14
0
 static void RegisterFunctions(int iterations)
 {
     JavascriptContext ctx = new JavascriptContext();
     ctx.SetParameter("XB", 0);
     ctx.RegisterMethod("XB += 1;", "a");
     ctx.RegisterMethod("var a = 5; var b = a*b*999; XB += 1;", "b");
     var watch = new Stopwatch();
     watch.Start();
     for (int i = 0; i < iterations; i++)
     {
         ctx.CallMethodFromDict("a");
         ctx.CallMethodFromDict("b");
     }
     watch.Stop();
     Console.WriteLine("Took {0}ms to run {1} iterations of simple methods using RegisterFunctions.", watch.ElapsedMilliseconds, iterations);
     ctx.Dispose();
 }
 public void Dispose()
 {
     Context.Dispose();
 }
예제 #16
0
        public string encrypt(string dKey, string data, string iv, string adata)
        {
            JavascriptContext context = new JavascriptContext();

            context.SetParameter("derivedk", dKey);
            context.SetParameter("data", data);
            context.SetParameter("iv", iv);
            context.SetParameter("adata", adata);

            string js = string.Concat(jssjcl,
                "var c = new sjcl.cipher.aes(sjcl.codec.hex.toBits(derivedk));",
                "var ct = sjcl.mode.ccm.encrypt(c, sjcl.codec.utf8String.toBits(data), sjcl.codec.hex.toBits(iv), sjcl.codec.hex.toBits(adata));",
                "ct = sjcl.codec.hex.fromBits(ct);");

            context.Run(js);

            string response = context.GetParameter("ct") as string;
            context.TerminateExecution();
            context.Dispose();
            context = null;
            js = null;

            return response;
        }
예제 #17
0
        /// <summary>
        /// Retrieves the JSON representation of a CLR object, 
        /// consisting of a dictionary of strings to objects.
        /// </summary>
        /// <param name="o">CLR object, 
        /// consisting of a dictionary of strings (attributes) to objects (values).</param>
        /// <returns>JSON representation, as string</returns>
        public static string SerializeObject(object o)
        {
            JavascriptContext context = null;
            try
            {
                if (o == null)
                    return "";

                context = new JavascriptContext();

                context.SetParameter("arg1", o);
                context.SetParameter("obj", null);
                context.Run(@" obj = JSON.stringify(arg1, null); ");

                return (string)context.GetParameter("obj");
            }
            catch (AccessViolationException)
            {
                throw new Exception("Access Violation Exception. Probably you should restart the app ...");
            }
            catch (Exception ex)
            {
                throw new Exception("Error in SerializeObject.", ex);
            }
            finally
            {
                if (context != null)
                {
                    context.Dispose();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
        }
예제 #18
0
 public void Destruct()
 {
     JsContext.TerminateExecution();
     JsContext.Dispose();
 }