Exemple #1
0
    public static void EvaluateFile(string fullName, IntPtr obj)
    {
        JSApi.jsval  val = new JSApi.jsval();
        StreamReader r   = new StreamReader(fullName, Encoding.UTF8);
        string       s   = r.ReadToEnd();

        JSApi.JSh_EvaluateScript(cx, obj, s, (uint)s.Length, fullName, 1, ref val);
        r.Close();
    }
Exemple #2
0
    public void returnObject(string className, object csObj)
    {
        JSApi.JSh_SetJsvalUndefined(ref this.valReturn);
        if (csObj == null)
        {
            JSApi.JSh_SetRvalJSVAL(cx, vp, ref this.valReturn);
            return;
        }

        if (!csObj.GetType().IsArray)
        {
            IntPtr jsObj;
            //jsObj = JSMgr.getJSObj(csObj);
            //if (jsObj == IntPtr.Zero)
            { // always add a new jsObj
                jsObj = JSApi.JSh_NewObjectAsClass(cx, JSMgr.glob, /*className*/ csObj.GetType().Name, JSMgr.mjsFinalizer);
                if (jsObj != IntPtr.Zero)
                {
                    JSMgr.addJSCSRelation(jsObj, csObj);
                }
            }

            if (jsObj == IntPtr.Zero)
            {
                JSApi.JSh_SetJsvalUndefined(ref this.valReturn);
            }
            else
            {
                JSApi.JSh_SetJsvalObject(ref this.valReturn, jsObj);
            }
        }
        else
        {
            Array arr = csObj as Array;
            if (arr.Length > 0 && arr.GetValue(0).GetType().IsArray)
            {
                Debug.LogWarning("cs return [][] may cause problems.");
            }

            IntPtr jsArr = JSApi.JSh_NewArrayObjectS(cx, arr.Length);

            for (int i = 0; i < arr.Length; i++)
            {
                JSApi.jsval subVal = CSObject_2_JSValue(arr.GetValue(i));
                JSApi.JSh_SetElement(cx, jsArr, (uint)i, ref subVal);
            }
            JSApi.JSh_SetJsvalObject(ref this.valReturn, jsArr);
        }

        JSApi.JSh_SetRvalJSVAL(cx, vp, ref this.valReturn);
    }
Exemple #3
0
    // no returns for now
    public bool CallJSFunction(IntPtr jsThis, IntPtr /* JSFunction* */ jsFunction, params object[] args)
    {
        if (args == null || args.Length == 0)
        {
            return(JSApi.JSh_CallFunction(JSMgr.cx, jsThis, jsFunction, 0, null /*IntPtr.Zero*/, ref rvalCallJS));
        }

        JSApi.jsval[] vals = new JSApi.jsval[args.Length];
        for (int i = 0; i < args.Length; i++)
        {
            vals[i] = CSObject_2_JSValue(args[i]);
        }

        return(JSApi.JSh_CallFunction(JSMgr.cx, jsThis, jsFunction, (UInt32)args.Length, vals, ref rvalCallJS));
    }
Exemple #4
0
    public void PushResult(object csObj)
    {
        if (/*this.op == Oper.METHOD && */ arrCSParam != null)
        {
            // handle ref/out parameters
            for (int i = 0; i < arrCSParamsLength; i++)
            {
                if (arrCSParam[i].isRef)
                {
                    arrJSParam[i].wrappedObj = callParams[i];
                }
            }
        }

        JSApi.jsval val = CSObject_2_JSValue(csObj);
        JSApi.JSh_SetRvalJSVAL(cx, vp, ref val);
    }
Exemple #5
0
    public object getObject(Type typeParam = null)
    {
        IntPtr jsObj = JSApi.JSh_ArgvObject(cx, vp, currIndex++);

        if (jsObj == IntPtr.Zero)
        {
            return(null);
        }

        object csObj = JSMgr.getCSObj(jsObj);

        if (csObj is JSValueWrap.Wrap)
        {
            return(((JSValueWrap.Wrap)csObj).obj);
        }
        else if (csObj != null)
        {
            return(csObj);
        }

        if (!JSApi.JSh_IsArrayObject(cx, jsObj))
        {
            return(null);
        }


        // array params don't work.
        // code must be generated, cann't be dynamically run.
        // because type is unknown during run-time.
        Type typeElement = typeParam.GetElementType();

        JSApi.jsval valElement = new JSApi.jsval();

        int length = JSApi.JSh_GetArrayLength(cx, jsObj);

        object[] arr = new object[length];
        for (int i = 0; i < length; i++)
        {
            JSApi.JSh_GetElement(cx, jsObj, (uint)i, ref valElement);
            object csObjElement = JSValue_2_CSObject(typeElement, ref valElement);
            arr[i] = csObjElement;
        }
        return(arr);
    }
Exemple #6
0
 public static bool ExecuteScript(IntPtr ptrScript, IntPtr obj)
 {
     JSApi.jsval val = new JSApi.jsval();
     return(JSApi.JSh_ExecuteScript(cx, obj, ptrScript, ref val));
 }
Exemple #7
0
 public static void EvaluateString(string name, string s, IntPtr obj)
 {
     JSApi.jsval val = new JSApi.jsval();
     JSApi.JSh_EvaluateScript(cx, obj, s, (uint)s.Length, name, 1, ref val);
 }
Exemple #8
0
    // CS -> JS
    public JSApi.jsval CSObject_2_JSValue(object csObj)
    {
        JSApi.jsval val = new JSApi.jsval();
        JSApi.JSh_SetJsvalUndefined(ref val);

        if (csObj == null)
        {
            return(val);
        }

        Type t = csObj.GetType();

        if (t == typeof(void))
        {
            return(val);
        }
        else if (t == typeof(string))
        {
            JSApi.JSh_SetJsvalString(cx, ref val, (string)csObj);
        }
        else if (t.IsEnum)
        {
            JSApi.JSh_SetJsvalInt(ref val, (int)csObj);
        }
        else if (t.IsPrimitive)
        {
            if (t == typeof(System.Boolean))
            {
                JSApi.JSh_SetJsvalBool(ref val, (bool)csObj);
            }
            else if (t == typeof(System.UInt32) || t == typeof(System.Int32))
            {
                int v = (Int32)csObj;
                JSApi.JSh_SetJsvalInt(ref val, v);
            }
            else if (t == typeof(System.Char) ||
                     t == typeof(System.Byte) || t == typeof(System.SByte) ||
                     t == typeof(System.UInt16) || t == typeof(System.Int16) ||
                     //t == typeof(System.UInt32) || t == typeof(System.Int32) ||
                     t == typeof(System.UInt64) || t == typeof(System.Int64))
            {
                JSApi.JSh_SetJsvalInt(ref val, (Int32)(Int64)csObj);
            }
            else if (t == typeof(System.Single) || t == typeof(System.Double))
            {
                JSApi.JSh_SetJsvalDouble(ref val, (double)csObj);
            }
            else
            {
                Debug.Log("CS -> JS: Unknown primitive type: " + t.ToString());
            }
        }
        //         else if (t.IsValueType)
        //         {
        //
        //         }
        else if (t.IsArray)
        {
            Debug.LogError("CSObject_2_JSValue: Cannot handle array here");

            // todo
            // return [][] may cause problems
//             Array arr = csObj as Array;
//             if (arr.Length > 0 && arr.GetValue(0).GetType().IsArray)
//             {
//                 Debug.LogWarning("cs return [][] may cause problems.");
//             }
//
//             IntPtr jsArr = JSApi.JSh_NewArrayObjectS(cx, arr.Length);
//
//             for (int i = 0; i < arr.Length; i++)
//             {
//                 JSApi.jsval subVal = CSObject_2_JSValue(arr.GetValue(i));
//                 JSApi.JSh_SetElement(cx, jsArr, (uint)i, ref subVal);
//             }
//             JSApi.JSh_SetJsvalObject(ref val, jsArr);
        }
        else// if (typeof(UnityEngine.Object).IsAssignableFrom(t) || t.IsClass || t.IsValueType)
        {
            IntPtr jsObj;
//             jsObj = JSMgr.getJSObj(csObj);
//             if (jsObj == IntPtr.Zero)
            {
                // always add a new jsObj
                jsObj = JSApi.JSh_NewObjectAsClass(cx, JSMgr.glob, t.Name, JSMgr.mjsFinalizer);
                if (jsObj != IntPtr.Zero)
                {
                    JSMgr.addJSCSRelation(jsObj, csObj);
                }
            }
            if (jsObj == IntPtr.Zero)
            {
                JSApi.JSh_SetJsvalUndefined(ref val);
            }
            else
            {
                JSApi.JSh_SetJsvalObject(ref val, jsObj);
            }
        }
//         else
//         {
//             Debug.Log("CS -> JS: Unknown CS type: " + t.ToString());
//             JSApi.JSh_SetJsvalUndefined(ref val);
//         }
        return(val);
    }
Exemple #9
0
    // used for js->cs array
    //
    public object JSValue_2_CSObject(Type t, ref JSApi.jsval val)
    {
        if (t.IsArray)
        {
            Debug.LogError("JSValue_2_CSObject: could not pass an array");
            return(null);
        }

        if (t.IsByRef)
        {
            t = t.GetElementType();
        }

        if (t == typeof(string))
        {
            return(JSApi.JSh_GetJsvalString(cx, ref val));
        }
        else if (t.IsEnum)
        {
            return(JSApi.JSh_GetJsvalInt(ref val));
        }
        else if (t.IsPrimitive)
        {
            if (t == typeof(System.Boolean))
            {
                return(JSApi.JSh_GetJsvalBool(ref val));
            }
            else if (t == typeof(System.Char) ||
                     t == typeof(System.Byte) || t == typeof(System.SByte) ||
                     t == typeof(System.UInt16) || t == typeof(System.Int16) ||
                     t == typeof(System.UInt32) || t == typeof(System.Int32) ||
                     t == typeof(System.UInt64) || t == typeof(System.Int64))
            {
                return(JSApi.JSh_GetJsvalInt(ref val));
            }
            else if (t == typeof(System.Single) || t == typeof(System.Double))
            {
                return(JSApi.JSh_GetJsvalDouble(ref val));
            }
            else
            {
                Debug.Log("ConvertJSValue2CSValue: Unknown primitive type: " + t.ToString());
            }
        }
        //         else if (t.IsValueType)
        //         {
        //
        //         }
        else// if (typeof(UnityEngine.Object).IsAssignableFrom(t) || t.IsValueType)
        {
            IntPtr jsObj = JSApi.JSh_GetJsvalObject(ref val);
            if (jsObj == IntPtr.Zero)
            {
                return(null);
            }

            object csObject = JSMgr.getCSObj(jsObj);
            return(csObject);
        }
        //         else
        //         {
        //             Debug.Log("ConvertJSValue2CSValue: Unknown CS type: " + t.ToString());
        //         }
        return(null);
    }