protected JsValue(IJavaScriptEngine engine, BaristaContext context, JavaScriptValueSafeHandle valueHandle) { m_javaScriptEngine = engine ?? throw new ArgumentNullException(nameof(engine)); m_context = context ?? throw new ArgumentNullException(nameof(context)); m_javaScriptReference = valueHandle ?? throw new ArgumentNullException(nameof(valueHandle)); //Let's just make sure that we're the correct type. var reportedType = engine.JsGetValueType(valueHandle); //if (reportedType != Type) // throw new InvalidOperationException($"The underlying type ({reportedType}) does not match the type that is being created ({Type}). Ensure that correct value is being created."); //Set the event that will be called prior to the engine collecting the value. if ((this is JsNumber) == false) { //Set the event that will be called prior to the engine collecting the value. JavaScriptObjectBeforeCollectCallback beforeCollectCallback = (IntPtr handle, IntPtr callbackState) => { try { OnBeforeCollect(handle, callbackState); } catch { //Do Nothing. } }; m_beforeCollectCallbackDelegateHandle = GCHandle.Alloc(beforeCollectCallback); //An exception thrown here is usually due to trying to associate a callback to a JsNumber. Engine.JsSetObjectBeforeCollectCallback(valueHandle, IntPtr.Zero, beforeCollectCallback); } }
public BaristaModuleRecord(string name, JavaScriptValueSafeHandle moduleSpecifier, BaristaModuleRecord parentModule, IJavaScriptEngine engine, BaristaContext context, IBaristaModuleRecordFactory moduleRecordFactory, IBaristaModuleLoader moduleLoader, JavaScriptModuleRecord moduleRecord) : base(engine, moduleRecord) { m_name = name ?? throw new ArgumentNullException(nameof(name)); m_moduleSpecifier = moduleSpecifier ?? throw new ArgumentNullException(nameof(moduleSpecifier)); m_parentModule = parentModule; m_context = context ?? throw new ArgumentNullException(nameof(context)); m_moduleRecordFactory = moduleRecordFactory ?? throw new ArgumentNullException(nameof(moduleRecordFactory)); //Module loader is not required, but if not specified, imports will fail. m_moduleLoader = moduleLoader; //Associate functions that will handle module loading if (m_parentModule == null) { //Set the fetch module callback for the module. m_fetchImportedModuleCallbackHandle = InitFetchImportedModuleCallback(Handle); //Set the notify callback for the module. m_notifyCallbackHandle = InitNotifyModuleReadyCallback(Handle); } //Set the event that will be called prior to the engine collecting the context. JavaScriptObjectBeforeCollectCallback beforeCollectCallback = (IntPtr handle, IntPtr callbackState) => { OnBeforeCollect(handle, callbackState); }; m_beforeCollectCallbackDelegateHandle = GCHandle.Alloc(beforeCollectCallback); Engine.JsSetObjectBeforeCollectCallback(moduleRecord, IntPtr.Zero, beforeCollectCallback); }
/// <summary> /// Creates a new instance of a Barista Context. /// </summary> /// <param name="engine"></param> /// <param name="contextHandle"></param> public BaristaContext(IJavaScriptEngine engine, IBaristaValueFactoryBuilder valueFactoryBuilder, IBaristaConversionStrategy conversionStrategy, IBaristaModuleRecordFactory moduleRecordFactory, IPromiseTaskQueue taskQueue, JavaScriptContextSafeHandle contextHandle) : base(engine, contextHandle) { if (valueFactoryBuilder == null) { throw new ArgumentNullException(nameof(valueFactoryBuilder)); } m_taskFactory = new TaskFactory( CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskContinuationOptions.None, new CurrentThreadTaskScheduler()); m_conversionStrategy = conversionStrategy ?? throw new ArgumentNullException(nameof(conversionStrategy)); m_valueFactory = valueFactoryBuilder.CreateValueFactory(this); m_undefinedValue = new Lazy <JsUndefined>(() => m_valueFactory.Undefined); m_nullValue = new Lazy <JsNull>(() => m_valueFactory.Null); m_trueValue = new Lazy <JsBoolean>(() => m_valueFactory.True); m_falseValue = new Lazy <JsBoolean>(() => m_valueFactory.False); m_globalValue = new Lazy <JsObject>(() => m_valueFactory.GlobalObject); m_jsonValue = new Lazy <JsJSON>(() => { var global = m_globalValue.Value; return(global.GetProperty <JsJSON>("JSON")); }); m_objectValue = new Lazy <JsObjectConstructor>(() => { var global = m_globalValue.Value; return(global.GetProperty <JsObjectConstructor>("Object")); }); m_promiseValue = new Lazy <JsPromiseConstructor>(() => { var global = m_globalValue.Value; return(global.GetProperty <JsPromiseConstructor>("Promise")); }); m_symbolValue = new Lazy <JsSymbolConstructor>(() => { var global = m_globalValue.Value; return(global.GetProperty <JsSymbolConstructor>("Symbol")); }); m_promiseTaskQueue = taskQueue; m_moduleRecordFactory = moduleRecordFactory ?? throw new ArgumentNullException(nameof(moduleRecordFactory)); //Set the event that will be called prior to the engine collecting the context. JavaScriptObjectBeforeCollectCallback beforeCollectCallback = (IntPtr handle, IntPtr callbackState) => { OnBeforeCollect(handle, callbackState); }; m_beforeCollectCallbackDelegateHandle = GCHandle.Alloc(beforeCollectCallback); Engine.JsSetObjectBeforeCollectCallback(contextHandle, IntPtr.Zero, beforeCollectCallback); }
internal static extern JavaScriptErrorCode JsSetObjectBeforeCollectCallback(JavaScriptValue reference, IntPtr callbackState, JavaScriptObjectBeforeCollectCallback beforeCollectCallback);
public static void SetObjectBeforeCollectCallback(JavaScriptValue reference, IntPtr callbackState, JavaScriptObjectBeforeCollectCallback beforeCollectCallback) { Native.ThrowIfError(Native.JsSetObjectBeforeCollectCallback(reference, callbackState, beforeCollectCallback)); }
public FaceTracker() { Inst = this; faceDetector = FaceDetector.CreateAsync().AsTask().Result; this.jsObjectCallback = OnFrameCollection; }
public GCSyncService() { callback = new JavaScriptObjectBeforeCollectCallback(JsValueCollectCallback); }
/// <summary> /// Creates a new <see cref="JsBinder"/> instance. /// </summary> public JsBinder(JsContextScope scope) { _scope = scope; _objects = new JsObjectCache(scope); _jsGcCollect = JsGcCollect; }