11//
2- // Copyright 2025 The Chainloop Authors.
2+ // Copyright 2024- 2025 The Chainloop Authors.
33//
44// Licensed under the Apache License, Version 2.0 (the "License");
55// you may not use this file except in compliance with the License.
@@ -23,59 +23,70 @@ import (
2323 "google.golang.org/grpc"
2424)
2525
26- // CreateDiscoverHostFunction creates an Extism host function for the discover builtin
27- // This allows WASM policies to call chainloop_discover(digest, kind) and get artifact graph data
28- func CreateDiscoverHostFunction (conn * grpc.ClientConn ) extism.HostFunction {
26+ // CreateDiscoverHostFunctions creates Extism host functions for the discover builtin.
27+ // Returns two host functions - one for each supported namespace:
28+ // 1. "env" namespace for Go (TinyGo) policies
29+ // 2. "extism:host/user" namespace for JavaScript policies
30+ func CreateDiscoverHostFunctions (conn * grpc.ClientConn ) []extism.HostFunction {
2931 discoverSvc := NewDiscoverService (conn )
3032
31- return extism .NewHostFunctionWithStack (
32- "chainloop_discover" ,
33- func (ctx context.Context , plugin * extism.CurrentPlugin , stack []uint64 ) {
34- // Read digest from WASM memory
35- digestOffset := stack [0 ]
36- digest , err := plugin .ReadString (digestOffset )
37- if err != nil {
38- // Return 0 to signal error
39- stack [0 ] = 0
40- return
41- }
33+ // Shared implementation for the host function
34+ impl := func (ctx context.Context , plugin * extism.CurrentPlugin , stack []uint64 ) {
35+ // Read digest from WASM memory
36+ digestOffset := stack [0 ]
37+ digest , err := plugin .ReadString (digestOffset )
38+ if err != nil {
39+ // Return 0 to signal error
40+ stack [0 ] = 0
41+ return
42+ }
4243
43- // Read kind from WASM memory (if provided)
44- var kind string
45- if len (stack ) > 1 && stack [1 ] != 0 {
46- kindOffset := stack [1 ]
47- kind , _ = plugin .ReadString (kindOffset )
48- }
44+ // Read kind from WASM memory (if provided)
45+ var kind string
46+ if len (stack ) > 1 && stack [1 ] != 0 {
47+ kindOffset := stack [1 ]
48+ kind , _ = plugin .ReadString (kindOffset )
49+ }
4950
50- // Call shared discover service
51- resp , err := discoverSvc .Discover (ctx , digest , kind )
52- if err != nil {
53- // Return 0 to signal error
54- stack [0 ] = 0
55- return
56- }
51+ // Call shared discover service
52+ resp , err := discoverSvc .Discover (ctx , digest , kind )
53+ if err != nil || resp = = nil {
54+ // Return 0 to signal error (no connection or error)
55+ stack [0 ] = 0
56+ return
57+ }
5758
58- // Serialize response to JSON
59- jsonData , err := json .Marshal (resp .Result )
60- if err != nil {
61- // Return 0 to signal error
62- stack [0 ] = 0
63- return
64- }
59+ // Serialize response to JSON
60+ jsonData , err := json .Marshal (resp .Result )
61+ if err != nil {
62+ // Return 0 to signal error
63+ stack [0 ] = 0
64+ return
65+ }
6566
66- // Write JSON to WASM memory and return offset
67- offset , err := plugin .WriteBytes ( jsonData )
68- if err != nil {
69- // Return 0 to signal error
70- stack [0 ] = 0
71- return
72- }
67+ // Write JSON string to WASM memory and return offset
68+ offset , err := plugin .WriteString ( string ( jsonData ) )
69+ if err != nil {
70+ // Return 0 to signal error
71+ stack [0 ] = 0
72+ return
73+ }
7374
74- stack [0 ] = offset
75- },
76- // inputs: digest offset, kind offset
77- []extism.ValueType {extism .ValueTypeI64 , extism .ValueTypeI64 },
78- // output: json result offset or 0 on error
79- []extism.ValueType {extism .ValueTypeI64 },
80- )
75+ stack [0 ] = offset
76+ }
77+
78+ // inputs: digest offset, kind offset
79+ inputs := []extism.ValueType {extism .ValueTypeI64 , extism .ValueTypeI64 }
80+ // output: json result offset or 0 on error
81+ outputs := []extism.ValueType {extism .ValueTypeI64 }
82+
83+ // Create host function for "env" namespace (Go/TinyGo policies)
84+ envFunc := extism .NewHostFunctionWithStack ("chainloop_discover" , impl , inputs , outputs )
85+ envFunc .SetNamespace ("env" )
86+
87+ // Create host function for "extism:host/user" namespace (JavaScript policies)
88+ jsFunc := extism .NewHostFunctionWithStack ("chainloop_discover" , impl , inputs , outputs )
89+ jsFunc .SetNamespace ("extism:host/user" )
90+
91+ return []extism.HostFunction {envFunc , jsFunc }
8192}
0 commit comments