private string ExecuteHandler( IntPtr commandHandlerHandle, string json, string[] other, out string newSharedState) { _reverseCommandHandlerException = null; _prelude.ScheduleTerminateExecution(); IntPtr resultJsonPtr; IntPtr result2JsonPtr; IntPtr memoryHandle; bool success = Js1.ExecuteCommandHandler( _script.GetHandle(), commandHandlerHandle, json, other, other != null ? other.Length : 0, out resultJsonPtr, out result2JsonPtr, out memoryHandle); var terminated = _prelude.CancelTerminateExecution(); if (!success) { CompiledScript.CheckResult(_script.GetHandle(), terminated, disposeScriptOnException: false); } string resultJson = Marshal.PtrToStringUni(resultJsonPtr); string result2Json = Marshal.PtrToStringUni(result2JsonPtr); Js1.FreeResult(memoryHandle); if (_reverseCommandHandlerException != null) { throw new ApplicationException( "An exception occurred while executing a reverse command handler. " + _reverseCommandHandlerException.Message, _reverseCommandHandlerException); } newSharedState = result2Json; return(resultJson); }
private CompiledScript CompileScript(string script, string fileName) { IntPtr prelude = Js1.CompilePrelude(script, fileName, _loadModuleDelegate, _logDelegate); CompiledScript.CheckResult(prelude, disposeScriptOnException: true); return(new CompiledScript(prelude, fileName)); }
private CompiledScript CompileScript(PreludeScript prelude, string script, string fileName) { IntPtr query = Js1.CompileQuery( prelude.GetHandle(), script, fileName, _commandHandlerRegisteredCallback, _reverseCommandHandlerDelegate); CompiledScript.CheckResult(query, disposeScriptOnException: true); return(new CompiledScript(query, fileName)); }
private CompiledScript CompileScript(string script, string fileName) { ScheduleTerminateExecution(); IntPtr prelude = Js1.CompilePrelude( script, fileName, _loadModuleDelegate, _enterCancellableRegion, _exitCancellableRegion, _logDelegate); CancelTerminateExecution(); CompiledScript.CheckResult(prelude, false, disposeScriptOnException: true); return(new CompiledScript(prelude, fileName)); }
private CompiledScript CompileScript(PreludeScript prelude, string script, string fileName) { prelude.ScheduleTerminateExecution(); IntPtr query = Js1.CompileQuery( prelude.GetHandle(), script, fileName, _commandHandlerRegisteredCallback, _reverseCommandHandlerDelegate); var terminated = prelude.CancelTerminateExecution(); CompiledScript.CheckResult(query, terminated, disposeScriptOnException: true); return(new CompiledScript(query, fileName)); }
private void Dispose(bool disposing) { if (_disposed) { return; } var scriptHandle = _script; _script = IntPtr.Zero; Js1.DisposeScript(scriptHandle); _disposed = true; }
private string ExecuteHandler(IntPtr commandHandlerHandle, string json, string[] other = null) { IntPtr resultJsonPtr; IntPtr resultHandle = Js1.ExecuteCommandHandler( _script.GetHandle(), commandHandlerHandle, json, other, other != null ? other.Length : 0, out resultJsonPtr); if (resultHandle == IntPtr.Zero) { CompiledScript.CheckResult(_script.GetHandle(), disposeScriptOnException: false); } string resultJson = Marshal.PtrToStringUni(resultJsonPtr); Js1.FreeResult(resultHandle); return(resultJson); }
private IntPtr GetModule(string moduleName) { try { var moduleSourceAndFileName = GetModuleSourceAndFileName(moduleName); var compiledModuleHandle = Js1.CompileModule( GetHandle(), moduleSourceAndFileName.Item1, moduleSourceAndFileName.Item2); CompiledScript.CheckResult(compiledModuleHandle, disposeScriptOnException: true); var compiledModule = new CompiledScript(compiledModuleHandle, moduleSourceAndFileName.Item2); _modules.Add(compiledModule); return(compiledModuleHandle); } catch (Exception) { //TODO: this is not a good way to report missing module and other exceptions back to caller return(IntPtr.Zero); } }
private CompiledScript CompileScript(string script, string fileName) { try { var attempts = 3; var prelude = default(IntPtr); do { attempts--; try { ScheduleTerminateExecution(); prelude = Js1.CompilePrelude( script, fileName, _loadModuleDelegate, _enterCancellableRegion, _exitCancellableRegion, _logDelegate); CancelTerminateExecution(); CompiledScript.CheckResult(prelude, false, disposeScriptOnException: true); } catch (Js1Exception ex) { if (attempts > 0 && (ex.ErrorCode == -1 || ex.ErrorCode == -2)) { // timeouts Thread.Sleep(2000); } else { throw; } } } while (prelude == default(IntPtr)); return(new CompiledScript(prelude)); } catch (DllNotFoundException ex) { Log.Info("{0}\n{1}\n{2}", ex.ToString(), ex.Message, ex.StackTrace); throw new ApplicationException( "The projection subsystem failed to load a libjs1.so/js1.dll/... or one of its dependencies. The original error message is: " + ex.Message, ex); } }
private IntPtr GetModule(IntPtr prelude, string moduleName) { try { var moduleSourceAndFileName = GetModuleSourceAndFileName(moduleName); // NOTE: no need to schedule termination; modules are loaded only in context if (_cancelTokenOrStatus == NonScheduled) { throw new InvalidOperationException("Requires scheduled terminate execution"); } var compiledModuleHandle = Js1.CompileModule( prelude, moduleSourceAndFileName.Item1, moduleSourceAndFileName.Item2); CompiledScript.CheckResult(compiledModuleHandle, terminated: false, disposeScriptOnException: true); var compiledModule = new CompiledScript(compiledModuleHandle); _modules.Add(compiledModule); return(compiledModuleHandle); } catch (Exception ex) { Log.Error(ex, "Cannot load module '{module}'", moduleName); //TODO: this is not a good way to report missing module and other exceptions back to caller return(IntPtr.Zero); } }
public static void CheckResult(IntPtr scriptHandle, bool terminated, bool disposeScriptOnException) { if (terminated) { throw new Js1Exception( -2, "Failed to compile script. Script execution terminated. Timeout expired. (1)"); } if (scriptHandle == IntPtr.Zero) { throw new Js1Exception( -1, "Failed to compile script. Script execution terminated. Timeout expired. (2)"); } int? errorCode = null; string errorMessage = null; _reportErrorCallback = // NOTE: this local delegate must go to a field to keep references while ReportErrors is being executed (code, message) => { //NOTE: do not throw exceptions directly in this handler // let the CPP code clean up errorCode = code; errorMessage = message; }; Js1.ReportErrors(scriptHandle, _reportErrorCallback); if (errorCode != null) { if (disposeScriptOnException) { Js1.DisposeScript(scriptHandle); } if (errorCode == 2) { throw new Js1Exception( errorCode.Value, "Failed to compile script. Script execution terminated. Timeout expired. (3)"); } throw new Js1Exception(errorCode.Value, errorMessage); } }
private string ExecuteHandler(IntPtr commandHandlerHandle, string json, string[] other = null) { _reverseCommandHandlerException = null; IntPtr resultJsonPtr; IntPtr resultHandle = Js1.ExecuteCommandHandler( _script.GetHandle(), commandHandlerHandle, json, other, other != null ? other.Length : 0, out resultJsonPtr); if (resultHandle == IntPtr.Zero) { CompiledScript.CheckResult(_script.GetHandle(), disposeScriptOnException: false); } //TODO: do we need to free resulktJsonPtr in case of exception thrown a line above string resultJson = Marshal.PtrToStringUni(resultJsonPtr); Js1.FreeResult(resultHandle); if (_reverseCommandHandlerException != null) { throw new ApplicationException( "An exception occurred while executing a reverse command handler. " + _reverseCommandHandlerException.Message, _reverseCommandHandlerException); } return(resultJson); }
public static void CheckResult(IntPtr scriptHandle, bool disposeScriptOnException) { int? errorCode = null; string errorMessage = null; _reportErrorCallback = // NOTE: this local delegate must go to a field to keep references while ReportErrors is being executed (code, message) => { //NOTE: do not throw exceptions directly in this handler // let the CPP code clean up errorCode = code; errorMessage = message; }; Js1.ReportErrors(scriptHandle, _reportErrorCallback); if (errorCode != null) { if (disposeScriptOnException) { Js1.DisposeScript(scriptHandle); } throw new Js1Exception(errorCode.Value, errorMessage); } }
private void CancelExecution(IntPtr scriptHandle) { Js1.TerminateExecution(scriptHandle); }