Beispiel #1
0
    void Awake()
    {
        JSDLL.JSClassDefinition GlobalContextClz = new JSDLL.JSClassDefinition()
        {
            Attributes = JSDLL.JSClassAttributes.kJSClassAttributeNoAutomaticPrototype,
            ClassName  = "GlobalContext"
        };

        try
        {
            INTPTR clz = JSDLL.JSClassCreate(ref GlobalContextClz);
            gtx = JSDLL.JSGlobalContextCreate(clz);
        }catch (System.Exception e)
        {
            print(e.ToString());
        }
    }
Beispiel #2
0
    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);
    }