@@ -8,6 +8,7 @@ module ScriptHost =
88 { TypeDefs: Map < string , Type >
99 Env: Env
1010 ExportedFunctionNames: string list
11+ ExportedValueNames: string list
1112 LastValue: Value }
1213
1314 let private isCallable value =
@@ -28,17 +29,29 @@ module ScriptHost =
2829 let program = FScript.parse source
2930 let typed = FScript.inferWithExterns externs program
3031 let state = Eval.evalProgramWithExternsState externs typed
31- let functionNames =
32+ let exportedNames =
3233 declaredExportedNames typed
34+ |> List.distinct
35+ |> List.sort
36+
37+ let functionNames =
38+ exportedNames
3339 |> List.filter ( fun name ->
3440 match state.Env.TryFind name with
3541 | Some value -> isCallable value
3642 | None -> false )
37- |> List.distinct
38- |> List.sort
43+
44+ let valueNames =
45+ exportedNames
46+ |> List.filter ( fun name ->
47+ match state.Env.TryFind name with
48+ | Some value -> not ( isCallable value)
49+ | None -> false )
50+
3951 { TypeDefs = state.TypeDefs
4052 Env = state.Env
4153 ExportedFunctionNames = functionNames
54+ ExportedValueNames = valueNames
4255 LastValue = state.LastValue }
4356
4457 let loadFile ( externs : ExternalFunction list ) ( path : string ) : LoadedScript =
@@ -47,9 +60,23 @@ module ScriptHost =
4760 let listFunctions ( loaded : LoadedScript ) : string list =
4861 loaded.ExportedFunctionNames
4962
63+ let listValues ( loaded : LoadedScript ) : string list =
64+ loaded.ExportedValueNames
65+
66+ let getValue ( loaded : LoadedScript ) ( valueName : string ) : Value =
67+ if not ( loaded.ExportedValueNames |> List.contains valueName) then
68+ raise ( HostCommon.evalError $" Unknown exported value '{valueName}'" )
69+ else
70+ match loaded.Env.TryFind valueName with
71+ | Some value -> value
72+ | None -> raise ( HostCommon.evalError $" Unknown exported value '{valueName}'" )
73+
5074 let invoke ( loaded : LoadedScript ) ( functionName : string ) ( args : Value list ) : Value =
5175 if not ( loaded.ExportedFunctionNames |> List.contains functionName) then
52- raise ( HostCommon.evalError $" Unknown exported function '{functionName}'" )
76+ if loaded.ExportedValueNames |> List.contains functionName then
77+ raise ( HostCommon.evalError $" '{functionName}' is a value and cannot be invoked" )
78+ else
79+ raise ( HostCommon.evalError $" Unknown exported function '{functionName}'" )
5380 else
5481 match loaded.Env.TryFind functionName with
5582 | Some fnValue when isCallable fnValue ->
0 commit comments