public override void Dispose()
        {
            if (_disposedFlag.Set())
            {
                lock (_executionSynchronizer)
                {
                    if (_jsContext != null)
                    {
                        _jsContext.Dispose();
                        _jsContext = null;
                    }

                    if (_jsEngine != null)
                    {
                        _jsEngine.Dispose();
                        _jsEngine = null;
                    }

                    if (_hostItems != null)
                    {
                        _hostItems.Clear();
                    }
                }
            }
        }
        /// <summary>
        /// 将js对象转化成.net对象
        /// added by chuan.yin in 2017/5/16
        /// </summary>
        /// <param name="context"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private object Convert2Object(OriginalJsContext context, object obj)
        {
            if (obj is JsObject)
            {
                var dic = new Dictionary <string, object>();
                var jo  = (JsObject)obj;
                foreach (var item in context.GetMemberNames(jo))
                {
                    var v = context.GetPropertyValue(jo, item);
                    dic.Add(item, Convert2Object(context, v));
                }

                return(dic);
            }
            else if (obj is DateTime)
            {
                return(((DateTime)obj).ToLocalTime());
            }
            else if (obj is JsFunction)
            {
                var jf = (JsFunction)obj;
                return(jf.ToString());
            }
            else
            {
                return(obj);
            }
        }
        public JsObject(JsContext context, IntPtr ptr)
        {
            if (ptr == IntPtr.Zero)
                throw new ArgumentException("can't wrap an empty object (ptr is Zero)", "ptr");

            _context = context;
            _handle = ptr;
        }
示例#4
0
        public JsFunction(JsContext context, IntPtr funcPtr, IntPtr thisPtr)
        {
            if (context == null)
                throw new ArgumentNullException("context");
            if (funcPtr == IntPtr.Zero)
                throw new ArgumentException("can't wrap an empty function (ptr is Zero)", "ptr");

            _context = context;
            _funcPtr = funcPtr;
            _thisPtr = thisPtr;
        }
        /// <summary>
        /// Constructs an instance of adapter for the Vroom JS engine
        /// (cross-platform bridge to the V8 JS engine)
        /// </summary>
        /// <param name="settings">Settings of the Vroom JS engine</param>
        public EFFCVroomJsEngine(EFFCVroomSettings settings)
        {
            EFFCVroomSettings vroomSettings = settings ?? new EFFCVroomSettings();

            try
            {
                _jsEngine = new OriginalJsEngine(vroomSettings.MaxYoungSpaceSize,
                                                 vroomSettings.MaxOldSpaceSize);
                _jsContext = _jsEngine.CreateContext();
            }
            catch (Exception e)
            {
                throw new JsEngineLoadException(
                          string.Format(CoreStrings.Runtime_JsEngineNotLoaded,
                                        EngineName, e.Message), EngineName, EngineVersion, e);
            }
        }
        /// <summary>
        /// 将js对象转化成.net对象
        /// added by chuan.yin in 2017/5/16
        /// </summary>
        /// <param name="context"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        private object Convert2Object(OriginalJsContext context, object obj)
        {
            if (obj is JsObject)
            {
                var dic = new Dictionary <string, object>();
                var jo  = (JsObject)obj;
                foreach (var item in context.GetMemberNames(jo))
                {
                    var v = context.GetPropertyValue(jo, item);
                    dic.Add(item, Convert2Object(context, v));
                }

                return(dic);
            }
            else if (obj is object[])
            {
                var jo   = (object[])obj;
                var list = new List <object>();
                foreach (var item in jo)
                {
                    list.Add(Convert2Object(context, item));
                }

                return(list.ToArray());
            }
            else if (obj is DateTime)
            {
                //不能转为本地时间
                return((DateTime)obj);
            }
            else if (obj is JsFunction)
            {
                var jf = (JsFunction)obj;
                return(jf.ToString());
            }
            else
            {
                return(obj);
            }
        }
示例#7
0
 public JsConvert(JsContext context)
 {
     _context = context;
 }
示例#8
0
 public NativeObjectProxyStore(JsContext ownerContext)
 {
     this.ownerContext = ownerContext;
 }
示例#9
0
 internal static void CtxRegisterManagedMethodCall(JsContext jsContext, ManagedMethodCallDel mMethodCall)
 {
     ContextRegisterManagedCallback(
         jsContext.Handle.Handle,
         System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(mMethodCall),
         (int)ManagedCallbackKind.MethodCall);
 }
示例#10
0
        public static unsafe void RegisterTypeDef(JsContext context, JsTypeDefinition jsTypeDefinition)
        {
            INativeRef proxObject = jsTypeDefinition.nativeProxy;
            byte[] finalBuffer = null;
            using (MemoryStream ms = new MemoryStream())
            {
                //serialize with our custom protocol
                //plan change to json ?

                //utf16
                BinaryWriter binWriter = new BinaryWriter(ms, System.Text.Encoding.Unicode);
                //binay format
                //1. typename
                //2. fields
                //3. method
                //4. indexer get/set
                binWriter.Write((short)1);//start marker

                context.CollectionTypeMembers(jsTypeDefinition);
                //------------------------------------------------

                jsTypeDefinition.WriteDefinitionToStream(binWriter);
                //------------------------------------------------
                finalBuffer = ms.ToArray();

                fixed (byte* tt = &finalBuffer[0])
                {
                    proxObject.SetUnmanagedPtr(
                        ContextRegisterTypeDefinition(
                        context.Handle.Handle,
                        0, tt, finalBuffer.Length));
                }

                ms.Close();
            }
        }
示例#11
0
 public static void CreateNativePart(JsContext context, INativeScriptable proxyObj)
 {
     if (!proxyObj.HasNativeSide)
     {
         proxyObj.SetUnmanagedPtr(
             CreateWrapperForManagedObject(
                 context.Handle.Handle,
                 proxyObj.ManagedIndex,
                 proxyObj.UnmanagedTypeDefinitionPtr));
     }
 }
示例#12
0
文件: JsConvert.cs 项目: killf/V8Net
 public JsConvert(JsContext context)
 {
     _context = context;
 }
示例#13
0
 public JsContext CreateContext()
 {
     CheckDisposed();
     int id = Interlocked.Increment(ref _currentContextId);
     JsContext ctx = new JsContext(id, this, _engine, ContextDisposed);
     _aliveContexts.Add(id, ctx);
     return ctx;
 }
示例#14
0
 public NativeObjectProxyStore(JsContext ownerContext)
 {
     this.ownerContext = ownerContext;
 }
示例#15
0
 public JsValue ToJsValue(char c)
 {
     // We need to allocate some memory on the other side; will be free'd by unmanaged code.
     return(JsContext.jsvalue_alloc_string(c.ToString()));
 }
示例#16
0
        public JsValue ToJsValue(object obj)
        {
            if (obj == null)
            {
                return new JsValue {
                           Type = JsValueType.Null
                }
            }
            ;

            Type type = obj.GetType();

            // Check for nullable types (we will cast the value out of the box later).

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                type = type.GetGenericArguments()[0];
            }

            if (type == typeof(Boolean))
            {
                return new JsValue {
                           Type = JsValueType.Boolean, I32 = (bool)obj ? 1 : 0
                }
            }
            ;

            if (type == typeof(String) || type == typeof(Char))
            {
                // We need to allocate some memory on the other side; will be free'd by unmanaged code.
                return(JsContext.jsvalue_alloc_string(obj.ToString()));
            }

            if (type == typeof(Byte))
            {
                return new JsValue {
                           Type = JsValueType.Integer, I32 = (int)(Byte)obj
                }
            }
            ;
            if (type == typeof(Int16))
            {
                return new JsValue {
                           Type = JsValueType.Integer, I32 = (int)(Int16)obj
                }
            }
            ;
            if (type == typeof(UInt16))
            {
                return new JsValue {
                           Type = JsValueType.Integer, I32 = (int)(UInt16)obj
                }
            }
            ;
            if (type == typeof(Int32))
            {
                return new JsValue {
                           Type = JsValueType.Integer, I32 = (int)obj
                }
            }
            ;
            if (type == typeof(UInt32))
            {
                return new JsValue {
                           Type = JsValueType.Integer, I32 = (int)(UInt32)obj
                }
            }
            ;

            if (type == typeof(Int64))
            {
                return new JsValue {
                           Type = JsValueType.Number, Num = (double)(Int64)obj
                }
            }
            ;
            if (type == typeof(UInt64))
            {
                return new JsValue {
                           Type = JsValueType.Number, Num = (double)(UInt64)obj
                }
            }
            ;
            if (type == typeof(Single))
            {
                return new JsValue {
                           Type = JsValueType.Number, Num = (double)(Single)obj
                }
            }
            ;
            if (type == typeof(Double))
            {
                return new JsValue {
                           Type = JsValueType.Number, Num = (double)obj
                }
            }
            ;
            if (type == typeof(Decimal))
            {
                return new JsValue {
                           Type = JsValueType.Number, Num = (double)(Decimal)obj
                }
            }
            ;

            if (type == typeof(DateTime))
            {
                return new JsValue {
                           Type = JsValueType.Date,
                           Num  = Convert.ToInt64(((DateTime)obj).Subtract(EPOCH).TotalMilliseconds) /*(((DateTime)obj).Ticks - 621355968000000000.0 + 26748000000000.0)/10000.0*/
                }
            }
            ;

            // Arrays of anything that can be cast to object[] are recursively convertef after
            // allocating an appropriate jsvalue on the unmanaged side.

            var array = obj as object[];

            if (array != null)
            {
                JsValue v = JsContext.jsvalue_alloc_array(array.Length);

                if (v.Length != array.Length)
                {
                    throw new JsInteropException("can't allocate memory on the unmanaged side");
                }
                for (int i = 0; i < array.Length; i++)
                {
                    Marshal.StructureToPtr(ToJsValue(array[i]), new IntPtr(v.Ptr.ToInt64() + (16 * i)), false);
                }
                return(v);
            }

            // Every object explicitly converted to a value becomes an entry of the
            // _keepalives list, to make sure the GC won't collect it while still in
            // use by the unmanaged Javascript engine. We don't try to track duplicates
            // because adding the same object more than one time acts more or less as
            // reference counting.

            return(new JsValue {
                Type = JsValueType.Managed, Index = _context.KeepAliveAdd(obj)
            });
        }
    }
}
示例#17
0
文件: JsEngine.cs 项目: killf/V8Net
        public JsContext CreateContext(JsTypeDefinitionBuilder customTypeDefBuilder)
        {
            CheckDisposed();
            int id = Interlocked.Increment(ref _currentContextId);

            JsContext ctx = new JsContext(id, this, _engine, ContextDisposed, customTypeDefBuilder);

            _aliveContexts.Add(id, ctx);
            return ctx;
        }