protected override JsTypeDefinition OnBuildRequest(Type t) { //find member that has JsPropertyAttribute or JsMethodAttribute JsTypeDefinition typedefinition = new JsTypeDefinition(t.Name); //only instance /public method /prop*** var methods = t.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (var met in methods) { var customAttrs = met.GetCustomAttributes(typeOfJsMethodAttr, false); if (customAttrs != null && customAttrs.Length > 0) { var attr = customAttrs[0] as JsMethodAttribute; typedefinition.AddMember(new JsMethodDefinition(attr.Name ?? met.Name, met)); } } var properties = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (var property in properties) { var customAttrs = property.GetCustomAttributes(typeOfJsPropertyAttr, false); if (customAttrs != null && customAttrs.Length > 0) { var attr = customAttrs[0] as JsPropertyAttribute; typedefinition.AddMember(new JsPropertyDefinition(attr.Name ?? property.Name, property)); } } return(typedefinition); }
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.NativeContextHandle.Handle, 0, tt, finalBuffer.Length)); } } }
protected override JsTypeDefinition OnBuildRequest(Type t) { JsTypeDefinition typedefinition = new JsTypeDefinition(t.Name); //only instance /public method /prop*** //MethodInfo[] methods = t.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); var methods = t.GetRuntimeMethods(); foreach (var met in methods) { if (!met.IsStatic && met.IsPublic) { typedefinition.AddMember(new JsMethodDefinition(met.Name, met)); } } //var properties = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); var properties = t.GetRuntimeProperties(); //TODO finding GetProperties with BindingFlags foreach (var property in properties) { typedefinition.AddMember(new JsPropertyDefinition(property.Name, property)); } return(typedefinition); }
public static void Run(NodeJsExecSessionSetup nodeExecSession) { //------------ NodeJsEngine.Run((eng, ctx) => { //------------- //this LibEspressoClass object is need, //so node can talk with us, //------------- JsTypeDefinition jstypedef = new JsTypeDefinition(LIB_ESPRESSO_CLASS); NodeJsExecSession nodeJsExecSession = new NodeJsExecSession(eng, ctx); string mainSrc = nodeExecSession(nodeJsExecSession); jstypedef.AddMember(new JsMethodDefinition(LOAD_MAIN_SRC_FILE, args => { args.SetResult(mainSrc); })); if (!jstypedef.IsRegisterd) { ctx.RegisterTypeDefinition(jstypedef); } //---------- //then register this as x*** //this object is just an instance for reference ctx.SetVariableFromAny(LIB_ESPRESSO, ctx.CreateWrapper(new object(), jstypedef)); }); }
public void SetVariableAutoWrap <T>(string name, T result) where T : class { Type actualType = result.GetType(); JsTypeDefinition jsTypeDef = this.GetJsTypeDefinition(actualType); INativeScriptable proxy = this.CreateWrapper(result, jsTypeDef); this.SetVariable(name, proxy); }
public void SetResultAutoWrap <T>(T result) where T : class, new() { Type actualType = result.GetType(); JsTypeDefinition jsTypeDef = this.context.GetJsTypeDefinition(actualType); INativeScriptable proxy = this.context.CreateWrapper(result, jsTypeDef); JsValue output = new JsValue(); this.context.Converter.ToJsValue(proxy, ref output); NativeV8JsInterOp.ResultSetValue(metArgsPtr, ref output); }
public INativeRef CreateProxyForTypeDefinition(JsTypeDefinition jsTypeDefinition) { var proxyObject = new NativeRef(nativeRefList.Count, jsTypeDefinition); //store data this side too jsTypeDefinition.nativeProxy = proxyObject; //store in exported list nativeRefList.Add(proxyObject); //register type definition NativeV8JsInterOp.RegisterTypeDef(ownerContext, jsTypeDefinition); return(proxyObject); }
public void SetResultObj(object result, JsTypeDefinition jsTypeDef) { if (!jsTypeDef.IsRegisterd) { this.context.RegisterTypeDefinition(jsTypeDef); } var proxy = this.context.CreateWrapper(result, jsTypeDef); NativeV8JsInterOp.ResultSetJsValue(metArgsPtr, this.context.Converter.ToJsValue(proxy)); }
public void SetResultObj(object result, JsTypeDefinition jsTypeDef) { if (!jsTypeDef.IsRegisterd) { this.context.RegisterTypeDefinition(jsTypeDef); } INativeScriptable proxy = this.context.CreateWrapper(result, jsTypeDef); JsValue output = new JsValue(); this.context.Converter.ToJsValue(proxy, ref output); NativeV8JsInterOp.ResultSetValue(metArgsPtr, ref output); }
public NativeJsInstanceProxy CreateProxyForObject(object o, JsTypeDefinition jsTypeDefinition) { if (_createdWrappers.TryGetValue(o, out NativeJsInstanceProxy found)) { return(found); } var proxyObject = new NativeJsInstanceProxy( _nativeRefList.Count, o, jsTypeDefinition); _nativeRefList.Add(proxyObject); _createdWrappers.Add(o, proxyObject); //register NativeV8JsInterOp.CreateNativePart(_ownerContext, proxyObject); return(proxyObject); }
protected override JsTypeDefinition OnBuildRequest(Type t) { JsTypeDefinition typedefinition = new JsTypeDefinition(t.Name); //only instance /public method /prop*** var methods = t.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (var met in methods) { typedefinition.AddMember(new JsMethodDefinition(met.Name, met)); } var properties = t.GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (var property in properties) { typedefinition.AddMember(new JsPropertyDefinition(property.Name, property)); } return(typedefinition); }
internal void CollectionTypeMembers(JsTypeDefinition jsTypeDefinition) { List <JsMethodDefinition> methods = jsTypeDefinition.GetMethods(); int j = methods.Count; for (int i = 0; i < j; ++i) { JsMethodDefinition met = methods[i]; met.SetMemberId(registerMethods.Count); registerMethods.Add(met); } List <JsPropertyDefinition> properties = jsTypeDefinition.GetProperties(); j = properties.Count; for (int i = 0; i < j; ++i) { var p = properties[i]; p.SetMemberId(registerProperties.Count); registerProperties.Add(p); } }
protected override JsTypeDefinition OnBuildRequest(Type t) { JsTypeDefinition typedefinition = new JsTypeDefinition(t.Name); //------- //only instance /public method /prop*** Dictionary <string, JsMethodGroup> methodGroups = new Dictionary <string, JsMethodGroup>(); foreach (MethodInfo met in GetMehodIter(t, BindingFlags.Instance | BindingFlags.Public)) { var jsMethodDef = new JsMethodDefinition(met.Name, met); JsMethodGroup existingGroup; if (!methodGroups.TryGetValue(met.Name, out existingGroup)) { //create new one existingGroup = new JsMethodGroup(met.Name, jsMethodDef); methodGroups.Add(met.Name, existingGroup); } else { existingGroup.AddMoreMember(jsMethodDef); } } //----------------- foreach (JsMethodGroup metGroup in methodGroups.Values) { JsMethodDefinition selectedMethod = metGroup.GetJsMethod(); if (selectedMethod != null) { typedefinition.AddMember(metGroup.GetJsMethod()); } } //----------------- foreach (var property in GetPropertyIter(t, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public)) { typedefinition.AddMember(new JsPropertyDefinition(property.Name, property)); } return(typedefinition); }
public void SetOwner(JsTypeDefinition owner) => _owner = owner;
public NativeJsInstanceProxy(int mIndex, object wrapObject, JsTypeDefinition jsTypeDef) : base(mIndex, wrapObject) { this.jsTypeDef = jsTypeDef; }
public unsafe void AnyToJsValuePtr(object obj, JsValue *output) { if (obj == null) { output->Type = JsValueType.Null; return; } //----- if (obj is INativeRef) { //extension INativeRef prox = (INativeRef)obj; output->I32 = _context.KeepAliveAdd(obj); output->Type = JsValueType.JsTypeWrap; output->Ptr = prox.UnmanagedPtr; return; } //----- Type type = obj.GetType(); // Check for nullable types (we will cast the value out of the box later). Type innerTypeOfNullable = type.ExtGetInnerTypeIfNullableValue(); if (innerTypeOfNullable != null) { type = innerTypeOfNullable; } if (type == typeof(Boolean)) { output->Type = JsValueType.Boolean; output->I32 = (bool)obj ? 1 : 0; return; } if (type == typeof(String) || type == typeof(Char)) { // We need to allocate some memory on the other side; // will be free'd by unmanaged code. string strdata = obj.ToString(); unsafe { fixed(char *b = strdata) { JsContext.jsvalue_alloc_string2(b, strdata.Length, output); } } output->Type = JsValueType.String; return; } //----------------------------------------------------------- if (type == typeof(Byte)) { output->Type = JsValueType.Integer; output->I32 = (int)(byte)obj; return; } if (type == typeof(Int16)) { output->Type = JsValueType.Integer; output->I32 = (int)(Int16)obj; return; } if (type == typeof(UInt16)) { output->Type = JsValueType.Integer; output->I32 = (int)(UInt16)obj; return; } if (type == typeof(Int32)) { output->Type = JsValueType.Integer; output->I32 = (int)obj; return; } if (type == typeof(UInt32)) { //TODO: review Type here when send to native side output->Type = JsValueType.Integer; output->I32 = (int)(uint)obj; return; } if (type == typeof(Int64)) { output->Type = JsValueType.Number; output->Num = (double)(Int64)obj; return; } if (type == typeof(UInt64)) { output->Type = JsValueType.Number; output->Num = (double)(UInt64)obj; return; } if (type == typeof(Single)) { output->Type = JsValueType.Number; output->Num = (double)(Single)obj; return; } if (type == typeof(Double)) { output->Type = JsValueType.Number; output->Num = (double)obj; return; } if (type == typeof(Decimal)) { //TODO: review here //.net decimal is larger than double? output->Type = JsValueType.Number; output->Num = (double)(Decimal)obj; return; } if (type == typeof(DateTime)) { output->Type = JsValueType.Date; output->Num = Convert.ToInt64(((DateTime)obj).Subtract(EPOCH).TotalMilliseconds); /*(((DateTime)obj).Ticks - 621355968000000000.0 + 26748000000000.0)/10000.0*/ return; } // 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) { //alloc space for array int arrLen = array.Length; JsContext.jsvalue_alloc_array(arrLen, output); if (output->I32 != arrLen) { throw new JsInteropException("can't allocate memory on the unmanaged side"); } // output->Type = JsValueType.Array; unsafe { JsValue *arr = (JsValue *)output->Ptr; for (int i = 0; i < arrLen; i++) { AnyToJsValuePtr(array[i], arr + i); } } return; } // 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. //check JsTypeDefinition jsTypeDefinition = _context.GetJsTypeDefinition(type); INativeRef prox2 = _context.CreateWrapper(obj, jsTypeDefinition); // output->Type = JsValueType.JsTypeWrap; output->Ptr = prox2.UnmanagedPtr; output->I32 = prox2.ManagedIndex; }
public void RegisterTypeDefinition(JsTypeDefinition jsTypeDefinition) { proxyStore.CreateProxyForTypeDefinition(jsTypeDefinition); }
public INativeScriptable CreateWrapper(object o, JsTypeDefinition jsTypeDefinition) { return(proxyStore.CreateProxyForObject(o, jsTypeDefinition)); }
public void SetOwner(JsTypeDefinition owner) { this.owner = owner; }