using Jint; using Jint.Native; using Jint.Native.Object; using Jint.Runtime; using durable_pi_omg; var projectRoot = Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", "..")); var engine = new Engine(options => { options.EnableModules(new NodeModuleLoader(projectRoot)); }); using var host = new JsHostRuntime(engine); var envObj = new JsObject(engine); foreach (System.Collections.DictionaryEntry entry in Environment.GetEnvironmentVariables()) { if (entry.Key is string key && entry.Value is string value) { envObj.Set(key, value); } } if (envObj.Get("RADIUS_API_KEY").IsUndefined() && Environment.GetEnvironmentVariable("DOTNET_RADIUS_API_KEY") is { Length: > 0 } dotnetKey) { envObj.Set("RADIUS_API_KEY", dotnetKey); } var processObj = new JsObject(engine); processObj.Set("env", envObj); engine.SetValue("process", processObj); engine.SetValue("__dotnet_news", () => { return "Awesome news. C# can embed PI-Durable via Jint. Woop Woop hooray"; }); try { var ns = await host.ImportModuleAsync("./hello-pi-durable.mjs"); Console.WriteLine("Loaded ./hello-pi-durable.mjs successfully!"); } catch (PromiseRejectedException ex) { PrintJsError(ex.RejectedValue, ex.Message); Environment.Exit(1); } catch (JavaScriptException ex) { PrintJsError(ex.Error, ex.Message, ex.JavaScriptStackTrace); Environment.Exit(1); } static void PrintJsError(JsValue error, string fallbackMessage, string? stackTrace = null) { Console.Error.WriteLine($"JS Error: {fallbackMessage}"); if (!string.IsNullOrEmpty(stackTrace)) { Console.Error.WriteLine(stackTrace); } else if (error is ObjectInstance errObj) { var stack = errObj.Get("stack"); if (!stack.IsUndefined() && !stack.IsNull()) { Console.Error.WriteLine(stack); } } var cur = error; int depth = 0; while (cur is ObjectInstance obj && depth++ < 10) { var cause = obj.Get("cause"); if (cause.IsUndefined() || cause.IsNull()) break; Console.Error.WriteLine($"Caused by [{depth}]: {cause}"); if (cause is ObjectInstance causeObj) { var stack = causeObj.Get("stack"); if (!stack.IsUndefined() && !stack.IsNull()) { Console.Error.WriteLine(stack); } } cur = cause; } }