Esempio n. 1
0
        public void Initialize(IFileSystem fileSystem, IFileResolver resolver, IScriptRuntimeListener listener, IScriptLogger logger, IO.IByteBufferAllocator byteBufferAllocator)
        {
            if (logger == null)
            {
                throw new NullReferenceException(nameof(logger));
            }

            if (fileSystem == null)
            {
                throw new NullReferenceException(nameof(fileSystem));
            }

            MethodInfo bindAll = null;

            if (!isWorker)
            {
                if (listener == null)
                {
                    throw new NullReferenceException(nameof(listener));
                }

                bindAll = typeof(Values).GetMethod("BindAll", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                if (bindAll == null)
                {
                    throw new Exception("Generate binding code before run");
                }
            }
            _listener            = listener;
            _fileResolver        = resolver;
            _byteBufferAllocator = byteBufferAllocator;
            _autorelease         = new Utils.AutoReleasePool();
            _fileSystem          = fileSystem;
            _logger       = logger;
            _timerManager = new TimerManager(_logger);
            _typeDB       = new TypeDB(this, _mainContext);

            var register = new TypeRegister(this, _mainContext);

            register.RegisterType(typeof(ScriptBridge));
            // await Task.Run(() => runner.OnBind(this, register));
            if (bindAll != null)
            {
                bindAll.Invoke(null, new object[] { register });
            }
            listener.OnBind(this, register);
            if (!_isWorker)
            {
                JSWorker.Bind(register);
            }
            TimerManager.Bind(register);
            ScriptContext.Bind(register);
            register.Finish();
            listener.OnComplete(this);
        }
Esempio n. 2
0
        // this method will be marked as private in the future
        public void Initialize(IFileSystem fileSystem, IPathResolver resolver, IScriptRuntimeListener listener, IAsyncManager asyncManager, IScriptLogger logger, IO.IByteBufferAllocator byteBufferAllocator, bool useReflectBind = true)
        {
            if (fileSystem == null)
            {
                throw new NullReferenceException(nameof(fileSystem));
            }

            MethodInfo bindAll = null;

            if (!isWorker)
            {
                if (listener == null)
                {
                    throw new NullReferenceException(nameof(listener));
                }

#if UNITY_EDITOR
                bindAll        = (useReflectBind ? GetReflectBind(logger) : null) ?? GetStaticBind(logger);
                _isReflectBind = bindAll.Name == "InvokeReflectBinding";
                logger?.Write(LogLevel.Info, _isReflectBind ? "Running in ReflectBind mode" : "Running in StaticBind mode");
#else
                bindAll = GetStaticBind(logger);
#endif
            }

            asyncManager.Initialize(_mainThreadId);

            _isValid   = true;
            _isRunning = true;
            _logger    = logger;
            // _rwlock = new ReaderWriterLockSlim();
            _rt = JSApi.JS_NewRuntime();
            JSApi.JS_SetHostPromiseRejectionTracker(_rt, JSApi.PromiseRejectionTracker, IntPtr.Zero);
#if UNITY_EDITOR
            JSApi.JS_SetInterruptHandler(_rt, _InterruptHandler, IntPtr.Zero);
#else
            if (isWorker)
            {
                JSApi.JS_SetInterruptHandler(_rt, _InterruptHandler, IntPtr.Zero);
            }
#endif
            JSApi.JS_SetRuntimeOpaque(_rt, (IntPtr)_runtimeId);
            JSApi.JS_SetModuleLoaderFunc(_rt, module_normalize, module_loader, IntPtr.Zero);
            CreateContext();
            JSApi.JS_NewClass(_rt, JSApi.JSB_GetBridgeClassID(), "CSharpClass", JSApi.class_finalizer);

            _listener            = listener;
            _pathResolver        = resolver;
            _asyncManager        = asyncManager;
            _byteBufferAllocator = byteBufferAllocator;
            _autorelease         = new Utils.AutoReleasePool();
            _fileSystem          = fileSystem;
            _objectCache         = new ObjectCache(_logger);
            _timerManager        = new TimerManager(_logger);
            _typeDB = new TypeDB(this, _mainContext);
            _typeDB.AddType(typeof(Unity.JSBehaviour), JSApi.JS_UNDEFINED);
#if UNITY_EDITOR
            _typeDB.AddType(typeof(Unity.JSEditorWindow), JSApi.JS_UNDEFINED);
            _typeDB.AddType(typeof(Unity.JSBehaviourInspector), JSApi.JS_UNDEFINED);
#endif
            listener.OnCreate(this);

            // await Task.Run(() => runner.OnBind(this, register));
            if (bindAll != null)
            {
                bindAll.Invoke(null, new object[] { this });
            }

            var register = new TypeRegister(_mainContext);
            listener.OnBind(this, register);
            if (!_isWorker)
            {
                JSWorker.Bind(register);
            }
            TimerManager.Bind(register);
            register.Finish();

            AddStaticModule("jsb", ScriptContext.Bind);
            FindModuleResolver <StaticModuleResolver>().Warmup(_mainContext);

            listener.OnComplete(this);
        }
Esempio n. 3
0
        // this method will be marked as private in the future
        public void Initialize(IFileSystem fileSystem, IPathResolver resolver, IAsyncManager asyncManager, IScriptLogger logger, IO.IByteBufferAllocator byteBufferAllocator, IBinder binder)
        {
            if (fileSystem == null)
            {
                throw new NullReferenceException(nameof(fileSystem));
            }

            asyncManager.Initialize(_mainThreadId);

            _isValid   = true;
            _isRunning = true;
            _logger    = logger;
            // _rwlock = new ReaderWriterLockSlim();
            _rt = JSApi.JS_NewRuntime();
            JSApi.JS_SetHostPromiseRejectionTracker(_rt, JSApi.PromiseRejectionTracker, IntPtr.Zero);
#if UNITY_EDITOR
            JSApi.JS_SetInterruptHandler(_rt, _InterruptHandler, IntPtr.Zero);
#else
            if (isWorker)
            {
                JSApi.JS_SetInterruptHandler(_rt, _InterruptHandler, IntPtr.Zero);
            }
#endif
            JSApi.JS_SetRuntimeOpaque(_rt, (IntPtr)_runtimeId);
            JSApi.JS_SetModuleLoaderFunc(_rt, module_normalize, module_loader, IntPtr.Zero);
            CreateContext();
            JSApi.JS_NewClass(_rt, JSApi.JSB_GetBridgeClassID(), "CSharpClass", JSApi.class_finalizer);

            _pathResolver        = resolver;
            _asyncManager        = asyncManager;
            _byteBufferAllocator = byteBufferAllocator;
            _autorelease         = new Utils.AutoReleasePool();
            _fileSystem          = fileSystem;
            _objectCache         = new ObjectCache(_logger);
            _timerManager        = new TimerManager(_logger);
            _typeDB = new TypeDB(this, _mainContext);
#if !JSB_UNITYLESS
            _typeDB.AddType(typeof(Unity.JSBehaviour), JSApi.JS_UNDEFINED);
#endif
#if UNITY_EDITOR
            _typeDB.AddType(Values.FindType("QuickJS.Unity.JSEditorWindow"), JSApi.JS_UNDEFINED);
            _typeDB.AddType(Values.FindType("QuickJS.Unity.JSBehaviourInspector"), JSApi.JS_UNDEFINED);
#endif

            // await Task.Run(() => runner.OnBind(this, register));
            try
            {
                binder?.Bind(this);
            }
            catch (Exception exception)
            {
                _logger?.WriteException(exception);
            }

            var register = new TypeRegister(_mainContext);
            if (!_isWorker)
            {
                JSWorker.Bind(register);
            }
            TimerManager.Bind(register);
            extraBinding?.Invoke(this, register);
            register.Finish();

            AddStaticModule("jsb", ScriptContext.Bind);
            FindModuleResolver <StaticModuleResolver>().Warmup(_mainContext);

            _isInitialized = true;
        }
Esempio n. 4
0
        public void Initialize(IFileSystem fileSystem, IPathResolver resolver, IScriptRuntimeListener listener, IAsyncManager asyncManager, IScriptLogger logger, IO.IByteBufferAllocator byteBufferAllocator)
        {
            if (fileSystem == null)
            {
                throw new NullReferenceException(nameof(fileSystem));
            }

            MethodInfo bindAll = null;

            if (!isWorker)
            {
                if (listener == null)
                {
                    throw new NullReferenceException(nameof(listener));
                }

                bindAll = typeof(Values).GetMethod("BindAll", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                if (bindAll == null)
                {
#if UNITY_EDITOR
                    var throwError = true;
                    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
                    {
                        var UnityHelper = assembly.GetType("QuickJS.Unity.UnityHelper");
                        if (UnityHelper != null)
                        {
                            var IsReflectBindingSupported = UnityHelper.GetMethod("IsReflectBindingSupported");
                            if (IsReflectBindingSupported != null)
                            {
                                if ((bool)IsReflectBindingSupported.Invoke(null, null))
                                {
                                    var InvokeReflectBinding = UnityHelper.GetMethod("InvokeReflectBinding");
                                    if (InvokeReflectBinding != null)
                                    {
                                        bindAll    = InvokeReflectBinding;
                                        throwError = false;
                                    }
                                }
                            }
                            break;
                        }
                    }

                    if (throwError)
                    {
                        throw new Exception("generate binding code before run");
                    }
#else
                    throw new Exception("generate binding code before run");
#endif
                }
                else
                {
                    var codeGenVersionField = typeof(Values).GetField("CodeGenVersion");
                    if (codeGenVersionField == null || !codeGenVersionField.IsStatic || !codeGenVersionField.IsLiteral || codeGenVersionField.FieldType != typeof(uint))
                    {
                        throw new Exception("binding code version mismatch");
                    }

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

            asyncManager.Initialize(_mainThreadId);

            _isValid   = true;
            _isRunning = true;
            // _rwlock = new ReaderWriterLockSlim();
            _rt = JSApi.JS_NewRuntime();
            JSApi.JS_SetHostPromiseRejectionTracker(_rt, JSApi.PromiseRejectionTracker, IntPtr.Zero);
#if UNITY_EDITOR
            JSApi.JS_SetInterruptHandler(_rt, _InterruptHandler, IntPtr.Zero);
#else
            if (isWorker)
            {
                JSApi.JS_SetInterruptHandler(_rt, _InterruptHandler, IntPtr.Zero);
            }
#endif
            JSApi.JS_SetRuntimeOpaque(_rt, (IntPtr)_runtimeId);
            JSApi.JS_SetModuleLoaderFunc(_rt, module_normalize, module_loader, IntPtr.Zero);
            CreateContext();
            JSApi.JS_NewClass(_rt, JSApi.JSB_GetBridgeClassID(), "CSharpClass", JSApi.class_finalizer);

            _listener            = listener;
            _pathResolver        = resolver;
            _asyncManager        = asyncManager;
            _byteBufferAllocator = byteBufferAllocator;
            _autorelease         = new Utils.AutoReleasePool();
            _fileSystem          = fileSystem;
            _logger       = logger;
            _objectCache  = new ObjectCache(_logger);
            _timerManager = new TimerManager(_logger);
            _typeDB       = new TypeDB(this, _mainContext);
            _typeDB.AddType(typeof(Unity.JSBehaviour), JSApi.JS_UNDEFINED);
#if UNITY_EDITOR
            _typeDB.AddType(typeof(Unity.JSEditorWindow), JSApi.JS_UNDEFINED);
            _typeDB.AddType(typeof(Unity.JSBehaviourInspector), JSApi.JS_UNDEFINED);
#endif
            listener.OnCreate(this);

            // await Task.Run(() => runner.OnBind(this, register));
            if (bindAll != null)
            {
                bindAll.Invoke(null, new object[] { this });
            }

            var register = new TypeRegister(_mainContext);
            listener.OnBind(this, register);
            if (!_isWorker)
            {
                JSWorker.Bind(register);
            }
            TimerManager.Bind(register);
            register.Finish();

            AddStaticModule("jsb", ScriptContext.Bind);
            FindModuleResolver <StaticModuleResolver>().Warmup(_mainContext);

            listener.OnComplete(this);
        }
Esempio n. 5
0
        public void Initialize(IFileSystem fileSystem, IPathResolver resolver, IScriptRuntimeListener listener, IScriptLogger logger, IO.IByteBufferAllocator byteBufferAllocator)
        {
            if (logger == null)
            {
                throw new NullReferenceException(nameof(logger));
            }

            if (fileSystem == null)
            {
                throw new NullReferenceException(nameof(fileSystem));
            }

            MethodInfo bindAll = null;

            if (!isWorker)
            {
                if (listener == null)
                {
                    throw new NullReferenceException(nameof(listener));
                }

                bindAll = typeof(Values).GetMethod("BindAll", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                if (bindAll == null)
                {
                    throw new Exception("generate binding code before run");
                }

                var codeGenVersionField = typeof(Values).GetField("CodeGenVersion");
                if (codeGenVersionField == null || !codeGenVersionField.IsStatic || !codeGenVersionField.IsLiteral || codeGenVersionField.FieldType != typeof(uint))
                {
                    throw new Exception("binding code version mismatch");
                }

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

            _isValid   = true;
            _isRunning = true;
            // _rwlock = new ReaderWriterLockSlim();
            _rt = JSApi.JS_NewRuntime();
            JSApi.JS_SetHostPromiseRejectionTracker(_rt, JSApi.PromiseRejectionTracker, IntPtr.Zero);
            JSApi.JS_SetRuntimeOpaque(_rt, (IntPtr)_runtimeId);
            JSApi.JS_SetModuleLoaderFunc(_rt, module_normalize, module_loader, IntPtr.Zero);
            CreateContext();
            JSApi.JS_NewClass(_rt, JSApi.JSB_GetBridgeClassID(), "CSharpClass", JSApi.class_finalizer);

            _listener            = listener;
            _pathResolver        = resolver;
            _byteBufferAllocator = byteBufferAllocator;
            _autorelease         = new Utils.AutoReleasePool();
            _fileSystem          = fileSystem;
            _logger       = logger;
            _objectCache  = new ObjectCache(_logger);
            _timerManager = new TimerManager(_logger);
            _typeDB       = new TypeDB(this, _mainContext);
            listener.OnCreate(this);

            var register = new TypeRegister(this, _mainContext);

            register.RegisterType(typeof(Unity.JSBehaviour));
#if UNITY_EDITOR
            register.RegisterType(typeof(Unity.JSEditorWindow));
#endif
            // await Task.Run(() => runner.OnBind(this, register));
            if (bindAll != null)
            {
                bindAll.Invoke(null, new object[] { register });
            }
            listener.OnBind(this, register);
            if (!_isWorker)
            {
                JSWorker.Bind(register);
            }
            TimerManager.Bind(register);
            ScriptContext.Bind(register);
            register.Finish();
            listener.OnComplete(this);
        }