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 }
/** * 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); } }
public T Get <T>(bool isByRef) { if (obj != null) { return((T)obj); } return(StaticTranslate <T> .Get(jsEnvIdx, isolate, NativeValueApi.GetValueFromArgument, value, isByRef)); }
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); }
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); }
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 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); }
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 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 }