public void PrintLastException(INTPTR ctx) { return; INTPTR p = M.ReadIntPtr(lastException); if (p != INTPTR.Zero) { JSDLL.JSType t = JSDLL.JSType.kJSTypeUndefined; t = JSDLL.JSValueGetType(ctx, p); if (t != JSDLL.JSType.kJSTypeUndefined) { print(t.ToString()); if (t != JSDLL.JSType.kJSTypeNull) { var eStr = JSDLL.JSValueToStringCopy(ctx, p, lastException); var eStrPtr = JSDLL.JSStringGetCharactersPtr(eStr); var eStrLen = JSDLL.JSStringGetLength(eStr); var eString = M.PtrToStringAuto(eStrPtr, eStrLen); print(eString); } } } M.WriteIntPtr(lastException, INTPTR.Zero); }
public void Test1() { M.WriteIntPtr(lastException, INTPTR.Zero); JSDLL.JSEvaluateScript(gtx, JSDLL.JSStringCreateWithUTF8CString("a.finalize();delete a;"), INTPTR.Zero, INTPTR.Zero, 0, lastException); JSDLL.JSGarbageCollect(gtx); PrintLastException(gtx); }
public void Test() { logHandle = FindObjectOfType <LogHandle>(); M.WriteIntPtr(lastException, INTPTR.Zero); JSDLL.JSEvaluateScript(gtx, JSDLL.JSStringCreateWithUTF8CString("a = new UnityEngine.GameObject('a123');"), INTPTR.Zero, INTPTR.Zero, 0, lastException); PrintLastException(gtx); }
/// <include file='doc\SelectionService.uex' path='docs/doc[@for="SelectionService.GetObjects"]/*' /> /// <devdoc> /// Returns an array of objects. /// </devdoc> void ISelectionContainer.GetObjects(int flags, int cObjects, IntPtr objects) { int cnt = 0; switch (flags) { case __GETOBJS.ALL: if (container != null) { ComponentCollection components = container.Components; foreach (IComponent comp in components) { cnt++; if (cnt > cObjects) { break; } Marshal.WriteIntPtr(objects, Marshal.GetIUnknownForObject(comp)); objects = (IntPtr)((long)objects + IntPtr.Size); } } break; case __GETOBJS.SELECTED: ICollection comps = GetSelectedComponents(); int ipSize = IntPtr.Size; foreach (object o in comps) { cnt++; if (cnt > cObjects) { break; } Marshal.WriteIntPtr(objects, Marshal.GetIUnknownForObject(o)); objects = (IntPtr)((long)objects + ipSize); } break; default: throw new COMException("Invalid flags", NativeMethods.E_INVALIDARG); } }
/// <devdoc> /// Copy the EncoderParameters data into a chunk of memory to be consumed by native GDI+ code. /// /// We need to marshal the EncoderParameters info from/to native GDI+ ourselve since the definition of the managed/unmanaged classes /// are different and the native class is a bit weird. The native EncoderParameters class is defined in GDI+ as follows: /// /// class EncoderParameters { /// UINT Count; // Number of parameters in this structure /// EncoderParameter Parameter[1]; // Parameter values /// }; /// /// We don't have the 'Count' field since the managed array contains it. In order for this structure to work with more than one /// EncoderParameter we need to preallocate memory for the extra n-1 elements, something like this: /// /// EncoderParameters* pEncoderParameters = (EncoderParameters*) malloc(sizeof(EncoderParameters) + (n-1) * sizeof(EncoderParameter)); /// /// Also, in 64-bit platforms, 'Count' is aligned in 8 bytes (4 extra padding bytes) so we use IntPtr instead of Int32 to account for /// that (See VSW#451333). /// </devdoc> internal IntPtr ConvertToMemory() { int size = Marshal.SizeOf(typeof(EncoderParameter)); int length = param.Length; IntPtr memory = Marshal.AllocHGlobal(checked (length * size + Marshal.SizeOf(typeof(IntPtr)))); if (memory == IntPtr.Zero) { throw SafeNativeMethods.Gdip.StatusException(SafeNativeMethods.Gdip.OutOfMemory); } Marshal.WriteIntPtr(memory, (IntPtr)length); long arrayOffset = checked ((long)memory + Marshal.SizeOf(typeof(IntPtr))); for (int i = 0; i < length; i++) { Marshal.StructureToPtr(param[i], (IntPtr)(arrayOffset + i * size), false); } return(memory); }
public void BindTypes() { M.WriteIntPtr(lastException, INTPTR.Zero); var gobj = JSDLL.JSContextGetGlobalObject(gtx); System.Reflection.BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase; JSDLL.JSClassDefinition CommonClass = new JSDLL.JSClassDefinition() { Attributes = JSDLL.JSClassAttributes.kJSClassAttributeNoAutomaticPrototype, ClassName = "CommonClass" }; System.Type type = typeof(UnityEngine.GameObject); INTPTR clz = INTPTR.Zero; var nodes = type.FullName.Split('.'); CommonClass.ClassName = nodes[nodes.Length - 1]; INTPTR obj = gobj; for (int i = 0, count = nodes.Length; i < count - 1; i++) { bool hasProperty = false; var node = nodes[i]; INTPTR propertyNamePtr = JSDLL.JSStringCreateWithUTF8CString(node); hasProperty = JSDLL.JSObjectHasProperty(gtx, obj, propertyNamePtr); if (!hasProperty) { var o = JSDLL.JSObjectMake(gtx, JSDLL.JSClassCreate(ref DefaultClz), INTPTR.Zero); JSDLL.JSObjectSetProperty(gtx, obj, propertyNamePtr, o, JSDLL.JSPropertyAttributes.kJSPropertyAttributeNone, lastException); obj = o; } else { obj = JSDLL.JSObjectGetProperty(gtx, obj, propertyNamePtr, lastException); } } var constructors = type.GetConstructors(bindingFlags); foreach (var c in constructors) { if (c.GetParameters().Length == 1) { CommonClass.callAsConstructor = delegate(INTPTR ctx, INTPTR constructor, INT argumentCount, INTPTR arguments, INTPTR exception) { print("Constructor fun called..."); logHandle.text.text = "Constructor fun called..."; //c.Invoke(new object[] { M.PtrToStringAuto(JSStringGetCharactersPtr(arguments)) }); var arg = M.ReadIntPtr(arguments, 0); var jsStr = JSDLL.JSValueToStringCopy(gtx, arg, lastException); var jsStrLen = JSDLL.JSStringGetLength(jsStr); var charPtr = JSDLL.JSStringGetCharactersPtr(jsStr); string gName = M.PtrToStringUni(charPtr, jsStrLen); print(gName); logHandle.text1.text = gName; var go = new GameObject(gName); var t = type; var cz = bindedClaz[t]; var o = JSDLL.JSObjectMake(gtx, cz, INTPTR.Zero); cachedObject[go.GetHashCode()] = go; var goValue = JSDLL.JSValueMakeNumber(gtx, go.GetHashCode()); //var goHandle = System.Runtime.InteropServices.GCHandle.Alloc(go, System.Runtime.InteropServices.GCHandleType.Weak); //var goPtr = System.Runtime.InteropServices.GCHandle.ToIntPtr(goHandle); //var goValue = JSValueMakeNumber(gtx, goPtr.ToInt64()); //var hr = new System.Runtime.InteropServices.HandleRef(go, goPtr); //print("handleRef addr:" + ((INTPTR)hr).ToInt64()); JSDLL.JSObjectSetProperty(gtx, o, JSDLL.JSStringCreateWithUTF8CString("TargetRefId"), goValue, JSDLL.JSPropertyAttributes.kJSPropertyAttributeNone, lastException); print(o.ToInt32()); logHandle.text2.text = o.ToInt32().ToString(); //print(goPtr.ToInt32()); return(o); }; CommonClass.finalize = delegate(INTPTR objPtr) { print("On finalize..."); //var targetRefId = JSObjectGetProperty(gtx, objPtr, JSStringCreateWithUTF8CString("TargetRefId"), lastException); //var gcHandle = System.Runtime.InteropServices.GCHandle.FromIntPtr(targetRefId); //gcHandle.Free(); }; } } bindedDef[type] = CommonClass; clz = JSDLL.JSClassCreate(ref CommonClass); bindedClaz[type] = clz; var obj1 = JSDLL.JSObjectMake(gtx, clz, INTPTR.Zero); JSDLL.JSValueProtect(gtx, obj1); JSDLL.JSObjectSetProperty(gtx, obj, JSDLL.JSStringCreateWithUTF8CString(CommonClass.ClassName), obj1, JSDLL.JSPropertyAttributes.kJSPropertyAttributeReadOnly, lastException); JSDLL.JSValueUnprotect(gtx, obj1); var fun = JSDLL.JSObjectMakeFunctionWithCallback(gtx, JSDLL.JSStringCreateWithUTF8CString("Print"), delegate(INTPTR ctx, INTPTR function, INTPTR thisObject, INT argumentCount, INTPTR arguments, INTPTR exception) { //var jsErrStr = JSValueMakeString(gtx, JSStringCreateWithUTF8CString("TestError throw exception from c# to js")); //var err = JSObjectMakeError(gtx, 1, new INTPTR[1] { jsErrStr }, lastException); //M.WriteIntPtr(exception, err); var res = JSDLL.JSValueMakeString(gtx, JSDLL.JSStringCreateWithUTF8CString("hello")); string txt = "Function fun called..."; print(txt); logHandle.text.text = txt; return(res); }); var funPrintGoName = JSDLL.JSObjectMakeFunctionWithCallback(gtx, JSDLL.JSStringCreateWithUTF8CString("PrintGo"), delegate(INTPTR ctx, INTPTR function, INTPTR thisObject, INT argumentCount, INTPTR arguments, INTPTR exception) { if (argumentCount == 1) { var arg = M.ReadIntPtr(arguments, 0); print(arg.ToInt32()); var o = JSDLL.JSValueToObject(gtx, arg, lastException); if (o != null) { var targetRefId = JSDLL.JSObjectGetProperty(gtx, o, JSDLL.JSStringCreateWithUTF8CString("TargetRefId"), lastException); int hashCode = (int)targetRefId;//(int)JSValueToNumber(gtx,targetRefId, lastException); GameObject go = null; if (cachedObject.ContainsKey(hashCode)) { go = cachedObject[hashCode] as GameObject; } //var targetRefId = JSObjectGetProperty(gtx, o, JSStringCreateWithUTF8CString("TargetRefId"), lastException); //var targetGcHandle = System.Runtime.InteropServices.GCHandle.FromIntPtr(targetRefId); //var go = targetGcHandle.Target as GameObject;\ string txt = "go Name:" + go.name; print(txt); logHandle.text1.text = txt; } } return(INTPTR.Zero); }); //var fun1 = JSObjectMakeFunctionWithCallback(gtx, JSStringCreateWithUTF8CString("Print1"), delegate (INTPTR ctx, INTPTR function, INTPTR thisObject, INT argumentCount, INTPTR arguments, INTPTR exception) //{ // var res = JSValueMakeString(gtx, JSStringCreateWithUTF8CString("hello")); // print("Function fun1 called..."); // return res; //}); PrintLastException(gtx); JSDLL.JSObjectSetProperty(gtx, gobj, JSDLL.JSStringCreateWithUTF8CString("Print"), fun, JSDLL.JSPropertyAttributes.kJSPropertyAttributeNone, lastException); JSDLL.JSObjectSetProperty(gtx, gobj, JSDLL.JSStringCreateWithUTF8CString("PrintGo"), funPrintGoName, JSDLL.JSPropertyAttributes.kJSPropertyAttributeNone, lastException); PrintLastException(gtx); }
public void Test3() { M.WriteIntPtr(lastException, INTPTR.Zero); JSDLL.JSEvaluateScript(gtx, JSDLL.JSStringCreateWithUTF8CString(" PrintGo(b);"), INTPTR.Zero, INTPTR.Zero, 0, lastException); PrintLastException(gtx); }