protected override void Dispose(bool disposing) { if (disposing && !IsDisposed) { BaristaExecutionScope scope = null; if (!HasCurrentScope) { scope = Scope(); } try { m_valueFactory.Dispose(); m_moduleRecordFactory.Dispose(); } finally { if (scope != null) { scope.Dispose(); } } } if (!IsDisposed) { //Unset the before collect callback. Engine.JsSetObjectBeforeCollectCallback(Handle, IntPtr.Zero, null); m_beforeCollectCallbackDelegateHandle.Free(); } base.Dispose(disposing); }
/// <summary> /// Converts the value to a number and returns the integer representation. /// </summary> /// <returns></returns> public virtual int ToInt32() { if (IsDisposed) { throw new ObjectDisposedException(nameof(JsValue)); } //Create a scope if we're not currently in one. BaristaExecutionScope scope = null; if (!Context.HasCurrentScope) { scope = Context.Scope(); } try { using (var numberValueHandle = Engine.JsConvertValueToNumber(Handle)) { return(Engine.JsNumberToInt(numberValueHandle)); } } finally { if (scope != null) { scope.Dispose(); } } }
/// <summary> /// Converts the value to a string (using standard JavaScript semantics) and returns the result /// </summary> /// <returns></returns> public override string ToString() { if (IsDisposed) { throw new ObjectDisposedException(nameof(JsValue)); } //Create a scope if we're not currently in one. BaristaExecutionScope scope = null; if (!Context.HasCurrentScope) { scope = Context.Scope(); } try { using (var stringValueHandle = Engine.JsConvertValueToString(Handle)) { //Get the size var size = Engine.JsCopyString(stringValueHandle, null, 0); if ((int)size > int.MaxValue) { throw new OutOfMemoryException("Exceeded maximum string length."); } byte[] result = new byte[(int)size]; var written = Engine.JsCopyString(stringValueHandle, result, (ulong)result.Length); return(Encoding.UTF8.GetString(result, 0, result.Length)); } } finally { if (scope != null) { scope.Dispose(); } } }
/// <summary> /// Evaluates the specified script as a module, the default export will be the returned value. /// </summary> /// <param name="script">Script to evaluate.</param> /// <returns></returns> public JsValue EvaluateModule(string script, IBaristaModuleLoader moduleLoader = null) { //Create a scope if we're not currently in one. BaristaExecutionScope scope = null; if (!HasCurrentScope) { scope = Scope(); } try { var globalName = EvaluateModuleInternal(script, moduleLoader); return(GlobalObject.GetProperty(globalName)); } finally { if (scope != null) { scope.Dispose(); } } }
/// <summary> /// Evaluates the specified script as a module, the default export will be the returned value. /// </summary> /// <param name="script">Script to evaluate.</param> /// <returns></returns> public T EvaluateModule <T>(string script, IBaristaModuleLoader moduleLoader = null) where T : JsValue { //Create a scope if we're not currently in one. BaristaExecutionScope scope = null; if (!HasCurrentScope) { scope = Scope(); } try { var moduleNamespace = EvaluateModuleInternal(script, moduleLoader); return(moduleNamespace.GetProperty <T>("default")); } finally { if (scope != null) { scope.Dispose(); } } }