private static JSValue _js_worker_ctor(JSContext ctx, JSValue new_target, int argc, JSValue[] argv, int magic) { if (argc < 1 || !argv[0].IsString()) { return(JSApi.JS_ThrowInternalError(ctx, "invalid parameter")); } var scriptPath = JSApi.GetString(ctx, argv[0]); var worker = new JSWorker(); var val = NewBridgeClassObject(ctx, new_target, worker, magic); try { if (val.IsObject()) { worker.Start(ctx, val, scriptPath); } } catch (Exception e) { JSApi.JS_FreeValue(ctx, val); return(JSApi.ThrowException(ctx, e)); } return(val); }
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); }
// 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); }
// 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; }
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); }
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); }