Пример #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);
        }
Пример #2
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);
        }