Exemple #1
0
        internal static void Init()
        {
            StaticTranslate <bool> .ReplaceDefault(PushBoolean, GetBoolean);

            StaticTranslate <char> .ReplaceDefault(PushChar, GetChar);

            StaticTranslate <sbyte> .ReplaceDefault(PushSByte, GetSByte);

            StaticTranslate <byte> .ReplaceDefault(PushByte, GetByte);

            StaticTranslate <short> .ReplaceDefault(PushInt16, GetInt16);

            StaticTranslate <ushort> .ReplaceDefault(PushUInt16, GetUInt16);

            StaticTranslate <int> .ReplaceDefault(PushInt32, GetInt32);

            StaticTranslate <uint> .ReplaceDefault(PushUInt32, GetUInt32);

            StaticTranslate <long> .ReplaceDefault(PushInt64, GetInt64);

            StaticTranslate <ulong> .ReplaceDefault(PushUInt64, GetUInt64);

            StaticTranslate <double> .ReplaceDefault(PushDouble, GetDouble);

            StaticTranslate <float> .ReplaceDefault(PushFloat, GetFloat);

            StaticTranslate <string> .ReplaceDefault(PushString, GetString);

            StaticTranslate <DateTime> .ReplaceDefault(PushDateTime, GetDateTime);

            StaticTranslate <ArrayBuffer> .ReplaceDefault(PushArrayBuffer, GetArrayBuffer);
        }
        public TResult Func <T1, T2, T3, T4, TResult>(T1 p1, T2 p2, T3 p3, T4 p4)
        {
#if THREAD_SAFE
            lock (jsEnv) {
#endif
            jsEnv.CheckLiveness();
            StaticTranslate <T1> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p1);

            StaticTranslate <T2> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p2);

            StaticTranslate <T3> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p3);

            StaticTranslate <T4> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p4);

            IntPtr resultInfo = PuertsDLL.InvokeJSFunction(nativeJsFuncPtr, true);
            if (resultInfo == IntPtr.Zero)
            {
                string exceptionInfo = PuertsDLL.GetFunctionLastExceptionInfo(nativeJsFuncPtr);
                throw new Exception(exceptionInfo);
            }
            TResult result = StaticTranslate <TResult> .Get(jsEnv.Idx, isolate, NativeValueApi.GetValueFromResult, resultInfo, false);

            PuertsDLL.ResetResult(resultInfo);
            return(result);

#if THREAD_SAFE
        }
#endif
        }
Exemple #3
0
        /**
         * execute the module and get the result
         * when exportee is null, get the module namespace
         * when exportee is not null, get the specified member of the module namespace
         *
         * example: JsEnv.ExecuteModule("main.mjs")
         */
        public T ExecuteModule <T>(string filename, string exportee = "")
        {
            if (exportee == "" && typeof(T) != typeof(JSObject))
            {
                throw new Exception("T must be Puerts.JSObject when getting the module namespace");
            }
            if (loader.FileExists(filename))
            {
#if THREAD_SAFE
                lock (this) {
#endif
                IntPtr resultInfo = PuertsDLL.ExecuteModule(isolate, filename, exportee);
                if (resultInfo == IntPtr.Zero)
                {
                    string exceptionInfo = PuertsDLL.GetLastExceptionInfo(isolate);
                    throw new Exception(exceptionInfo);
                }
                T result = StaticTranslate <T> .Get(Idx, isolate, NativeValueApi.GetValueFromResult, resultInfo, false);

                PuertsDLL.ResetResult(resultInfo);

                return(result);

#if THREAD_SAFE
            }
#endif
            }
            else
            {
                throw new InvalidProgramException("can not find " + filename);
            }
        }
Exemple #4
0
 public T Get <T>(bool isByRef)
 {
     if (obj != null)
     {
         return((T)obj);
     }
     return(StaticTranslate <T> .Get(jsEnvIdx, isolate, NativeValueApi.GetValueFromArgument, value, isByRef));
 }
Exemple #5
0
        public T[] GetParams <T>(IntPtr info, int start, int end)
        {
            T[] result = new T[end - start];

            for (int i = start; i < end; i++)
            {
                var val = PuertsDLL.GetArgumentValue(info, i);
                result[i - start] = StaticTranslate <T> .Get(jsEnvIdx, isolate, NativeValueApi.GetValueFromArgument, val, false);
            }

            return(result);
        }
Exemple #6
0
        public void Action <T1>(T1 p1)
        {
            StaticTranslate <T1> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p1);

            IntPtr resultInfo = PuertsDLL.InvokeJSFunction(nativeJsFuncPtr, false);

            if (resultInfo == IntPtr.Zero)
            {
                string exceptionInfo = PuertsDLL.GetFunctionLastExceptionInfo(nativeJsFuncPtr);
                throw new Exception(exceptionInfo);
            }
        }
Exemple #7
0
        public TResult Func <TResult>()
        {
            IntPtr resultInfo = PuertsDLL.InvokeJSFunction(nativeJsFuncPtr, true);

            if (resultInfo == IntPtr.Zero)
            {
                string exceptionInfo = PuertsDLL.GetFunctionLastExceptionInfo(nativeJsFuncPtr);
                throw new Exception(exceptionInfo);
            }
            TResult result = StaticTranslate <TResult> .Get(jsEnv.Idx, isolate, NativeValueApi.GetValueFromResult, resultInfo, false);

            PuertsDLL.ResetResult(resultInfo);
            return(result);
        }
Exemple #8
0
        public TResult Eval <TResult>(string chunk, string chunkName = "chunk")
        {
            IntPtr resultInfo = PuertsDLL.Eval(isolate, chunk, chunkName);

            if (resultInfo == IntPtr.Zero)
            {
                string exceptionInfo = PuertsDLL.GetLastExceptionInfo(isolate);
                throw new Exception(exceptionInfo);
            }
            TResult result = StaticTranslate <TResult> .Get(Idx, isolate, NativeValueApi.GetValueFromResult, resultInfo, false);

            PuertsDLL.ResetResult(resultInfo);
            return(result);
        }
 public static void Set <T>(int jsEnvIdx, IntPtr isolate, IntPtr info, T result, bool isNullValue = false)
 {
     // Generic function issue.
     // If pass the null value to the generic function, the parameter is not null and it is a default value.
     // Explicitly pass <null or not> together. (isNullValue)
     if (isNullValue)
     {
         // TODO : need to make a code in a prettier way.
         Set(jsEnvIdx, isolate, info, Puerts.JsValueType.NullOrUndefined);
     }
     else
     {
         StaticTranslate <T> .Set(jsEnvIdx, isolate, NativeValueApi.SetValueToResult, info, result);
     }
 }
Exemple #10
0
 void GetNestedTypes(IntPtr isolate, IntPtr info, IntPtr self, int paramLen)
 {
     try
     {
         Type type = GetTypeFromJs(isolate, info, self, paramLen);
         if (type != null)
         {
             StaticTranslate <Type[]> .Set(Index, isolate, NativeValueApi.SetValueToResult, info, type.GetNestedTypes());
         }
     }
     catch (Exception e)
     {
         PuertsDLL.ThrowException(isolate, "GetNestedType throw c# exception:" + e.Message + ",stack:" + e.StackTrace);
     }
 }
Exemple #11
0
        public TResult Func <T1, TResult>(T1 p1)
        {
            jsEnv.CheckLiveness();
            StaticTranslate <T1> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p1);

            IntPtr resultInfo = PuertsDLL.InvokeJSFunction(nativeJsFuncPtr, true);

            if (resultInfo == IntPtr.Zero)
            {
                string exceptionInfo = PuertsDLL.GetFunctionLastExceptionInfo(nativeJsFuncPtr);
                throw new Exception(exceptionInfo);
            }
            TResult result = StaticTranslate <TResult> .Get(jsEnv.Idx, isolate, NativeValueApi.GetValueFromResult, resultInfo, false);

            PuertsDLL.ResetResult(resultInfo);
            return(result);
        }
        public void Action <T1>(T1 p1)
        {
#if THREAD_SAFE
            lock (jsEnv) {
#endif
            jsEnv.CheckLiveness();
            StaticTranslate <T1> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p1);

            IntPtr resultInfo = PuertsDLL.InvokeJSFunction(nativeJsFuncPtr, false);
            if (resultInfo == IntPtr.Zero)
            {
                string exceptionInfo = PuertsDLL.GetFunctionLastExceptionInfo(nativeJsFuncPtr);
                throw new Exception(exceptionInfo);
            }
#if THREAD_SAFE
        }
#endif
        }
Exemple #13
0
        public void Action <T1, T2, T3, T4>(T1 p1, T2 p2, T3 p3, T4 p4)
        {
            jsEnv.CheckLiveness();
            StaticTranslate <T1> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p1);

            StaticTranslate <T2> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p2);

            StaticTranslate <T3> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p3);

            StaticTranslate <T4> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p4);

            IntPtr resultInfo = PuertsDLL.InvokeJSFunction(nativeJsFuncPtr, false);

            if (resultInfo == IntPtr.Zero)
            {
                string exceptionInfo = PuertsDLL.GetFunctionLastExceptionInfo(nativeJsFuncPtr);
                throw new Exception(exceptionInfo);
            }
        }
Exemple #14
0
        Type GetTypeFromJs(IntPtr isolate, IntPtr info, IntPtr self, int paramLen)
        {
            Type type = null;
            var value = PuertsDLL.GetArgumentValue(info, 0);

            if (PuertsDLL.GetJsValueType(isolate, value, false) == JsValueType.String)
            {
                string classFullName = PuertsDLL.GetStringFromValue(isolate, value, false);
                var maybeType        = TypeRegister.GetType(classFullName);
                if (paramLen == 1)
                {
                    type = maybeType;
                }
                else if (maybeType != null &&
                         maybeType.IsGenericTypeDefinition &&
                         maybeType.GetGenericArguments().Length == (paramLen - 1)) //泛型
                {
                    var genericArguments = new Type[paramLen - 1];
                    for (int i = 1; i < paramLen; i++)
                    {
                        value = PuertsDLL.GetArgumentValue(info, i);
                        if (PuertsDLL.GetJsValueType(isolate, value, false) != JsValueType.Function)
                        {
                            return(null);
                        }
                        var argTypeId = PuertsDLL.GetTypeIdFromValue(isolate, value, false);
                        if (argTypeId == -1)
                        {
                            return(null);
                        }
                        genericArguments[i - 1] = TypeRegister.GetType(argTypeId);
                    }
                    type = maybeType.MakeGenericType(genericArguments);
                }
            }
            else if (PuertsDLL.GetJsValueType(isolate, value, false) == JsValueType.NativeObject)
            {
                type = StaticTranslate <Type> .Get(Index, isolate, NativeValueApi.GetValueFromArgument, value, false);
            }

            return(type);
        }
        public void Action <T1, T2, T3>(T1 p1, T2 p2, T3 p3)
        {
#if THREAD_SAFE
            lock (jsEnv) {
#endif
            jsEnv.CheckLiveness();
            IntPtr resultInfo = InvokeJSFunction(
                jsEnv, nativeJsFuncPtr, 3, false,
                (IntPtr isolate, int envIdx, IntPtr nativeJsFuncPtr) => {
                StaticTranslate <T1> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p1);
                StaticTranslate <T2> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p2);
                StaticTranslate <T3> .Set(jsEnv.Idx, isolate, NativeValueApi.SetValueToArgument, nativeJsFuncPtr, p3);
            }
                );
            if (resultInfo == IntPtr.Zero)
            {
                string exceptionInfo = PuertsDLL.GetFunctionLastExceptionInfo(nativeJsFuncPtr);
                throw new Exception(exceptionInfo);
            }
#if THREAD_SAFE
        }
#endif
        }
        public TResult Func <TResult>()
        {
#if THREAD_SAFE
            lock (jsEnv) {
#endif
            jsEnv.CheckLiveness();
            IntPtr resultInfo = InvokeJSFunction(
                jsEnv, nativeJsFuncPtr, 0, true,
                (IntPtr isolate, int envIdx, IntPtr nativeJsFuncPtr) => {}
                );
            if (resultInfo == IntPtr.Zero)
            {
                string exceptionInfo = PuertsDLL.GetFunctionLastExceptionInfo(nativeJsFuncPtr);
                throw new Exception(exceptionInfo);
            }
            TResult result = StaticTranslate <TResult> .Get(jsEnv.Idx, isolate, NativeValueApi.GetValueFromResult, resultInfo, false);

            PuertsDLL.ResetResult(resultInfo);
            return(result);

#if THREAD_SAFE
        }
#endif
        }
Exemple #17
0
 public static void Set <T>(int jsEnvIdx, IntPtr isolate, IntPtr info, T result)
 {
     StaticTranslate <T> .Set(jsEnvIdx, isolate, NativeValueApi.SetValueToResult, info, result);
 }
Exemple #18
0
 public void SetByRefValue <T>(T val)
 {
     StaticTranslate <T> .Set(jsEnvIdx, isolate, NativeValueApi.SetValueToByRefArgument, value, val);
 }