Example #1
0
        public static void ReflectBind(ScriptRuntime runtime)
        {
            var logger = runtime.GetLogger();

            try
            {
                var UnityHelper = Binding.Values.FindType("QuickJS.Unity.UnityHelper");
                if (UnityHelper != null)
                {
                    var IsReflectBindingSupported = UnityHelper.GetMethod("IsReflectBindingSupported");
                    if (IsReflectBindingSupported != null && (bool)IsReflectBindingSupported.Invoke(null, null))
                    {
                        var bindAll = UnityHelper.GetMethod("InvokeReflectBinding");
                        if (bindAll != null)
                        {
                            bindAll.Invoke(null, new object[] { runtime });
                            return;
                        }
                    }
                }
            }
            catch (Exception)
            {
            }

            if (logger != null)
            {
                logger.Write(Utils.LogLevel.Error, "failed to get method: UnityHelper.InvokeReflectBinding");
            }
        }
Example #2
0
        private static void _OnTaskCompleted(ScriptRuntime runtime, JSAction action)
        {
            var context     = runtime.GetMainContext();
            var logger      = runtime.GetLogger();
            var args        = (JSTaskCompletionArgs)action.args;
            var task        = args.task;
            var safeRelease = args.safeRelease;

            if (!safeRelease.isValid)
            {
                logger?.Write(LogLevel.Error, "pormise func has already been released");
                return;
            }

            object result   = null;
            var    taskType = task.GetType();

            if (taskType.IsGenericType && taskType.GetGenericTypeDefinition() == typeof(Task <>))
            {
                try
                {
                    result = taskType.GetProperty("Result").GetValue(task, null);
                }
                catch (Exception exception)
                {
                    logger?.WriteException(exception);
                }
            }

            var ctx     = (JSContext)context;
            var backVal = Binding.Values.js_push_var(ctx, result);

            if (backVal.IsException())
            {
                ctx.print_exception();
                safeRelease.Release();
                return;
            }

            var argv = new[] { backVal };
            var rval = JSApi.JS_Call(ctx, safeRelease[0], JSApi.JS_UNDEFINED, 1, argv);

            JSApi.JS_FreeValue(ctx, backVal);
            if (rval.IsException())
            {
                ctx.print_exception();
                safeRelease.Release();
                return;
            }

            JSApi.JS_FreeValue(ctx, rval);
            safeRelease.Release();

            context.GetRuntime().ExecutePendingJob();
        }
Example #3
0
        public static void StaticBind(ScriptRuntime runtime)
        {
            var logger  = runtime.GetLogger();
            var bindAll = typeof(Binding.Values).GetMethod("BindAll", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);

            if (bindAll == null)
            {
                if (logger != null)
                {
                    logger.Write(Utils.LogLevel.Error, "generate binding code before run, or turn on ReflectBind");
                }
                return;
            }

            var codeGenVersionField = typeof(Binding.Values).GetField("CodeGenVersion");

            if (codeGenVersionField == null || !codeGenVersionField.IsStatic || !codeGenVersionField.IsLiteral || codeGenVersionField.FieldType != typeof(uint))
            {
                if (logger != null)
                {
                    logger.Write(Utils.LogLevel.Error, "binding code version mismatch");
                }
                return;
            }

            var codeGenVersion = (uint)codeGenVersionField.GetValue(null);

            if (codeGenVersion != ScriptEngine.VERSION)
            {
                if (logger != null)
                {
                    logger.Write(Utils.LogLevel.Warn, "CodeGenVersion: {0} != {1}", codeGenVersion, ScriptEngine.VERSION);
                }
            }

            bindAll.Invoke(null, new object[] { runtime });
        }
Example #4
0
        public void Bind(ScriptRuntime runtime)
        {
#if JSB_UNITYLESS
            var bm = new Binding.BindingManager(new Binding.Prefs(), new Binding.ReflectBindingCallback(runtime));
            bm.Collect();
            bm.Generate(Binding.TypeBindingFlags.None);
            bm.Report();
#else
            var logger = runtime.GetLogger();
            try
            {
                var UnityHelper = Binding.Values.FindType("QuickJS.Unity.UnityHelper");
                if (UnityHelper != null)
                {
                    var IsReflectBindingSupported = UnityHelper.GetMethod("IsReflectBindingSupported");
                    if (IsReflectBindingSupported != null && (bool)IsReflectBindingSupported.Invoke(null, null))
                    {
                        var bindAll = UnityHelper.GetMethod("InvokeReflectBinding");
                        if (bindAll != null)
                        {
                            bindAll.Invoke(null, new object[] { runtime });
                            return;
                        }
                    }
                }
            }
            catch (Exception)
            {
            }

            if (logger != null)
            {
                logger.Write(LogLevel.Error, "failed to get method: UnityHelper.InvokeReflectBinding");
            }
#endif
        }