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

Function TestBridgeGetContextFromJSReturnNil

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

Source from the content-addressed store, hash-verified

20}
21
22func TestBridgeGetContextFromJSReturnNil(t *testing.T) {
23 // Test getContextFromJS return nil
24 t.Run("GetContextFromJSReturnNil", func(t *testing.T) {
25 rt := NewRuntime()
26 defer rt.Close()
27 ctx := rt.NewContext()
28
29 // Create function and store it globally - MODIFIED: now uses pointer signature
30 fn := ctx.NewFunction(func(ctx *Context, this *Value, args []*Value) *Value { // Changed: Function() → NewFunction()
31 return ctx.NewString("test") // Changed: String() → NewString()
32 })
33 ctx.Globals().Set("testFn", fn)
34
35 // Unregister context from mapping to simulate context not found
36 unregisterContext(ctx.ref)
37
38 // Call function from JavaScript - triggers goFunctionProxy -> getContextFromJS with unmapped context
39 result := ctx.Eval(`
40 try {
41 testFn();
42 } catch(e) {
43 e.toString();
44 }
45 `)
46
47 // Should get an error or exception
48 if result.IsException() {
49 err := ctx.Exception()
50 t.Logf("Expected exception when context not in mapping: %v", err)
51 } else {
52 defer result.Free()
53 resultStr := result.ToString() // Changed: String() → ToString()
54 t.Logf("Exception result: %s", resultStr)
55 require.True(t, len(resultStr) > 0)
56 }
57
58 // Re-register context for proper cleanup
59 registerContext(ctx.ref, ctx)
60 ctx.Close()
61
62 t.Log("Successfully triggered getContextFromJS return nil branch")
63 })
64}
65
66func TestBridgeGetRuntimeFromJSReturnNil(t *testing.T) {
67 // Test getRuntimeFromJS return nil in goInterruptHandler

Callers

nothing calls this directly

Calls 15

CloseMethod · 0.95
NewContextMethod · 0.95
NewFunctionMethod · 0.95
NewStringMethod · 0.95
GlobalsMethod · 0.95
EvalMethod · 0.95
ExceptionMethod · 0.95
CloseMethod · 0.95
NewRuntimeFunction · 0.85
unregisterContextFunction · 0.85
registerContextFunction · 0.85
SetMethod · 0.80

Tested by

no test coverage detected