MCPcopy Create free account
hub / github.com/buke/quickjs-go / TestBridgeGetRuntimeFromJSReturnNil

Function TestBridgeGetRuntimeFromJSReturnNil

bridge_test.go:66–114  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

64}
65
66func TestBridgeGetRuntimeFromJSReturnNil(t *testing.T) {
67 // Test getRuntimeFromJS return nil in goInterruptHandler
68 t.Run("GetRuntimeFromJSReturnNil", func(t *testing.T) {
69 rt := NewRuntime()
70 ctx := rt.NewContext()
71 require.NotNil(t, ctx)
72
73 // Set interrupt handler
74 interruptCalled := false
75 rt.SetInterruptHandler(func() int {
76 interruptCalled = true
77 return 1 // Request interrupt
78 })
79
80 // Unregister runtime from mapping before executing long-running code
81 unregisterRuntime(rt.ref)
82
83 // Execute long-running code that may trigger interrupt handler
84 result := ctx.Eval(`
85 var sum = 0;
86 for(var i = 0; i < 100000; i++) {
87 sum += i;
88 if (i % 1000 === 0) {
89 var temp = Math.sqrt(i);
90 }
91 }
92 sum;
93 `)
94
95 // Since runtime is not in mapping, goInterruptHandler should return 0
96 t.Logf("Interrupt handler called: %v", interruptCalled)
97 if result.IsException() {
98 err := ctx.Exception()
99 t.Logf("Execution resulted in exception: %v", err)
100 } else {
101 defer result.Free()
102 t.Logf("Computation completed with result: %d", result.ToInt32()) // Changed: Int32() → ToInt32()
103 }
104
105 // Re-register runtime for proper cleanup
106 registerRuntime(rt.ref, rt)
107
108 // Close context first, then runtime
109 ctx.Close()
110 rt.Close()
111
112 t.Log("Successfully triggered getRuntimeFromJS return nil branch")
113 })
114}
115
116func TestBridgeMappingCorruptionFailClosed(t *testing.T) {
117 rt := NewRuntime()

Callers

nothing calls this directly

Calls 12

NewContextMethod · 0.95
SetInterruptHandlerMethod · 0.95
EvalMethod · 0.95
ExceptionMethod · 0.95
CloseMethod · 0.95
CloseMethod · 0.95
NewRuntimeFunction · 0.85
unregisterRuntimeFunction · 0.85
registerRuntimeFunction · 0.85
IsExceptionMethod · 0.80
ToInt32Method · 0.80
FreeMethod · 0.45

Tested by

no test coverage detected